From nobody Tue Nov 11 10:02:06 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=yandex-team.ru ARC-Seal: i=1; a=rsa-sha256; t=1568034970; cv=none; d=zoho.com; s=zohoarc; b=XMJVAiYAVJli/fofbUp6UHwpM/GUnbAzSlOticTPID2qvmF3GQ44eO8kZ9BTa9MsDC8yypdFVw9E9EDReDOXHbYy7L8G6VWy4KrgxC/ESCKZu4ainRd3ZTwyKfEaEopsvLUtbwAiCF+XeF0k3JUx0wFmL6WOPOMuKXoCiv66w7c= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zoho.com; s=zohoarc; t=1568034970; h=Content-Transfer-Encoding:Cc:Date:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Archive:List-Help:List-Unsubscribe:MIME-Version:Message-ID:References:Sender:Subject:To:ARC-Authentication-Results; bh=pfU6dIRCe/BxK2F3UEhRwfaVGOwrDPHmit2rkxh5lIs=; b=mtwsVtBXvxrAwHRigbj1WVu1UY0sCDLNfEuxkJ58ELF4HWdibO+JAsqEatTq754mGl9IydcM4dHFiZqt1m+05BKPgCEyEos8+dqkeIcCZHCmFv8Cb+3JJnXdmXGkVWnox8fHjWTatxW6g7/B5Iq16AwAm+Sp/xteXfNKFTT9DS4= 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 (209.51.188.17 [209.51.188.17]) by mx.zohomail.com with SMTPS id 1568034970244189.38839612140362; Mon, 9 Sep 2019 06:16:10 -0700 (PDT) Received: from localhost ([::1]:56506 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1i7JWS-0005Rv-WA for importer@patchew.org; Mon, 09 Sep 2019 09:16:01 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]:56987) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1i7JUP-0004Dk-GQ for qemu-devel@nongnu.org; Mon, 09 Sep 2019 09:13:54 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1i7JUO-0002ag-8F for qemu-devel@nongnu.org; Mon, 09 Sep 2019 09:13:53 -0400 Received: from forwardcorp1j.mail.yandex.net ([2a02:6b8:0:1619::183]:46168) by eggs.gnu.org with esmtps (TLS1.0:DHE_RSA_AES_256_CBC_SHA1:32) (Exim 4.71) (envelope-from ) id 1i7JUN-0002Yw-UL for qemu-devel@nongnu.org; Mon, 09 Sep 2019 09:13:52 -0400 Received: from mxbackcorp1g.mail.yandex.net (mxbackcorp1g.mail.yandex.net [IPv6:2a02:6b8:0:1402::301]) by forwardcorp1j.mail.yandex.net (Yandex) with ESMTP id BEFE52E1481; Mon, 9 Sep 2019 16:13:47 +0300 (MSK) Received: from smtpcorp1j.mail.yandex.net (smtpcorp1j.mail.yandex.net [2a02:6b8:0:1619::137]) by mxbackcorp1g.mail.yandex.net (nwsmtp/Yandex) with ESMTP id 81RcB9CVoZ-DhCuu20S; Mon, 09 Sep 2019 16:13:47 +0300 Received: from dynamic-red.dhcp.yndx.net (dynamic-red.dhcp.yndx.net [2a02:6b8:0:408:c41:6db7:e9d4:e9e0]) by smtpcorp1j.mail.yandex.net (nwsmtp/Yandex) with ESMTPSA id epZFdTz6L0-Dh7CYkbB; Mon, 09 Sep 2019 16:13:43 +0300 (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits)) (Client certificate not present) Precedence: bulk DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=yandex-team.ru; s=default; t=1568034827; bh=SDYDVXTpeI0Jj9mPg05YC4j/rBpohEZTKIE7+3MKUQI=; h=In-Reply-To:Message-Id:References:Date:Subject:To:From:Cc; b=1cu+3DFhOWbfXjwAq1fhvouOBOIR1oWylTlQVXc8VtFMjj/TSGPVkhizwxaqF4zWM rTiHJENhir3KbD5sOGK78EFIcNgYABWHPiYXYCqw9522rpLykXW2bN40bv2SDavGHM PxvI8rMsnlqAT7dpAHmufLj9ni4ZjA4FWpT0seIk= Authentication-Results: mxbackcorp1g.mail.yandex.net; dkim=pass header.i=@yandex-team.ru From: Yury Kotov To: "Dr. David Alan Gilbert" , Eric Blake , Juan Quintela , Laurent Vivier , Paolo Bonzini , Richard Henderson , Stefan Weil , Thomas Huth Date: Mon, 9 Sep 2019 16:13:34 +0300 Message-Id: <20190909131335.16848-3-yury-kotov@yandex-team.ru> X-Mailer: git-send-email 2.23.0 In-Reply-To: <20190909131335.16848-1-yury-kotov@yandex-team.ru> References: <20190909131335.16848-1-yury-kotov@yandex-team.ru> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-detected-operating-system: by eggs.gnu.org: Genre and OS details not recognized. X-Received-From: 2a02:6b8:0:1619::183 Subject: [Qemu-devel] [PATCH v7 2/3] cpus: Fix throttling during vm_stop X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.23 List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: qemu-devel@nongnu.org, yc-core@yandex-team.ru Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: "Qemu-devel" X-ZohoMail-DKIM: fail (Header signature does not verify) Content-Type: text/plain; charset="utf-8" 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 --- cpus.c | 25 +++++++++++++++++-------- 1 file changed, 17 insertions(+), 8 deletions(-) diff --git a/cpus.c b/cpus.c index 85cd451a86..d2c61ff155 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 2.23.0