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