[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
RE: [RFC 2/2] sem-posix: use monotonic clock instead
From: |
Longpeng (Mike, Cloud Infrastructure Service Product Dept.) |
Subject: |
RE: [RFC 2/2] sem-posix: use monotonic clock instead |
Date: |
Mon, 21 Feb 2022 14:39:18 +0000 |
> -----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