~mixxxdevelopers/mixxx/engine-control-refactor

« back to all changes in this revision

Viewing changes to mixxx/src/util/performancetimer.cpp

  • Committer: RJ Ryan
  • Date: 2013-06-04 00:41:29 UTC
  • mfrom: (2890.22.101 mixxx)
  • Revision ID: rryan@mixxx.org-20130604004129-8jjxkicsb3givu4a
MergingĀ fromĀ lp:mixxx.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/****************************************************************************
 
2
**
 
3
** Copyright (C) 2012 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
** 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.
 
17
**
 
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.
 
21
**
 
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.
 
29
**
 
30
** Other Usage
 
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.
 
33
**
 
34
**
 
35
**
 
36
**
 
37
**
 
38
** $QT_END_LICENSE$
 
39
**
 
40
****************************************************************************/
 
41
 
 
42
#include "performancetimer.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
 
 
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 PerformanceTimer::start()
 
77
{
 
78
    t1 = mach_absolute_time();
 
79
}
 
80
 
 
81
qint64 PerformanceTimer::elapsed() const
 
82
{
 
83
    uint64_t cpu_time = mach_absolute_time();
 
84
    return absoluteToNSecs(cpu_time - t1);
 
85
}
 
86
 
 
87
qint64 PerformanceTimer::restart()
 
88
{
 
89
    qint64 start;
 
90
    start = t1;
 
91
    t1 = mach_absolute_time();
 
92
    return absoluteToNSecs(t1-start);
 
93
}
 
94
 
 
95
qint64 PerformanceTimer::difference(PerformanceTimer* timer)
 
96
{
 
97
    return absoluteToNSecs(t1 - timer->t1);
 
98
}
 
99
 
 
100
////////////////////////////// Symbian //////////////////////////////
 
101
#elif defined(Q_OS_SYMBIAN)
 
102
 
 
103
static qint64 getTimeFromTick(quint64 elapsed)
 
104
{
 
105
    static TInt freq = 0;
 
106
    if (!freq)
 
107
        HAL::Get(HALData::EFastCounterFrequency, freq);
 
108
 
 
109
    return (elapsed * 1000000000) / freq;
 
110
}
 
111
 
 
112
void PerformanceTimer::start()
 
113
{
 
114
    t1 = User::FastCounter();
 
115
}
 
116
 
 
117
qint64 PerformanceTimer::elapsed() const
 
118
{
 
119
    return getTimeFromTick(User::FastCounter() - t1);
 
120
}
 
121
 
 
122
qint64 PerformanceTimer::restart()
 
123
{
 
124
    qint64 start;
 
125
    start = t1;
 
126
    t1 = User::FastCounter();
 
127
    return getTimeFromTick(t1 - start);
 
128
}
 
129
 
 
130
qint64 PerformanceTimer::difference(PerformanceTimer* timer)
 
131
{
 
132
    return getTimeFromTick(t1 - timer->t1);
 
133
}
 
134
 
 
135
////////////////////////////// Unix //////////////////////////////
 
136
#elif defined(Q_OS_UNIX)
 
137
 
 
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
 
142
# endif
 
143
# define _POSIX_MONOTONIC_CLOCK -1
 
144
#endif
 
145
 
 
146
#if (_POSIX_MONOTONIC_CLOCK-0 != 0)
 
147
static const bool monotonicClockChecked = true;
 
148
static const bool monotonicClockAvailable = _POSIX_MONOTONIC_CLOCK > 0;
 
149
#else
 
150
static int monotonicClockChecked = false;
 
151
static int monotonicClockAvailable = false;
 
152
#endif
 
153
 
 
154
#ifdef Q_CC_GNU
 
155
# define is_likely(x) __builtin_expect((x), 1)
 
156
#else
 
157
# define is_likely(x) (x)
 
158
#endif
 
159
#define load_acquire(x) ((volatile const int&)(x))
 
160
#define store_release(x,v) ((volatile int&)(x) = (v))
 
161
 
 
162
static void unixCheckClockType()
 
163
{
 
164
#if (_POSIX_MONOTONIC_CLOCK-0 == 0)
 
165
    if (is_likely(load_acquire(monotonicClockChecked)))
 
166
        return;
 
167
 
 
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);
 
172
# endif
 
173
 
 
174
    store_release(monotonicClockChecked, true);
 
175
#endif
 
176
}
 
177
 
 
178
static inline void do_gettime(qint64 *sec, qint64 *frac)
 
179
{
 
180
#if (_POSIX_MONOTONIC_CLOCK-0 >= 0)
 
181
    unixCheckClockType();
 
182
    if (is_likely(monotonicClockAvailable)) {
 
183
        timespec ts;
 
184
        clock_gettime(CLOCK_MONOTONIC, &ts);
 
185
        *sec = ts.tv_sec;
 
186
        *frac = ts.tv_nsec;
 
187
        return;
 
188
    }
 
189
#endif
 
190
    *sec = 0;
 
191
    *frac = 0;
 
192
}
 
193
 
 
194
void PerformanceTimer::start()
 
195
{
 
196
    do_gettime(&t1, &t2);
 
197
}
 
198
 
 
199
qint64 PerformanceTimer::elapsed() const
 
200
{
 
201
    qint64 sec, frac;
 
202
    do_gettime(&sec, &frac);
 
203
    sec = sec - t1;
 
204
    frac = frac - t2;
 
205
 
 
206
    return sec * Q_INT64_C(1000000000) + frac;
 
207
}
 
208
 
 
209
qint64 PerformanceTimer::restart()
 
210
{
 
211
    qint64 sec, frac;
 
212
    sec = t1;
 
213
    frac = t2;
 
214
    do_gettime(&t1, &t2);
 
215
    sec = t1 - sec;
 
216
    frac = t2 - frac;
 
217
    return sec * Q_INT64_C(1000000000) + frac;
 
218
}
 
219
 
 
220
qint64 PerformanceTimer::difference(PerformanceTimer* timer)
 
221
{
 
222
    qint64 sec, frac;
 
223
    sec = t1 - timer->t1;
 
224
    frac = t2 - timer->t2;
 
225
    return sec * Q_INT64_C(1000000000) + frac;
 
226
}
 
227
 
 
228
////////////////////////////// Windows //////////////////////////////
 
229
#elif defined(Q_OS_WIN)
 
230
 
 
231
static qint64 getTimeFromTick(quint64 elapsed)
 
232
{
 
233
    static LARGE_INTEGER freq = {{ 0, 0 }};
 
234
    if (!freq.QuadPart)
 
235
        QueryPerformanceFrequency(&freq);
 
236
    return 1000000000 * elapsed / freq.QuadPart;
 
237
}
 
238
 
 
239
void PerformanceTimer::start()
 
240
{
 
241
    LARGE_INTEGER li;
 
242
    QueryPerformanceCounter(&li);
 
243
    t1 = li.QuadPart;
 
244
}
 
245
 
 
246
qint64 PerformanceTimer::elapsed() const
 
247
{
 
248
    LARGE_INTEGER li;
 
249
    QueryPerformanceCounter(&li);
 
250
    return getTimeFromTick(li.QuadPart - t1);
 
251
}
 
252
 
 
253
qint64 PerformanceTimer::restart()
 
254
{
 
255
    LARGE_INTEGER li;
 
256
    qint64 start;
 
257
    start = t1;
 
258
    QueryPerformanceCounter(&li);
 
259
    t1 = li.QuadPart;
 
260
    return getTimeFromTick(t1 - start);
 
261
}
 
262
 
 
263
qint64 PerformanceTimer::difference(PerformanceTimer* timer)
 
264
{
 
265
    return getTimeFromTick(t1 - timer->t1);
 
266
}
 
267
 
 
268
////////////////////////////// Default //////////////////////////////
 
269
#else
 
270
 
 
271
// default implementation (no hi-perf timer) does nothing
 
272
void PerformanceTimer::start()
 
273
{
 
274
}
 
275
 
 
276
qint64 PerformanceTimer::elapsed() const
 
277
{
 
278
    return 0;
 
279
}
 
280
 
 
281
qint64 PerformanceTimer::restart() const
 
282
{
 
283
    return 0;
 
284
}
 
285
 
 
286
qint64 PerformanceTimer::difference(PerformanceTimer* timer)
 
287
{
 
288
    return 0;
 
289
}
 
290
 
 
291
#endif
 
292
 
 
293
 
 
294