From nobody Mon Apr 29 10:01:23 2024 Delivered-To: importer@patchew.org Received-SPF: pass (zoho.com: domain of gnu.org designates 208.118.235.17 as permitted sender) client-ip=208.118.235.17; envelope-from=qemu-devel-bounces+importer=patchew.org@nongnu.org; helo=lists.gnu.org; Authentication-Results: mx.zohomail.com; spf=pass (zoho.com: domain of gnu.org designates 208.118.235.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org Return-Path: Received: from lists.gnu.org (lists.gnu.org [208.118.235.17]) by mx.zohomail.com with SMTPS id 1517987960080418.56325015197183; Tue, 6 Feb 2018 23:19:20 -0800 (PST) Received: from localhost ([::1]:57239 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1ejK0i-0005st-JO for importer@patchew.org; Wed, 07 Feb 2018 02:19:16 -0500 Received: from eggs.gnu.org ([2001:4830:134:3::10]:41393) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1ejJzf-0005J8-SG for qemu-devel@nongnu.org; Wed, 07 Feb 2018 02:18:13 -0500 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1ejJze-0005E8-Rh for qemu-devel@nongnu.org; Wed, 07 Feb 2018 02:18:11 -0500 Received: from proxmox-new.maurer-it.com ([212.186.127.180]:48757) by eggs.gnu.org with esmtps (TLS1.0:DHE_RSA_AES_256_CBC_SHA1:32) (Exim 4.71) (envelope-from ) id 1ejJzZ-00050T-Hb; Wed, 07 Feb 2018 02:18:05 -0500 Received: from proxmox-new.maurer-it.com (localhost.localdomain [127.0.0.1]) by proxmox-new.maurer-it.com (Proxmox) with ESMTP id 9566941BBF; Wed, 7 Feb 2018 08:17:59 +0100 (CET) From: Wolfgang Bumiller To: qemu-devel@nongnu.org Date: Wed, 7 Feb 2018 08:17:58 +0100 Message-Id: <20180207071758.6818-1-w.bumiller@proxmox.com> X-Mailer: git-send-email 2.11.0 X-detected-operating-system: by eggs.gnu.org: GNU/Linux 2.2.x-3.x [generic] [fuzzy] X-Received-From: 212.186.127.180 Subject: [Qemu-devel] [PATCH] ratelimit: don't align wait time with slices X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.21 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: Alberto Garcia , Qemu-block , Stefan Hajnoczi Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: "Qemu-devel" X-ZohoMail: RSF_0 Z_629925259 SPT_0 Content-Transfer-Encoding: quoted-printable MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" It is possible for rate limited writes to keep overshooting a slice's quota by a tiny amount causing the slice-aligned waiting period to effectively halve the rate. Signed-off-by: Wolfgang Bumiller Reviewed-by: Alberto Garcia --- Copied the Ccs from the discussion thread, hope that's fine, as I also just noticed that for my reply containing this snippet I had hit reply on the mail that did not contain those Ccs yet, sorry about that. include/qemu/ratelimit.h | 11 +++++------ 1 file changed, 5 insertions(+), 6 deletions(-) diff --git a/include/qemu/ratelimit.h b/include/qemu/ratelimit.h index 8dece483f5..1b38291823 100644 --- a/include/qemu/ratelimit.h +++ b/include/qemu/ratelimit.h @@ -36,7 +36,7 @@ typedef struct { static inline int64_t ratelimit_calculate_delay(RateLimit *limit, uint64_t= n) { int64_t now =3D qemu_clock_get_ns(QEMU_CLOCK_REALTIME); - uint64_t delay_slices; + double delay_slices; =20 assert(limit->slice_quota && limit->slice_ns); =20 @@ -55,12 +55,11 @@ static inline int64_t ratelimit_calculate_delay(RateLim= it *limit, uint64_t n) return 0; } =20 - /* 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; + /* Quota exceeded. Wait based on the excess amount and then start a new + * slice. */ + delay_slices =3D (double)limit->dispatched / limit->slice_quota; limit->slice_end_time =3D limit->slice_start_time + - delay_slices * limit->slice_ns; + (uint64_t)(delay_slices * limit->slice_ns); return limit->slice_end_time - now; } =20 --=20 2.11.0