From nobody Tue May 21 06:39:20 2024 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=1562750727; cv=none; d=zoho.com; s=zohoarc; b=bL8Oj6xheF9L1E06mD6onRLdFndigdi47aYHdY2yPhgQYO5yceENej6h9R0HVKFhZj0Up+tQ6lS3+8HiIFM2GnRUhMsdTxnipyI241iP5YzImku4+NIjSYqqur4f9c/iSiBTxWOikkTp7B/o1DhDiTs3Idc2e+IkYfYZ/5ColQk= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zoho.com; s=zohoarc; t=1562750727; 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=0hWWKkKWG9hvOroSEkwqYUGqBPUVOb8Z8Iczkvys/6c=; b=IrszxWTobYJlgqiN5PgElyq+MjYxOpNTjp4l+tUfpj4M5+339vJzbrZ8PIbKzw/GKzArBjfZbOIBq4C0SpVslAa3cc5gNHBGfT1zxZYTB+Igu97dlc7bJbKk7MzxCqMJJ93JJuWPAbBaevJHOucODu3C98N4xW5aJPC09viwiJ8= 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 1562750727991211.98494742053288; Wed, 10 Jul 2019 02:25:27 -0700 (PDT) Received: from localhost ([::1]:59286 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.86_2) (envelope-from ) id 1hl8qq-0000j8-Sr for importer@patchew.org; Wed, 10 Jul 2019 05:25:24 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]:47446) by lists.gnu.org with esmtp (Exim 4.86_2) (envelope-from ) id 1hl8pe-0008B4-G8 for qemu-devel@nongnu.org; Wed, 10 Jul 2019 05:24:11 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1hl8pc-0007tK-Ti for qemu-devel@nongnu.org; Wed, 10 Jul 2019 05:24:10 -0400 Received: from forwardcorp1p.mail.yandex.net ([2a02:6b8:0:1472:2741:0:8b6:217]:37888) by eggs.gnu.org with esmtps (TLS1.0:DHE_RSA_AES_256_CBC_SHA1:32) (Exim 4.71) (envelope-from ) id 1hl8pb-0007l4-TE for qemu-devel@nongnu.org; Wed, 10 Jul 2019 05:24:08 -0400 Received: from mxbackcorp1g.mail.yandex.net (mxbackcorp1g.mail.yandex.net [IPv6:2a02:6b8:0:1402::301]) by forwardcorp1p.mail.yandex.net (Yandex) with ESMTP id CA4042E0466; Wed, 10 Jul 2019 12:24:02 +0300 (MSK) Received: from smtpcorp1p.mail.yandex.net (smtpcorp1p.mail.yandex.net [2a02:6b8:0:1472:2741:0:8b6:10]) by mxbackcorp1g.mail.yandex.net (nwsmtp/Yandex) with ESMTP id t9gaglKD8z-O1tmQUTT; Wed, 10 Jul 2019 12:24:02 +0300 Received: from dynamic-red.dhcp.yndx.net (dynamic-red.dhcp.yndx.net [2a02:6b8:0:40c:649c:ccd6:daab:d003]) by smtpcorp1p.mail.yandex.net (nwsmtp/Yandex) with ESMTPSA id c6HGiW8rtf-O1waakWw; Wed, 10 Jul 2019 12:24:01 +0300 (using TLSv1.2 with cipher ECDHE-RSA-AES128-SHA256 (128/128 bits)) (Client certificate not present) Precedence: bulk DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=yandex-team.ru; s=default; t=1562750642; bh=h4G/QjfAJsFRsNw+o0CNfQMAlC3NaMMgRZUhG6VtW5Q=; h=In-Reply-To:Message-Id:References:Date:Subject:To:From:Cc; b=S1g9Yt94sJA9NVsnabY4OoRif89MqtKvc9fbLn5T5f2gXQwfyew9j6qlr+qTXfBWl yyGP8mczasIAJwN3cXM47CrAcQHnbnh1NjOMJrLikoO/AfoSLATkg/7sTusYmNfHuX VitFdfSAO7Lg0dtP2c0Unwxl5ksrlaRYYAHNQ078= Authentication-Results: mxbackcorp1g.mail.yandex.net; dkim=pass header.i=@yandex-team.ru From: Yury Kotov To: Paolo Bonzini , Richard Henderson , Stefan Weil , Juan Quintela , "Dr. David Alan Gilbert" Date: Wed, 10 Jul 2019 12:23:37 +0300 Message-Id: <20190710092338.23559-2-yury-kotov@yandex-team.ru> X-Mailer: git-send-email 2.22.0 In-Reply-To: <20190710092338.23559-1-yury-kotov@yandex-team.ru> References: <20190710092338.23559-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:1472:2741:0:8b6:217 Subject: [Qemu-devel] [RFC PATCH 1/2] qemu-thread: Add qemu_cond_timedwait 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+importer=patchew.org@nongnu.org Sender: "Qemu-devel" X-ZohoMail-DKIM: fail (Header signature does not verify) Content-Type: text/plain; charset="utf-8" Signed-off-by: Yury Kotov --- include/qemu/thread.h | 12 ++++++++++++ util/qemu-thread-posix.c | 40 ++++++++++++++++++++++++++++------------ util/qemu-thread-win32.c | 16 ++++++++++++++++ 3 files changed, 56 insertions(+), 12 deletions(-) diff --git a/include/qemu/thread.h b/include/qemu/thread.h index 55d83a907c..80898c5db4 100644 --- a/include/qemu/thread.h +++ b/include/qemu/thread.h @@ -140,6 +140,18 @@ static inline void (qemu_cond_wait)(QemuCond *cond, Qe= muMutex *mutex) qemu_cond_wait(cond, mutex); } =20 +void qemu_cond_timedwait_impl(QemuCond *cond, QemuMutex *mutex, int ms, + const char *file, const int line); + +#define qemu_cond_timedwait(cond, mutex, ms) \ + qemu_cond_timedwait_impl(cond, mutex, ms, __FILE__, __LINE__) + +static inline void (qemu_cond_timedwait)(QemuCond *cond, QemuMutex *mutex, + int ms) +{ + qemu_cond_timedwait(cond, mutex, ms); +} + void qemu_sem_init(QemuSemaphore *sem, int init); void qemu_sem_post(QemuSemaphore *sem); void qemu_sem_wait(QemuSemaphore *sem); diff --git a/util/qemu-thread-posix.c b/util/qemu-thread-posix.c index 1bf5e65dea..eed777d9ec 100644 --- a/util/qemu-thread-posix.c +++ b/util/qemu-thread-posix.c @@ -36,6 +36,18 @@ static void error_exit(int err, const char *msg) abort(); } =20 +static void compute_abs_deadline(struct timespec *ts, int ms) +{ + struct timeval tv; + gettimeofday(&tv, NULL); + ts->tv_nsec =3D tv.tv_usec * 1000 + (ms % 1000) * 1000000; + ts->tv_sec =3D tv.tv_sec + ms / 1000; + if (ts->tv_nsec >=3D 1000000000) { + ts->tv_sec++; + ts->tv_nsec -=3D 1000000000; + } +} + void qemu_mutex_init(QemuMutex *mutex) { int err; @@ -164,6 +176,22 @@ void qemu_cond_wait_impl(QemuCond *cond, QemuMutex *mu= tex, const char *file, con error_exit(err, __func__); } =20 +void qemu_cond_timedwait_impl(QemuCond *cond, QemuMutex *mutex, int ms, + const char *file, const int line) +{ + int err; + struct timespec ts; + + assert(cond->initialized); + trace_qemu_mutex_unlock(mutex, file, line); + compute_abs_deadline(&ts, ms); + err =3D pthread_cond_timedwait(&cond->cond, &mutex->lock, &ts); + trace_qemu_mutex_locked(mutex, file, line); + if (err && err !=3D ETIMEDOUT) { + error_exit(err, __func__); + } +} + void qemu_sem_init(QemuSemaphore *sem, int init) { int rc; @@ -238,18 +266,6 @@ void qemu_sem_post(QemuSemaphore *sem) #endif } =20 -static void compute_abs_deadline(struct timespec *ts, int ms) -{ - struct timeval tv; - gettimeofday(&tv, NULL); - ts->tv_nsec =3D tv.tv_usec * 1000 + (ms % 1000) * 1000000; - ts->tv_sec =3D tv.tv_sec + ms / 1000; - if (ts->tv_nsec >=3D 1000000000) { - ts->tv_sec++; - ts->tv_nsec -=3D 1000000000; - } -} - int qemu_sem_timedwait(QemuSemaphore *sem, int ms) { int rc; diff --git a/util/qemu-thread-win32.c b/util/qemu-thread-win32.c index 572f88535d..5faa01cb61 100644 --- a/util/qemu-thread-win32.c +++ b/util/qemu-thread-win32.c @@ -145,6 +145,22 @@ void qemu_cond_wait_impl(QemuCond *cond, QemuMutex *mu= tex, const char *file, con qemu_mutex_post_lock(mutex, file, line); } =20 +void qemu_cond_timedwait_impl(QemuCond *cond, QemuMutex *mutex, int ms, + const char *file, const int line) +{ + int rc =3D 0; + + assert(cond->initialized); + trace_qemu_mutex_unlock(mutex, file, line); + if (!SleepConditionVariableSRW(&cond->var, &mutex->lock, ms, 0)) { + rc =3D GetLastError(); + } + trace_qemu_mutex_locked(mutex, file, line); + if (rc && rc !=3D ERROR_TIMEOUT) { + error_exit(rc, __func__); + } +} + void qemu_sem_init(QemuSemaphore *sem, int init) { /* Manual reset. */ --=20 2.22.0 From nobody Tue May 21 06:39:20 2024 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=1562750807; cv=none; d=zoho.com; s=zohoarc; b=MAhE597qCY2j5igOK6N/EwMR9HsEzU4OEUc16WbGtwZPcVAI1vRVgm4ftsr7q2xGvicipoX3/NQldWE7nX41nI9pZj8NhJS5z+8PEOcVL2qKqDWoRrtJ9gEmXXnqfI8CrkwO2QIZDHU74z5qou+r1+0FI6gRtGPvXVzZDTN20Cw= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zoho.com; s=zohoarc; t=1562750807; 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=/oVKr5BUh1lW3TVnSfmFAAtOHxb3oy9d/3FELg2yI0U=; b=nkS2T6j7Z9pv3h/wV+CD3FD1Yr7uCPDl/OB0u5CTVPE6EvLZh4/wqmCkYrdZjkBGBsIrhS0k2MuwrMCOGZ/EFVhYi1EQLnE9rtXpH1wK5QAM2vjqSX2EiGMHGyzRlo4BFqm+nNdZMv8E+AaeI8yD+0yN3IIB6BfOiXZXckdq7zE= 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 1562750807020114.2606564428985; Wed, 10 Jul 2019 02:26:47 -0700 (PDT) Received: from localhost ([::1]:59296 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.86_2) (envelope-from ) id 1hl8sA-0002Ft-20 for importer@patchew.org; Wed, 10 Jul 2019 05:26:46 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]:47440) by lists.gnu.org with esmtp (Exim 4.86_2) (envelope-from ) id 1hl8pe-0008Ak-8c for qemu-devel@nongnu.org; Wed, 10 Jul 2019 05:24:11 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1hl8pc-0007sg-Ls for qemu-devel@nongnu.org; Wed, 10 Jul 2019 05:24:10 -0400 Received: from forwardcorp1p.mail.yandex.net ([77.88.29.217]:54452) by eggs.gnu.org with esmtps (TLS1.0:DHE_RSA_AES_256_CBC_SHA1:32) (Exim 4.71) (envelope-from ) id 1hl8pb-0007l9-St for qemu-devel@nongnu.org; Wed, 10 Jul 2019 05:24:08 -0400 Received: from mxbackcorp1g.mail.yandex.net (mxbackcorp1g.mail.yandex.net [IPv6:2a02:6b8:0:1402::301]) by forwardcorp1p.mail.yandex.net (Yandex) with ESMTP id 6A6922E0A7B; Wed, 10 Jul 2019 12:24:03 +0300 (MSK) Received: from smtpcorp1p.mail.yandex.net (smtpcorp1p.mail.yandex.net [2a02:6b8:0:1472:2741:0:8b6:10]) by mxbackcorp1g.mail.yandex.net (nwsmtp/Yandex) with ESMTP id X8sHBziPgT-O2t030o3; Wed, 10 Jul 2019 12:24:03 +0300 Received: from dynamic-red.dhcp.yndx.net (dynamic-red.dhcp.yndx.net [2a02:6b8:0:40c:649c:ccd6:daab:d003]) by smtpcorp1p.mail.yandex.net (nwsmtp/Yandex) with ESMTPSA id c6HGiW8rtf-O2waPrXp; Wed, 10 Jul 2019 12:24:02 +0300 (using TLSv1.2 with cipher ECDHE-RSA-AES128-SHA256 (128/128 bits)) (Client certificate not present) Precedence: bulk DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=yandex-team.ru; s=default; t=1562750643; bh=iNLAWaDyxeZFg8sfSzat7F4C9cqCNn/ipANDNCAdqNw=; h=In-Reply-To:Message-Id:References:Date:Subject:To:From:Cc; b=i2Xdgw/tTAx2HWOUSXLtnWVLvbl4/YDJSkB6cHeYvxA3cXMEMnV88AohiruJSbweR 9LhzNLCbAHRH2UhRafw9IX1I/SxFD91yK0LLq4IW02ZFgj2CnLAQAirqV4jz+1pe2l l+sd0xaRv/D4xttWvLHqCS+1PmqJaXLhoUXDOJdM= Authentication-Results: mxbackcorp1g.mail.yandex.net; dkim=pass header.i=@yandex-team.ru From: Yury Kotov To: Paolo Bonzini , Richard Henderson , Stefan Weil , Juan Quintela , "Dr. David Alan Gilbert" Date: Wed, 10 Jul 2019 12:23:38 +0300 Message-Id: <20190710092338.23559-3-yury-kotov@yandex-team.ru> X-Mailer: git-send-email 2.22.0 In-Reply-To: <20190710092338.23559-1-yury-kotov@yandex-team.ru> References: <20190710092338.23559-1-yury-kotov@yandex-team.ru> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-detected-operating-system: by eggs.gnu.org: GNU/Linux 2.2.x-3.x [generic] [fuzzy] X-Received-From: 77.88.29.217 Subject: [Qemu-devel] [RFC PATCH 2/2] 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+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 --- cpus.c | 27 +++++++++++++++++++-------- 1 file changed, 19 insertions(+), 8 deletions(-) diff --git a/cpus.c b/cpus.c index ffc57119ca..3c069cdc33 100644 --- a/cpus.c +++ b/cpus.c @@ -74,6 +74,8 @@ =20 #endif /* CONFIG_LINUX */ =20 +static QemuMutex qemu_global_mutex; + int64_t max_delay; int64_t max_advance; =20 @@ -776,7 +778,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; =20 if (!cpu_throttle_get_percentage()) { return; @@ -784,11 +786,22 @@ 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); + + while (sleeptime_ns >=3D SCALE_MS && !cpu->stop) { + int64_t start, end; + start =3D qemu_clock_get_ns(QEMU_CLOCK_REALTIME); + qemu_cond_timedwait(cpu->halt_cond, &qemu_global_mutex, + sleeptime_ns / SCALE_MS); + end =3D qemu_clock_get_ns(QEMU_CLOCK_REALTIME); + sleeptime_ns -=3D end - start; + } + if (sleeptime_ns >=3D 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); } =20 @@ -1166,8 +1179,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.22.0