Received: from eggs.gnu.org ([2001:4830:134:3::10]:35477) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1bJKsL-00082Y-CF for qemu-devel@nongnu.org; Sat, 02 Jul 2016 09:22:26 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1bJKsJ-0007xs-1G for qemu-devel@nongnu.org; Sat, 02 Jul 2016 09:22:24 -0400 References: <1467127721-9564-1-git-send-email-silbe@linux.vnet.ibm.com> <1467127721-9564-2-git-send-email-silbe@linux.vnet.ibm.com> From: Max Reitz Message-ID: <818452bb-a196-bac8-87a7-e283451a9494@redhat.com> Date: Sat, 2 Jul 2016 15:22:11 +0200 MIME-Version: 1.0 In-Reply-To: <1467127721-9564-2-git-send-email-silbe@linux.vnet.ibm.com> Content-Type: multipart/signed; micalg=pgp-sha256; protocol="application/pgp-signature"; boundary="06RDiggAnTwPrQNqMuK90F6ePJmpJuTkb" 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: Sascha Silbe , qemu-block@nongnu.org Cc: qemu-devel@nongnu.org, Kevin Wolf , Jeff Cody This is an OpenPGP/MIME signed message (RFC 4880 and 3156) --06RDiggAnTwPrQNqMuK90F6ePJmpJuTkb From: Max Reitz To: Sascha Silbe , qemu-block@nongnu.org Cc: qemu-devel@nongnu.org, Kevin Wolf , Jeff Cody Message-ID: <818452bb-a196-bac8-87a7-e283451a9494@redhat.com> Subject: Re: [PATCH 1/1] Improve block job rate limiting for small bandwidth values References: <1467127721-9564-1-git-send-email-silbe@linux.vnet.ibm.com> <1467127721-9564-2-git-send-email-silbe@linux.vnet.ibm.com> In-Reply-To: <1467127721-9564-2-git-send-email-silbe@linux.vnet.ibm.com> Content-Type: text/plain; charset=iso-8859-15 Content-Transfer-Encoding: quoted-printable On 28.06.2016 17:28, Sascha Silbe wrote: > ratelimit_calculate_delay() previously reset the accounting every time > slice, no matter how much data had been processed before. This had (at > least) two consequences: >=20 > 1. The minimum speed is rather large, e.g. 5 MiB/s for commit and strea= m. >=20 > Not sure if there are real-world use cases where this would be a > problem. Mirroring and backup over a slow link (e.g. DSL) would > come to mind, though. >=20 > 2. Tests for block job operations (e.g. cancel) were rather racy >=20 > All block jobs currently use a time slice of 100ms. That's a > reasonable value to get smooth output during regular > operation. However this also meant that the state of block jobs > changed every 100ms, no matter how low the configured limit was. On > busy hosts, qemu often transferred additional chunks until the test > case had a chance to cancel the job. >=20 > Fix the block job rate limit code to delay for more than one time > slice to address the above issues. To make it easier to handle > oversized chunks we switch the semantics from returning a delay > _before_ the current request to a delay _after_ the current > request. If necessary, this delay consists of multiple time slice > units. >=20 > Since the mirror job sends multiple chunks in one go even if the rate > limit was exceeded in between, we need to keep track of the start of > the current time slice so we can correctly re-compute the delay for > the updated amount of data. >=20 > The minimum bandwidth now is 1 data unit per time slice. The block > jobs are currently passing the amount of data transferred in sectors > and using 100ms time slices, so this translates to 5120 > bytes/second. With chunk sizes usually being O(512KiB), tests have > plenty of time (O(100s)) to operate on block jobs. The chance of a > race condition now is fairly remote, except possibly on insanely > loaded systems. >=20 > Signed-off-by: Sascha Silbe > --- > block/commit.c | 13 +++++-------- > block/mirror.c | 4 +++- > block/stream.c | 12 ++++-------- > include/qemu/ratelimit.h | 43 ++++++++++++++++++++++++++++++++++------= --- > 4 files changed, 46 insertions(+), 26 deletions(-) >=20 [...] > diff --git a/block/mirror.c b/block/mirror.c > index a04ed9c..d9d70f0 100644 > --- a/block/mirror.c > +++ b/block/mirror.c > @@ -416,7 +416,9 @@ static uint64_t coroutine_fn mirror_iteration(Mirro= rBlockJob *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_secto= rs); > + } Hmm... Was it a bug that ratelimit_calculate_delay() was called unconditionally before? > } > return delay_ns; > } [...] > diff --git a/include/qemu/ratelimit.h b/include/qemu/ratelimit.h > index d413a4a..12db769 100644 > --- a/include/qemu/ratelimit.h > +++ b/include/qemu/ratelimit.h > @@ -15,34 +15,59 @@ > #define QEMU_RATELIMIT_H 1 > =20 > typedef struct { > - int64_t next_slice_time; > + int64_t slice_start_time; > + int64_t slice_end_time; > uint64_t slice_quota; > uint64_t slice_ns; > uint64_t dispatched; > } RateLimit; > =20 > +/** Calculate and return delay for next request in ns > + * > + * Record that we sent @p n data units. If we may send more data units= > + * in the current time slice, return 0 (i.e. no delay). Otherwise > + * return the amount of time (in ns) until the start of the next time > + * slice that will permit sending the next chunk of data. > + * > + * Recording sent data units even after exceeding the quota is > + * permitted; the time slice will be extended accordingly. > + */ > static inline int64_t ratelimit_calculate_delay(RateLimit *limit, uint= 64_t n) > { > int64_t now =3D qemu_clock_get_ns(QEMU_CLOCK_REALTIME); > + uint64_t delay_slices; > =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. > + /* We may send further data within the current time slice, no > + * need to delay the next request. */ > return 0; > - } else { > - limit->dispatched =3D n; > - return limit->next_slice_time - now; > } > + > + /* 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. If you don't agree, though: Reviewed-by: Max Reitz > + return limit->slice_end_time - now; > } > =20 > static inline void ratelimit_set_speed(RateLimit *limit, uint64_t spee= d, > uint64_t slice_ns) > { > limit->slice_ns =3D slice_ns; > - limit->slice_quota =3D ((double)speed * slice_ns)/1000000000ULL; > + limit->slice_quota =3D MAX(((double)speed * slice_ns) / 1000000000= ULL, 1); > } > =20 > #endif >=20 --06RDiggAnTwPrQNqMuK90F6ePJmpJuTkb Content-Type: application/pgp-signature; name="signature.asc" Content-Description: OpenPGP digital signature Content-Disposition: attachment; filename="signature.asc" -----BEGIN PGP SIGNATURE----- Version: GnuPG v2 iQEvBAEBCAAZBQJXd8ADEhxtcmVpdHpAcmVkaGF0LmNvbQAKCRA7sUIC6Disraz/ B/0XfPp0ueeq8TfWQLWlrSm7RCrkF6kVuE06YCo997/rfkni2WOl/M5mGH+wGTiI HHNDDsAFjEpOzJWGycea36D8Uo2rghM5auQMWvTUWLhmVAX1D9TESvCoaZzTdlnK 2jRJdHCtjwYBHkncRaNsi/9YpivkOsNDHYV8GbKQR3eumFHE2ULXHlSQxWHvVuPh Xv1zb0e/XUEJayIviroVg+77rv7BKX2Def3oxibU8iQTxTjN7QWJh8Gb8vpV+ech dLQwaHQOZvhm+Ouw3Pg8Aha3wZiSQSo7vJuWh8mx1q6JVwEyQTb3bw09z7N17/Sm 6475PB0dlrXsJkvk020IcUeQ =QhDQ -----END PGP SIGNATURE----- --06RDiggAnTwPrQNqMuK90F6ePJmpJuTkb--