-
Notifications
You must be signed in to change notification settings - Fork 8
/
bench_syscalls.cc
178 lines (138 loc) · 3.57 KB
/
bench_syscalls.cc
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
// SPDX-License-Identifier: GPL-3.0-or-later
// SPDX-FileCopyrightText: © 2021 Georg Sauthoff <mail@gms.tf>
#include <benchmark/benchmark.h>
#include <unistd.h>
#include <sys/types.h>
#include <sched.h>
#include <pthread.h>
#include <time.h>
#include <math.h>
#include <sys/prctl.h>
#include <assert.h>
static void bench_getuid(benchmark::State& state) {
for (auto _ : state) {
getuid();
}
}
BENCHMARK(bench_getuid);
static void bench_getpid(benchmark::State& state) {
for (auto _ : state) {
getpid();
}
}
BENCHMARK(bench_getpid);
static void bench_close(benchmark::State& state) {
for (auto _ : state) {
close(999);
}
}
BENCHMARK(bench_close);
static void bench_syscall(benchmark::State& state) {
for (auto _ : state) {
syscall(423);
}
}
BENCHMARK(bench_syscall);
static void bench_sched_yield(benchmark::State& state) {
for (auto _ : state) {
sched_yield();
}
}
BENCHMARK(bench_sched_yield);
static void bench_clock_gettime(benchmark::State& state) {
struct timespec ts = {0};
for (auto _ : state) {
clock_gettime(CLOCK_REALTIME, &ts);
}
}
BENCHMARK(bench_clock_gettime);
static void bench_clock_gettime_tai(benchmark::State& state) {
struct timespec ts = {0};
for (auto _ : state) {
clock_gettime(CLOCK_TAI, &ts);
}
}
BENCHMARK(bench_clock_gettime_tai);
static void bench_clock_gettime_monotonic(benchmark::State& state) {
struct timespec ts = {0};
for (auto _ : state) {
clock_gettime(CLOCK_MONOTONIC, &ts);
}
}
BENCHMARK(bench_clock_gettime_monotonic);
static void bench_clock_gettime_monotonic_raw(benchmark::State& state) {
struct timespec ts = {0};
for (auto _ : state) {
clock_gettime(CLOCK_MONOTONIC_RAW, &ts);
}
}
BENCHMARK(bench_clock_gettime_monotonic_raw);
static void bench_nanosleep0(benchmark::State& state) {
struct timespec ts = {0};
for (auto _ : state) {
int r = nanosleep(&ts, 0);
assert(!r);
}
}
BENCHMARK(bench_nanosleep0);
static void bench_nanosleep0_slack1(benchmark::State& state) {
int r = prctl(PR_SET_TIMERSLACK, 1, 0, 0, 0);
assert(!r);
struct timespec ts = {0};
for (auto _ : state) {
int r = nanosleep(&ts, 0);
assert(!r);
}
}
BENCHMARK(bench_nanosleep0_slack1);
static void bench_nanosleep1_slack1(benchmark::State& state) {
int r = prctl(PR_SET_TIMERSLACK, 1, 0, 0, 0);
assert(!r);
struct timespec ts = { .tv_nsec = 1 };
for (auto _ : state) {
int r = nanosleep(&ts, 0);
assert(!r);
}
}
BENCHMARK(bench_nanosleep1_slack1);
static void bench_pthread_cond_signal(benchmark::State& state) {
pthread_cond_t cv = PTHREAD_COND_INITIALIZER;
for (auto _ : state) {
int r = pthread_cond_signal(&cv);
assert(!r);
}
}
BENCHMARK(bench_pthread_cond_signal);
static void bench_assign(benchmark::State& state) {
double f = 0;
for (auto _ : state) {
f = 23;
benchmark::DoNotOptimize(f);
}
}
BENCHMARK(bench_assign);
static void bench_sqrt(benchmark::State& state) {
double f = 23;
double g = 0;
for (auto _ : state) {
benchmark::DoNotOptimize(f);
g = sqrt(f);
benchmark::DoNotOptimize(g);
}
}
BENCHMARK(bench_sqrt);
static void bench_sqrtrec(benchmark::State& state) {
double f = 23;
for (auto _ : state) {
f = sqrt(f);
}
}
BENCHMARK(bench_sqrtrec);
static void bench_nothing(benchmark::State& state) {
unsigned i = 0;
for (auto _ : state) {
++i;
}
}
BENCHMARK(bench_nothing);
BENCHMARK_MAIN();