-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathlinux-test-clocks.c
145 lines (126 loc) · 3.49 KB
/
linux-test-clocks.c
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
/*
* linux-test-clocks.c
* Test various clock sources available through clock_gettime().
*
* Author: Mikael Hirki <[email protected]>
*/
#include <stdio.h>
#include <stdint.h>
#include <sys/time.h>
#include <time.h>
#if __x86_64__ || __i386__
#define HAVE_RDTSC
#define RDTSC(v) \
do { unsigned lo, hi; \
__asm__ volatile("rdtsc" : "=a" (lo), "=d" (hi)); \
(v) = ((uint64_t) lo) | ((uint64_t) hi << 32); \
} while (0)
#endif
clockid_t clk_ids[] = {
CLOCK_REALTIME,
CLOCK_REALTIME_COARSE,
CLOCK_MONOTONIC,
CLOCK_MONOTONIC_COARSE,
CLOCK_MONOTONIC_RAW,
#ifdef CLOCK_BOOTTIME
CLOCK_BOOTTIME,
#endif
CLOCK_PROCESS_CPUTIME_ID,
CLOCK_THREAD_CPUTIME_ID,
};
const char *clk_names[] = {
"CLOCK_REALTIME",
"CLOCK_REALTIME_COARSE",
"CLOCK_MONOTONIC",
"CLOCK_MONOTONIC_COARSE",
"CLOCK_MONOTONIC_RAW",
#ifdef CLOCK_BOOTTIME
"CLOCK_BOOTTIME",
#endif
"CLOCK_PROCESS_CPUTIME_ID",
"CLOCK_THREAD_CPUTIME_ID",
};
const int num_clocks = sizeof(clk_ids) / sizeof(*clk_ids);
const int num_iterations = 10000000;
static double gettimeofday_double() {
struct timeval now;
gettimeofday(&now, NULL);
return now.tv_sec + now.tv_usec * 0.000001;
}
static void gettimeofday_getres(struct timespec *res) {
struct timeval begin, now;
gettimeofday(&begin, NULL);
while (1) {
gettimeofday(&now, NULL);
if (now.tv_usec != begin.tv_usec) break;
}
if (now.tv_usec == 0) {
now.tv_usec += 1000000;
}
int delta = now.tv_usec - begin.tv_usec;
res->tv_sec = 0;
res->tv_nsec = delta * 1000;
}
int main() {
int i = 0, j = 0;
struct timespec res = {0, 0};
struct timeval now = {0, 0};
printf("Clock time resolutions\n");
printf("======================\n\n");
for (i = 0; i < num_clocks; i++) {
clock_getres(clk_ids[i], &res);
printf("%s : %lld.%09lld\n", clk_names[i], (long long)res.tv_sec, (long long)res.tv_nsec);
}
// Special case: gettimeofday
gettimeofday_getres(&res);
printf("gettimeofday : %lld.%09lld\n", (long long)res.tv_sec, (long long)res.tv_nsec);
#ifdef HAVE_RDTSC
printf("RDTSC : ?\n");
#endif
printf("\n");
printf("Current values\n");
printf("==============\n\n");
for (i = 0; i < num_clocks; i++) {
clock_gettime(clk_ids[i], &res);
printf("%s : %lld.%09lld\n", clk_names[i], (long long)res.tv_sec, (long long)res.tv_nsec);
}
// Special case: gettimeofday
gettimeofday(&now, NULL);
printf("gettimeofday : %lld.%09lld\n", (long long)now.tv_sec, ((long long)now.tv_usec) * 1000);
#ifdef HAVE_RDTSC
uint64_t tsc = 0;
RDTSC(tsc);
printf("RDTSC : %llu\n", (long long unsigned)tsc);
#endif
printf("\n");
printf("Polling latencies\n");
printf("=================\n\n");
for (i = 0; i < num_clocks; i++) {
double time_start = gettimeofday_double();
for (j = 0; j < num_iterations; j++) {
clock_gettime(clk_ids[i], &res);
}
double time_end = gettimeofday_double();
printf("%s : %f nanoseconds\n", clk_names[i], (time_end - time_start) * 1000000000.0 / num_iterations);
}
// Special case: gettimeofday
{
double time_start = gettimeofday_double();
for (j = 0; j < num_iterations; j++) {
gettimeofday(&now, NULL);
}
double time_end = gettimeofday_double();
printf("gettimeofday : %f nanoseconds\n", (time_end - time_start) * 1000000000.0 / num_iterations);
}
#ifdef HAVE_RDTSC
{
double time_start = gettimeofday_double();
for (j = 0; j < num_iterations; j++) {
RDTSC(tsc);
}
double time_end = gettimeofday_double();
printf("RDTSC : %f nanoseconds\n", (time_end - time_start) * 1000000000.0 / num_iterations);
}
#endif
return 0;
}