From nobody Wed Nov 12 07:03:43 2025 Delivered-To: importer@patchew.org Received-SPF: pass (zoho.com: domain of gnu.org designates 209.51.188.17 as permitted sender) client-ip=209.51.188.17; envelope-from=qemu-devel-bounces+importer=patchew.org@nongnu.org; helo=lists.gnu.org; Authentication-Results: mx.zohomail.com; dkim=fail; spf=pass (zoho.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=fail(p=none dis=none) header.from=redhat.com ARC-Seal: i=1; a=rsa-sha256; t=1568648877; cv=none; d=zoho.com; s=zohoarc; b=ZPwGSVzItuVttB6tCLIb09ncRARiqVKvmXkIsY29CBi/+UIxeBcKZsweyPdwFzmXv2tplHCJpJ9X6WjZZ1zmg2E4LYPWijVRbYZh8n2gatCC9SV5jeMcFrzalErvRdTKvL3LbWokVG7wh0OS+81iTppTKlhWvrFKAqISA2Gnvpw= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zoho.com; s=zohoarc; t=1568648877; h=Cc:Date:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Archive:List-Help:List-Unsubscribe:Message-ID:References:Sender:Subject:To:ARC-Authentication-Results; bh=cYGflcnLYW7UqqaIj7H9EUpz5deH3v/RJ+Xg1QiY1Lo=; b=AGkCAD3inkefD8q/cZ6mzexJ+NBx0+UeLs/Ou9nQeEeHM5S/cQc+v6pebziHacOKbGZLcJKzTQpljLlbn85peMxpL803EKh1dCKvHeXoS9K+zLNNF4JylSnFQlPkljqaU6/CCwfvfFy7QnXnMt10pl4sgI3htdX5xFYPmPR5KgU= ARC-Authentication-Results: i=1; mx.zoho.com; dkim=fail; spf=pass (zoho.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=fail header.from= (p=none dis=none) header.from= Return-Path: Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 1568648877676895.6075735040549; Mon, 16 Sep 2019 08:47:57 -0700 (PDT) Received: from localhost ([::1]:36106 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1i9tEK-0005Io-8M for importer@patchew.org; Mon, 16 Sep 2019 11:47:56 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]:53742) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1i9sD0-0006ZJ-GB for qemu-devel@nongnu.org; Mon, 16 Sep 2019 10:42:31 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1i9sCy-0007VQ-NN for qemu-devel@nongnu.org; Mon, 16 Sep 2019 10:42:30 -0400 Received: from mail-wr1-x441.google.com ([2a00:1450:4864:20::441]:38090) by eggs.gnu.org with esmtps (TLS1.0:RSA_AES_128_CBC_SHA1:16) (Exim 4.71) (envelope-from ) id 1i9sCy-0007V1-8p for qemu-devel@nongnu.org; Mon, 16 Sep 2019 10:42:28 -0400 Received: by mail-wr1-x441.google.com with SMTP id l11so39145167wrx.5 for ; Mon, 16 Sep 2019 07:42:28 -0700 (PDT) Received: from 640k.lan ([93.56.166.5]) by smtp.gmail.com with ESMTPSA id i14sm17024298wra.78.2019.09.16.07.42.25 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Mon, 16 Sep 2019 07:42:25 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=sender:from:to:cc:subject:date:message-id:in-reply-to:references; bh=cYGflcnLYW7UqqaIj7H9EUpz5deH3v/RJ+Xg1QiY1Lo=; b=arT9LUaGEBE0a/oPLB1rrIChwmhT1k9ov5UTXsM7GHYpKNdjEOcCWMyioyQl4cpjm1 cI452Abyjrws+FOvvdbuba/w2ZlHk/JtUaV3ptU44pus2FtvIaoSv7KyTbXTSJi3mr+a dPXCH7YWoPw8tRwjV37pOmqXkkwRYjvFtMD/6Rq2nmmN69mTM9sleMXK03+R+1mcFABe f3POTnpbEBa8QJtXrfMKGOvPpqs5ggCKP9OdYStgVVoNbkXewlI64299+bOL8M1wG9nr LUg4XNBVbs5KjVaPXyCFUPjGt+UfjLH715Zc3iD9dLCbLVIUfVpacRqgKdxlB8z04nvC Xj9Q== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:sender:from:to:cc:subject:date:message-id :in-reply-to:references; bh=cYGflcnLYW7UqqaIj7H9EUpz5deH3v/RJ+Xg1QiY1Lo=; b=Ga2qbS+3RjaPuAVEUPyINag1fyqFXDsrsCRZTYy5oslmrhKwtXzfQwI95R26HvSKWA HFqEgWzA/4lBNak1AdyxblmeKvL7X4Jf0DAARE21/vNqzruH5HXigpt5VpwkKMBHnFWz I8GrqyDvp3Q9tKj37Jsgig1LZLegtQ+KocHOf8AP0qxyhvlaMSguOZiQaQTm5bP5NsZC Q5I50k3g+mteDZCaLTuj0q/iLbptcd0T17Bqv/v6shXv8ZHVICtJypupZlpkNRW/F26a V/C4EiUpqvlAzVZRdAq0oRt/Ppvn9BmwUNQGxoF5S8mdoKYFak8vA8HPhW70mFvmIY63 lFoQ== X-Gm-Message-State: APjAAAUbKUxyFrfI2kd4jXhuxk1l90kAg6HwZ37OWo5ZKtcN7P9sa9Hj hh6NzJCX2wFAceRgM2+VsF8+JpmG X-Google-Smtp-Source: APXvYqwSiIjFdXfCQUwB5bWGHgvEECnLPIv6U22bV8ptt5N5yVIcnVNXVXcqArp/CON1CNGIayevUA== X-Received: by 2002:a5d:46cb:: with SMTP id g11mr111270wrs.268.1568644946700; Mon, 16 Sep 2019 07:42:26 -0700 (PDT) From: Paolo Bonzini To: qemu-devel@nongnu.org Date: Mon, 16 Sep 2019 16:41:54 +0200 Message-Id: <1568644929-9124-15-git-send-email-pbonzini@redhat.com> X-Mailer: git-send-email 1.8.3.1 In-Reply-To: <1568644929-9124-1-git-send-email-pbonzini@redhat.com> References: <1568644929-9124-1-git-send-email-pbonzini@redhat.com> X-detected-operating-system: by eggs.gnu.org: Genre and OS details not recognized. X-Received-From: 2a00:1450:4864:20::441 Subject: [Qemu-devel] [PULL 14/29] cpus: Fix throttling during vm_stop X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.23 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: Yury Kotov Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: "Qemu-devel" X-ZohoMail-DKIM: fail (Header signature does not verify) Content-Transfer-Encoding: quoted-printable MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" From: Yury Kotov Throttling thread sleeps in VCPU thread. For high throttle percentage this sleep is more than 10ms. E.g. for 60% - 15ms, for 99% - 990ms. vm_stop() kicks all VCPUs and waits for them. It's called at the end of migration and because of the long sleep the migration downtime might be more than 100ms even for downtime-limit 1ms. Use qemu_cond_timedwait for high percentage to wake up during vm_stop. Signed-off-by: Yury Kotov Reviewed-by: Eric Blake Message-Id: <20190909131335.16848-3-yury-kotov@yandex-team.ru> --- cpus.c | 25 +++++++++++++++++-------- 1 file changed, 17 insertions(+), 8 deletions(-) diff --git a/cpus.c b/cpus.c index 85cd451..d2c61ff 100644 --- a/cpus.c +++ b/cpus.c @@ -77,6 +77,8 @@ =20 #endif /* CONFIG_LINUX */ =20 +static QemuMutex qemu_global_mutex; + int64_t max_delay; int64_t max_advance; =20 @@ -782,7 +784,7 @@ static void cpu_throttle_thread(CPUState *cpu, run_on_c= pu_data opaque) { double pct; double throttle_ratio; - long sleeptime_ns; + int64_t sleeptime_ns, endtime_ns; =20 if (!cpu_throttle_get_percentage()) { return; @@ -790,11 +792,20 @@ static void cpu_throttle_thread(CPUState *cpu, run_on= _cpu_data opaque) =20 pct =3D (double)cpu_throttle_get_percentage()/100; throttle_ratio =3D pct / (1 - pct); - sleeptime_ns =3D (long)(throttle_ratio * CPU_THROTTLE_TIMESLICE_NS); - - qemu_mutex_unlock_iothread(); - g_usleep(sleeptime_ns / 1000); /* Convert ns to us for usleep call */ - qemu_mutex_lock_iothread(); + /* Add 1ns to fix double's rounding error (like 0.9999999...) */ + sleeptime_ns =3D (int64_t)(throttle_ratio * CPU_THROTTLE_TIMESLICE_NS = + 1); + endtime_ns =3D qemu_clock_get_ns(QEMU_CLOCK_REALTIME) + sleeptime_ns; + while (sleeptime_ns > 0 && !cpu->stop) { + if (sleeptime_ns > SCALE_MS) { + qemu_cond_timedwait(cpu->halt_cond, &qemu_global_mutex, + sleeptime_ns / SCALE_MS); + } else { + qemu_mutex_unlock_iothread(); + g_usleep(sleeptime_ns / SCALE_US); + qemu_mutex_lock_iothread(); + } + sleeptime_ns =3D endtime_ns - qemu_clock_get_ns(QEMU_CLOCK_REALTIM= E); + } atomic_set(&cpu->throttle_thread_scheduled, 0); } =20 @@ -1172,8 +1183,6 @@ static void qemu_init_sigbus(void) } #endif /* !CONFIG_LINUX */ =20 -static QemuMutex qemu_global_mutex; - static QemuThread io_thread; =20 /* cpu creation */ --=20 1.8.3.1