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#include "qdeadlinetimer.h"
43#include "qdeadlinetimer_p.h"
44#if defined(Q_OS_VXWORKS)
45#include "qfunctions_vxworks.h"
46#else
47#include <sys/time.h>
48#include <time.h>
49#endif
50#include <unistd.h>
51
52#include <qatomic.h>
53#include "private/qcore_unix_p.h"
54
55#if defined(QT_NO_CLOCK_MONOTONIC) || defined(QT_BOOTSTRAPPED)
56// turn off the monotonic clock
57# ifdef _POSIX_MONOTONIC_CLOCK
58# undef _POSIX_MONOTONIC_CLOCK
59# endif
60# define _POSIX_MONOTONIC_CLOCK -1
61#endif
62
63QT_BEGIN_NAMESPACE
64
65/*
66 * Design:
67 *
68 * POSIX offers a facility to select the system's monotonic clock when getting
69 * the current timestamp. Whereas the functions are mandatory in POSIX.1-2008,
70 * the presence of a monotonic clock is a POSIX Option (see the document
71 * http://pubs.opengroup.org/onlinepubs/9699919799/basedefs/V1_chap02.html#tag_02_01_06 )
72 *
73 * The macro _POSIX_MONOTONIC_CLOCK can therefore assume the following values:
74 * -1 monotonic clock is never supported on this system
75 * 0 monotonic clock might be supported, runtime check is needed
76 * >1 (such as 200809L) monotonic clock is always supported
77 *
78 * The unixCheckClockType() function will return the clock to use: either
79 * CLOCK_MONOTONIC or CLOCK_REALTIME. In the case the POSIX option has a value
80 * of zero, then this function stores a static that contains the clock to be
81 * used.
82 *
83 * There's one extra case, which is when CLOCK_REALTIME isn't defined. When
84 * that's the case, we'll emulate the clock_gettime function with gettimeofday.
85 *
86 * Conforming to:
87 * POSIX.1b-1993 section "Clocks and Timers"
88 * included in UNIX98 (Single Unix Specification v2)
89 * included in POSIX.1-2001
90 * see http://pubs.opengroup.org/onlinepubs/9699919799/functions/clock_getres.html
91 */
92
93#if !defined(CLOCK_REALTIME)
94# define CLOCK_REALTIME 0
95static inline void qt_clock_gettime(int, struct timespec *ts)
96{
97 // support clock_gettime with gettimeofday
98 struct timeval tv;
99 gettimeofday(&tv, 0);
100 ts->tv_sec = tv.tv_sec;
101 ts->tv_nsec = tv.tv_usec * 1000;
102}
103
104# ifdef _POSIX_MONOTONIC_CLOCK
105# undef _POSIX_MONOTONIC_CLOCK
106# define _POSIX_MONOTONIC_CLOCK -1
107# endif
108#else
109static inline void qt_clock_gettime(clockid_t clock, struct timespec *ts)
110{
111 clock_gettime(clock, ts);
112}
113#endif
114
115static int unixCheckClockType()
116{
117#ifdef Q_OS_LINUX
118 // Despite glibc claiming that we should check at runtime, the Linux kernel
119 // always supports the monotonic clock
120 return CLOCK_MONOTONIC;
121#elif (_POSIX_MONOTONIC_CLOCK-0 == 0) && defined(_SC_MONOTONIC_CLOCK)
122 // we need a value we can store in a clockid_t that isn't a valid clock
123 // check if the valid ones are both non-negative or both non-positive
124# if CLOCK_MONOTONIC >= 0 && CLOCK_REALTIME >= 0
125# define IS_VALID_CLOCK(clock) (clock >= 0)
126# define INVALID_CLOCK -1
127# elif CLOCK_MONOTONIC <= 0 && CLOCK_REALTIME <= 0
128# define IS_VALID_CLOCK(clock) (clock <= 0)
129# define INVALID_CLOCK 1
130# else
131# error "Sorry, your system has weird values for CLOCK_MONOTONIC and CLOCK_REALTIME"
132# endif
133
134 static QBasicAtomicInt clockToUse = Q_BASIC_ATOMIC_INITIALIZER(INVALID_CLOCK);
135 int clock = clockToUse.loadAcquire();
136 if (Q_LIKELY(IS_VALID_CLOCK(clock)))
137 return clock;
138
139 // detect if the system supports monotonic timers
140 clock = sysconf(_SC_MONOTONIC_CLOCK) > 0 ? CLOCK_MONOTONIC : CLOCK_REALTIME;
141 clockToUse.storeRelease(clock);
142 return clock;
143
144# undef INVALID_CLOCK
145# undef IS_VALID_CLOCK
146#elif (_POSIX_MONOTONIC_CLOCK-0) > 0
147 return CLOCK_MONOTONIC;
148#else
149 return CLOCK_REALTIME;
150#endif
151}
152
153bool QElapsedTimer::isMonotonic() noexcept
154{
155 return clockType() == MonotonicClock;
156}
157
158QElapsedTimer::ClockType QElapsedTimer::clockType() noexcept
159{
160 return unixCheckClockType() == CLOCK_REALTIME ? SystemTime : MonotonicClock;
161}
162
163static inline void do_gettime(qint64 *sec, qint64 *frac)
164{
165 timespec ts;
166 qt_clock_gettime(unixCheckClockType(), &ts);
167 *sec = ts.tv_sec;
168 *frac = ts.tv_nsec;
169}
170
171// used in qcore_unix.cpp and qeventdispatcher_unix.cpp
172struct timespec qt_gettime() noexcept
173{
174 qint64 sec, frac;
175 do_gettime(&sec, &frac);
176
177 timespec tv;
178 tv.tv_sec = sec;
179 tv.tv_nsec = frac;
180
181 return tv;
182}
183
184void qt_nanosleep(timespec amount)
185{
186 // We'd like to use clock_nanosleep.
187 //
188 // But clock_nanosleep is from POSIX.1-2001 and both are *not*
189 // affected by clock changes when using relative sleeps, even for
190 // CLOCK_REALTIME.
191 //
192 // nanosleep is POSIX.1-1993
193
194 int r;
195 EINTR_LOOP(r, nanosleep(&amount, &amount));
196}
197
198static qint64 elapsedAndRestart(qint64 sec, qint64 frac,
199 qint64 *nowsec, qint64 *nowfrac)
200{
201 do_gettime(nowsec, nowfrac);
202 sec = *nowsec - sec;
203 frac = *nowfrac - frac;
204 return (sec * Q_INT64_C(1000000000) + frac) / Q_INT64_C(1000000);
205}
206
207void QElapsedTimer::start() noexcept
208{
209 do_gettime(&t1, &t2);
210}
211
212qint64 QElapsedTimer::restart() noexcept
213{
214 return elapsedAndRestart(t1, t2, &t1, &t2);
215}
216
217qint64 QElapsedTimer::nsecsElapsed() const noexcept
218{
219 qint64 sec, frac;
220 do_gettime(&sec, &frac);
221 sec = sec - t1;
222 frac = frac - t2;
223 return sec * Q_INT64_C(1000000000) + frac;
224}
225
226qint64 QElapsedTimer::elapsed() const noexcept
227{
228 return nsecsElapsed() / Q_INT64_C(1000000);
229}
230
231qint64 QElapsedTimer::msecsSinceReference() const noexcept
232{
233 return t1 * Q_INT64_C(1000) + t2 / Q_INT64_C(1000000);
234}
235
236qint64 QElapsedTimer::msecsTo(const QElapsedTimer &other) const noexcept
237{
238 qint64 secs = other.t1 - t1;
239 qint64 fraction = other.t2 - t2;
240 return (secs * Q_INT64_C(1000000000) + fraction) / Q_INT64_C(1000000);
241}
242
243qint64 QElapsedTimer::secsTo(const QElapsedTimer &other) const noexcept
244{
245 return other.t1 - t1;
246}
247
248bool operator<(const QElapsedTimer &v1, const QElapsedTimer &v2) noexcept
249{
250 return v1.t1 < v2.t1 || (v1.t1 == v2.t1 && v1.t2 < v2.t2);
251}
252
253QDeadlineTimer QDeadlineTimer::current(Qt::TimerType timerType) noexcept
254{
255 static_assert(QDeadlineTimerNanosecondsInT2);
256 QDeadlineTimer result;
257 qint64 cursec, curnsec;
258 do_gettime(&cursec, &curnsec);
259 result.t1 = cursec;
260 result.t2 = curnsec;
261 result.type = timerType;
262 return result;
263}
264
265QT_END_NAMESPACE
266