From patchwork Tue Jul 23 13:42:13 2019 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Yury Kotov X-Patchwork-Id: 1135694 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Authentication-Results: ozlabs.org; spf=pass (mailfrom) smtp.mailfrom=nongnu.org (client-ip=209.51.188.17; helo=lists.gnu.org; envelope-from=qemu-devel-bounces+incoming=patchwork.ozlabs.org@nongnu.org; receiver=) Authentication-Results: ozlabs.org; dmarc=fail (p=none dis=none) header.from=yandex-team.ru Authentication-Results: ozlabs.org; dkim=fail reason="signature verification failed" (1024-bit key; unprotected) header.d=yandex-team.ru header.i=@yandex-team.ru header.b="nVUfoe5s"; dkim-atps=neutral Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by ozlabs.org (Postfix) with ESMTPS id 45tKRg5Dyzz9sBF for ; Tue, 23 Jul 2019 23:42:54 +1000 (AEST) Received: from localhost ([::1]:42554 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.86_2) (envelope-from ) id 1hpv48-0008RF-9K for incoming@patchwork.ozlabs.org; Tue, 23 Jul 2019 09:42:52 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]:33645) by lists.gnu.org with esmtp (Exim 4.86_2) (envelope-from ) id 1hpv3z-0008Lu-7E for qemu-devel@nongnu.org; Tue, 23 Jul 2019 09:42:44 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1hpv3x-0003vd-MA for qemu-devel@nongnu.org; Tue, 23 Jul 2019 09:42:43 -0400 Received: from forwardcorp1j.mail.yandex.net ([5.45.199.163]:56062) by eggs.gnu.org with esmtps (TLS1.0:DHE_RSA_AES_256_CBC_SHA1:32) (Exim 4.71) (envelope-from ) id 1hpv3x-0003v2-9E for qemu-devel@nongnu.org; Tue, 23 Jul 2019 09:42:41 -0400 Received: from mxbackcorp1j.mail.yandex.net (mxbackcorp1j.mail.yandex.net [IPv6:2a02:6b8:0:1619::162]) by forwardcorp1j.mail.yandex.net (Yandex) with ESMTP id 82EDA2E14C2; Tue, 23 Jul 2019 16:42:39 +0300 (MSK) Received: from smtpcorp1j.mail.yandex.net (smtpcorp1j.mail.yandex.net [2a02:6b8:0:1619::137]) by mxbackcorp1j.mail.yandex.net (nwsmtp/Yandex) with ESMTP id 273MrmAwNs-gd5KWRcP; Tue, 23 Jul 2019 16:42:39 +0300 Precedence: bulk DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=yandex-team.ru; s=default; t=1563889359; bh=6vm/tvcGqZqYrL0pivEHJIBSrkUhSvvB9qfJPKUmrp0=; h=In-Reply-To:Message-Id:References:Date:Subject:To:From:Cc; b=nVUfoe5sd2fbS6Ev6D+MSRKyl3GteHXIMWZSVHYCclI3I7b899k2X+VpvQLZfazns +ro6pIea+g76M6gKH7+AnJpwaaxSYicJw7qAH60NypNrEyNvxDTDt3d9K6NNITSJPh jPzH4FGXj3MCR30PUevC3AgqhBuBtJO/msPzDxRg= Authentication-Results: mxbackcorp1j.mail.yandex.net; dkim=pass header.i=@yandex-team.ru Received: from unknown (unknown [2a02:6b8:b080:8808::1:1a]) by smtpcorp1j.mail.yandex.net (nwsmtp/Yandex) with ESMTPSA id cJSCJL4WEY-gdAm3S4Y; Tue, 23 Jul 2019 16:42:39 +0300 (using TLSv1.2 with cipher ECDHE-RSA-AES128-SHA256 (128/128 bits)) (Client certificate not present) From: Yury Kotov To: Paolo Bonzini , Peter Crosthwaite , Richard Henderson , Juan Quintela , "Dr. David Alan Gilbert" , Stefan Weil Date: Tue, 23 Jul 2019 16:42:13 +0300 Message-Id: <20190723134215.25095-3-yury-kotov@yandex-team.ru> X-Mailer: git-send-email 2.22.0 In-Reply-To: <20190723134215.25095-1-yury-kotov@yandex-team.ru> References: <20190723134215.25095-1-yury-kotov@yandex-team.ru> MIME-Version: 1.0 X-detected-operating-system: by eggs.gnu.org: GNU/Linux 2.2.x-3.x [generic] [fuzzy] X-Received-From: 5.45.199.163 Subject: [Qemu-devel] [PATCH v4 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: "open list:Overall" , yc-core@yandex-team.ru Errors-To: qemu-devel-bounces+incoming=patchwork.ozlabs.org@nongnu.org Sender: "Qemu-devel" 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 --- cpus.c | 27 +++++++++++++++++++-------- 1 file changed, 19 insertions(+), 8 deletions(-) diff --git a/cpus.c b/cpus.c index 927a00aa90..3baedd554c 100644 --- a/cpus.c +++ b/cpus.c @@ -74,6 +74,8 @@ #endif /* CONFIG_LINUX */ +static QemuMutex qemu_global_mutex; + int64_t max_delay; int64_t max_advance; @@ -777,7 +779,7 @@ static void cpu_throttle_thread(CPUState *cpu, run_on_cpu_data opaque) { double pct; double throttle_ratio; - long sleeptime_ns; + int64_t sleeptime_ns; if (!cpu_throttle_get_percentage()) { return; @@ -785,11 +787,22 @@ static void cpu_throttle_thread(CPUState *cpu, run_on_cpu_data opaque) pct = (double)cpu_throttle_get_percentage()/100; throttle_ratio = pct / (1 - pct); - sleeptime_ns = (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 = (int64_t)(throttle_ratio * CPU_THROTTLE_TIMESLICE_NS + 1); + + while (sleeptime_ns >= SCALE_MS && !cpu->stop) { + int64_t start, end; + start = qemu_clock_get_ns(QEMU_CLOCK_REALTIME); + qemu_cond_timedwait(cpu->halt_cond, &qemu_global_mutex, + sleeptime_ns / SCALE_MS); + end = qemu_clock_get_ns(QEMU_CLOCK_REALTIME); + sleeptime_ns -= end - start; + } + if (sleeptime_ns >= SCALE_US && !cpu->stop) { + qemu_mutex_unlock_iothread(); + g_usleep(sleeptime_ns / SCALE_US); + qemu_mutex_lock_iothread(); + } atomic_set(&cpu->throttle_thread_scheduled, 0); } @@ -1167,8 +1180,6 @@ static void qemu_init_sigbus(void) } #endif /* !CONFIG_LINUX */ -static QemuMutex qemu_global_mutex; - static QemuThread io_thread; /* cpu creation */