Re: [PATCH v1] Use CLOCK_MONOTONIC_RAW if available for get_clock().

2021-09-30 Thread Joe Tanen
Peter,

Thanks for the quick response. I've informally socialized the issue and will 
update this thread when I get more information.

That aside, I'd find using CLOCK_MONOTONIC_RAW valuable if, e.g., I wanted test 
an NTP daemon inside of a guest and didn't want the host providing an 
already-adjusted timebase. Would the behavior from my patch be more appropriate 
as a command-line option?

v/r
Joe

On 9/30/21, 12:11 PM, "Peter Maydell"  wrote:

On Thu, 30 Sept 2021 at 17:04, Joe Tanen  wrote:
>
> CLOCK_MONOTONIC_RAW provides an unadjusted system clock on some platforms,
> which is closer in spirit to providing a guest with a raw hardware clock 
than
> CLOCK_MONOTONIC.
>
> Using CLOCK_MONOTONIC_RAW also works around a current issue in OSX where
> CLOCK_MONOTONIC has been observed to go backwards.
>
> Since CLOCK_MONOTONIC_RAW might not be available on all platforms, revert 
to
> using CLOCK_MONOTONIC if it is not present.
>
> Signed-off-by: Joe Tanen 

I'm not sure we want to change behaviour everywhere to work
around an OSX bug, though...

Has this bug been reported to Apple ? Is there some kind of bug
report ID or URL we can quote in the commit message ?

-- PMM



Re: [PATCH v1] Use CLOCK_MONOTONIC_RAW if available for get_clock().

2021-09-30 Thread Peter Maydell
On Thu, 30 Sept 2021 at 17:04, Joe Tanen  wrote:
>
> CLOCK_MONOTONIC_RAW provides an unadjusted system clock on some platforms,
> which is closer in spirit to providing a guest with a raw hardware clock than
> CLOCK_MONOTONIC.
>
> Using CLOCK_MONOTONIC_RAW also works around a current issue in OSX where
> CLOCK_MONOTONIC has been observed to go backwards.
>
> Since CLOCK_MONOTONIC_RAW might not be available on all platforms, revert to
> using CLOCK_MONOTONIC if it is not present.
>
> Signed-off-by: Joe Tanen 

I'm not sure we want to change behaviour everywhere to work
around an OSX bug, though...

Has this bug been reported to Apple ? Is there some kind of bug
report ID or URL we can quote in the commit message ?

-- PMM



[PATCH v1] Use CLOCK_MONOTONIC_RAW if available for get_clock().

2021-09-30 Thread Joe Tanen
CLOCK_MONOTONIC_RAW provides an unadjusted system clock on some platforms,
which is closer in spirit to providing a guest with a raw hardware clock than
CLOCK_MONOTONIC.

Using CLOCK_MONOTONIC_RAW also works around a current issue in OSX where
CLOCK_MONOTONIC has been observed to go backwards.

Since CLOCK_MONOTONIC_RAW might not be available on all platforms, revert to
using CLOCK_MONOTONIC if it is not present.

Signed-off-by: Joe Tanen 
---
 include/qemu/timer.h |  3 ++-
 util/qemu-timer-common.c | 11 +++
 2 files changed, 13 insertions(+), 1 deletion(-)

diff --git a/include/qemu/timer.h b/include/qemu/timer.h
index 88ef114689..fb8f5074df 100644
--- a/include/qemu/timer.h
+++ b/include/qemu/timer.h
@@ -828,12 +828,13 @@ static inline int64_t get_clock(void)
 #else
 
 extern int use_rt_clock;
+extern clockid_t rt_clock;
 
 static inline int64_t get_clock(void)
 {
 if (use_rt_clock) {
 struct timespec ts;
-clock_gettime(CLOCK_MONOTONIC, );
+clock_gettime(rt_clock, );
 return ts.tv_sec * 10LL + ts.tv_nsec;
 } else {
 /* XXX: using gettimeofday leads to problems if the date
diff --git a/util/qemu-timer-common.c b/util/qemu-timer-common.c
index cc1326f726..5039c5406c 100644
--- a/util/qemu-timer-common.c
+++ b/util/qemu-timer-common.c
@@ -49,13 +49,24 @@ static void __attribute__((constructor)) 
init_get_clock(void)
 #else
 
 int use_rt_clock;
+clockid_t rt_clock;
 
 static void __attribute__((constructor)) init_get_clock(void)
 {
 struct timespec ts;
 
 use_rt_clock = 0;
+#if (defined(__APPLE__) || defined(__linux__)) && defined(CLOCK_MONOTONIC_RAW)
+/* CLOCK_MONOTONIC_RAW is not available on all platforms or with all
+ * compiler flags.
+ */
+if (clock_gettime(CLOCK_MONOTONIC_RAW, ) == 0) {
+rt_clock = CLOCK_MONOTONIC_RAW;
+use_rt_clock = 1;
+} else
+#endif
 if (clock_gettime(CLOCK_MONOTONIC, ) == 0) {
+rt_clock = CLOCK_MONOTONIC;
 use_rt_clock = 1;
 }
 clock_start = get_clock();
-- 
2.31.1