Received: from eggs.gnu.org ([2001:4830:134:3::10]:40811) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1XTT6C-0003Pg-AC for qemu-devel@nongnu.org; Mon, 15 Sep 2014 06:01:40 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1XTT64-0001hP-Rp for qemu-devel@nongnu.org; Mon, 15 Sep 2014 06:01:32 -0400 Received: from mx1.redhat.com ([209.132.183.28]:11233) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1XTT64-0001hE-LY for qemu-devel@nongnu.org; Mon, 15 Sep 2014 06:01:24 -0400 From: Markus Armbruster References: <1410178693-23370-1-git-send-email-benoit.canet@nodalink.com> <1410178693-23370-2-git-send-email-benoit.canet@nodalink.com> Date: Mon, 15 Sep 2014 12:01:16 +0200 In-Reply-To: <1410178693-23370-2-git-send-email-benoit.canet@nodalink.com> (=?utf-8?Q?=22Beno=C3=AEt?= Canet"'s message of "Mon, 8 Sep 2014 14:18:11 +0200") Message-ID: <87egvdjj83.fsf@blackfin.pond.sub.org> MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: quoted-printable Subject: Re: [Qemu-devel] [PATCH v2 1/3] throttle: Make NANOSECONDS_PER_SECOND an integer List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , To: =?utf-8?Q?Beno=C3=AEt?= Canet Cc: peter.maydell@linaro.org, qemu-devel@nongnu.org Beno=C3=AEt Canet writes: > We will want to reuse this define in the future by making it common to mu= ltiples > QEMU modules. > It would be safer that this define be an integer so we avoid stranges flo= at > rounding errors. > Do this conversion. > > Signed-off-by: Beno=C3=AEt Canet > --- > include/qemu/throttle.h | 2 +- > util/throttle.c | 4 ++-- > 2 files changed, 3 insertions(+), 3 deletions(-) > > diff --git a/include/qemu/throttle.h b/include/qemu/throttle.h > index b890613..8f9e611 100644 > --- a/include/qemu/throttle.h > +++ b/include/qemu/throttle.h > @@ -27,7 +27,7 @@ > #include "qemu-common.h" > #include "qemu/timer.h" >=20=20 > -#define NANOSECONDS_PER_SECOND 1000000000.0 > +#define NANOSECONDS_PER_SECOND 1000000000 >=20=20 > typedef enum { > THROTTLE_BPS_TOTAL, > diff --git a/util/throttle.c b/util/throttle.c > index f976ac7..af8445a 100644 > --- a/util/throttle.c > +++ b/util/throttle.c > @@ -34,7 +34,7 @@ void throttle_leak_bucket(LeakyBucket *bkt, int64_t del= ta_ns) > double leak; >=20=20 > /* compute how much to leak */ > - leak =3D (bkt->avg * (double) delta_ns) / NANOSECONDS_PER_SECOND; > + leak =3D (bkt->avg * (double) delta_ns) / (double) NANOSECONDS_PER_S= ECOND; >=20=20 > /* make the bucket leak */ > bkt->level =3D MAX(bkt->level - leak, 0); > @@ -70,7 +70,7 @@ static void throttle_do_leak(ThrottleState *ts, int64_t= now) > */ > static int64_t throttle_do_compute_wait(double limit, double extra) > { > - double wait =3D extra * NANOSECONDS_PER_SECOND; > + double wait =3D extra * (double) NANOSECONDS_PER_SECOND; > wait /=3D limit; > return wait; > } I agree with Eric: casts aren't necessary here. Such clutter is okay when it makes the code easier to understand, or more robust against someone messing with types. Matter of taste here. I guess I'd omit the casts. Other uses of NANOSECONDS_PER_SECOND are in tests/test-throttle.c. test-throttle.c:38: throttle_leak_bucket(&bkt, NANOSECONDS_PER_SECOND / = 150); test-throttle.c:44: throttle_leak_bucket(&bkt, NANOSECONDS_PER_SECOND / = 150); test-throttle.c:50: throttle_leak_bucket(&bkt, NANOSECONDS_PER_SECOND / = 150); test-throttle.c:88: result =3D (int64_t) NANOSECONDS_PER_SECOND / 150 /= 2; Your patch makes it divide in int, then widen to int64_t. Before, it divided in double, then converted to int64_t, discarding fractions. Looks okay.