-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathvdso.c
More file actions
227 lines (174 loc) · 4.34 KB
/
vdso.c
File metadata and controls
227 lines (174 loc) · 4.34 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
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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
#define _GNU_SOURCE
#include <stdio.h>
#include <sys/auxv.h>
#include <dlfcn.h>
#include <time.h>
#include <unistd.h>
#include <sys/syscall.h>
#include "common.h"
static int (*__vdso_clock_gettime)(int clock_id, struct timespec *ts);
static int (*__vdso_clock_getres)(int clock_id, struct timespec *ts);
static int (*__vdso_gettimeofday)(struct timeval *tv, void *tz);
static int (*__vdso_getcpu)(unsigned int *cpu, unsigned int *node, void *unused);
#ifdef __NR_time
static time_t (*__vdso_time)(time_t *t);
#endif
static int sys_clock_gettime(void)
{
int clock_id = CLOCK_MONOTONIC;
struct timespec ts;
return syscall(__NR_clock_gettime, clock_id, &ts);
}
static int vdso_clock_gettime(void)
{
int clock_id = CLOCK_MONOTONIC;
struct timespec ts;
return __vdso_clock_gettime(clock_id, &ts);
}
static int sys_clock_getres(void)
{
int clock_id = CLOCK_MONOTONIC;
struct timespec ts;
return syscall(__NR_clock_getres, clock_id, &ts);
}
static int vdso_clock_getres(void)
{
int clock_id = CLOCK_MONOTONIC;
struct timespec ts;
return __vdso_clock_getres(clock_id, &ts);
}
static int sys_gettimeofday(void)
{
struct timeval tv;
return syscall(__NR_gettimeofday, &tv, NULL);
}
static int vdso_gettimeofday(void)
{
struct timeval tv;
return __vdso_gettimeofday(&tv, NULL);
}
static int sys_getcpu(void)
{
unsigned int cpu, node;
return syscall(__NR_getcpu, &cpu, &node, NULL);
}
static int vdso_getcpu(void)
{
unsigned int cpu, node;
return (int)__vdso_getcpu(&cpu, &node, NULL);
}
#ifdef __NR_time
static int sys_time(void)
{
time_t t;
return syscall(__NR_time, &t);
}
static int vdso_time(void)
{
time_t t;
return (int)__vdso_time(&t);
}
#endif
struct test {
const char *desc;
void *test;
int (*sys)(void);
int (*vdso)(void);
} static tests[] = {
{
.desc = "clock_gettime()",
.test = &__vdso_clock_gettime,
.sys = sys_clock_gettime,
.vdso = vdso_clock_gettime,
}, {
.desc = "clock_getres()",
.test = &__vdso_clock_getres,
.sys = sys_clock_getres,
.vdso = vdso_clock_getres,
}, {
.desc = "gettimeofday()",
.test = &__vdso_gettimeofday,
.sys = sys_gettimeofday,
.vdso = vdso_gettimeofday,
}, {
.desc = "getcpu()",
.test = &__vdso_getcpu,
.sys = sys_getcpu,
.vdso = vdso_getcpu,
},
#ifdef __NR_time
{
.desc = "time()",
.test = &__vdso_time,
.sys = sys_time,
.vdso = vdso_time,
},
#endif
};
static unsigned long long diff_ns(struct timespec a)
{
struct timespec b;
clock_gettime(CLOCK_MONOTONIC, &b);
return (b.tv_sec * 1000000000ULL + b.tv_nsec) - (a.tv_sec * 1000000000ULL + a.tv_nsec);
}
static void do_benchmark(int (*func)(void), unsigned long *calls, unsigned long *diff)
{
struct timespec ts;
clock_gettime(CLOCK_MONOTONIC, &ts);
for (;;) {
for (int i = 0; i < 1000; i++)
func();
*calls += 1000;
*diff = diff_ns(ts);
if (*diff >= 250000000ULL)
break;
}
}
static void benchmark(void)
{
struct test *t;
for (t = ARRAY_BEGIN(tests); t < ARRAY_END(tests); t++) {
unsigned long calls = 0;
unsigned long diff = 0;
double sys_time = 0;
double vdso_time = 0;
do_benchmark(t->sys, &calls, &diff);
sys_time = (double)diff / calls;
log("syscall %18s %9lu k calls in %.02f ms %10.02f ns / call\n",
t->desc, calls / 1000, (double)diff / 1000000UL, sys_time);
if (!*((unsigned long *)t->test)) {
log("vDSO %18s not available\n", t->desc);
continue;
}
do_benchmark(t->vdso, &calls, &diff);
vdso_time = (double)diff / calls;
log("vDSO %18s %9lu k calls in %.02f ms %10.02f ns / call (%.02fx faster)\n",
t->desc, calls / 1000, (double)diff / 1000000UL, vdso_time, sys_time / vdso_time);
}
}
int main(int argc, char *argv[])
{
int ret;
unsigned long val;
void *vdso;
val = getauxval(AT_SYSINFO_EHDR);
if (!val && errno == ENOENT)
err("vDSO not available? vDSO address not found in auxiliary vector\n");
else
log("vDSO available @ 0x%lx\n", val);
vdso = dlopen("linux-vdso.so.1", RTLD_LAZY);
if (!vdso)
die("dlopen() failed: %s\n", dlerror());
__vdso_clock_gettime = dlsym(vdso, "__vdso_clock_gettime");
__vdso_gettimeofday = dlsym(vdso, "__vdso_gettimeofday");
__vdso_getcpu = dlsym(vdso, "__vdso_getcpu");
__vdso_clock_getres = dlsym(vdso, "__vdso_clock_getres");
#ifdef __NR_time
__vdso_time = dlsym(vdso, "__vdso_time");
#endif
benchmark();
ret = dlclose(vdso);
if (ret)
die("dlclose() failed: %s\n", dlerror());
return 0;
}