-
Notifications
You must be signed in to change notification settings - Fork 5
/
gettimeofday-bench.c
69 lines (52 loc) · 1.48 KB
/
gettimeofday-bench.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
#define _GNU_SOURCE /* See feature_test_macros(7) */
#include <unistd.h>
#include <sys/syscall.h>
#include <sys/time.h>
#include <stdio.h>
#define LOOP 10000000
static inline unsigned long rdtsc(void)
{
unsigned long low, high;
asm volatile("rdtsc" : "=a" (low), "=d" (high) );
return ((low) | (high) << 32);
}
void show_tv(struct timeval *tv, char *prefix)
{
printf("[%s]sec = %ld, usec = %ld\n", prefix, tv->tv_sec, tv->tv_usec);
}
int main()
{
long loop;
struct timeval tmp;
unsigned long start, end, elapsed;
int (*__gettimeofday)(struct timeval *, struct timezone *);
__gettimeofday = 0xffffffffff600000;
gettimeofday(&tmp, NULL);
show_tv(&tmp, "VDSO");
syscall(SYS_gettimeofday, &tmp, NULL);
show_tv(&tmp, "SYSCALL");
__gettimeofday(&tmp, NULL);
show_tv(&tmp, "VSYSCALL");
start = rdtsc();
for (loop = 0; loop < LOOP; loop++) {
gettimeofday(&tmp, NULL);
}
end = rdtsc();
elapsed = end - start;
printf("VDSO gettimeofday test %ld cycles, everage %ld cycles\n", elapsed, elapsed / LOOP);
start = rdtsc();
for (loop = 0; loop < LOOP; loop++) {
syscall(SYS_gettimeofday, &tmp, NULL);
}
end = rdtsc();
elapsed = end - start;
printf("SYSCALL gettimeofday test %ld cycles, everage %ld cycles\n", elapsed, elapsed / LOOP);
start = rdtsc();
for (loop = 0; loop < LOOP; loop++) {
__gettimeofday(&tmp, NULL);
}
end = rdtsc();
elapsed = end - start;
printf("VSYSCALL gettimeofday test %ld cycles, everage %ld cycles\n", elapsed, elapsed / LOOP);
return 0;
}