|
| 1 | +/**************************************************************************** |
| 2 | +** |
| 3 | +** Copyright (C) 2013 Digia Plc and/or its subsidiary(-ies). |
| 4 | +** Contact: http://www.qt-project.org/legal |
| 5 | +** |
| 6 | +** This file is part of the QtDeclarative 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 Digia. For licensing terms and |
| 14 | +** conditions see http://qt.digia.com/licensing. For further information |
| 15 | +** use the contact form at http://qt.digia.com/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 2.1 as published by the Free Software |
| 20 | +** Foundation and appearing in the file LICENSE.LGPL included in the |
| 21 | +** packaging of this file. Please review the following information to |
| 22 | +** ensure the GNU Lesser General Public License version 2.1 requirements |
| 23 | +** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. |
| 24 | +** |
| 25 | +** In addition, as a special exception, Digia gives you certain additional |
| 26 | +** rights. These rights are described in the Digia Qt LGPL Exception |
| 27 | +** version 1.1, included in the file LGPL_EXCEPTION.txt in this package. |
| 28 | +** |
| 29 | +** GNU General Public License Usage |
| 30 | +** Alternatively, this file may be used under the terms of the GNU |
| 31 | +** General Public License version 3.0 as published by the Free Software |
| 32 | +** Foundation and appearing in the file LICENSE.GPL included in the |
| 33 | +** packaging of this file. Please review the following information to |
| 34 | +** ensure the GNU General Public License version 3.0 requirements will be |
| 35 | +** met: http://www.gnu.org/copyleft/gpl.html. |
| 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_SYMBIAN) |
| 49 | +#include <e32std.h> |
| 50 | +#include <sys/time.h> |
| 51 | +#include <hal.h> |
| 52 | +#include <hal_data.h> |
| 53 | +#elif defined(Q_OS_UNIX) |
| 54 | +#include <sys/time.h> |
| 55 | +#include <time.h> |
| 56 | +#include <unistd.h> |
| 57 | +#elif defined(Q_OS_WIN) |
| 58 | +#include <windows.h> |
| 59 | +#endif |
| 60 | + |
| 61 | +// mac/unix code heavily copied from QElapsedTimer |
| 62 | + |
| 63 | +QT_BEGIN_NAMESPACE |
| 64 | + |
| 65 | +////////////////////////////// Mac ////////////////////////////// |
| 66 | +#if defined(Q_OS_MAC) |
| 67 | + |
| 68 | +static mach_timebase_info_data_t info = {0,0}; |
| 69 | +static qint64 absoluteToNSecs(qint64 cpuTime) |
| 70 | +{ |
| 71 | + if (info.denom == 0) |
| 72 | + mach_timebase_info(&info); |
| 73 | + qint64 nsecs = cpuTime * info.numer / info.denom; |
| 74 | + return nsecs; |
| 75 | +} |
| 76 | + |
| 77 | +void QPerformanceTimer::start() |
| 78 | +{ |
| 79 | + t1 = mach_absolute_time(); |
| 80 | +} |
| 81 | + |
| 82 | +qint64 QPerformanceTimer::elapsed() const |
| 83 | +{ |
| 84 | + qint64 cpu_time = mach_absolute_time(); |
| 85 | + return absoluteToNSecs(cpu_time - t1); |
| 86 | +} |
| 87 | + |
| 88 | +// return number of nsecs elapsed from timer start time till absoluteMonotonicTimeNs |
| 89 | +// elapsedToAbsoluteTime(0) returns negative value of absolute time (ns) when the timer was started |
| 90 | +qint64 QPerformanceTimer::elapsedToAbsoluteTime(qint64 absoluteMonotonicTimeNs) const |
| 91 | +{ |
| 92 | + qint64 absolute_t1_ns = absoluteToNSecs(t1); |
| 93 | + return absoluteMonotonicTimeNs - absolute_t1_ns; |
| 94 | +} |
| 95 | + |
| 96 | +////////////////////////////// Symbian ////////////////////////////// |
| 97 | +#elif defined(Q_OS_SYMBIAN) |
| 98 | + |
| 99 | +static qint64 getTimeFromTick(quint64 elapsed) |
| 100 | +{ |
| 101 | + static TInt freq = 0; |
| 102 | + if (!freq) |
| 103 | + HAL::Get(HALData::EFastCounterFrequency, freq); |
| 104 | + |
| 105 | + return (elapsed * 1000000000) / freq; |
| 106 | +} |
| 107 | + |
| 108 | +void QPerformanceTimer::start() |
| 109 | +{ |
| 110 | + t1 = User::FastCounter(); |
| 111 | +} |
| 112 | + |
| 113 | +qint64 QPerformanceTimer::elapsed() const |
| 114 | +{ |
| 115 | + return getTimeFromTick(User::FastCounter() - t1); |
| 116 | +} |
| 117 | + |
| 118 | + |
| 119 | +qint64 QPerformanceTimer::elapsedToAbsoluteTime(qint64 absoluteMonotonicTimeNs) const |
| 120 | +{ |
| 121 | + qint64 absolute_t1_ns = getTimeFromTick(t1); |
| 122 | + return absoluteMonotonicTimeNs - absolute_t1_ns; |
| 123 | +} |
| 124 | + |
| 125 | +////////////////////////////// Unix ////////////////////////////// |
| 126 | +#elif defined(Q_OS_UNIX) |
| 127 | + |
| 128 | +#if defined(QT_NO_CLOCK_MONOTONIC) || defined(QT_BOOTSTRAPPED) |
| 129 | +// turn off the monotonic clock |
| 130 | +# ifdef _POSIX_MONOTONIC_CLOCK |
| 131 | +# undef _POSIX_MONOTONIC_CLOCK |
| 132 | +# endif |
| 133 | +# define _POSIX_MONOTONIC_CLOCK -1 |
| 134 | +#endif |
| 135 | + |
| 136 | +#if (_POSIX_MONOTONIC_CLOCK-0 != 0) |
| 137 | +static const bool monotonicClockChecked = true; |
| 138 | +static const bool monotonicClockAvailable = _POSIX_MONOTONIC_CLOCK > 0; |
| 139 | +#else |
| 140 | +static int monotonicClockChecked = false; |
| 141 | +static int monotonicClockAvailable = false; |
| 142 | +#endif |
| 143 | + |
| 144 | +#ifdef Q_CC_GNU |
| 145 | +# define is_likely(x) __builtin_expect((x), 1) |
| 146 | +#else |
| 147 | +# define is_likely(x) (x) |
| 148 | +#endif |
| 149 | +#define load_acquire(x) ((volatile const int&)(x)) |
| 150 | +#define store_release(x,v) ((volatile int&)(x) = (v)) |
| 151 | + |
| 152 | +static void unixCheckClockType() |
| 153 | +{ |
| 154 | +#if (_POSIX_MONOTONIC_CLOCK-0 == 0) |
| 155 | + if (is_likely(load_acquire(monotonicClockChecked))) |
| 156 | + return; |
| 157 | + |
| 158 | +# if defined(_SC_MONOTONIC_CLOCK) |
| 159 | + // detect if the system support monotonic timers |
| 160 | + long x = sysconf(_SC_MONOTONIC_CLOCK); |
| 161 | + store_release(monotonicClockAvailable, x >= 200112L); |
| 162 | +# endif |
| 163 | + |
| 164 | + store_release(monotonicClockChecked, true); |
| 165 | +#endif |
| 166 | +} |
| 167 | + |
| 168 | +static inline void do_gettime(qint64 *sec, qint64 *frac) |
| 169 | +{ |
| 170 | +#if (_POSIX_MONOTONIC_CLOCK-0 >= 0) |
| 171 | + unixCheckClockType(); |
| 172 | + if (is_likely(monotonicClockAvailable)) { |
| 173 | + timespec ts; |
| 174 | + clock_gettime(CLOCK_MONOTONIC, &ts); |
| 175 | + *sec = ts.tv_sec; |
| 176 | + *frac = ts.tv_nsec; |
| 177 | + return; |
| 178 | + } |
| 179 | +#endif |
| 180 | + *sec = 0; |
| 181 | + *frac = 0; |
| 182 | +} |
| 183 | + |
| 184 | +void QPerformanceTimer::start() |
| 185 | +{ |
| 186 | + do_gettime(&t1, &t2); |
| 187 | +} |
| 188 | + |
| 189 | +qint64 QPerformanceTimer::elapsed() const |
| 190 | +{ |
| 191 | + qint64 sec, frac; |
| 192 | + do_gettime(&sec, &frac); |
| 193 | + sec = sec - t1; |
| 194 | + frac = frac - t2; |
| 195 | + |
| 196 | + return sec * Q_INT64_C(1000000000) + frac; |
| 197 | +} |
| 198 | + |
| 199 | +qint64 QPerformanceTimer::elapsedToAbsoluteTime(qint64 absoluteMonotonicTimeNs) const |
| 200 | +{ |
| 201 | + qint64 sec = absoluteMonotonicTimeNs / Q_INT64_C(1000000000); |
| 202 | + qint64 frac = absoluteMonotonicTimeNs % Q_INT64_C(1000000000); |
| 203 | + sec = sec - t1; |
| 204 | + frac = frac - t2; |
| 205 | + |
| 206 | + return sec * Q_INT64_C(1000000000) + frac; |
| 207 | +} |
| 208 | + |
| 209 | +////////////////////////////// Windows ////////////////////////////// |
| 210 | +#elif defined(Q_OS_WIN) |
| 211 | + |
| 212 | +static qint64 getTimeFromTick(quint64 elapsed) |
| 213 | +{ |
| 214 | + static LARGE_INTEGER freq; |
| 215 | + if (!freq.QuadPart) |
| 216 | + QueryPerformanceFrequency(&freq); |
| 217 | + return 1000000000 * elapsed / freq.QuadPart; |
| 218 | +} |
| 219 | + |
| 220 | +void QPerformanceTimer::start() |
| 221 | +{ |
| 222 | + LARGE_INTEGER li; |
| 223 | + QueryPerformanceCounter(&li); |
| 224 | + t1 = li.QuadPart; |
| 225 | +} |
| 226 | + |
| 227 | +qint64 QPerformanceTimer::elapsed() const |
| 228 | +{ |
| 229 | + LARGE_INTEGER li; |
| 230 | + QueryPerformanceCounter(&li); |
| 231 | + return getTimeFromTick(li.QuadPart - t1); |
| 232 | +} |
| 233 | + |
| 234 | +qint64 QPerformanceTimer::elapsedToAbsoluteTime(qint64 absoluteMonotonicTimeNs) const |
| 235 | +{ |
| 236 | + qint64 absolute_t1_ns = getTimeFromTick(t1); |
| 237 | + return absoluteMonotonicTimeNs - absolute_t1_ns; |
| 238 | +} |
| 239 | + |
| 240 | +////////////////////////////// Default ////////////////////////////// |
| 241 | +#else |
| 242 | + |
| 243 | +// default implementation (no hi-perf timer) does nothing |
| 244 | +void QPerformanceTimer::start() |
| 245 | +{ |
| 246 | +} |
| 247 | + |
| 248 | +qint64 QPerformanceTimer::elapsed() const |
| 249 | +{ |
| 250 | + return 0; |
| 251 | +} |
| 252 | + |
| 253 | +qint64 QPerformanceTimer::elapsedToAbsoluteTime(qint64 absoluteMonotonicTimeNs) const |
| 254 | +{ |
| 255 | + Q_UNUSED(absoluteMonotonicTimeNs); |
| 256 | + return 0; |
| 257 | +} |
| 258 | + |
| 259 | +#endif |
| 260 | + |
| 261 | +QT_END_NAMESPACE |
| 262 | + |
| 263 | + |
0 commit comments