From: Eric Blake <eblake@redhat.com>
To: Yury Kotov <yury-kotov@yandex-team.ru>,
Paolo Bonzini <pbonzini@redhat.com>,
Richard Henderson <rth@twiddle.net>,
Juan Quintela <quintela@redhat.com>,
"Dr. David Alan Gilbert" <dgilbert@redhat.com>,
Thomas Huth <thuth@redhat.com>,
Laurent Vivier <lvivier@redhat.com>, Stefan Weil <sw@weilnetz.de>
Cc: "open list:Overall" <qemu-devel@nongnu.org>,
"yc-core@yandex-team.ru" <yc-core@yandex-team.ru>
Subject: Re: [Qemu-devel] [PATCH v5 1/3] qemu-thread: Add qemu_cond_timedwait
Date: Thu, 5 Sep 2019 14:45:50 -0500 [thread overview]
Message-ID: <79c7fa75-063b-700f-fcc8-4f71781f9664@redhat.com> (raw)
In-Reply-To: <20190826103726.25538-2-yury-kotov@yandex-team.ru>
On 8/26/19 5:37 AM, Yury Kotov wrote:
> Signed-off-by: Yury Kotov <yury-kotov@yandex-team.ru>
> ---
Rather sparse on the commit message details.
> 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(-)
>
> +++ b/util/qemu-thread-posix.c
> @@ -36,6 +36,18 @@ static void error_exit(int err, const char *msg)
> abort();
> }
>
> +static void compute_abs_deadline(struct timespec *ts, int ms)
> +{
> + struct timeval tv;
> + gettimeofday(&tv, NULL);
> + ts->tv_nsec = tv.tv_usec * 1000 + (ms % 1000) * 1000000;
> + ts->tv_sec = tv.tv_sec + ms / 1000;
> + if (ts->tv_nsec >= 1000000000) {
> + ts->tv_sec++;
> + ts->tv_nsec -= 1000000000;
> + }
I don't know if any named constants would make this easier or harder to
read (such as USEC_PER_SEC 1000000 or NSEC_PER_SEC 1000000000), but the
conversion from relative ms to absolute timespec looks correct. [1]
> +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 = pthread_cond_timedwait(&cond->cond, &mutex->lock, &ts);
> + trace_qemu_mutex_locked(mutex, file, line);
> + if (err && err != ETIMEDOUT) {
> + error_exit(err, __func__);
> + }
> +}
However, this function returning void looks odd. Although ETIMEDOUT is
the one error that guarantees that mutex is reobtained (all other errors
occur before the mutex is given up in the first place), and even though
the man page warns that you MUST recheck the condition variable in a
while loop regardless of success or failure (it might be a spurious
successful wake-up due to a broadcast where neither the condition nor
the timeout has actually been reached yet; or it might be a race where
the function reports a timeout immediately before the condition variable
became available after all), it still seems like callers might like to
know if a timeout happened, without having to calculate an ending
absolute time themselves.
>
> -static void compute_abs_deadline(struct timespec *ts, int ms)
> -{
> - struct timeval tv;
[1] Oh, you mixed code motion with new code, but the commit message
didn't mention that. It's not necessarily worth splitting the patch,
but at least mentioning it would be worthwhile.
> +++ b/util/qemu-thread-win32.c
> @@ -145,6 +145,22 @@ void qemu_cond_wait_impl(QemuCond *cond, QemuMutex *mutex, const char *file, con
> qemu_mutex_post_lock(mutex, file, line);
> }
>
> +void qemu_cond_timedwait_impl(QemuCond *cond, QemuMutex *mutex, int ms,
> + const char *file, const int line)
> +{
> + int rc = 0;
> +
> + assert(cond->initialized);
> + trace_qemu_mutex_unlock(mutex, file, line);
> + if (!SleepConditionVariableSRW(&cond->var, &mutex->lock, ms, 0)) {
> + rc = GetLastError();
> + }
> + trace_qemu_mutex_locked(mutex, file, line);
> + if (rc && rc != ERROR_TIMEOUT) {
> + error_exit(rc, __func__);
> + }
> +}
I am less certain that this implementation is correct, but on the
surface it seems okay.
>
> +static void
> +qsp_cond_timedwait(QemuCond *cond, QemuMutex *mutex, int ms,
> + const char *file, int line)
> +{
> + QSPEntry *e;
> + int64_t t0, t1;
> +
> + t0 = get_clock();
> + qemu_cond_timedwait_impl(cond, mutex, ms, file, line);
> + t1 = get_clock();
> +
> + e = qsp_entry_get(cond, file, line, QSP_CONDVAR);
> + qsp_entry_record(e, t1 - t0);
> +}
Another function where a bool or int return (to distinguish success from
timeout) might be worthwhile to some callers.
--
Eric Blake, Principal Software Engineer
Red Hat, Inc. +1-919-301-3226
Virtualization: qemu.org | libvirt.org
next prev parent reply other threads:[~2019-09-05 19:46 UTC|newest]
Thread overview: 7+ messages / expand[flat|nested] mbox.gz Atom feed top
2019-08-26 10:37 [Qemu-devel] [PATCH v5 0/3] High downtime with 95+ throttle pct Yury Kotov
2019-08-26 10:37 ` [Qemu-devel] [PATCH v5 1/3] qemu-thread: Add qemu_cond_timedwait Yury Kotov
2019-09-05 19:45 ` Eric Blake [this message]
2019-08-26 10:37 ` [Qemu-devel] [PATCH v5 2/3] cpus: Fix throttling during vm_stop Yury Kotov
2019-09-05 19:56 ` Eric Blake
2019-08-26 10:37 ` [Qemu-devel] [PATCH v5 3/3] tests/migration: Add a test for auto converge Yury Kotov
2019-08-26 10:42 ` Yury Kotov
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=79c7fa75-063b-700f-fcc8-4f71781f9664@redhat.com \
--to=eblake@redhat.com \
--cc=dgilbert@redhat.com \
--cc=lvivier@redhat.com \
--cc=pbonzini@redhat.com \
--cc=qemu-devel@nongnu.org \
--cc=quintela@redhat.com \
--cc=rth@twiddle.net \
--cc=sw@weilnetz.de \
--cc=thuth@redhat.com \
--cc=yc-core@yandex-team.ru \
--cc=yury-kotov@yandex-team.ru \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).