summaryrefslogtreecommitdiffstats
path: root/src/util/performancetimer.cpp
blob: 8f2769cf0c76dd389abfeb498bd08725b78f1589 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
/****************************************************************************
**
** Copyright (C) 2012 Nokia Corporation and/or its subsidiary(-ies).
** All rights reserved.
** Contact: Nokia Corporation (qt-info@nokia.com)
**
** This file is part of the QtDeclarative module of the Qt Toolkit.
**
** $QT_BEGIN_LICENSE:LGPL$
** GNU Lesser General Public License Usage
** This file may be used under the terms of the GNU Lesser General Public
** License version 2.1 as published by the Free Software Foundation and
** appearing in the file LICENSE.LGPL included in the packaging of this
** file. Please review the following information to ensure the GNU Lesser
** General Public License version 2.1 requirements will be met:
** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
**
** In addition, as a special exception, Nokia gives you certain additional
** rights. These rights are described in the Nokia Qt LGPL Exception
** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
**
** GNU General Public License Usage
** Alternatively, this file may be used under the terms of the GNU General
** Public License version 3.0 as published by the Free Software Foundation
** and appearing in the file LICENSE.GPL included in the packaging of this
** file. Please review the following information to ensure the GNU General
** Public License version 3.0 requirements will be met:
** http://www.gnu.org/copyleft/gpl.html.
**
** Other Usage
** Alternatively, this file may be used in accordance with the terms and
** conditions contained in a signed written agreement between you and Nokia.
**
**
**
**
**
** $QT_END_LICENSE$
**
****************************************************************************/

#include "performancetimer.h"

#if defined(Q_OS_MAC)
#include <sys/time.h>
#include <unistd.h>
#include <mach/mach_time.h>
#elif defined(Q_OS_SYMBIAN)
#include <e32std.h>
#include <sys/time.h>
#include <hal.h>
#include <hal_data.h>
#elif defined(Q_OS_UNIX)
#include <sys/time.h>
#include <time.h>
#include <unistd.h>
#elif defined(Q_OS_WIN)
#include <windows.h>
#endif

// mac/unix code heavily copied from QElapsedTimer


////////////////////////////// Mac //////////////////////////////
#if defined(Q_OS_MAC)

static mach_timebase_info_data_t info = {0,0};
static qint64 absoluteToNSecs(qint64 cpuTime)
{
    if (info.denom == 0)
        mach_timebase_info(&info);
    qint64 nsecs = cpuTime * info.numer / info.denom;
    return nsecs;
}

void PerformanceTimer::start()
{
    t1 = mach_absolute_time();
}

mixxx::Duration PerformanceTimer::elapsed() const
{
    uint64_t cpu_time = mach_absolute_time();
    return mixxx::Duration::fromNanos(absoluteToNSecs(cpu_time - t1));
}

mixxx::Duration PerformanceTimer::restart()
{
    qint64 start;
    start = t1;
    t1 = mach_absolute_time();
    return mixxx::Duration::fromNanos(absoluteToNSecs(t1-start));
}

mixxx::Duration PerformanceTimer::difference(const PerformanceTimer& timer) const
{
    return mixxx::Duration::fromNanos(absoluteToNSecs(t1 - timer.t1));
}

////////////////////////////// Symbian //////////////////////////////
#elif defined(Q_OS_SYMBIAN)

static qint64 getTimeFromTick(qint64 elapsed)
{
    static TInt freq = 0;
    if (!freq)
        HAL::Get(HALData::EFastCounterFrequency, freq);

    return (elapsed * 1000000000) / freq;
}

void PerformanceTimer::start()
{
    t1 = User::FastCounter();
}

mixxx::Duration PerformanceTimer::elapsed() const
{
    return mixxx::Duration::fromNanos(getTimeFromTick(User::FastCounter() - t1));
}

mixxx::Duration PerformanceTimer::restart()
{
    qint64 start;
    start = t1;
    t1 = User::FastCounter();
    return mixxx::Duration::fromNanos(getTimeFromTick(t1 - start));
}

mixxx::Duration PerformanceTimer::difference(const PerformanceTimer& timer) const
{
    return mixxx::Duration::fromNanos(getTimeFromTick(t1 - timer.t1));
}

////////////////////////////// Unix //////////////////////////////
#elif defined(Q_OS_UNIX)

#if defined(QT_NO_CLOCK_MONOTONIC) || defined(QT_BOOTSTRAPPED)
// turn off the monotonic clock
# ifdef _POSIX_MONOTONIC_CLOCK
#  undef _POSIX_MONOTONIC_CLOCK
# endif
# define _POSIX_MONOTONIC_CLOCK -1
#endif

#if (_POSIX_MONOTONIC_CLOCK-0 != 0)
static const bool monotonicClockChecked = true;
static const bool monotonicClockAvailable = _POSIX_MONOTONIC_CLOCK > 0;
#else
static int monotonicClockChecked = false;
static int monotonicClockAvailable = false;
#endif

#ifdef Q_CC_GNU
# define is_likely(x) __builtin_expect((x), 1)
#else
# define is_likely(x) (x)
#endif
#define load_acquire(x) ((volatile const int&)(x))
#define store_release(x,v) ((volatile int&)(x) = (v))

static void unixCheckClockType()
{
#if (_POSIX_MONOTONIC_CLOCK-0 == 0)
    if (is_likely(load_acquire(monotonicClockChecked))) {
        return;
    }

# if defined(_SC_MONOTONIC_CLOCK)
    // detect if the system support monotonic timers
    long x = sysconf(_SC_MONOTONIC_CLOCK);
    store_release(monotonicClockAvailable, x >= 200112L);
# endif

    store_release(monotonicClockChecked, true);
#endif
}

static inline void do_gettime(qint64 *sec, qint64 *frac)
{
#if (_POSIX_MONOTONIC_CLOCK-0 >= 0)
    unixCheckClockType();
    if (is_likely(monotonicClockAvailable)) {
        timespec ts;
        clock_gettime(CLOCK_MONOTONIC, &ts);
        *sec = ts.tv_sec;
        *frac = ts.tv_nsec;
        return;
    }
#endif
    *sec = 0;
    *frac = 0;
}

void PerformanceTimer::start()
{
    do_gettime(&t1, &t2);
}

mixxx::Duration PerformanceTimer::elapsed() const
{
    qint64 sec, frac;
    do_gettime(&sec, &frac);
    sec = sec - t1;
    frac = frac - t2;

    return mixxx::Duration::fromNanos(sec * Q_INT64_C(1000000000) + frac);
}

mixxx::Duration PerformanceTimer::restart()
{
    qint64 sec, frac;
    sec = t1;
    frac = t2;
    do_gettime(&t1, &t2);
    sec = t1 - sec;
    frac = t2 - frac;
    return mixxx::Duration::fromNanos(sec * Q_INT64_C(1000000000) + frac);
}

mixxx::Duration PerformanceTimer::difference(const PerformanceTimer& timer) const
{
    qint64 sec, frac;
    sec = t1 - timer.t1;
    frac = t2 - timer.t2;
    return mixxx::Duration::fromNanos(sec * Q_INT64_C(1000000000) + frac);
}

////////////////////////////// Windows //////////////////////////////
#elif defined(Q_OS_WIN)

static qint64 getTimeFromTick(qint64 elapsed)
{
    static LARGE_INTEGER freq = {{ 0, 0 }};
    if (!freq.QuadPart)
        QueryPerformanceFrequency(&freq);
    return 1000000000 * elapsed / freq.QuadPart;
}

void PerformanceTimer::start()
{
    LARGE_INTEGER li;
    QueryPerformanceCounter(&li);
    t1 = li.QuadPart;
}

mixxx::Duration PerformanceTimer::elapsed() const
{
    LARGE_INTEGER li;
    QueryPerformanceCounter(&li);
    return mixxx::Duration::fromNanos(getTimeFromTick(li.QuadPart - t1));
}

mixxx::Duration PerformanceTimer::restart()
{
    LARGE_INTEGER li;
    qint64 start;
    start = t1;
    QueryPerformanceCounter(&li);
    t1 = li.QuadPart;
    return mixxx::Duration::fromNanos(getTimeFromTick(t1 - start));
}

mixxx::Duration PerformanceTimer::difference(const PerformanceTimer& timer) const
{
    return mixxx::Duration::fromNanos(getTimeFromTick(t1 - timer.t1));
}

////////////////////////////// Default //////////////////////////////
#else

// default implementation (no hi-perf timer) does nothing
void PerformanceTimer::start()
{
}

mixxx::Duration PerformanceTimer::elapsed() const
{
    return mixxx::Duration::fromNanos(0);
}

mixxx::Duration PerformanceTimer::restart() const
{
    return mixxx::Duration::fromNanos(0);
}

mixxx::Duration PerformanceTimer::difference(const PerformanceTimer& timer) const
{
    return mixxx::Duration::fromNanos(0);
}

#endif