From 891efca9d79d4532f6bbe763d8f90ba837bbe0a4 Mon Sep 17 00:00:00 2001 From: Dudemanguy Date: Fri, 27 Oct 2023 13:11:30 -0500 Subject: [PATCH] timer-linux: fallback to CLOCK_MONOTONIC instead of timespec_get CLOCK_MONOTONIC_RAW is linux-specific (macOS later supported it but it has its own timer code) and not neccessarily available everywhere like on BSDs. It makes sense to prefer it because mpv does a lot of measurements at small intervals (e.g. every frame) so theoretically it should be more accurate. However if the OS doesn't have it, fallback to CLOCK_MONOTONIC instead which is almost exactly the same and very widely supported across unix-like systems. This clock is technically optional according to POSIX, but any half-decent OS supports it anyway (sorry Solaris users). As a benefit, we now know that the clock from mp_time is always monotonic. --- osdep/timer-linux.c | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/osdep/timer-linux.c b/osdep/timer-linux.c index 25bb70bccad3f..bcae787645838 100644 --- a/osdep/timer-linux.c +++ b/osdep/timer-linux.c @@ -35,11 +35,12 @@ void mp_sleep_ns(int64_t ns) uint64_t mp_raw_time_ns(void) { struct timespec tp = {0}; + int ret = -1; #if defined(CLOCK_MONOTONIC_RAW) - clock_gettime(CLOCK_MONOTONIC_RAW, &tp); -#else - timespec_get(&tp, TIME_UTC); + ret = clock_gettime(CLOCK_MONOTONIC_RAW, &tp); #endif + if (ret) + clock_gettime(CLOCK_MONOTONIC, &tp); return MP_TIME_S_TO_NS(tp.tv_sec) + tp.tv_nsec; }