From nobody Wed Nov 12 07:03:42 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=1568647170; cv=none; d=zoho.com; s=zohoarc; b=HTHf/MIOZTFY6CAaFUcmbrC1Afc+0MAIPPq06hpYuVPIuj1xYjYgveaXKRRswirCRJGkh2xaftaCeH22PhDXmoNegM4RLI9MNBkUG6We6F80sC+tT9tDVajSTSuR28dP7GQ2e8HWYRpwDqozSqVM6U89gps1lx2zRdleaulpj7o= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zoho.com; s=zohoarc; t=1568647170; 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=/9mBH+AAUSSLGhvpwHNCY+2vRoaPSjC+sHXPNN0R2GE=; b=DtwlsAZNFbFvZJQmrq/uiiMvGbKa5VZZLoSXsikvKx7VWfX25K8l0HVglkTxQRqOFoKgsS6LuYU83++wV/3rZhSK6PtwOZnsmDrAWv04kytpDHjx1/o4U6Q0hRLRGgpXsUSppUASfOq/vOUPsHyROAdJ6be5eQgUZmpkkdyII5I= 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 1568647169998246.03780430761276; Mon, 16 Sep 2019 08:19:29 -0700 (PDT) Received: from localhost ([::1]:35810 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1i9sml-0000fd-S6 for importer@patchew.org; Mon, 16 Sep 2019 11:19:27 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]:53732) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1i9sCz-0006YF-Mn 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 1i9sCx-0007Uv-D2 for qemu-devel@nongnu.org; Mon, 16 Sep 2019 10:42:29 -0400 Received: from mail-wr1-x443.google.com ([2a00:1450:4864:20::443]:37491) by eggs.gnu.org with esmtps (TLS1.0:RSA_AES_128_CBC_SHA1:16) (Exim 4.71) (envelope-from ) id 1i9sCw-0007UP-Tl for qemu-devel@nongnu.org; Mon, 16 Sep 2019 10:42:27 -0400 Received: by mail-wr1-x443.google.com with SMTP id i1so38605971wro.4 for ; Mon, 16 Sep 2019 07:42:26 -0700 (PDT) Received: from 640k.lan ([93.56.166.5]) by smtp.gmail.com with ESMTPSA id i14sm17024298wra.78.2019.09.16.07.42.24 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Mon, 16 Sep 2019 07:42:24 -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=/9mBH+AAUSSLGhvpwHNCY+2vRoaPSjC+sHXPNN0R2GE=; b=vVRi1n1Fo7h1bTbe9nLJNBdtxVfAITHMCIjx+iHSNzlhZ5RL10SD/Acjv6yzQP1NOX BGuEX9Q0/pphMhxpYgU/7BuqttF0FsUDahOPsTrRxfwxMfTIWLj7/D5NF4/GKo217WxG ej3CCOYHw5LrhcdpmtcBkJgXWiUii1xWJ4TjfIpCsgj1vJtzKzUw9n73sSePPcG08KRh Y+PPGDKJbhlXJobyJWgk3tKd3LY68Ap1dHoW4GGfOuAMHlw8/OWCf8MhteIizAKRuKuZ 3YJp9+7Q5TFqa5ecybbyW2CwFWYj4sBbuH+SVLmtAhV/K059rNR1k6FaxDwLG5+Ifzsv JZtQ== 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=/9mBH+AAUSSLGhvpwHNCY+2vRoaPSjC+sHXPNN0R2GE=; b=nZmn6C47QFHG4/OkowReVCD47N6jcNWDavWjvyEhBjKiKbPclLFUyCzR1gqvBlz5Qg 2T0YhoRqitcqwTSqGHZkuAr5kMHIuL1wng1T9PvKP3BF13m0v1Yqrnuz+L/BAHyJxiGN W2dpEXFi75oAhec2O4hXKKmHN32lVW8RLKbL4q58+4oWRlmVHXeXhmJS999/h2ujEBbK oOPC2D0RcVwLiZcMTjB6OSzqNOa+AycgWhsdkusPl/V+GJK+A2Wiw9CLqSL6dZ/l8c3x bTPyjRbXMGnltNc9qupHev84GlPumdOYG2ofVvTu1vOLxf0OsnH6mdcU3a4hos9bH9F5 j84A== X-Gm-Message-State: APjAAAU5yKEL6PhFhINsdbFpDxnhvQ9QglToQSRswXOMbAhL7Yaly1xp h6GkOWBd4jSMqQibFy0Bl4VX04or X-Google-Smtp-Source: APXvYqzeBhoCUGIKG4O/kVauOIf5Z2URiXuE8eYG/fvDasKAZtTcJB6+icvc86JO5p3EIpFstsFEJA== X-Received: by 2002:adf:b3c1:: with SMTP id x1mr109032wrd.33.1568644945435; Mon, 16 Sep 2019 07:42:25 -0700 (PDT) From: Paolo Bonzini To: qemu-devel@nongnu.org Date: Mon, 16 Sep 2019 16:41:53 +0200 Message-Id: <1568644929-9124-14-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::443 Subject: [Qemu-devel] [PULL 13/29] qemu-thread: Add qemu_cond_timedwait 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 The new function is needed to implement conditional sleep for CPU throttling. It's possible to reuse qemu_sem_timedwait, but it's more difficult than just add qemu_cond_timedwait. Also moved compute_abs_deadline function up the code to reuse it in qemu_cond_timedwait_impl win32. Signed-off-by: Yury Kotov Message-Id: <20190909131335.16848-2-yury-kotov@yandex-team.ru> --- include/qemu/thread.h | 19 +++++++++++++++++++ util/qemu-thread-posix.c | 41 +++++++++++++++++++++++++++++------------ util/qemu-thread-win32.c | 17 +++++++++++++++++ util/qsp.c | 20 ++++++++++++++++++++ 4 files changed, 85 insertions(+), 12 deletions(-) diff --git a/include/qemu/thread.h b/include/qemu/thread.h index 55d83a9..047db03 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 bool (*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,21 @@ 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); +bool 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 +/* Returns true if timeout has not expired, and false otherwise */ +static inline bool (qemu_cond_timedwait)(QemuCond *cond, QemuMutex *mutex, + int ms) +{ + return 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 1bf5e65..838980a 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,23 @@ void qemu_cond_wait_impl(QemuCond *cond, QemuMutex *mu= tex, const char *file, con error_exit(err, __func__); } =20 +bool 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__); + } + return err !=3D ETIMEDOUT; +} + void qemu_sem_init(QemuSemaphore *sem, int init) { int rc; @@ -238,18 +267,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 572f885..56a8333 100644 --- a/util/qemu-thread-win32.c +++ b/util/qemu-thread-win32.c @@ -145,6 +145,23 @@ void qemu_cond_wait_impl(QemuCond *cond, QemuMutex *mu= tex, const char *file, con qemu_mutex_post_lock(mutex, file, line); } =20 +bool 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__); + } + return rc !=3D ERROR_TIMEOUT; +} + void qemu_sem_init(QemuSemaphore *sem, int init) { /* Manual reset. */ diff --git a/util/qsp.c b/util/qsp.c index 5264c97..6226541 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,23 @@ qsp_cond_wait(QemuCond *cond, QemuMutex *mutex, const = char *file, int line) qsp_entry_record(e, t1 - t0); } =20 +static bool +qsp_cond_timedwait(QemuCond *cond, QemuMutex *mutex, int ms, + const char *file, int line) +{ + QSPEntry *e; + int64_t t0, t1; + bool ret; + + t0 =3D get_clock(); + ret =3D 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); + return ret; +} + bool qsp_is_enabled(void) { return atomic_read(&qemu_mutex_lock_func) =3D=3D qsp_mutex_lock; @@ -425,6 +443,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 +454,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 1.8.3.1