Received: from eggs.gnu.org ([2001:4830:134:3::10]:48384) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1ZpchL-0004oz-0C for qemu-devel@nongnu.org; Fri, 23 Oct 2015 09:47:59 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1ZpchH-0005OQ-QQ for qemu-devel@nongnu.org; Fri, 23 Oct 2015 09:47:58 -0400 From: Alberto Garcia In-Reply-To: <20151023133138.GB5977@stefanha-x1.localdomain> References: <553b81f91762993bf5d43a691aedb4fa2168bc3a.1445500464.git.berto@igalia.com> <20151023133138.GB5977@stefanha-x1.localdomain> Date: Fri, 23 Oct 2015 15:47:52 +0200 Message-ID: MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Subject: Re: [Qemu-devel] [PATCH v3 09/21] block: Add average I/O queue depth to BlockDeviceTimedStats List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , To: Stefan Hajnoczi Cc: Kevin Wolf , qemu-block@nongnu.org, Markus Armbruster , qemu-devel@nongnu.org, Max Reitz On Fri 23 Oct 2015 03:31:38 PM CEST, Stefan Hajnoczi wrote: >> +uint64_t timed_average_sum(TimedAverage *ta, uint64_t *elapsed) >> +{ >> + TimedAverageWindow *w; >> + check_expirations(ta); >> + w = current_window(ta); >> + if (elapsed != NULL) { >> + int64_t remaining = w->expiration - qemu_clock_get_ns(ta->clock_type); >> + *elapsed = ta->period - remaining; > > There is no guarantee that qemu_clock_get_ns(ta->clock_type) executes > quickly after check_expirations(ta). If the system is under heavy load > and swapping, maybe significant amounts of time have passed. This race > condition could result in bad elapsed values being calculated. > > We need to be careful about elapsed <= 0 values, especially if the > caller divides by elapsed and is exposed to Divide By 0 exceptions. > > Either check_expirations() needs to integrate qemu_clock_get_ns() or we > simply need to deal with bogus values here. Good catch, thanks! I think having check_expirations() return the time is probably the best option. Berto