diff options
author | Thiago Macieira <thiago.macieira@intel.com> | 2012-08-10 15:00:50 +0200 |
---|---|---|
committer | Qt by Nokia <qt-info@nokia.com> | 2012-08-13 09:34:10 +0200 |
commit | 46e2c9441761f75f19b2780e8f1616102128dedd (patch) | |
tree | ce5fcf273299e9cd33faa28263d4553acae305c7 /src/corelib/tools | |
parent | bc6568291a1cd01a2552ea2570b271c375b441d1 (diff) |
Make QElapsedTimer always store nanoseconds in t2
Up until now, the value stored in t2 depended on whether we had found
a monotonic clock or not. Fix that by always storing nanoseconds: we
avoid extra calculations and accessing a global variable all the time.
The impact is contained to the actual getting of the time. And we
mitigate by using clock_gettime to get the realtime clock instead of
gettimeofday, if that's available.
Change-Id: Iceef2d050fd9472f1a66e11e7ded79fe5163a132
Reviewed-by: Olivier Goffart <ogoffart@woboq.com>
Diffstat (limited to 'src/corelib/tools')
-rw-r--r-- | src/corelib/tools/qelapsedtimer_unix.cpp | 33 |
1 files changed, 15 insertions, 18 deletions
diff --git a/src/corelib/tools/qelapsedtimer_unix.cpp b/src/corelib/tools/qelapsedtimer_unix.cpp index b4e72b066d..3b2c0ed1f3 100644 --- a/src/corelib/tools/qelapsedtimer_unix.cpp +++ b/src/corelib/tools/qelapsedtimer_unix.cpp @@ -39,6 +39,9 @@ ** ****************************************************************************/ +// ask for the latest POSIX, just in case +#define _POSIX_C_SOURCE 200809L + #include "qelapsedtimer.h" #include <sys/time.h> #include <time.h> @@ -83,18 +86,7 @@ static void unixCheckClockType() static inline qint64 fractionAdjustment() { - // disabled, but otherwise indicates bad usage of QElapsedTimer - //Q_ASSERT(monotonicClockChecked); - - if (monotonicClockAvailable) { - // the monotonic timer is measured in nanoseconds - // 1 ms = 1000000 ns - return 1000*1000ull; - } else { - // gettimeofday is measured in microseconds - // 1 ms = 1000 us - return 1000; - } + return 1000*1000ull; } bool QElapsedTimer::isMonotonic() Q_DECL_NOTHROW @@ -121,11 +113,20 @@ static inline void do_gettime(qint64 *sec, qint64 *frac) return; } #endif +#ifdef CLOCK_REALTIME + // even if we don't have a monotonic clock, + // we can use clock_gettime -> nanosecond resolution + timespec ts; + clock_gettime(CLOCK_REALTIME, &ts); + *sec = ts.tv_sec; + *frac = ts.tv_nsec; +#else // use gettimeofday timeval tv; ::gettimeofday(&tv, 0); *sec = tv.tv_sec; - *frac = tv.tv_usec; + *frac = tv.tv_usec * 1000; +#endif } // used in qcore_unix.cpp and qeventdispatcher_unix.cpp @@ -136,9 +137,7 @@ timeval qt_gettime() Q_DECL_NOTHROW timeval tv; tv.tv_sec = sec; - tv.tv_usec = frac; - if (monotonicClockAvailable) - tv.tv_usec /= 1000; + tv.tv_usec = frac / 1000; return tv; } @@ -168,8 +167,6 @@ qint64 QElapsedTimer::nsecsElapsed() const Q_DECL_NOTHROW do_gettime(&sec, &frac); sec = sec - t1; frac = frac - t2; - if (!monotonicClockAvailable) - frac *= 1000; return sec * Q_INT64_C(1000000000) + frac; } |