1/****************************************************************************
2**
3** Copyright (C) 2016 The Qt Company Ltd.
4** Contact: https://www.qt.io/licensing/
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 The Qt Company. For licensing terms
14** and conditions see https://www.qt.io/terms-conditions. For further
15** information use the contact form at https://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 3 as published by the Free Software
20** Foundation and appearing in the file LICENSE.LGPL3 included in the
21** packaging of this file. Please review the following information to
22** ensure the GNU Lesser General Public License version 3 requirements
23** will be met: https://www.gnu.org/licenses/lgpl-3.0.html.
24**
25** GNU General Public License Usage
26** Alternatively, this file may be used under the terms of the GNU
27** General Public License version 2.0 or (at your option) the GNU General
28** Public license version 3 or any later version approved by the KDE Free
29** Qt Foundation. The licenses are as published by the Free Software
30** Foundation and appearing in the file LICENSE.GPL2 and LICENSE.GPL3
31** included in the packaging of this file. Please review the following
32** information to ensure the GNU General Public License requirements will
33** be met: https://www.gnu.org/licenses/gpl-2.0.html and
34** https://www.gnu.org/licenses/gpl-3.0.html.
35**
36** $QT_END_LICENSE$
37**
38****************************************************************************/
39
40#include "qelapsedtimer.h"
41
42QT_BEGIN_NAMESPACE
43
44/*!
45 \class QElapsedTimer
46 \inmodule QtCore
47 \brief The QElapsedTimer class provides a fast way to calculate elapsed times.
48 \since 4.7
49
50 \reentrant
51 \ingroup tools
52
53 The QElapsedTimer class is usually used to quickly calculate how much
54 time has elapsed between two events. Its API is similar to that of QTime,
55 so code that was using that can be ported quickly to the new class.
56
57 However, unlike QTime, QElapsedTimer tries to use monotonic clocks if
58 possible. This means it's not possible to convert QElapsedTimer objects
59 to a human-readable time.
60
61 The typical use-case for the class is to determine how much time was
62 spent in a slow operation. The simplest example of such a case is for
63 debugging purposes, as in the following example:
64
65 \snippet qelapsedtimer/main.cpp 0
66
67 In this example, the timer is started by a call to start() and the
68 elapsed time is calculated by the elapsed() function.
69
70 The time elapsed can also be used to recalculate the time available for
71 another operation, after the first one is complete. This is useful when
72 the execution must complete within a certain time period, but several
73 steps are needed. The \tt{waitFor}-type functions in QIODevice and its
74 subclasses are good examples of such need. In that case, the code could
75 be as follows:
76
77 \snippet qelapsedtimer/main.cpp 1
78
79 Another use-case is to execute a certain operation for a specific
80 timeslice. For this, QElapsedTimer provides the hasExpired() convenience
81 function, which can be used to determine if a certain number of
82 milliseconds has already elapsed:
83
84 \snippet qelapsedtimer/main.cpp 2
85
86 It is often more convenient to use \l{QDeadlineTimer} in this case, which
87 counts towards a timeout in the future instead of tracking elapsed time.
88
89 \section1 Reference Clocks
90
91 QElapsedTimer will use the platform's monotonic reference clock in all
92 platforms that support it (see QElapsedTimer::isMonotonic()). This has
93 the added benefit that QElapsedTimer is immune to time adjustments, such
94 as the user correcting the time. Also unlike QTime, QElapsedTimer is
95 immune to changes in the timezone settings, such as daylight-saving
96 periods.
97
98 On the other hand, this means QElapsedTimer values can only be compared
99 with other values that use the same reference. This is especially true if
100 the time since the reference is extracted from the QElapsedTimer object
101 (QElapsedTimer::msecsSinceReference()) and serialised. These values
102 should never be exchanged across the network or saved to disk, since
103 there's no telling whether the computer node receiving the data is the
104 same as the one originating it or if it has rebooted since.
105
106 It is, however, possible to exchange the value with other processes
107 running on the same machine, provided that they also use the same
108 reference clock. QElapsedTimer will always use the same clock, so it's
109 safe to compare with the value coming from another process in the same
110 machine. If comparing to values produced by other APIs, you should check
111 that the clock used is the same as QElapsedTimer (see
112 QElapsedTimer::clockType()).
113
114 \section2 32-bit overflows
115
116 Some of the clocks used by QElapsedTimer have a limited range and may
117 overflow after hitting the upper limit (usually 32-bit). QElapsedTimer
118 deals with this overflow issue and presents a consistent timing. However,
119 when extracting the time since reference from QElapsedTimer, two
120 different processes in the same machine may have different understanding
121 of how much time has actually elapsed.
122
123 The information on which clocks types may overflow and how to remedy that
124 issue is documented along with the clock types.
125
126 \sa QTime, QTimer, QDeadlineTimer
127*/
128
129/*!
130 \enum QElapsedTimer::ClockType
131
132 This enum contains the different clock types that QElapsedTimer may use.
133
134 QElapsedTimer will always use the same clock type in a particular
135 machine, so this value will not change during the lifetime of a program.
136 It is provided so that QElapsedTimer can be used with other non-Qt
137 implementations, to guarantee that the same reference clock is being
138 used.
139
140 \value SystemTime The human-readable system time. This clock is not monotonic.
141 \value MonotonicClock The system's monotonic clock, usually found in Unix systems. This clock is monotonic and does not overflow.
142 \value TickCounter The system's tick counter, used on Windows systems. This clock may overflow.
143 \value MachAbsoluteTime The Mach kernel's absolute time (\macos and iOS). This clock is monotonic and does not overflow.
144 \value PerformanceCounter The high-resolution performance counter provided by Windows. This clock is monotonic and does not overflow.
145
146 \section2 SystemTime
147
148 The system time clock is purely the real time, expressed in milliseconds
149 since Jan 1, 1970 at 0:00 UTC. It's equivalent to the value returned by
150 the C and POSIX \tt{time} function, with the milliseconds added. This
151 clock type is currently only used on Unix systems that do not support
152 monotonic clocks (see below).
153
154 This is the only non-monotonic clock that QElapsedTimer may use.
155
156 \section2 MonotonicClock
157
158 This is the system's monotonic clock, expressed in milliseconds since an
159 arbitrary point in the past. This clock type is used on Unix systems
160 which support POSIX monotonic clocks (\tt{_POSIX_MONOTONIC_CLOCK}).
161
162 This clock does not overflow.
163
164 \section2 TickCounter
165
166 The tick counter clock type is based on the system's or the processor's
167 tick counter, multiplied by the duration of a tick. This clock type is
168 used on Windows platforms. If the high-precision performance
169 counter is available on Windows, the \tt{PerformanceCounter} clock type
170 is used instead.
171
172 The TickCounter clock type is the only clock type that may overflow.
173 Windows Vista and Windows Server 2008 support the extended 64-bit tick
174 counter, which allows avoiding the overflow.
175
176 On Windows systems, the clock overflows after 2^32 milliseconds, which
177 corresponds to roughly 49.7 days. This means two processes' reckoning of
178 the time since the reference may be different by multiples of 2^32
179 milliseconds. When comparing such values, it's recommended that the high
180 32 bits of the millisecond count be masked off.
181
182 \section2 MachAbsoluteTime
183
184 This clock type is based on the absolute time presented by Mach kernels,
185 such as that found on \macos. This clock type is presented separately
186 from MonotonicClock since \macos and iOS are also Unix systems and may support
187 a POSIX monotonic clock with values differing from the Mach absolute
188 time.
189
190 This clock is monotonic and does not overflow.
191
192 \section2 PerformanceCounter
193
194 This clock uses the Windows functions \tt{QueryPerformanceCounter} and
195 \tt{QueryPerformanceFrequency} to access the system's high-precision
196 performance counter. Since this counter may not be available on all
197 systems, QElapsedTimer will fall back to the \tt{TickCounter} clock
198 automatically, if this clock cannot be used.
199
200 This clock is monotonic and does not overflow.
201
202 \sa clockType(), isMonotonic()
203*/
204
205/*!
206 \fn QElapsedTimer::QElapsedTimer()
207 \since 5.4
208
209 Constructs an invalid QElapsedTimer. A timer becomes valid once it has been
210 started.
211
212 \sa isValid(), start()
213*/
214
215/*!
216 \fn bool QElapsedTimer::operator ==(const QElapsedTimer &other) const
217
218 Returns \c true if this object and \a other contain the same time.
219*/
220
221/*!
222 \fn bool QElapsedTimer::operator !=(const QElapsedTimer &other) const
223
224 Returns \c true if this object and \a other contain different times.
225*/
226
227static const qint64 invalidData = Q_INT64_C(0x8000000000000000);
228
229/*!
230 Marks this QElapsedTimer object as invalid.
231
232 An invalid object can be checked with isValid(). Calculations of timer
233 elapsed since invalid data are undefined and will likely produce bizarre
234 results.
235
236 \sa isValid(), start(), restart()
237*/
238void QElapsedTimer::invalidate() noexcept
239{
240 t1 = t2 = invalidData;
241}
242
243/*!
244 Returns \c false if the timer has never been started or invalidated by a
245 call to invalidate().
246
247 \sa invalidate(), start(), restart()
248*/
249bool QElapsedTimer::isValid() const noexcept
250{
251 return t1 != invalidData && t2 != invalidData;
252}
253
254/*!
255 Returns \c true if this QElapsedTimer has already expired by \a timeout
256 milliseconds (that is, more than \a timeout milliseconds have elapsed).
257 The value of \a timeout can be -1 to indicate that this timer does not
258 expire, in which case this function will always return false.
259
260 \sa elapsed(), QDeadlineTimer
261*/
262bool QElapsedTimer::hasExpired(qint64 timeout) const noexcept
263{
264 // if timeout is -1, quint64(timeout) is LLINT_MAX, so this will be
265 // considered as never expired
266 return quint64(elapsed()) > quint64(timeout);
267}
268
269QT_END_NAMESPACE
270