Received: from eggs.gnu.org ([2001:4830:134:3::10]:53494) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1bK4u0-0005NC-F4 for qemu-devel@nongnu.org; Mon, 04 Jul 2016 10:31:13 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1bK4tu-0000I9-Tz for qemu-devel@nongnu.org; Mon, 04 Jul 2016 10:31:11 -0400 Received: from mx0a-001b2d01.pphosted.com ([148.163.156.1]:60012) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1bK4tu-0000Ho-Le for qemu-devel@nongnu.org; Mon, 04 Jul 2016 10:31:06 -0400 Received: from pps.filterd (m0098404.ppops.net [127.0.0.1]) by mx0a-001b2d01.pphosted.com (8.16.0.11/8.16.0.11) with SMTP id u64ETGwH061160 for ; Mon, 4 Jul 2016 10:31:05 -0400 Received: from e06smtp14.uk.ibm.com (e06smtp14.uk.ibm.com [195.75.94.110]) by mx0a-001b2d01.pphosted.com with ESMTP id 23x9ppyxrr-1 (version=TLSv1.2 cipher=AES256-SHA bits=256 verify=NOT) for ; Mon, 04 Jul 2016 10:31:05 -0400 Received: from localhost by e06smtp14.uk.ibm.com with IBM ESMTP SMTP Gateway: Authorized Use Only! Violators will be prosecuted for from ; Mon, 4 Jul 2016 15:31:03 +0100 From: Sascha Silbe In-Reply-To: <818452bb-a196-bac8-87a7-e283451a9494@redhat.com> References: <1467127721-9564-1-git-send-email-silbe@linux.vnet.ibm.com> <1467127721-9564-2-git-send-email-silbe@linux.vnet.ibm.com> <818452bb-a196-bac8-87a7-e283451a9494@redhat.com> Date: Mon, 04 Jul 2016 16:30:58 +0200 MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: quoted-printable Message-Id: <87a8hxa319.fsf@oc4731375738.ibm.com> Subject: Re: [Qemu-devel] [PATCH 1/1] Improve block job rate limiting for small bandwidth values List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , To: Max Reitz , qemu-block@nongnu.org Cc: Kevin Wolf , Jeff Cody , qemu-devel@nongnu.org Dear Max, Max Reitz writes: > On 28.06.2016 17:28, Sascha Silbe wrote: [block/mirror.c] >> @@ -416,7 +416,9 @@ static uint64_t coroutine_fn mirror_iteration(Mirror= BlockJob *s) >> assert(io_sectors); >> sector_num +=3D io_sectors; >> nb_chunks -=3D DIV_ROUND_UP(io_sectors, sectors_per_chunk); >> - delay_ns +=3D ratelimit_calculate_delay(&s->limit, io_sectors); >> + if (s->common.speed) { >> + delay_ns =3D ratelimit_calculate_delay(&s->limit, io_sector= s); >> + } > > Hmm... Was it a bug that ratelimit_calculate_delay() was called > unconditionally before? One could argue either way. It happened to work because ratelimit_calculate_delay() only delayed the _second_ time (within one time slice) the quota was exceeded. With zero duration time slices, there never was a second time. With the new implementation we would divide by zero when slice_quota is 0, so we need to guard against that. Most callers already did, only mirror_iteration() needed to be adjusted. [...] [include/qemu/ratelimit.h] >> static inline int64_t ratelimit_calculate_delay(RateLimit *limit, uint6= 4_t n) >> { >> int64_t now =3D qemu_clock_get_ns(QEMU_CLOCK_REALTIME); >> + uint64_t delay_slices; >>=20=20 >> - if (limit->next_slice_time < now) { >> - limit->next_slice_time =3D now + limit->slice_ns; >> + assert(limit->slice_quota && limit->slice_ns); >> + >> + if (limit->slice_end_time < now) { >> + /* Previous, possibly extended, time slice finished; reset the >> + * accounting. */ >> + limit->slice_start_time =3D now; >> + limit->slice_end_time =3D now + limit->slice_ns; >> limit->dispatched =3D 0; >> } >> - if (limit->dispatched =3D=3D 0 || limit->dispatched + n <=3D limit-= >slice_quota) { >> - limit->dispatched +=3D n; >> + >> + limit->dispatched +=3D n; >> + if (limit->dispatched < limit->slice_quota) { > > Nitpick: This should probably stay <=3D. This is a subtle edge case. Previously, when limit->dispatched =3D=3D limit->slice_quota, we returned 0 so that the _current_ request (which is still within quota) wouldn't be delayed. Now, we return a delay so that the _next_ request (which would be over quota) will be delayed. [...] >> + /* Quota exceeded. Calculate the next time slice we may start >> + * sending data again. */ >> + delay_slices =3D (limit->dispatched + limit->slice_quota - 1) / >> + limit->slice_quota; >> + limit->slice_end_time =3D limit->slice_start_time + >> + delay_slices * limit->slice_ns; > > I think it would make sense to make this a floating point calculation. Then we'd have fully variable length time slices, instead of just "occupying" multiple fixed-length time slices with a single request. Maybe that would be even better, or maybe we'd cause other interesting things to happen (think interactions with the scheduler). As this code will hopefully disappear during the 2.8 time line anyway, I'd prefer to go with the lowest risk option that is enough to fix the race conditions encountered by the test suite. > If you don't agree, though: > > Reviewed-by: Max Reitz Thanks for the review! Sascha --=20 Softwareentwicklung Sascha Silbe, Niederhofenstra=C3=9Fe 5/1, 71229 Leonberg https://se-silbe.de/ USt-IdNr. DE281696641