aboutsummaryrefslogtreecommitdiff
path: root/src/cz/crcs/ectester/standalone/libs/jni/c_timing.c
blob: a6ffc7e9d3f4808b064163df838af2faddb33c67 (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
#include "c_timing.h"

#if _POSIX_TIMERS > 0

#include <time.h>


static struct timespec start = {0};
static struct timespec end = {0};
static jlong partial = 0;

jboolean native_timing_supported() {
    return JNI_TRUE;
}

jlong native_timing_resolution() {
    struct timespec timeval;
    clock_getres(CLOCK_MONOTONIC, &timeval);
    return timeval.tv_nsec;
}

void native_timing_start() {
    partial = 0;
    clock_gettime(CLOCK_MONOTONIC, &start);
}

void native_timing_pause() {
    clock_gettime(CLOCK_MONOTONIC, &end);
    partial += (end.tv_sec - start.tv_sec) * 1000000000 + (end.tv_nsec - start.tv_nsec);
}

void native_timing_restart() {
    clock_gettime(CLOCK_MONOTONIC, &start);
}

void native_timing_stop() {
    clock_gettime(CLOCK_MONOTONIC, &end);
}

jlong native_timing_last() {
    jlong res = (end.tv_sec - start.tv_sec) * 1000000000 + (end.tv_nsec - start.tv_nsec) + partial;
    if (res < 0) {
        return 0;
    } else {
        return res;
    }
}

#elif defined(__WIN32__) || defined(_MSC_VER)

#include <Windows.h>

static LARGE_INTEGER start = {0};
static LARGE_INTEGER end = {0};
static jlong partial = 0;

jboolean native_timing_supported() {
    return JNI_TRUE;
}

jlong native_timing_resolution() {
    LARGE_INTEGER freq;
    QueryPerformanceFrequency(&freq);
    return 1000000000 / freq.QuadPart;
}

void native_timing_start() {
    partial = 0;
    QueryPerformanceCounter(&start);
}

void native_timing_pause() {
    QueryPerformanceCounter(&end);
    partial = (end.QuadPart - start.QuadPart) * native_timing_resolution();
}

void native_timing_restart() {
    QueryPerformanceCounter(&start);
}

void native_timing_stop() {
    QueryPerformanceCounter(&end);
}

jlong native_timing_last() {
    jlong res = (end.QuadPart - start.QuadPart) * native_timing_resolution() + partial;
    if (res < 0) {
        return 0;
    } else {
        return res;
    }
}

#else

jboolean native_timing_supported() {
    return JNI_FALSE;
}

jlong native_timing_resolution() {
    return 0;
}

void native_timing_start() {}

void native_timing_pause() {}

void native_timing_restart() {}

void native_timing_stop() {}

jlong native_timing_last() {
    return 0;
}

#endif