tools/qelapsedtimer_unix.cpp

Source codeSwitch to Preprocessed file
LineSource CodeCoverage
1/**************************************************************************** -
2** -
3** Copyright (C) 2013 Digia Plc and/or its subsidiary(-ies). -
4** Contact: http://www.qt-project.org/legal -
5** -
6** This file is part of the QtCore module of the Qt Toolkit. -
7** -
8** $QT_BEGIN_LICENSE:LGPL$ -
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 Digia. For licensing terms and -
14** conditions see http://qt.digia.com/licensing. For further information -
15** use the contact form at http://qt.digia.com/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 as published by the Free Software -
20** Foundation and appearing in the file LICENSE.LGPL included in the -
21** packaging of this file. Please review the following information to -
22** ensure the GNU Lesser General Public License version 2.1 requirements -
23** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. -
24** -
25** In addition, as a special exception, Digia gives you certain additional -
26** rights. These rights are described in the Digia Qt LGPL Exception -
27** version 1.1, included in the file LGPL_EXCEPTION.txt in this package. -
28** -
29** GNU General Public License Usage -
30** Alternatively, this file may be used under the terms of the GNU -
31** General Public License version 3.0 as published by the Free Software -
32** Foundation and appearing in the file LICENSE.GPL included in the -
33** packaging of this file. Please review the following information to -
34** ensure the GNU General Public License version 3.0 requirements will be -
35** met: http://www.gnu.org/copyleft/gpl.html. -
36** -
37** -
38** $QT_END_LICENSE$ -
39** -
40****************************************************************************/ -
41 -
42// ask for the latest POSIX, just in case -
43#define _POSIX_C_SOURCE 200809L -
44 -
45#include "qelapsedtimer.h" -
46#include <sys/time.h> -
47#include <time.h> -
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 -
61QT_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#ifndef CLOCK_REALTIME -
92# define CLOCK_REALTIME 0 -
93static 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 -
107static inline void qt_clock_gettime(clockid_t clock, struct timespec *ts) -
108{ -
109 clock_gettime(clock, ts);
executed (the execution status of this line is deduced): clock_gettime(clock, ts);
-
110}
executed: }
Execution Count:7883884
7883884
111#endif -
112 -
113static int unixCheckClockType() -
114{ -
115#if (_POSIX_MONOTONIC_CLOCK-0 == 0) && defined(_SC_MONOTONIC_CLOCK) -
116 // we need a value we can store in a clockid_t that isn't a valid clock -
117 // check if the valid ones are both non-negative or both non-positive -
118# if CLOCK_MONOTONIC >= 0 && CLOCK_REALTIME >= 0 -
119# define IS_VALID_CLOCK(clock) (clock >= 0) -
120# define INVALID_CLOCK -1 -
121# elif CLOCK_MONOTONIC <= 0 && CLOCK_REALTIME <= 0 -
122# define IS_VALID_CLOCK(clock) (clock <= 0) -
123# define INVALID_CLOCK 1 -
124# else -
125# error "Sorry, your system has weird values for CLOCK_MONOTONIC and CLOCK_REALTIME" -
126# endif -
127 -
128 static QBasicAtomicInt clockToUse = Q_BASIC_ATOMIC_INITIALIZER(INVALID_CLOCK); -
129 int clock = clockToUse.loadAcquire();
executed (the execution status of this line is deduced): int clock = clockToUse.loadAcquire();
-
130 if (Q_LIKELY(IS_VALID_CLOCK(clock)))
evaluated: __builtin_expect(!!((clock >= 0)), true)
TRUEFALSE
yes
Evaluation Count:10264550
yes
Evaluation Count:8
8-10264550
131 return clock;
executed: return clock;
Execution Count:10266141
10266141
132 -
133 // detect if the system supports monotonic timers -
134 clock = sysconf(_SC_MONOTONIC_CLOCK) > 0 ? CLOCK_MONOTONIC : CLOCK_REALTIME;
partially evaluated: sysconf(_SC_MONOTONIC_CLOCK) > 0
TRUEFALSE
yes
Evaluation Count:8
no
Evaluation Count:0
0-8
135 clockToUse.storeRelease(clock);
executed (the execution status of this line is deduced): clockToUse.storeRelease(clock);
-
136 return clock;
executed: return clock;
Execution Count:8
8
137 -
138# undef INVALID_CLOCK -
139# undef IS_VALID_CLOCK -
140#elif (_POSIX_MONOTONIC_CLOCK-0) > 0 -
141 return CLOCK_MONOTONIC; -
142#else -
143 return CLOCK_REALTIME; -
144#endif -
145} -
146 -
147static inline qint64 fractionAdjustment() -
148{ -
149 return 1000*1000ull;
executed: return 1000*1000ull;
Execution Count:2437196
2437196
150} -
151 -
152bool QElapsedTimer::isMonotonic() Q_DECL_NOTHROW -
153{ -
154 return clockType() == MonotonicClock;
executed: return clockType() == MonotonicClock;
Execution Count:2385222
2385222
155} -
156 -
157QElapsedTimer::ClockType QElapsedTimer::clockType() Q_DECL_NOTHROW -
158{ -
159 return unixCheckClockType() == CLOCK_REALTIME ? SystemTime : MonotonicClock;
executed: return unixCheckClockType() == 0 ? SystemTime : MonotonicClock;
Execution Count:2385280
2385280
160} -
161 -
162static inline void do_gettime(qint64 *sec, qint64 *frac) -
163{ -
164 timespec ts;
executed (the execution status of this line is deduced): timespec ts;
-
165 qt_clock_gettime(unixCheckClockType(), &ts);
executed (the execution status of this line is deduced): qt_clock_gettime(unixCheckClockType(), &ts);
-
166 *sec = ts.tv_sec;
executed (the execution status of this line is deduced): *sec = ts.tv_sec;
-
167 *frac = ts.tv_nsec;
executed (the execution status of this line is deduced): *frac = ts.tv_nsec;
-
168}
executed: }
Execution Count:7883993
7883993
169 -
170// used in qcore_unix.cpp and qeventdispatcher_unix.cpp -
171timeval qt_gettime() Q_DECL_NOTHROW -
172{ -
173 qint64 sec, frac;
executed (the execution status of this line is deduced): qint64 sec, frac;
-
174 do_gettime(&sec, &frac);
executed (the execution status of this line is deduced): do_gettime(&sec, &frac);
-
175 -
176 timeval tv;
executed (the execution status of this line is deduced): timeval tv;
-
177 tv.tv_sec = sec;
executed (the execution status of this line is deduced): tv.tv_sec = sec;
-
178 tv.tv_usec = frac / 1000;
executed (the execution status of this line is deduced): tv.tv_usec = frac / 1000;
-
179 -
180 return tv;
executed: return tv;
Execution Count:1123443
1123443
181} -
182 -
183void qt_nanosleep(timespec amount) -
184{ -
185 // We'd like to use clock_nanosleep. -
186 // -
187 // But clock_nanosleep is from POSIX.1-2001 and both are *not* -
188 // affected by clock changes when using relative sleeps, even for -
189 // CLOCK_REALTIME. -
190 // -
191 // nanosleep is POSIX.1-1993 -
192 -
193 int r;
executed (the execution status of this line is deduced): int r;
-
194 EINTR_LOOP(r, nanosleep(&amount, &amount));
executed: }
Execution Count:414261
partially evaluated: r == -1
TRUEFALSE
no
Evaluation Count:0
yes
Evaluation Count:414252
never evaluated: (*__errno_location ()) == 4
0-414261
195}
executed: }
Execution Count:414247
414247
196 -
197static qint64 elapsedAndRestart(qint64 sec, qint64 frac, -
198 qint64 *nowsec, qint64 *nowfrac) -
199{ -
200 do_gettime(nowsec, nowfrac);
executed (the execution status of this line is deduced): do_gettime(nowsec, nowfrac);
-
201 sec = *nowsec - sec;
executed (the execution status of this line is deduced): sec = *nowsec - sec;
-
202 frac = *nowfrac - frac;
executed (the execution status of this line is deduced): frac = *nowfrac - frac;
-
203 return sec * Q_INT64_C(1000) + frac / fractionAdjustment();
executed: return sec * static_cast<long long>(1000LL) + frac / fractionAdjustment();
Execution Count:2386771
2386771
204} -
205 -
206void QElapsedTimer::start() Q_DECL_NOTHROW -
207{ -
208 do_gettime(&t1, &t2);
executed (the execution status of this line is deduced): do_gettime(&t1, &t2);
-
209}
executed: }
Execution Count:4373639
4373639
210 -
211qint64 QElapsedTimer::restart() Q_DECL_NOTHROW -
212{ -
213 return elapsedAndRestart(t1, t2, &t1, &t2);
executed: return elapsedAndRestart(t1, t2, &t1, &t2);
Execution Count:916
916
214} -
215 -
216qint64 QElapsedTimer::nsecsElapsed() const Q_DECL_NOTHROW -
217{ -
218 qint64 sec, frac;
executed (the execution status of this line is deduced): qint64 sec, frac;
-
219 do_gettime(&sec, &frac);
executed (the execution status of this line is deduced): do_gettime(&sec, &frac);
-
220 sec = sec - t1;
executed (the execution status of this line is deduced): sec = sec - t1;
-
221 frac = frac - t2;
executed (the execution status of this line is deduced): frac = frac - t2;
-
222 return sec * Q_INT64_C(1000000000) + frac;
executed: return sec * static_cast<long long>(1000000000LL) + frac;
Execution Count:295
295
223} -
224 -
225qint64 QElapsedTimer::elapsed() const Q_DECL_NOTHROW -
226{ -
227 qint64 sec, frac;
executed (the execution status of this line is deduced): qint64 sec, frac;
-
228 return elapsedAndRestart(t1, t2, &sec, &frac);
executed: return elapsedAndRestart(t1, t2, &sec, &frac);
Execution Count:2374730
2374730
229} -
230 -
231qint64 QElapsedTimer::msecsSinceReference() const Q_DECL_NOTHROW -
232{ -
233 return t1 * Q_INT64_C(1000) + t2 / fractionAdjustment();
executed: return t1 * static_cast<long long>(1000LL) + t2 / fractionAdjustment();
Execution Count:4
4
234} -
235 -
236qint64 QElapsedTimer::msecsTo(const QElapsedTimer &other) const Q_DECL_NOTHROW -
237{ -
238 qint64 secs = other.t1 - t1;
executed (the execution status of this line is deduced): qint64 secs = other.t1 - t1;
-
239 qint64 fraction = other.t2 - t2;
executed (the execution status of this line is deduced): qint64 fraction = other.t2 - t2;
-
240 return secs * Q_INT64_C(1000) + fraction / fractionAdjustment();
executed: return secs * static_cast<long long>(1000LL) + fraction / fractionAdjustment();
Execution Count:50300
50300
241} -
242 -
243qint64 QElapsedTimer::secsTo(const QElapsedTimer &other) const Q_DECL_NOTHROW -
244{ -
245 return other.t1 - t1;
executed: return other.t1 - t1;
Execution Count:1
1
246} -
247 -
248bool operator<(const QElapsedTimer &v1, const QElapsedTimer &v2) Q_DECL_NOTHROW -
249{ -
250 return v1.t1 < v2.t1 || (v1.t1 == v2.t1 && v1.t2 < v2.t2);
executed: return v1.t1 < v2.t1 || (v1.t1 == v2.t1 && v1.t2 < v2.t2);
Execution Count:2
2
251} -
252 -
253QT_END_NAMESPACE -
254 -
Source codeSwitch to Preprocessed file

Generated by Squish Coco Non-Commercial