qemu-devel.nongnu.org archive mirror
 help / color / mirror / Atom feed
From: longpeng2--- via <qemu-devel@nongnu.org>
To: Paolo Bonzini <pbonzini@redhat.com>,
	"berrange@redhat.com" <berrange@redhat.com>,
	"mst@redhat.com" <mst@redhat.com>
Cc: "qemu-devel@nongnu.org" <qemu-devel@nongnu.org>,
	"Gonglei (Arei)" <arei.gonglei@huawei.com>
Subject: RE: [RFC 2/2] sem-posix: use monotonic clock instead
Date: Mon, 21 Feb 2022 14:39:18 +0000	[thread overview]
Message-ID: <1f7b0ebec553434495aad5945b0c4634@huawei.com> (raw)
In-Reply-To: <5e92a220-eb61-c510-0cdc-bf418b9025bc@redhat.com>



> -----Original Message-----
> From: Paolo Bonzini [mailto:paolo.bonzini@gmail.com] On Behalf Of Paolo Bonzini
> Sent: Monday, February 21, 2022 7:42 PM
> To: Longpeng (Mike, Cloud Infrastructure Service Product Dept.)
> <longpeng2@huawei.com>; berrange@redhat.com; mst@redhat.com
> Cc: qemu-devel@nongnu.org; Gonglei (Arei) <arei.gonglei@huawei.com>
> Subject: Re: [RFC 2/2] sem-posix: use monotonic clock instead
> 
> On 2/21/22 10:56, Longpeng(Mike) via wrote:
> > +    long now_nsec;
> > +#ifdef CONFIG_PTHREAD_CONDATTR_SETCLOCK
> > +    struct timespec now;
> > +    clock_gettime(CLOCK_MONOTONIC, &now);
> > +    now_sec = now.tv_sec;
> > +    now_nsec = now.tv_nsec;
> > +#else
> >       struct timeval tv;
> >       gettimeofday(&tv, NULL);
> > -    ts->tv_nsec = tv.tv_usec * 1000 + (ms % 1000) * 1000000;
> > -    ts->tv_sec = tv.tv_sec + ms / 1000;
> > +    now_sec = tv.tv_sec;
> > +    now_nsec = tv.tv_usec * 1000;
> > +#endif
> > +
> 
> Perhaps this might minimize the amount of conditional code, too:
> 
> diff --git a/util/qemu-thread-posix.c b/util/qemu-thread-posix.c
> index 903fa33965..4743d7b714 100644
> --- a/util/qemu-thread-posix.c
> +++ b/util/qemu-thread-posix.c
> @@ -40,10 +40,14 @@ static void error_exit(int err, const char *msg)
> 
>   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;
> +#ifdef CONFIG_PTHREAD_CONDATTR_SETCLOCK
> +    clock_gettime(CLOCK_MONOTONIC, ts);
> +#else
> +    clock_gettime(CLOCK_REALTIME, ts);
> +#endif
> +
> +    ts->tv_nsec += (ms % 1000) * 1000000;
> +    ts->tv_sec += ms / 1000;
>       if (ts->tv_nsec >= 1000000000) {
>           ts->tv_sec++;
>           ts->tv_nsec -= 1000000000;
> 
> 
> Finally, the conditional variables initialization qemu_cond_init must
> also use pthread_condattr_setclock.
> 

Make sense! Will optimize the code in the next version. Thanks.

> Paolo

  reply	other threads:[~2022-02-21 14:42 UTC|newest]

Thread overview: 10+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2022-02-21  9:56 [RFC 0/2] qemu-sem-posix: use monotonic clock instead Longpeng(Mike) via
2022-02-21  9:56 ` [RFC 1/2] sem-posix: remove the posix semaphore support Longpeng(Mike) via
2022-02-21 11:11   ` Daniel P. Berrangé
2022-02-21 14:35     ` longpeng2--- via
2022-02-23  9:42     ` Paolo Bonzini
2022-02-21  9:56 ` [RFC 2/2] sem-posix: use monotonic clock instead Longpeng(Mike) via
2022-02-21 11:42   ` Paolo Bonzini
2022-02-21 14:39     ` longpeng2--- via [this message]
2022-02-21 11:31 ` [RFC 0/2] qemu-sem-posix: " Paolo Bonzini
2022-02-21 14:37   ` longpeng2--- via

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=1f7b0ebec553434495aad5945b0c4634@huawei.com \
    --to=qemu-devel@nongnu.org \
    --cc=arei.gonglei@huawei.com \
    --cc=berrange@redhat.com \
    --cc=longpeng2@huawei.com \
    --cc=mst@redhat.com \
    --cc=pbonzini@redhat.com \
    /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).