From 98c62f920d6b8b2ab196f449be2b569074618835 Mon Sep 17 00:00:00 2001 From: Ivailo Monev Date: Fri, 8 Jan 2021 12:25:03 +0200 Subject: [PATCH] always use clock_gettime() in QElapsedTimer() and qt_gettime() upstream commits: https://github.com/qt/qtbase/commit/46e2c9441761f75f19b2780e8f1616102128dedd https://github.com/qt/qtbase/commit/51ddaabb81ae55d13aaabfdf45ee7db4d7c9bba2 Signed-off-by: Ivailo Monev --- README | 2 +- src/core/tools/qelapsedtimer_unix.cpp | 45 +++++++++-------------------------- 2 files changed, 12 insertions(+), 35 deletions(-) diff --git a/README b/README index 128e8a6a5..84ef6ded4 100644 --- a/README +++ b/README @@ -84,7 +84,7 @@ QTBUG-25114, QTBUG-24672, QTBUG-23524 (WIP), QTBUG-56088, QTBUG-42189, QTBUG-39285, QTBUG-18173, QTBUG-28968, QTBUG-34336, QTBUG-40974, QTBUG-44286, QTBUG-12564, QTBUG-20028, QTBUG-71967, QTBUG-70956, QTBUG-71446, QTBUG-61307, QTBUG-27287, QTBUG-25143, QTBUG-22833, QTBUG-57399, QTBUG-59159, QTBUG-15773, -QTBUG-70506, QTBUG-46054, QTBUG-11223, QTBUG-63108, QTBUG-6932 +QTBUG-70506, QTBUG-46054, QTBUG-11223, QTBUG-63108, QTBUG-6932, QTBUG-42365 Unless you use QMake and QDoc porting to Katie or even supporting it along with Qt4 in the same codebase is trivial and requires only minor changes because diff --git a/src/core/tools/qelapsedtimer_unix.cpp b/src/core/tools/qelapsedtimer_unix.cpp index f8db8809d..6e30b9367 100644 --- a/src/core/tools/qelapsedtimer_unix.cpp +++ b/src/core/tools/qelapsedtimer_unix.cpp @@ -45,18 +45,6 @@ static const bool monotonicClockAvailable = (sysconf(_SC_MONOTONIC_CLOCK) >= 200 static const bool monotonicClockAvailable = (_POSIX_MONOTONIC_CLOCK > 0); #endif -static inline qint64 fractionAdjustment() -{ - if (Q_LIKELY(monotonicClockAvailable)) { - // the monotonic timer is measured in nanoseconds - // 1 ms = 1000000 ns - return 1000*1000ull; - } - // gettimeofday is measured in microseconds - // 1 ms = 1000 us - return 1000; -} - bool QElapsedTimer::isMonotonic() { return monotonicClockAvailable; @@ -72,18 +60,14 @@ QElapsedTimer::ClockType QElapsedTimer::clockType() static inline void do_gettime(qint64 *sec, qint64 *frac) { + timespec ts; if (Q_LIKELY(monotonicClockAvailable)) { - timespec ts; ::clock_gettime(CLOCK_MONOTONIC, &ts); - *sec = ts.tv_sec; - *frac = ts.tv_nsec; - return; + } else { + ::clock_gettime(CLOCK_REALTIME, &ts); } - // use gettimeofday - struct timeval tv; - ::gettimeofday(&tv, Q_NULLPTR); - *sec = tv.tv_sec; - *frac = tv.tv_usec; + *sec = ts.tv_sec; + *frac = ts.tv_nsec; } // used in qcore_unix.cpp and qeventdispatcher_unix.cpp @@ -94,11 +78,7 @@ timeval qt_gettime() timeval tv; tv.tv_sec = sec; - if (Q_LIKELY(monotonicClockAvailable)) { - tv.tv_usec = frac / 1000; - } else { - tv.tv_usec = frac; - } + tv.tv_usec = frac / 1000; return tv; } @@ -108,7 +88,7 @@ static inline qint64 elapsedAndRestart(qint64 sec, qint64 frac, do_gettime(nowsec, nowfrac); sec = *nowsec - sec; frac = *nowfrac - frac; - return sec * Q_INT64_C(1000) + frac / fractionAdjustment(); + return (sec * Q_INT64_C(1000000000) + frac) / Q_INT64_C(1000000); } void QElapsedTimer::start() @@ -127,27 +107,24 @@ qint64 QElapsedTimer::nsecsElapsed() const do_gettime(&sec, &frac); sec = sec - t1; frac = frac - t2; - if (Q_UNLIKELY(!monotonicClockAvailable)) - frac *= 1000; - return sec * Q_INT64_C(1000000000) + frac; + return (sec * Q_INT64_C(1000000000) + frac); } qint64 QElapsedTimer::elapsed() const { - qint64 sec, frac; - return elapsedAndRestart(t1, t2, &sec, &frac); + return (nsecsElapsed() / Q_INT64_C(1000000)); } qint64 QElapsedTimer::msecsSinceReference() const { - return t1 * Q_INT64_C(1000) + t2 / fractionAdjustment(); + return (t1 * Q_INT64_C(1000) + t2 / Q_INT64_C(1000000)); } qint64 QElapsedTimer::msecsTo(const QElapsedTimer &other) const { qint64 secs = other.t1 - t1; qint64 fraction = other.t2 - t2; - return secs * Q_INT64_C(1000) + fraction / fractionAdjustment(); + return (secs * Q_INT64_C(1000000000) + fraction) / Q_INT64_C(1000000);; } qint64 QElapsedTimer::secsTo(const QElapsedTimer &other) const -- 2.11.0