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 #include "qdeadlinetimer.h"
42 #include "qdeadlinetimer_p.h"
43 #include <qt_windows.h>
44
45 QT_BEGIN_NAMESPACE
46
47 // Result of QueryPerformanceFrequency, 0 indicates that the high resolution timer is unavailable
48 static quint64 counterFrequency = 0;
49
resolveCounterFrequency()50 static void resolveCounterFrequency()
51 {
52 static bool done = false;
53 if (done)
54 return;
55
56 // Retrieve the number of high-resolution performance counter ticks per second
57 LARGE_INTEGER frequency;
58 if (!QueryPerformanceFrequency(&frequency)) {
59 qFatal("QueryPerformanceFrequency failed, even though Microsoft documentation promises it wouldn't.");
60 counterFrequency = 0;
61 } else {
62 counterFrequency = frequency.QuadPart;
63 }
64
65 done = true;
66 }
67
ticksToNanoseconds(qint64 ticks)68 static inline qint64 ticksToNanoseconds(qint64 ticks)
69 {
70 if (counterFrequency > 0) {
71 // QueryPerformanceCounter uses an arbitrary frequency
72 qint64 seconds = ticks / counterFrequency;
73 qint64 nanoSeconds = (ticks - seconds * counterFrequency) * 1000000000 / counterFrequency;
74 return seconds * 1000000000 + nanoSeconds;
75 }
76 // GetTickCount(64) returns milliseconds
77 return ticks * 1000000;
78 }
79
80
getTickCount()81 static quint64 getTickCount()
82 {
83 resolveCounterFrequency();
84
85 // This avoids a division by zero and disables the high performance counter if it's not available
86 if (counterFrequency > 0) {
87 LARGE_INTEGER counter;
88
89 bool ok = QueryPerformanceCounter(&counter);
90 Q_ASSERT_X(ok, "QElapsedTimer::start()",
91 "QueryPerformanceCounter failed, although QueryPerformanceFrequency succeeded.");
92 Q_UNUSED(ok);
93 return counter.QuadPart;
94 }
95
96 return GetTickCount64();
97 }
98
qt_msectime()99 quint64 qt_msectime()
100 {
101 return ticksToNanoseconds(getTickCount()) / 1000000;
102 }
103
clockType()104 QElapsedTimer::ClockType QElapsedTimer::clockType() noexcept
105 {
106 resolveCounterFrequency();
107
108 return counterFrequency > 0 ? PerformanceCounter : TickCounter;
109 }
110
isMonotonic()111 bool QElapsedTimer::isMonotonic() noexcept
112 {
113 return true;
114 }
115
start()116 void QElapsedTimer::start() noexcept
117 {
118 t1 = getTickCount();
119 t2 = 0;
120 }
121
restart()122 qint64 QElapsedTimer::restart() noexcept
123 {
124 qint64 oldt1 = t1;
125 t1 = getTickCount();
126 t2 = 0;
127 return ticksToNanoseconds(t1 - oldt1) / 1000000;
128 }
129
nsecsElapsed() const130 qint64 QElapsedTimer::nsecsElapsed() const noexcept
131 {
132 qint64 elapsed = getTickCount() - t1;
133 return ticksToNanoseconds(elapsed);
134 }
135
elapsed() const136 qint64 QElapsedTimer::elapsed() const noexcept
137 {
138 qint64 elapsed = getTickCount() - t1;
139 return ticksToNanoseconds(elapsed) / 1000000;
140 }
141
msecsSinceReference() const142 qint64 QElapsedTimer::msecsSinceReference() const noexcept
143 {
144 return ticksToNanoseconds(t1) / 1000000;
145 }
146
msecsTo(const QElapsedTimer & other) const147 qint64 QElapsedTimer::msecsTo(const QElapsedTimer &other) const noexcept
148 {
149 qint64 difference = other.t1 - t1;
150 return ticksToNanoseconds(difference) / 1000000;
151 }
152
secsTo(const QElapsedTimer & other) const153 qint64 QElapsedTimer::secsTo(const QElapsedTimer &other) const noexcept
154 {
155 return msecsTo(other) / 1000;
156 }
157
operator <(const QElapsedTimer & v1,const QElapsedTimer & v2)158 bool operator<(const QElapsedTimer &v1, const QElapsedTimer &v2) noexcept
159 {
160 return (v1.t1 - v2.t1) < 0;
161 }
162
current(Qt::TimerType timerType)163 QDeadlineTimer QDeadlineTimer::current(Qt::TimerType timerType) noexcept
164 {
165 Q_STATIC_ASSERT(!QDeadlineTimerNanosecondsInT2);
166 QDeadlineTimer result;
167 result.t1 = ticksToNanoseconds(getTickCount());
168 result.type = timerType;
169 return result;
170 }
171
172 QT_END_NAMESPACE
173