From nobody Sat May 18 14:01:33 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=1563289046; cv=none; d=zoho.com; s=zohoarc; b=fZfcr0iNJbbnZ0grzuZePBXlD/ICEr3LGyRvtovB9ceAEKlhew2hu+Hl2iyTocoxJUv6ax9D4QVFNmz4IVLTD8oAHplzdXX8YcrnaiM4aG28J2oAF3EvtVjQKp70qqIQo/Cieg79lXDYzsZZJAeRLqX9fL6xoutlDoa1YPvWJfI= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zoho.com; s=zohoarc; t=1563289046; 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=2irphpz9Xp2m9rcUGLThB1tc+Zjnw5dkEfNglUxgfVI=; b=d31/vB0pSDciPXRAuY/KTTGl6gs1JdXgaEjEsN6g1D7f8oBBhlwlTgavavj+qMMwX+7iOzuKVVQX12YBl5WJXSeJF16xQNzDJ91AsiykoJN3LptuXDdshVpkF1zLb2nN2TIxwRzYubiUmmccldr9Kt584Jk4Tt4hPL+/r7geaHg= 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 1563289046036471.3574648310723; Tue, 16 Jul 2019 07:57:26 -0700 (PDT) Received: from localhost ([::1]:49816 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.86_2) (envelope-from ) id 1hnOtQ-0008OW-Tv for importer@patchew.org; Tue, 16 Jul 2019 10:57:24 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]:50034) by lists.gnu.org with esmtp (Exim 4.86_2) (envelope-from ) id 1hnOrE-0007kK-7l for qemu-devel@nongnu.org; Tue, 16 Jul 2019 10:55:09 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1hnOrB-0001Ln-MZ for qemu-devel@nongnu.org; Tue, 16 Jul 2019 10:55:07 -0400 Received: from forwardcorp1j.mail.yandex.net ([5.45.199.163]:41860) by eggs.gnu.org with esmtps (TLS1.0:DHE_RSA_AES_256_CBC_SHA1:32) (Exim 4.71) (envelope-from ) id 1hnOr9-0001Ie-PQ for qemu-devel@nongnu.org; Tue, 16 Jul 2019 10:55:04 -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 CAB1C2E14C3; Tue, 16 Jul 2019 17:54:57 +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 hpdpF5Z1gr-svtOs3Tt; Tue, 16 Jul 2019 17:54:57 +0300 Received: from dynamic-red.dhcp.yndx.net (dynamic-red.dhcp.yndx.net [2a02:6b8:0:40c:68fa:19fe:5639:24b5]) by smtpcorp1j.mail.yandex.net (nwsmtp/Yandex) with ESMTPSA id RzIFhwRP0o-suQicNfH; Tue, 16 Jul 2019 17:54:57 +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=1563288897; bh=pdYBsi4yOtIzET/6XmZ1Glh9q8tSBBe6AsJssWPKHrM=; h=In-Reply-To:Message-Id:References:Date:Subject:To:From:Cc; b=FGO4dM/IFWVIGO7Age6ACTc1NTw/aExENMO2uUzY4Ic4Hoz71vMkJnzIc4HYtF74X Is9PirL46Elnb3CWblRgLJjqbOvcTHp8HiDTcqLmlNN2garieTrCIla0TSZdRBKzNp +ncq3Oy6R9iPfbyKInuIhF6y3ha7bNum17K6/ykE= Authentication-Results: mxbackcorp1g.mail.yandex.net; dkim=pass header.i=@yandex-team.ru From: Yury Kotov To: Paolo Bonzini , Richard Henderson , Thomas Huth , Laurent Vivier , Juan Quintela , "Dr. David Alan Gilbert" , Stefan Weil Date: Tue, 16 Jul 2019 17:54:19 +0300 Message-Id: <20190716145421.17645-2-yury-kotov@yandex-team.ru> X-Mailer: git-send-email 2.22.0 In-Reply-To: <20190716145421.17645-1-yury-kotov@yandex-team.ru> References: <20190716145421.17645-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: 5.45.199.163 Subject: [Qemu-devel] [PATCH v2 1/3] 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: 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" Signed-off-by: Yury Kotov --- include/qemu/thread.h | 18 ++++++++++++++++++ util/qemu-thread-posix.c | 40 ++++++++++++++++++++++++++++------------ util/qemu-thread-win32.c | 16 ++++++++++++++++ util/qsp.c | 18 ++++++++++++++++++ 4 files changed, 80 insertions(+), 12 deletions(-) diff --git a/include/qemu/thread.h b/include/qemu/thread.h index 55d83a907c..d0cd7b9ae0 100644 --- a/include/qemu/thread.h +++ b/include/qemu/thread.h @@ -34,6 +34,8 @@ typedef void (*QemuRecMutexLockFunc)(QemuRecMutex *m, con= st char *f, int l); typedef int (*QemuRecMutexTrylockFunc)(QemuRecMutex *m, const char *f, int= l); typedef void (*QemuCondWaitFunc)(QemuCond *c, QemuMutex *m, const char *f, int l); +typedef void (*QemuCondTimedWaitFunc)(QemuCond *c, QemuMutex *m, int ms, + const char *f, int l); =20 extern QemuMutexLockFunc qemu_bql_mutex_lock_func; extern QemuMutexLockFunc qemu_mutex_lock_func; @@ -41,6 +43,7 @@ extern QemuMutexTrylockFunc qemu_mutex_trylock_func; extern QemuRecMutexLockFunc qemu_rec_mutex_lock_func; extern QemuRecMutexTrylockFunc qemu_rec_mutex_trylock_func; extern QemuCondWaitFunc qemu_cond_wait_func; +extern QemuCondTimedWaitFunc qemu_cond_timedwait_func; =20 /* convenience macros to bypass the profiler */ #define qemu_mutex_lock__raw(m) \ @@ -63,6 +66,8 @@ extern QemuCondWaitFunc qemu_cond_wait_func; qemu_rec_mutex_trylock_impl(m, __FILE__, __LINE__); #define qemu_cond_wait(c, m) \ qemu_cond_wait_impl(c, m, __FILE__, __LINE__); +#define qemu_cond_timedwait(c, m, ms) \ + qemu_cond_wait_impl(c, m, ms, __FILE__, __LINE__); #else #define qemu_mutex_lock(m) ({ \ QemuMutexLockFunc _f =3D atomic_read(&qemu_mutex_lock_func); \ @@ -89,6 +94,11 @@ extern QemuCondWaitFunc qemu_cond_wait_func; QemuCondWaitFunc _f =3D atomic_read(&qemu_cond_wait_func); \ _f(c, m, __FILE__, __LINE__); \ }) + +#define qemu_cond_timedwait(c, m, ms) ({ = \ + QemuCondTimedWaitFunc _f =3D atomic_read(&qemu_cond_timedwait_= func); \ + _f(c, m, ms, __FILE__, __LINE__); = \ + }) #endif =20 #define qemu_mutex_unlock(mutex) \ @@ -134,12 +144,20 @@ void qemu_cond_signal(QemuCond *cond); void qemu_cond_broadcast(QemuCond *cond); void qemu_cond_wait_impl(QemuCond *cond, QemuMutex *mutex, const char *file, const int line); +void qemu_cond_timedwait_impl(QemuCond *cond, QemuMutex *mutex, int ms, + const char *file, const int line); =20 static inline void (qemu_cond_wait)(QemuCond *cond, QemuMutex *mutex) { qemu_cond_wait(cond, mutex); } =20 +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. */ diff --git a/util/qsp.c b/util/qsp.c index 5264c97342..904dcb7436 100644 --- a/util/qsp.c +++ b/util/qsp.c @@ -131,6 +131,7 @@ QemuRecMutexLockFunc qemu_rec_mutex_lock_func =3D qemu_= rec_mutex_lock_impl; QemuRecMutexTrylockFunc qemu_rec_mutex_trylock_func =3D qemu_rec_mutex_trylock_impl; QemuCondWaitFunc qemu_cond_wait_func =3D qemu_cond_wait_impl; +QemuCondTimedWaitFunc qemu_cond_timedwait_func =3D qemu_cond_timedwait_imp= l; =20 /* * It pays off to _not_ hash callsite->file; hashing a string is slow, and @@ -412,6 +413,21 @@ qsp_cond_wait(QemuCond *cond, QemuMutex *mutex, const = char *file, int line) qsp_entry_record(e, t1 - t0); } =20 +static void +qsp_cond_timedwait(QemuCond *cond, QemuMutex *mutex, int ms, + const char *file, int line) +{ + QSPEntry *e; + int64_t t0, t1; + + t0 =3D get_clock(); + qemu_cond_timedwait_impl(cond, mutex, ms, file, line); + t1 =3D get_clock(); + + e =3D qsp_entry_get(cond, file, line, QSP_CONDVAR); + qsp_entry_record(e, t1 - t0); +} + bool qsp_is_enabled(void) { return atomic_read(&qemu_mutex_lock_func) =3D=3D qsp_mutex_lock; @@ -425,6 +441,7 @@ void qsp_enable(void) atomic_set(&qemu_rec_mutex_lock_func, qsp_rec_mutex_lock); atomic_set(&qemu_rec_mutex_trylock_func, qsp_rec_mutex_trylock); atomic_set(&qemu_cond_wait_func, qsp_cond_wait); + atomic_set(&qemu_cond_timedwait_func, qsp_cond_timedwait); } =20 void qsp_disable(void) @@ -435,6 +452,7 @@ void qsp_disable(void) atomic_set(&qemu_rec_mutex_lock_func, qemu_rec_mutex_lock_impl); atomic_set(&qemu_rec_mutex_trylock_func, qemu_rec_mutex_trylock_impl); atomic_set(&qemu_cond_wait_func, qemu_cond_wait_impl); + atomic_set(&qemu_cond_timedwait_func, qemu_cond_timedwait_impl); } =20 static gint qsp_tree_cmp(gconstpointer ap, gconstpointer bp, gpointer up) --=20 2.22.0 From nobody Sat May 18 14:01:33 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=1563289012; cv=none; d=zoho.com; s=zohoarc; b=M9eX3Wm9PZB/GFDRl9d6FDSpxmAeej5l5ctZp3Mr01jsCiSaHqARaXOgRyMApEAgqvziX6ZL14M8qCoOOf7aUCbClr7MbOxRLRQSpgK+S89uFZ9LGYcm5974RqMRFfWtVgRk51l0S5wfdJ6sA6EVJyOo74395UMCDWtCtPn0AnI= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zoho.com; s=zohoarc; t=1563289012; 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=6Skub9vdvdN02NMEwKXuBUsjTjIyVyJHgVquPjtj9Yc=; b=lXHm6tt0ycedrPQ/g9bVpRPxDHKc9s7R7ByQ4VKCzND8lZ88vcAAol7OdZv1GidCniChaMZlwjc2nVs9nG03QY7w++384kJ9gqeu47aMKfPKXEI8UPjDP56WBHO0AXABWaZupX10D7lp+hoKBB14b0W/dMdzEsplyzbXgaz3iaU= 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 1563289012340968.92262366298; Tue, 16 Jul 2019 07:56:52 -0700 (PDT) Received: from localhost ([::1]:49798 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.86_2) (envelope-from ) id 1hnOst-0005vg-6D for importer@patchew.org; Tue, 16 Jul 2019 10:56:51 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]:50031) by lists.gnu.org with esmtp (Exim 4.86_2) (envelope-from ) id 1hnOrE-0007kJ-7l for qemu-devel@nongnu.org; Tue, 16 Jul 2019 10:55:09 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1hnOrB-0001M1-PN for qemu-devel@nongnu.org; Tue, 16 Jul 2019 10:55:07 -0400 Received: from forwardcorp1p.mail.yandex.net ([77.88.29.217]:54264) by eggs.gnu.org with esmtps (TLS1.0:DHE_RSA_AES_256_CBC_SHA1:32) (Exim 4.71) (envelope-from ) id 1hnOrA-0001Jl-Bw for qemu-devel@nongnu.org; Tue, 16 Jul 2019 10:55:05 -0400 Received: from mxbackcorp1j.mail.yandex.net (mxbackcorp1j.mail.yandex.net [IPv6:2a02:6b8:0:1619::162]) by forwardcorp1p.mail.yandex.net (Yandex) with ESMTP id A16312E1495; Tue, 16 Jul 2019 17:54:58 +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 8MGZrCVKIU-sw4Chkx7; Tue, 16 Jul 2019 17:54:58 +0300 Received: from dynamic-red.dhcp.yndx.net (dynamic-red.dhcp.yndx.net [2a02:6b8:0:40c:68fa:19fe:5639:24b5]) by smtpcorp1j.mail.yandex.net (nwsmtp/Yandex) with ESMTPSA id RzIFhwRP0o-svQip9CH; Tue, 16 Jul 2019 17:54:58 +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=1563288898; bh=JNDXgoKL65F2qbz8oiwPd7P/tXvktCtAirRQDC8fhvY=; h=In-Reply-To:Message-Id:References:Date:Subject:To:From:Cc; b=spexxzfEJYNqa068Xo1ZmmEdWjIuaivr3ogaayoTHti1MMHVzrwI5ldWRANR27sfl Rs3FkMYHaEhPYI7yF6c/HVKNBlxWv4l+PjXGeMGwHJepiGW8qMlpU4MWs8l81fMn1f 41e5sHSeUgAFxkd8Wi2n4ieSAS24GsaZ5HP70SuA= Authentication-Results: mxbackcorp1j.mail.yandex.net; dkim=pass header.i=@yandex-team.ru From: Yury Kotov To: Paolo Bonzini , Richard Henderson , Thomas Huth , Laurent Vivier , Juan Quintela , "Dr. David Alan Gilbert" , Stefan Weil Date: Tue, 16 Jul 2019 17:54:20 +0300 Message-Id: <20190716145421.17645-3-yury-kotov@yandex-team.ru> X-Mailer: git-send-email 2.22.0 In-Reply-To: <20190716145421.17645-1-yury-kotov@yandex-team.ru> References: <20190716145421.17645-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] [PATCH v2 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 --- cpus.c | 27 +++++++++++++++++++-------- 1 file changed, 19 insertions(+), 8 deletions(-) diff --git a/cpus.c b/cpus.c index ffc57119ca..c8817c9da7 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 From nobody Sat May 18 14:01:33 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=1563289061; cv=none; d=zoho.com; s=zohoarc; b=QF7t1p+PyZ1Xduh7EWWZ2V98MbI8Coirt3FASzBupxMq/O5ynZTUAaEJF/an9jLBEQIC9UPyh/ki3njXHak/dS4GtR/Cb83T7P7jCygSNTaOGIFO40KPBTsPNkG6dm8u27DbSufzWd9fH8o5QQpWDn8dwEluk3CsRRzduKnEaBQ= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zoho.com; s=zohoarc; t=1563289061; 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=55HY6PBNcBpkKXNETRmpIpH2ryGBnbqosTzeu9PNbuw=; b=F8PRTMeu1xJI1mh9hBTjjbDqjMsg07xxnF6Z95ataWyxFGPdSl4pMjsTN+XhMjx2TqVqMcK00736GNaE6CPWPndQk6BsMs6sbhvNc5Gyrarx+e4/l6BQL21RPZkKtxgVn3ljF7rzPW7D4UZq+KDA3qFs+kfmcSblZZamVutxiSo= 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 1563289061559295.186627698258; Tue, 16 Jul 2019 07:57:41 -0700 (PDT) Received: from localhost ([::1]:49832 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.86_2) (envelope-from ) id 1hnOtf-000176-DW for importer@patchew.org; Tue, 16 Jul 2019 10:57:39 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]:50035) by lists.gnu.org with esmtp (Exim 4.86_2) (envelope-from ) id 1hnOrE-0007kL-7l for qemu-devel@nongnu.org; Tue, 16 Jul 2019 10:55:09 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1hnOrB-0001Lt-Nk for qemu-devel@nongnu.org; Tue, 16 Jul 2019 10:55:07 -0400 Received: from forwardcorp1j.mail.yandex.net ([5.45.199.163]:41894) by eggs.gnu.org with esmtps (TLS1.0:DHE_RSA_AES_256_CBC_SHA1:32) (Exim 4.71) (envelope-from ) id 1hnOr9-0001JM-Oy for qemu-devel@nongnu.org; Tue, 16 Jul 2019 10:55:05 -0400 Received: from mxbackcorp1o.mail.yandex.net (mxbackcorp1o.mail.yandex.net [IPv6:2a02:6b8:0:1a2d::301]) by forwardcorp1j.mail.yandex.net (Yandex) with ESMTP id 495A62E14CB; Tue, 16 Jul 2019 17:54:59 +0300 (MSK) Received: from smtpcorp1j.mail.yandex.net (smtpcorp1j.mail.yandex.net [2a02:6b8:0:1619::137]) by mxbackcorp1o.mail.yandex.net (nwsmtp/Yandex) with ESMTP id rBKl0R8qAI-sxi8aH1M; Tue, 16 Jul 2019 17:54:59 +0300 Received: from dynamic-red.dhcp.yndx.net (dynamic-red.dhcp.yndx.net [2a02:6b8:0:40c:68fa:19fe:5639:24b5]) by smtpcorp1j.mail.yandex.net (nwsmtp/Yandex) with ESMTPSA id RzIFhwRP0o-swQiWvsR; Tue, 16 Jul 2019 17:54:58 +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=1563288899; bh=xRh29Dw9Cfd0xEIMjZVO3dh4OdPRUBmM+9AVkE70jm0=; h=In-Reply-To:Message-Id:References:Date:Subject:To:From:Cc; b=MvcNGG+Z9QSUzcV1V3s3XjjSBKVCaFC9d2TOR7uRgwnTx4mLv5eO7+SADaRbUOg5l 3EwNErqrShRtuJ2bseJypibCwuh3QbYzRbz3/XpFi4F3GDknc5rBdhRfIdLbzd63PN Pd0F+/pE7j1KFTUzP+ysBMuxTEAT0Mr0lViSvlwk= Authentication-Results: mxbackcorp1o.mail.yandex.net; dkim=pass header.i=@yandex-team.ru From: Yury Kotov To: Paolo Bonzini , Richard Henderson , Thomas Huth , Laurent Vivier , Juan Quintela , "Dr. David Alan Gilbert" , Stefan Weil Date: Tue, 16 Jul 2019 17:54:21 +0300 Message-Id: <20190716145421.17645-4-yury-kotov@yandex-team.ru> X-Mailer: git-send-email 2.22.0 In-Reply-To: <20190716145421.17645-1-yury-kotov@yandex-team.ru> References: <20190716145421.17645-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: 5.45.199.163 Subject: [Qemu-devel] [PATCH v2 3/3] tests/migration: Add a test for auto converge 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" Signed-off-by: Yury Kotov --- tests/migration-test.c | 119 +++++++++++++++++++++++++++++++++++++---- 1 file changed, 108 insertions(+), 11 deletions(-) diff --git a/tests/migration-test.c b/tests/migration-test.c index e0407576cb..8a755dfaf2 100644 --- a/tests/migration-test.c +++ b/tests/migration-test.c @@ -250,6 +250,17 @@ static int64_t read_ram_property_int(QTestState *who, = const char *property) return result; } =20 +static int64_t read_migrate_property_int(QTestState *who, const char *prop= erty) +{ + QDict *rsp_return; + int64_t result; + + rsp_return =3D migrate_query(who); + result =3D qdict_get_try_int(rsp_return, property, 0); + qobject_unref(rsp_return); + return result; +} + static uint64_t get_migration_pass(QTestState *who) { return read_ram_property_int(who, "dirty-sync-count"); @@ -264,20 +275,22 @@ static void read_blocktime(QTestState *who) qobject_unref(rsp_return); } =20 +static bool check_migration_status(QTestState *who, const char *status) +{ + bool completed; + char *current_status; + + current_status =3D migrate_query_status(who); + completed =3D strcmp(current_status, status) =3D=3D 0; + g_assert_cmpstr(current_status, !=3D, "failed"); + g_free(current_status); + return completed; +} + static void wait_for_migration_status(QTestState *who, const char *goal) { - while (true) { - bool completed; - char *status; - - status =3D migrate_query_status(who); - completed =3D strcmp(status, goal) =3D=3D 0; - g_assert_cmpstr(status, !=3D, "failed"); - g_free(status); - if (completed) { - return; - } + while (!check_migration_status(who, goal)) { usleep(1000); } } @@ -1127,6 +1140,89 @@ static void test_migrate_fd_proto(void) test_migrate_end(from, to, true); } =20 +static void test_migrate_auto_converge(void) +{ + char *uri =3D g_strdup_printf("unix:%s/migsocket", tmpfs); + QTestState *from, *to; + int i; + int64_t remaining, downtime; + + /* + * We want the test to be fast enough, but stable. + * Throttle percentages are chosen to cover all cases (init, increment= , max) + */ + static const int64_t expected_pcts[] =3D { 0, 1, 51, 98 }; + const int64_t max_bandwidth =3D 200000000; /* ~200Mb/s */ + const int64_t downtime_limit =3D 50; /* 50ms */ + /* + * We migrate through unix-socket (> 500Mb/s). + * Thus, expected migration speed ~=3D bandwidth limit (< 500Mb/s). + * So, we can predict expected_threshold + */ + const int64_t expected_threshold =3D max_bandwidth * downtime_limit / = 1000; + + if (test_migrate_start(&from, &to, uri, false, false)) { + return; + } + + migrate_set_capability(from, "auto-converge", true); + migrate_set_parameter(from, "cpu-throttle-initial", expected_pcts[1]); + migrate_set_parameter(from, "cpu-throttle-increment", + expected_pcts[2] - expected_pcts[1]); + migrate_set_parameter(from, "max-cpu-throttle", expected_pcts[3]); + + migrate_set_parameter(from, "max-bandwidth", max_bandwidth); + migrate_set_parameter(from, "downtime-limit", downtime_limit); + + /* To check remaining size after precopy */ + migrate_set_capability(from, "pause-before-switchover", true); + + /* Wait for the first serial output from the source */ + wait_for_serial("src_serial"); + + migrate(from, uri, "{}"); + + for (i =3D 0; i < ARRAY_SIZE(expected_pcts); i++) { + int64_t pct; + pct =3D read_migrate_property_int(from, "cpu-throttle-percentage"); + g_assert_cmpint(pct, =3D=3D, expected_pcts[i]); + while (pct =3D=3D expected_pcts[i] && !got_stop) { + usleep(1000); + pct =3D read_migrate_property_int(from, "cpu-throttle-percenta= ge"); + } + /* We break out of this loop only in paused state */ + if (got_stop || i + 1 =3D=3D ARRAY_SIZE(expected_pcts)) { + /* Check unexpected throttle percentage change */ + g_assert_true(got_stop); + /* Check unexpected converge */ + g_assert_cmpint(i + 1, =3D=3D, ARRAY_SIZE(expected_pcts)); + g_assert_true(check_migration_status(from, "pre-switchover")); + } + } + + remaining =3D read_ram_property_int(from, "remaining"); + g_assert_cmpint(remaining, <, expected_threshold); + + wait_command(from, "{ 'execute': 'migrate-continue' , 'arguments':" + " { 'state': 'pre-switchover' } }"); + + qtest_qmp_eventwait(to, "RESUME"); + + wait_for_serial("dest_serial"); + wait_for_migration_complete(from); + + downtime =3D read_migrate_property_int(from, "downtime"); + /* + * Actual downtime may be greater than downtime limit, + * but the difference should be small enough (~20ms) + */ + g_assert_cmpint(downtime, <, downtime_limit + 20); + + g_free(uri); + + test_migrate_end(from, to, true); +} + int main(int argc, char **argv) { char template[] =3D "/tmp/migration-test-XXXXXX"; @@ -1182,6 +1278,7 @@ int main(int argc, char **argv) /* qtest_add_func("/migration/ignore_shared", test_ignore_shared); */ qtest_add_func("/migration/xbzrle/unix", test_xbzrle_unix); qtest_add_func("/migration/fd_proto", test_migrate_fd_proto); + qtest_add_func("/migration/auto_converge", test_migrate_auto_converge); =20 ret =3D g_test_run(); =20 --=20 2.22.0