summaryrefslogtreecommitdiffstats
path: root/src/util/threadcputimer.cpp
blob: 13f62803160a1b1e91c5f9450c9350f222eecd3d (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
#include "util/threadcputimer.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


////////////////////////////// Unix //////////////////////////////
#if defined(Q_OS_UNIX)

#if (_POSIX_THREAD_CPUTIME-0 != 0)
static const bool threadCpuTimeChecked = true;
static const bool threadCpuTimeAvailable = _POSIX_THREAD_CPUTIME > 0;
#else
static int threadCpuTimeChecked = false;
static int threadCpuTimeAvailable = 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_THREAD_CPUTIME-0 == 0)
    if (is_likely(load_acquire(threadCpuTimeChecked)))
        return;

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

    store_release(threadCpuTimeChecked, true);
#endif
}

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

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

mixxx::Duration ThreadCpuTimer::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 ThreadCpuTimer::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);
}

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

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

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

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

#endif