1 /****************************************************************************
3 ** Copyright (C) 2011 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 "qperformancetimer_p.h"
47 #include <mach/mach_time.h>
48 #elif defined(Q_OS_UNIX)
52 #elif defined(Q_OS_SYMBIAN)
56 #elif defined(Q_OS_WIN)
60 // 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 QPerformanceTimer::start()
78 t1 = mach_absolute_time();
81 qint64 QPerformanceTimer::elapsed() const
83 uint64_t cpu_time = mach_absolute_time();
84 return absoluteToNSecs(cpu_time - t1);
87 ////////////////////////////// Unix //////////////////////////////
88 #elif defined(Q_OS_UNIX)
90 #if defined(QT_NO_CLOCK_MONOTONIC) || defined(QT_BOOTSTRAPPED)
91 // turn off the monotonic clock
92 # ifdef _POSIX_MONOTONIC_CLOCK
93 # undef _POSIX_MONOTONIC_CLOCK
95 # define _POSIX_MONOTONIC_CLOCK -1
98 #if (_POSIX_MONOTONIC_CLOCK-0 != 0)
99 static const bool monotonicClockChecked = true;
100 static const bool monotonicClockAvailable = _POSIX_MONOTONIC_CLOCK > 0;
102 static int monotonicClockChecked = false;
103 static int monotonicClockAvailable = false;
107 # define is_likely(x) __builtin_expect((x), 1)
109 # define is_likely(x) (x)
111 #define load_acquire(x) ((volatile const int&)(x))
112 #define store_release(x,v) ((volatile int&)(x) = (v))
114 static void unixCheckClockType()
116 #if (_POSIX_MONOTONIC_CLOCK-0 == 0)
117 if (is_likely(load_acquire(monotonicClockChecked)))
120 # if defined(_SC_MONOTONIC_CLOCK)
121 // detect if the system support monotonic timers
122 long x = sysconf(_SC_MONOTONIC_CLOCK);
123 store_release(monotonicClockAvailable, x >= 200112L);
126 store_release(monotonicClockChecked, true);
130 static inline void do_gettime(qint64 *sec, qint64 *frac)
132 #if (_POSIX_MONOTONIC_CLOCK-0 >= 0)
133 unixCheckClockType();
134 if (is_likely(monotonicClockAvailable)) {
136 clock_gettime(CLOCK_MONOTONIC, &ts);
146 void QPerformanceTimer::start()
148 do_gettime(&t1, &t2);
151 qint64 QPerformanceTimer::elapsed() const
154 do_gettime(&sec, &frac);
158 return sec * Q_INT64_C(1000000000) + frac;
161 ////////////////////////////// Symbian //////////////////////////////
162 #elif defined(Q_OS_SYMBIAN)
164 static qint64 getTimeFromTick(quint64 elapsed)
168 HAL::Get(HALData::EFastCounterFrequency, freq);
170 // ### not sure on units
171 return elapsed / freq;
174 void QPerformanceTimer::start()
176 t1 = User::FastCounter();
179 qint64 QPerformanceTimer::elapsed() const
181 return getTimeFromTick(User::FastCounter() - t1);
184 ////////////////////////////// Windows //////////////////////////////
185 #elif defined(Q_OS_WIN)
187 static qint64 getTimeFromTick(quint64 elapsed)
189 static LARGE_INTEGER freq;
191 QueryPerformanceFrequency(&freq);
192 return 1000000000 * elapsed / freq.QuadPart;
195 void QPerformanceTimer::start()
198 QueryPerformanceCounter(&li);
202 qint64 QPerformanceTimer::elapsed() const
205 QueryPerformanceCounter(&li);
206 return getTimeFromTick(li.QuadPart - t1);
209 ////////////////////////////// Default //////////////////////////////
212 // default implementation (no hi-perf timer) does nothing
213 void QPerformanceTimer::start()
217 qint64 QPerformanceTimer::elapsed() const