Initial import from the monolithic Qt.
[profile/ivi/qtdeclarative.git] / src / declarative / qml / qperformancetimer.cpp
1 /****************************************************************************
2 **
3 ** Copyright (C) 2011 Nokia Corporation and/or its subsidiary(-ies).
4 ** All rights reserved.
5 ** Contact: Nokia Corporation (qt-info@nokia.com)
6 **
7 ** This file is part of the QtDeclarative module of the Qt Toolkit.
8 **
9 ** $QT_BEGIN_LICENSE:LGPL$
10 ** No Commercial Usage
11 ** This file contains pre-release code and may not be distributed.
12 ** You may use this file in accordance with the terms and conditions
13 ** contained in the Technology Preview License Agreement accompanying
14 ** this package.
15 **
16 ** GNU Lesser General Public License Usage
17 ** Alternatively, this file may be used under the terms of the GNU Lesser
18 ** General Public License version 2.1 as published by the Free Software
19 ** Foundation and appearing in the file LICENSE.LGPL included in the
20 ** packaging of this file.  Please review the following information to
21 ** ensure the GNU Lesser General Public License version 2.1 requirements
22 ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
23 **
24 ** In addition, as a special exception, Nokia gives you certain additional
25 ** rights.  These rights are described in the Nokia Qt LGPL Exception
26 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
27 **
28 ** If you have questions regarding the use of this file, please contact
29 ** Nokia at qt-info@nokia.com.
30 **
31 **
32 **
33 **
34 **
35 **
36 **
37 **
38 ** $QT_END_LICENSE$
39 **
40 ****************************************************************************/
41
42 #include "qperformancetimer_p.h"
43
44 #if defined(Q_OS_MAC)
45 #include <sys/time.h>
46 #include <unistd.h>
47 #include <mach/mach_time.h>
48 #elif defined(Q_OS_UNIX)
49 #include <sys/time.h>
50 #include <time.h>
51 #include <unistd.h>
52 #elif defined(Q_OS_SYMBIAN)
53 #include <e32std.h>
54 #include <sys/time.h>
55 #include <hal.h>
56 #elif defined(Q_OS_WIN)
57 #include <windows.h>
58 #endif
59
60 // mac/unix code heavily copied from QElapsedTimer
61
62 QT_BEGIN_NAMESPACE
63
64 ////////////////////////////// Mac //////////////////////////////
65 #if defined(Q_OS_MAC)
66
67 static mach_timebase_info_data_t info = {0,0};
68 static qint64 absoluteToNSecs(qint64 cpuTime)
69 {
70     if (info.denom == 0)
71         mach_timebase_info(&info);
72     qint64 nsecs = cpuTime * info.numer / info.denom;
73     return nsecs;
74 }
75
76 void QPerformanceTimer::start()
77 {
78     t1 = mach_absolute_time();
79 }
80
81 qint64 QPerformanceTimer::elapsed() const
82 {
83     uint64_t cpu_time = mach_absolute_time();
84     return absoluteToNSecs(cpu_time - t1);
85 }
86
87 ////////////////////////////// Unix //////////////////////////////
88 #elif defined(Q_OS_UNIX)
89
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
94 # endif
95 # define _POSIX_MONOTONIC_CLOCK -1
96 #endif
97
98 #if (_POSIX_MONOTONIC_CLOCK-0 != 0)
99 static const bool monotonicClockChecked = true;
100 static const bool monotonicClockAvailable = _POSIX_MONOTONIC_CLOCK > 0;
101 #else
102 static int monotonicClockChecked = false;
103 static int monotonicClockAvailable = false;
104 #endif
105
106 #ifdef Q_CC_GNU
107 # define is_likely(x) __builtin_expect((x), 1)
108 #else
109 # define is_likely(x) (x)
110 #endif
111 #define load_acquire(x) ((volatile const int&)(x))
112 #define store_release(x,v) ((volatile int&)(x) = (v))
113
114 static void unixCheckClockType()
115 {
116 #if (_POSIX_MONOTONIC_CLOCK-0 == 0)
117     if (is_likely(load_acquire(monotonicClockChecked)))
118         return;
119
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);
124 # endif
125
126     store_release(monotonicClockChecked, true);
127 #endif
128 }
129
130 static inline void do_gettime(qint64 *sec, qint64 *frac)
131 {
132 #if (_POSIX_MONOTONIC_CLOCK-0 >= 0)
133     unixCheckClockType();
134     if (is_likely(monotonicClockAvailable)) {
135         timespec ts;
136         clock_gettime(CLOCK_MONOTONIC, &ts);
137         *sec = ts.tv_sec;
138         *frac = ts.tv_nsec;
139         return;
140     }
141 #endif
142     *sec = 0;
143     *frac = 0;
144 }
145
146 void QPerformanceTimer::start()
147 {
148     do_gettime(&t1, &t2);
149 }
150
151 qint64 QPerformanceTimer::elapsed() const
152 {
153     qint64 sec, frac;
154     do_gettime(&sec, &frac);
155     sec = sec - t1;
156     frac = frac - t2;
157
158     return sec * Q_INT64_C(1000000000) + frac;
159 }
160
161 ////////////////////////////// Symbian //////////////////////////////
162 #elif defined(Q_OS_SYMBIAN)
163
164 static qint64 getTimeFromTick(quint64 elapsed)
165 {
166     static TInt freq;
167     if (!freq)
168         HAL::Get(HALData::EFastCounterFrequency, freq);
169
170     // ### not sure on units
171     return elapsed / freq;
172 }
173
174 void QPerformanceTimer::start()
175 {
176     t1 = User::FastCounter();
177 }
178
179 qint64 QPerformanceTimer::elapsed() const
180 {
181     return getTimeFromTick(User::FastCounter() - t1);
182 }
183
184 ////////////////////////////// Windows //////////////////////////////
185 #elif defined(Q_OS_WIN)
186
187 static qint64 getTimeFromTick(quint64 elapsed)
188 {
189     static LARGE_INTEGER freq;
190     if (!freq.QuadPart)
191         QueryPerformanceFrequency(&freq);
192     return 1000000000 * elapsed / freq.QuadPart;
193 }
194
195 void QPerformanceTimer::start()
196 {
197     LARGE_INTEGER li;
198     QueryPerformanceCounter(&li);
199     t1 = li.QuadPart;
200 }
201
202 qint64 QPerformanceTimer::elapsed() const
203 {
204     LARGE_INTEGER li;
205     QueryPerformanceCounter(&li);
206     return getTimeFromTick(li.QuadPart - t1);
207 }
208
209 ////////////////////////////// Default //////////////////////////////
210 #else
211
212 // default implementation (no hi-perf timer) does nothing
213 void QPerformanceTimer::start()
214 {
215 }
216
217 qint64 QPerformanceTimer::elapsed() const
218 {
219     return 0;
220 }
221
222 #endif
223
224 QT_END_NAMESPACE
225
226