summaryrefslogtreecommitdiff
path: root/os
diff options
context:
space:
mode:
authorDaniel Stone <daniel@fooishbar.org>2010-08-27 20:36:37 +1000
committerKeith Packard <keithp@keithp.com>2010-12-27 09:43:44 -0800
commit44adb31bfece29260a9bbd9075c9212ebf00d24d (patch)
tree630914bb83dc8bf853ad7fbede70fd5fbb43add4 /os
parente06fa804009798ea95efa8babaabb0228dfdfe65 (diff)
GetTimeInMillis: Use CLOCK_MONOTONIC_COARSE where available
On some systems, using CLOCK_MONOTONIC forces a readback of HPET or some similarly expensive timer. CLOCK_MONOTONIC_COARSE can alleviate this, at the cost of negligibly-reduced resolution, so prefer that where we can. Signed-off-by: Daniel Stone <daniel@fooishbar.org> Reviewed-by: Julien Cristau <jcristau@debian.org> Reviewed-by: Tiago Vignatti <tiago.vignatti@nokia.com>
Diffstat (limited to 'os')
-rw-r--r--os/utils.c16
1 files changed, 15 insertions, 1 deletions
diff --git a/os/utils.c b/os/utils.c
index afdff0c5a..18fd91151 100644
--- a/os/utils.c
+++ b/os/utils.c
@@ -427,7 +427,21 @@ GetTimeInMillis(void)
#ifdef MONOTONIC_CLOCK
struct timespec tp;
- if (clock_gettime(CLOCK_MONOTONIC, &tp) == 0)
+ static clockid_t clockid;
+ if (!clockid) {
+#ifdef CLOCK_MONOTONIC_COARSE
+ if (clock_getres(CLOCK_MONOTONIC_COARSE, &tp) == 0 &&
+ (tp.tv_nsec / 1000) <= 1000 &&
+ clock_gettime(CLOCK_MONOTONIC_COARSE, &tp) == 0)
+ clockid = CLOCK_MONOTONIC_COARSE;
+ else
+#endif
+ if (clock_gettime(CLOCK_MONOTONIC, &tp) == 0)
+ clockid = CLOCK_MONOTONIC;
+ else
+ clockid = ~0L;
+ }
+ if (clockid != ~0L && clock_gettime(clockid, &tp) == 0)
return (tp.tv_sec * 1000) + (tp.tv_nsec / 1000000L);
#endif