-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathmain.c
295 lines (261 loc) · 8.52 KB
/
main.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
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
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
/* SPDX-License-Identifier: BSD-3-Clause */
/*
* Copyright (c) 2021, Hugo Lefeuvre <[email protected]>
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* 3. Neither the name of the copyright holder nor the names of its
* contributors may be used to endorse or promote products derived from
* this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*/
#if !LINUX_USERLAND
#include <flexos/microbenchmarks/isolated.h>
#include <flexos/isolation.h>
#include <uk/alloc.h>
#else
#include <inttypes.h>
#endif
#if CONFIG_LIBFLEXOS_VMEPT
#include <flexos/impl/main_annotation.h>
#endif
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <stdint.h>
// some config checks here...
#if !LINUX_USERLAND && CONFIG_LIBFLEXOS_INTELPKU && !CONFIG_LIBFLEXOS_GATE_INTELPKU_NO_INSTRUMENT
#error "Microbenchmarks should not be executed with gate instrumentation!"
#endif
// bench_start returns a timestamp for use to measure the start of a benchmark
// run.
__attribute__ ((always_inline)) static inline uint64_t bench_start(void)
{
unsigned cycles_low, cycles_high;
asm volatile( "CPUID\n\t" // serialize
"RDTSC\n\t" // read clock
"MOV %%edx, %0\n\t"
"MOV %%eax, %1\n\t"
: "=r" (cycles_high), "=r" (cycles_low)
:: "%rax", "%rbx", "%rcx", "%rdx" );
return ((uint64_t) cycles_high << 32) | cycles_low;
}
// bench_end returns a timestamp for use to measure the end of a benchmark run.
__attribute__ ((always_inline)) static inline uint64_t bench_end(void)
{
unsigned cycles_low, cycles_high;
asm volatile( "RDTSCP\n\t" // read clock + serialize
"MOV %%edx, %0\n\t"
"MOV %%eax, %1\n\t"
"CPUID\n\t" // serialize -- but outside clock region!
: "=r" (cycles_high), "=r" (cycles_low)
:: "%rax", "%rbx", "%rcx", "%rdx" );
return ((uint64_t) cycles_high << 32) | cycles_low;
}
#if !LINUX_USERLAND
/*
* make sure function does not get inlined
*/
__attribute__ ((noinline))
void empty_fcall(void) {
/* keep the call from being optimized away */
asm volatile ("");
}
__attribute__ ((noinline))
void empty_fcall_1xB(void) {
char characters[1];
/* keep the call from being optimized away */
asm volatile ("");
}
__attribute__ ((noinline))
void empty_fcall_1xBs(void) {
char characters[1] __attribute__((flexos_whitelist));
/* keep the call from being optimized away */
asm volatile ("");
}
__attribute__ ((noinline))
void empty_fcall_2xB(void) {
char characters1[1];
char characters2[1];
/* keep the call from being optimized away */
asm volatile ("");
}
__attribute__ ((noinline))
void empty_fcall_2xBs(void) {
char characters1[1] __attribute__((flexos_whitelist));
char characters2[1] __attribute__((flexos_whitelist));
/* keep the call from being optimized away */
asm volatile ("");
}
__attribute__ ((noinline))
void empty_fcall_3xB(void) {
char characters1[1];
char characters2[1];
char characters3[1];
/* keep the call from being optimized away */
asm volatile ("");
}
__attribute__ ((noinline))
void empty_fcall_3xBs(void) {
char characters1[1] __attribute__((flexos_whitelist));
char characters2[1] __attribute__((flexos_whitelist));
char characters3[1] __attribute__((flexos_whitelist));
/* keep the call from being optimized away */
asm volatile ("");
}
__attribute__ ((noinline))
void empty_fcall_4xB(void) {
char characters1[1];
char characters2[1];
char characters3[1];
char characters4[1];
/* keep the call from being optimized away */
asm volatile ("");
}
__attribute__ ((noinline))
void empty_fcall_4xBs(void) {
char characters1[1] __attribute__((flexos_whitelist));
char characters2[1] __attribute__((flexos_whitelist));
char characters3[1] __attribute__((flexos_whitelist));
char characters4[1] __attribute__((flexos_whitelist));
/* keep the call from being optimized away */
asm volatile ("");
}
#endif
#define REPS 1000000
#define SERIAL 0
static inline void RUN_ISOLATED_FCALL(void)
{
#if !LINUX_USERLAND
flexos_gate(libflexosmicrobenchmarks, flexos_microbenchmarks_empty_fcall);
#else
syscall(1000);
#endif
}
__attribute__ ((noinline)) void RUN_FCALL(void)
{
asm volatile ("");
}
int main(int argc, char *argv[])
{
uint32_t overhead_tsc, overhead_gate, overhead_fcall, t0, t1;
#if SERIAL
printf("> serial\n");
printf("TSC\tgate\tfcall\n");
for(int i = 0; i < REPS; i++) {
t0 = bench_start();
asm volatile("");
t1 = bench_end();
overhead_tsc = t1 - t0;
t0 = bench_start();
RUN_ISOLATED_FCALL();
t1 = bench_end();
overhead_gate = t1 - t0;
t0 = bench_start();
RUN_FCALL();
t1 = bench_end();
overhead_fcall = t1 - t0;
printf("%" PRId64 "\t%" PRId64 "\t%" PRId64 "\n", overhead_tsc,
overhead_gate, overhead_fcall);
}
#else
printf("\n#instruction,latency\n");
uint64_t min = 1000, max = 0, sum = 0;
int ok = 0;
while (!ok) {
for(int i = 0; i < REPS; i++) {
t0 = bench_start();
asm volatile("");
t1 = bench_end();
if ((t1 - t0) < min) { min = (t1 - t0); }
if ((t1 - t0) > max) { max = (t1 - t0); }
sum += (t1 - t0);
}
overhead_tsc = min;
min = 1000, max = 0, sum = 0;
for(int i = 0; i < REPS; i++) {
t0 = bench_start();
RUN_FCALL();
t1 = bench_end();
if ((t1 - t0) < min) { min = (t1 - t0); }
if ((t1 - t0) > max) { max = (t1 - t0); }
sum += (t1 - t0);
}
overhead_fcall = min;
if (overhead_fcall > overhead_tsc) ok = 1;
}
printf("fcall,%" PRId64 "\n", overhead_fcall - overhead_tsc);
min = 1000, max = 0, sum = 0;
for(int i = 0; i < REPS; i++) {
t0 = bench_start();
RUN_ISOLATED_FCALL();
t1 = bench_end();
if ((t1 - t0) < min) { min = (t1 - t0); }
if ((t1 - t0) > max) { max = (t1 - t0); }
sum += (t1 - t0);
}
printf(
#if CONFIG_LIBFLEXOS_GATE_INTELPKU_PRIVATE_STACKS && CONFIG_LIBFLEXOS_ENABLE_DSS
"pku-dss,%"
#elif CONFIG_LIBFLEXOS_GATE_INTELPKU_PRIVATE_STACKS
"pku-heap,%"
#elif CONFIG_LIBFLEXOS_GATE_INTELPKU_SHARED_STACKS
"pku-shared,%"
#elif CONFIG_LIBFLEXOS_VMEPT
"ept,%"
#elif LINUX_USERLAND
"scall,%"
#else
"gate,%"
#endif
PRId64 "\n", min - overhead_tsc);
#endif
#define BENCH_NB(NB,p) \
do { \
min = 1000, max = 0, sum = 0; \
for(int i = 0; i < REPS; i++) { \
t0 = bench_start(); \
empty_fcall_ ## NB ## xBs(); \
t1 = bench_end(); \
if ((t1 - t0) < min) { min = (t1 - t0); } \
if ((t1 - t0) > max) { max = (t1 - t0); } \
sum += (t1 - t0); \
} \
overhead_gate = min - overhead_tsc; \
\
printf(p); \
printf(STRINGIFY(NB) ",%" PRId64 "\n", overhead_gate);\
} while(0)
#if CONFIG_LIBFLEXOS_GATE_INTELPKU_PRIVATE_STACKS
#if CONFIG_LIBFLEXOS_ENABLE_DSS
char prefix[] = "dss";
printf("\n\n#allocations,dss_latency\n");
#else
char prefix[] = "heap";
printf("\n\n#allocations,heap_latency\n");
#endif
BENCH_NB(1, prefix);
BENCH_NB(2, prefix);
BENCH_NB(3, prefix);
BENCH_NB(4, prefix);
#endif /* CONFIG_LIBFLEXOS_GATE_INTELPKU_PRIVATE_STACKS */
return 0;
}