| Absolute File Name: | /home/qt/qt5_coco/qt5/qtbase/src/corelib/tools/qelapsedtimer_unix.cpp |
| Source code | Switch to Preprocessed file |
| Line | Source | Count |
|---|---|---|
| 1 | /**************************************************************************** | - |
| 2 | ** | - |
| 3 | ** Copyright (C) 2016 The Qt Company Ltd. | - |
| 4 | ** Copyright (C) 2016 Intel Corporation. | - |
| 5 | ** Contact: https://www.qt.io/licensing/ | - |
| 6 | ** | - |
| 7 | ** This file is part of the QtCore module of the Qt Toolkit. | - |
| 8 | ** | - |
| 9 | ** $QT_BEGIN_LICENSE:LGPL$ | - |
| 10 | ** Commercial License Usage | - |
| 11 | ** Licensees holding valid commercial Qt licenses may use this file in | - |
| 12 | ** accordance with the commercial license agreement provided with the | - |
| 13 | ** Software or, alternatively, in accordance with the terms contained in | - |
| 14 | ** a written agreement between you and The Qt Company. For licensing terms | - |
| 15 | ** and conditions see https://www.qt.io/terms-conditions. For further | - |
| 16 | ** information use the contact form at https://www.qt.io/contact-us. | - |
| 17 | ** | - |
| 18 | ** GNU Lesser General Public License Usage | - |
| 19 | ** Alternatively, this file may be used under the terms of the GNU Lesser | - |
| 20 | ** General Public License version 3 as published by the Free Software | - |
| 21 | ** Foundation and appearing in the file LICENSE.LGPL3 included in the | - |
| 22 | ** packaging of this file. Please review the following information to | - |
| 23 | ** ensure the GNU Lesser General Public License version 3 requirements | - |
| 24 | ** will be met: https://www.gnu.org/licenses/lgpl-3.0.html. | - |
| 25 | ** | - |
| 26 | ** GNU General Public License Usage | - |
| 27 | ** Alternatively, this file may be used under the terms of the GNU | - |
| 28 | ** General Public License version 2.0 or (at your option) the GNU General | - |
| 29 | ** Public license version 3 or any later version approved by the KDE Free | - |
| 30 | ** Qt Foundation. The licenses are as published by the Free Software | - |
| 31 | ** Foundation and appearing in the file LICENSE.GPL2 and LICENSE.GPL3 | - |
| 32 | ** included in the packaging of this file. Please review the following | - |
| 33 | ** information to ensure the GNU General Public License requirements will | - |
| 34 | ** be met: https://www.gnu.org/licenses/gpl-2.0.html and | - |
| 35 | ** https://www.gnu.org/licenses/gpl-3.0.html. | - |
| 36 | ** | - |
| 37 | ** $QT_END_LICENSE$ | - |
| 38 | ** | - |
| 39 | ****************************************************************************/ | - |
| 40 | - | |
| 41 | #include "qelapsedtimer.h" | - |
| 42 | #if defined(Q_OS_VXWORKS) | - |
| 43 | #include "qfunctions_vxworks.h" | - |
| 44 | #else | - |
| 45 | #include <sys/time.h> | - |
| 46 | #include <time.h> | - |
| 47 | #endif | - |
| 48 | #include <unistd.h> | - |
| 49 | - | |
| 50 | #include <qatomic.h> | - |
| 51 | #include "private/qcore_unix_p.h" | - |
| 52 | - | |
| 53 | #if defined(QT_NO_CLOCK_MONOTONIC) || defined(QT_BOOTSTRAPPED) | - |
| 54 | // turn off the monotonic clock | - |
| 55 | # ifdef _POSIX_MONOTONIC_CLOCK | - |
| 56 | # undef _POSIX_MONOTONIC_CLOCK | - |
| 57 | # endif | - |
| 58 | # define _POSIX_MONOTONIC_CLOCK -1 | - |
| 59 | #endif | - |
| 60 | - | |
| 61 | QT_BEGIN_NAMESPACE | - |
| 62 | - | |
| 63 | /* | - |
| 64 | * Design: | - |
| 65 | * | - |
| 66 | * POSIX offers a facility to select the system's monotonic clock when getting | - |
| 67 | * the current timestamp. Whereas the functions are mandatory in POSIX.1-2008, | - |
| 68 | * the presence of a monotonic clock is a POSIX Option (see the document | - |
| 69 | * http://pubs.opengroup.org/onlinepubs/9699919799/basedefs/V1_chap02.html#tag_02_01_06 ) | - |
| 70 | * | - |
| 71 | * The macro _POSIX_MONOTONIC_CLOCK can therefore assume the following values: | - |
| 72 | * -1 monotonic clock is never supported on this system | - |
| 73 | * 0 monotonic clock might be supported, runtime check is needed | - |
| 74 | * >1 (such as 200809L) monotonic clock is always supported | - |
| 75 | * | - |
| 76 | * The unixCheckClockType() function will return the clock to use: either | - |
| 77 | * CLOCK_MONOTONIC or CLOCK_REALTIME. In the case the POSIX option has a value | - |
| 78 | * of zero, then this function stores a static that contains the clock to be | - |
| 79 | * used. | - |
| 80 | * | - |
| 81 | * There's one extra case, which is when CLOCK_REALTIME isn't defined. When | - |
| 82 | * that's the case, we'll emulate the clock_gettime function with gettimeofday. | - |
| 83 | * | - |
| 84 | * Conforming to: | - |
| 85 | * POSIX.1b-1993 section "Clocks and Timers" | - |
| 86 | * included in UNIX98 (Single Unix Specification v2) | - |
| 87 | * included in POSIX.1-2001 | - |
| 88 | * see http://pubs.opengroup.org/onlinepubs/9699919799/functions/clock_getres.html | - |
| 89 | */ | - |
| 90 | - | |
| 91 | #if !defined(CLOCK_REALTIME) | - |
| 92 | # define CLOCK_REALTIME 0 | - |
| 93 | static inline void qt_clock_gettime(int, struct timespec *ts) | - |
| 94 | { | - |
| 95 | // support clock_gettime with gettimeofday | - |
| 96 | struct timeval tv; | - |
| 97 | gettimeofday(&tv, 0); | - |
| 98 | ts->tv_sec = tv.tv_sec; | - |
| 99 | ts->tv_nsec = tv.tv_usec * 1000; | - |
| 100 | } | - |
| 101 | - | |
| 102 | # ifdef _POSIX_MONOTONIC_CLOCK | - |
| 103 | # undef _POSIX_MONOTONIC_CLOCK | - |
| 104 | # define _POSIX_MONOTONIC_CLOCK -1 | - |
| 105 | # endif | - |
| 106 | #else | - |
| 107 | static inline void qt_clock_gettime(clockid_t clock, struct timespec *ts) | - |
| 108 | { | - |
| 109 | clock_gettime(clock, ts); | - |
| 110 | } | - |
| 111 | #endif | - |
| 112 | - | |
| 113 | static int unixCheckClockType() | - |
| 114 | { | - |
| #if#ifdef Q_OS_LINUX | ||
| 116 | // Despite glibc claiming that we should check at runtime, the Linux kernel | - |
| 117 | // always supports the monotonic clock | - |
| 118 | executed 19794545 times by 907 tests: return CLOCK_MONOTONIC;return 1;Executed by:
executed 19794545 times by 907 tests: return 1;Executed by:
| 19794545 |
| 119 | #elif (_POSIX_MONOTONIC_CLOCK-0 == 0) && defined(_SC_MONOTONIC_CLOCK) | - |
| 120 | // we need a value we can store in a clockid_t that isn't a valid clock | - |
| 121 | // check if the valid ones are both non-negative or both non-positive | - |
| 122 | # if CLOCK_MONOTONIC >= 0 && CLOCK_REALTIME >= 0 | - |
| 123 | # define IS_VALID_CLOCK(clock) (clock >= 0) | - |
| 124 | # define INVALID_CLOCK -1 | - |
| 125 | # elif CLOCK_MONOTONIC <= 0 && CLOCK_REALTIME <= 0 | - |
| 126 | # define IS_VALID_CLOCK(clock) (clock <= 0) | - |
| 127 | # define INVALID_CLOCK 1 | - |
| 128 | # else | - |
| 129 | # error "Sorry, your system has weird values for CLOCK_MONOTONIC and CLOCK_REALTIME" | - |
| 130 | # endif | - |
| 131 | - | |
| 132 | static QBasicAtomicInt clockToUse = Q_BASIC_ATOMIC_INITIALIZER(INVALID_CLOCK); | - |
| 133 | int clock = clockToUse.loadAcquire(); | - |
| 134 | if (Q_LIKELY(IS_VALID_CLOCK(clock))) | - |
| 135 | return clock; | - |
| 136 | - | |
| 137 | // detect if the system supports monotonic timers | - |
| 138 | clock = sysconf(_SC_MONOTONIC_CLOCK) > 0 ? CLOCK_MONOTONIC : CLOCK_REALTIME; | - |
| 139 | clockToUse.storeRelease(clock); | - |
| 140 | return clock; | - |
| 141 | - | |
| 142 | # undef INVALID_CLOCK | - |
| 143 | # undef IS_VALID_CLOCK | - |
| 144 | #elif (_POSIX_MONOTONIC_CLOCK-0) > 0 | - |
| 145 | return CLOCK_MONOTONIC; | - |
| 146 | #else | - |
| 147 | return CLOCK_REALTIME; | - |
| 148 | #endif | - |
| 149 | } | - |
| 150 | - | |
| 151 | bool QElapsedTimer::isMonotonic() Q_DECL_NOTHROW | - |
| 152 | { | - |
| 153 | return clockType() == MonotonicClock; executed 4145156 times by 666 tests: return clockType() == MonotonicClock;Executed by:
| 4145156 |
| 154 | } | - |
| 155 | - | |
| 156 | QElapsedTimer::ClockType QElapsedTimer::clockType() Q_DECL_NOTHROW | - |
| 157 | { | - |
| 158 | return unixCheckClockType() == CLOCK_REALTIME ? SystemTime : MonotonicClock; executed 6365588 times by 903 tests: return unixCheckClockType() == 0 ? SystemTime : MonotonicClock;Executed by:
| 6365588 |
| 159 | } | - |
| 160 | - | |
| 161 | static inline void do_gettime(qint64 *sec, qint64 *frac) | - |
| 162 | { | - |
| 163 | timespec ts; | - |
| 164 | qt_clock_gettime(unixCheckClockType(), &ts); | - |
| 165 | *sec = ts.tv_sec; | - |
| 166 | *frac = ts.tv_nsec; | - |
| 167 | } | - |
| 168 | - | |
| 169 | // used in qcore_unix.cpp and qeventdispatcher_unix.cpp | - |
| 170 | struct timespec qt_gettime() Q_DECL_NOTHROW | - |
| 171 | { | - |
| 172 | qint64 sec, frac; | - |
| 173 | do_gettime(&sec, &frac); | - |
| 174 | - | |
| 175 | timespec tv; | - |
| 176 | tv.tv_sec = sec; | - |
| 177 | tv.tv_nsec = frac; | - |
| 178 | - | |
| 179 | return tv; executed 7483893 times by 552 tests: return tv;Executed by:
| 7483893 |
| 180 | } | - |
| 181 | - | |
| 182 | void qt_nanosleep(timespec amount) | - |
| 183 | { | - |
| 184 | // We'd like to use clock_nanosleep. | - |
| 185 | // | - |
| 186 | // But clock_nanosleep is from POSIX.1-2001 and both are *not* | - |
| 187 | // affected by clock changes when using relative sleeps, even for | - |
| 188 | // CLOCK_REALTIME. | - |
| 189 | // | - |
| 190 | // nanosleep is POSIX.1-1993 | - |
| 191 | - | |
| 192 | int r; | - |
| 193 | EINTR_LOOP(r, nanosleep(&amount, &amount)); | - |
| 194 | } | - |
| 195 | - | |
| 196 | static qint64 elapsedAndRestart(qint64 sec, qint64 frac, | - |
| 197 | qint64 *nowsec, qint64 *nowfrac) | - |
| 198 | { | - |
| 199 | do_gettime(nowsec, nowfrac); | - |
| 200 | sec = *nowsec - sec; | - |
| 201 | frac = *nowfrac - frac; | - |
| 202 | return (sec * Q_INT64_C(1000000000) + frac) / Q_INT64_C(1000000); | - |
| 203 | } | - |
| 204 | - | |
| 205 | void QElapsedTimer::start() Q_DECL_NOTHROW | - |
| 206 | { | - |
| 207 | do_gettime(&t1, &t2); | - |
| 208 | } executed 3238936 times by 522 tests: end of blockExecuted by:
| 3238936 |
| 209 | - | |
| 210 | qint64 QElapsedTimer::restart() Q_DECL_NOTHROW | - |
| 211 | { | - |
| 212 | return elapsedAndRestart(t1, t2, &t1, &t2); executed 16652 times by 507 tests: return elapsedAndRestart(t1, t2, &t1, &t2);Executed by:
| 16652 |
| 213 | } | - |
| 214 | - | |
| 215 | qint64 QElapsedTimer::nsecsElapsed() const Q_DECL_NOTHROW | - |
| 216 | { | - |
| 217 | qint64 sec, frac; | - |
| 218 | do_gettime(&sec, &frac); | - |
| 219 | sec = sec - t1; | - |
| 220 | frac = frac - t2; | - |
| 221 | return sec * Q_INT64_C(1000000000) + frac; executed 2689476 times by 517 tests: return sec * static_cast<long long>(1000000000LL) + frac;Executed by:
| 2689476 |
| 222 | } | - |
| 223 | - | |
| 224 | qint64 QElapsedTimer::elapsed() const Q_DECL_NOTHROW | - |
| 225 | { | - |
| 226 | return nsecsElapsed() / Q_INT64_C(1000000); executed 2685921 times by 241 tests: return nsecsElapsed() / static_cast<long long>(1000000LL);Executed by:
| 2685921 |
| 227 | } | - |
| 228 | - | |
| 229 | qint64 QElapsedTimer::msecsSinceReference() const Q_DECL_NOTHROW | - |
| 230 | { | - |
| 231 | return t1 * Q_INT64_C(1000) + t2 / Q_INT64_C(1000000); executed 6 times by 1 test: return t1 * static_cast<long long>(1000LL) + t2 / static_cast<long long>(1000000LL);Executed by:
| 6 |
| 232 | } | - |
| 233 | - | |
| 234 | qint64 QElapsedTimer::msecsTo(const QElapsedTimer &other) const Q_DECL_NOTHROW | - |
| 235 | { | - |
| 236 | qint64 secs = other.t1 - t1; | - |
| 237 | qint64 fraction = other.t2 - t2; | - |
| 238 | return (secs * Q_INT64_C(1000000000) + fraction) / Q_INT64_C(1000000); executed 6983 times by 8 tests: return (secs * static_cast<long long>(1000000000LL) + fraction) / static_cast<long long>(1000000LL);Executed by:
| 6983 |
| 239 | } | - |
| 240 | - | |
| 241 | qint64 QElapsedTimer::secsTo(const QElapsedTimer &other) const Q_DECL_NOTHROW | - |
| 242 | { | - |
| 243 | return other.t1 - t1; executed 1 time by 1 test: return other.t1 - t1;Executed by:
| 1 |
| 244 | } | - |
| 245 | - | |
| 246 | bool operator<(const QElapsedTimer &v1, const QElapsedTimer &v2) Q_DECL_NOTHROW | - |
| 247 | { | - |
| 248 | return v1.t1 < v2.t1 || (v1.t1 == v2.t1 && v1.t2 < v2.t2); executed 2 times by 1 test: return v1.t1 < v2.t1 || (v1.t1 == v2.t1 && v1.t2 < v2.t2);Executed by:
| 2 |
| 249 | } | - |
| 250 | - | |
| 251 | QT_END_NAMESPACE | - |
| Source code | Switch to Preprocessed file |