1
/****************************************************************************
3
** Copyright (C) 2012 Nokia Corporation and/or its subsidiary(-ies).
4
** All rights reserved.
5
** Contact: Nokia Corporation (qt-info@nokia.com)
7
** This file is part of the QtDeclarative module of the Qt Toolkit.
9
** $QT_BEGIN_LICENSE:LGPL$
10
** GNU Lesser General Public License Usage
11
** This file may be used under the terms of the GNU Lesser General Public
12
** License version 2.1 as published by the Free Software Foundation and
13
** appearing in the file LICENSE.LGPL included in the packaging of this
14
** file. Please review the following information to ensure the GNU Lesser
15
** General Public License version 2.1 requirements will be met:
16
** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
18
** In addition, as a special exception, Nokia gives you certain additional
19
** rights. These rights are described in the Nokia Qt LGPL Exception
20
** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
22
** GNU General Public License Usage
23
** Alternatively, this file may be used under the terms of the GNU General
24
** Public License version 3.0 as published by the Free Software Foundation
25
** and appearing in the file LICENSE.GPL included in the packaging of this
26
** file. Please review the following information to ensure the GNU General
27
** Public License version 3.0 requirements will be met:
28
** http://www.gnu.org/copyleft/gpl.html.
31
** Alternatively, this file may be used in accordance with the terms and
32
** conditions contained in a signed written agreement between you and Nokia.
40
****************************************************************************/
42
#include "performancetimer.h"
47
#include <mach/mach_time.h>
48
#elif defined(Q_OS_SYMBIAN)
53
#elif defined(Q_OS_UNIX)
57
#elif defined(Q_OS_WIN)
61
// mac/unix code heavily copied from QElapsedTimer
64
////////////////////////////// Mac //////////////////////////////
67
static mach_timebase_info_data_t info = {0,0};
68
static qint64 absoluteToNSecs(qint64 cpuTime)
71
mach_timebase_info(&info);
72
qint64 nsecs = cpuTime * info.numer / info.denom;
76
void PerformanceTimer::start()
78
t1 = mach_absolute_time();
81
qint64 PerformanceTimer::elapsed() const
83
uint64_t cpu_time = mach_absolute_time();
84
return absoluteToNSecs(cpu_time - t1);
87
qint64 PerformanceTimer::restart()
91
t1 = mach_absolute_time();
92
return absoluteToNSecs(t1-start);
95
qint64 PerformanceTimer::difference(PerformanceTimer* timer)
97
return absoluteToNSecs(t1 - timer->t1);
100
////////////////////////////// Symbian //////////////////////////////
101
#elif defined(Q_OS_SYMBIAN)
103
static qint64 getTimeFromTick(quint64 elapsed)
105
static TInt freq = 0;
107
HAL::Get(HALData::EFastCounterFrequency, freq);
109
return (elapsed * 1000000000) / freq;
112
void PerformanceTimer::start()
114
t1 = User::FastCounter();
117
qint64 PerformanceTimer::elapsed() const
119
return getTimeFromTick(User::FastCounter() - t1);
122
qint64 PerformanceTimer::restart()
126
t1 = User::FastCounter();
127
return getTimeFromTick(t1 - start);
130
qint64 PerformanceTimer::difference(PerformanceTimer* timer)
132
return getTimeFromTick(t1 - timer->t1);
135
////////////////////////////// Unix //////////////////////////////
136
#elif defined(Q_OS_UNIX)
138
#if defined(QT_NO_CLOCK_MONOTONIC) || defined(QT_BOOTSTRAPPED)
139
// turn off the monotonic clock
140
# ifdef _POSIX_MONOTONIC_CLOCK
141
# undef _POSIX_MONOTONIC_CLOCK
143
# define _POSIX_MONOTONIC_CLOCK -1
146
#if (_POSIX_MONOTONIC_CLOCK-0 != 0)
147
static const bool monotonicClockChecked = true;
148
static const bool monotonicClockAvailable = _POSIX_MONOTONIC_CLOCK > 0;
150
static int monotonicClockChecked = false;
151
static int monotonicClockAvailable = false;
155
# define is_likely(x) __builtin_expect((x), 1)
157
# define is_likely(x) (x)
159
#define load_acquire(x) ((volatile const int&)(x))
160
#define store_release(x,v) ((volatile int&)(x) = (v))
162
static void unixCheckClockType()
164
#if (_POSIX_MONOTONIC_CLOCK-0 == 0)
165
if (is_likely(load_acquire(monotonicClockChecked)))
168
# if defined(_SC_MONOTONIC_CLOCK)
169
// detect if the system support monotonic timers
170
long x = sysconf(_SC_MONOTONIC_CLOCK);
171
store_release(monotonicClockAvailable, x >= 200112L);
174
store_release(monotonicClockChecked, true);
178
static inline void do_gettime(qint64 *sec, qint64 *frac)
180
#if (_POSIX_MONOTONIC_CLOCK-0 >= 0)
181
unixCheckClockType();
182
if (is_likely(monotonicClockAvailable)) {
184
clock_gettime(CLOCK_MONOTONIC, &ts);
194
void PerformanceTimer::start()
196
do_gettime(&t1, &t2);
199
qint64 PerformanceTimer::elapsed() const
202
do_gettime(&sec, &frac);
206
return sec * Q_INT64_C(1000000000) + frac;
209
qint64 PerformanceTimer::restart()
214
do_gettime(&t1, &t2);
217
return sec * Q_INT64_C(1000000000) + frac;
220
qint64 PerformanceTimer::difference(PerformanceTimer* timer)
223
sec = t1 - timer->t1;
224
frac = t2 - timer->t2;
225
return sec * Q_INT64_C(1000000000) + frac;
228
////////////////////////////// Windows //////////////////////////////
229
#elif defined(Q_OS_WIN)
231
static qint64 getTimeFromTick(quint64 elapsed)
233
static LARGE_INTEGER freq = {{ 0, 0 }};
235
QueryPerformanceFrequency(&freq);
236
return 1000000000 * elapsed / freq.QuadPart;
239
void PerformanceTimer::start()
242
QueryPerformanceCounter(&li);
246
qint64 PerformanceTimer::elapsed() const
249
QueryPerformanceCounter(&li);
250
return getTimeFromTick(li.QuadPart - t1);
253
qint64 PerformanceTimer::restart()
258
QueryPerformanceCounter(&li);
260
return getTimeFromTick(t1 - start);
263
qint64 PerformanceTimer::difference(PerformanceTimer* timer)
265
return getTimeFromTick(t1 - timer->t1);
268
////////////////////////////// Default //////////////////////////////
271
// default implementation (no hi-perf timer) does nothing
272
void PerformanceTimer::start()
276
qint64 PerformanceTimer::elapsed() const
281
qint64 PerformanceTimer::restart() const
286
qint64 PerformanceTimer::difference(PerformanceTimer* timer)