From 529b4bd185eb130580c09c24450d80d62c9ae769 Mon Sep 17 00:00:00 2001 From: Max Kellermann Date: Sun, 24 Nov 2013 21:14:38 +0100 Subject: Stats: use monotonic clock instead of GTimer Reduce GLib usage. --- src/system/Clock.cxx | 22 ++++++++++++++++++++++ src/system/Clock.hxx | 7 +++++++ 2 files changed, 29 insertions(+) (limited to 'src/system') diff --git a/src/system/Clock.cxx b/src/system/Clock.cxx index 17b221c15..8fbd76d22 100644 --- a/src/system/Clock.cxx +++ b/src/system/Clock.cxx @@ -30,6 +30,28 @@ #endif #endif +unsigned +MonotonicClockS(void) +{ +#ifdef WIN32 + return GetTickCount() / 1000; +#elif defined(__APPLE__) /* OS X does not define CLOCK_MONOTONIC */ + static mach_timebase_info_data_t base; + if (base.denom == 0) + (void)mach_timebase_info(&base); + + return (unsigned)((mach_absolute_time() * base.numer / 1000) + / (1000000 * base.denom)); +#elif defined(CLOCK_MONOTONIC) + struct timespec ts; + clock_gettime(CLOCK_MONOTONIC, &ts); + return ts.tv_sec; +#else + /* we have no monotonic clock, fall back to time() */ + return time(nullptr); +#endif +} + unsigned MonotonicClockMS(void) { diff --git a/src/system/Clock.hxx b/src/system/Clock.hxx index 28df0c7e0..1c3651a99 100644 --- a/src/system/Clock.hxx +++ b/src/system/Clock.hxx @@ -24,6 +24,13 @@ #include +/** + * Returns the value of a monotonic clock in seconds. + */ +gcc_pure +unsigned +MonotonicClockS(); + /** * Returns the value of a monotonic clock in milliseconds. */ -- cgit v1.2.3