-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathvdso_time.c
More file actions
49 lines (47 loc) · 1.3 KB
/
vdso_time.c
File metadata and controls
49 lines (47 loc) · 1.3 KB
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
#define _POSIX_C_SOURCE 200809L
#include <stdio.h>
#include <time.h>
#include <sys/time.h>
#include <unistd.h>
#include <string.h>
static long diff_ns(struct timespec a, struct timespec b)
{
long s = b.tv_sec - a.tv_sec;
long n = b.tv_nsec - a.tv_nsec;
return s * 1000000000L + n;
}
int main(void)
{
struct timespec t1, t2;
clock_gettime(CLOCK_MONOTONIC, &t1);
for (int i = 0; i < 1000000; i++) {
struct timespec tmp;
clock_gettime(CLOCK_MONOTONIC, &tmp);
}
clock_gettime(CLOCK_MONOTONIC, &t2);
long mono_ns = diff_ns(t1, t2) / 1000000;
struct timeval v1, v2;
gettimeofday(&v1, NULL);
for (int i = 0; i < 1000000; i++) {
struct timeval tmp;
gettimeofday(&tmp, NULL);
}
gettimeofday(&v2, NULL);
long gtod_ns = (v2.tv_sec - v1.tv_sec) * 1000000L + (v2.tv_usec - v1.tv_usec);
printf("CLOCK_MONOTONIC: %ld ms per 1e6 calls\n", mono_ns);
printf("gettimeofday: %ld ms per 1e6 calls\n", gtod_ns);
#if defined(__linux__)
FILE *f = fopen("/proc/self/maps", "r");
if (f) {
char line[512];
printf("vDSO mappings:\n");
while (fgets(line, sizeof line, f)) {
if (strstr(line, "vdso")) {
fputs(line, stdout);
}
}
fclose(f);
}
#endif
return 0;
}