-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbenchmark.h
291 lines (228 loc) · 7.39 KB
/
benchmark.h
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
/*
* File: benchmark.h
* Desc: Contains RDTSC cycle-counting benchmarks. All code to be benchmarked is
* inline so that the compiler ensures no instructions other than the ones
* we test are benchmarked.
*/
#pragma once
#include "constants.h"
#include "cpu_tests.h"
#include "utils.h"
#include "limits.h"
#include <stdlib.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <unistd.h>
typedef struct {
uint32_t siz;
uint64_t median;
uint64_t min;
uint64_t max;
double mean;
double stdev;
} benchmark_stats;
extern benchmark_stats fill_stats(uint64_t* arr, uint32_t siz) {
benchmark_stats stats = {0};
stats.siz = siz;
stats.median = median(arr, siz);
stats.mean = mean(arr,siz);
stats.stdev = stdev(arr, stats.mean, siz);
uint64_t min = LONG_MAX;
// the smallest unsigned number is 0
uint64_t max = 0;
for (int i=0; i<siz; i++) {
if(arr[i] < min){
min = arr[i];
}
if (arr[i] > max){
max = arr[i];
}
}
stats.min = min;
stats.max = max;
return stats;
}
extern uint64_t benchmarkEmpty(fun_ptr test) {
uint32_t cycles_high0, cycles_low0, cycles_low1, cycles_high1;
asm volatile (
"CPUID\n\t"
"RDTSC\n\t"
"mov %%edx, %0\n\t"
"mov %%eax, %1\n\t": "=r" (cycles_high0),
"=r" (cycles_low0)::"rax", "%rbx", "%rcx", "%rdx"
);
asm volatile (
"RDTSCP\n\t"
"mov %%edx, %0\n\t"
"mov %%eax, %1\n\t": "=r" (cycles_high1),
"=r" (cycles_low1)::"rax", "%rbx", "%rcx", "%rdx"
);
uint64_t start = ((uint64_t)cycles_high0 << 32) | cycles_low0;
uint64_t end = ((uint64_t)cycles_high1 << 32) | cycles_low1;
return end-start;
}
extern uint64_t benchmarkCycles(fun_ptr test) {
uint32_t cycles_high0, cycles_low0, cycles_low1, cycles_high1;
asm volatile (
"CPUID\n\t"
"RDTSC\n\t"
"mov %%edx, %0\n\t"
"mov %%eax, %1\n\t": "=r" (cycles_high0),
"=r" (cycles_low0)::"rax", "%rbx", "%rcx", "%rdx"
);
// time function below
//testMeasurementOverhead();
test();
asm volatile (
"RDTSCP\n\t"
"mov %%edx, %0\n\t"
"mov %%eax, %1\n\t": "=r" (cycles_high1),
"=r" (cycles_low1)::"rax", "%rbx", "%rcx", "%rdx"
);
uint64_t start = ((uint64_t)cycles_high0 << 32) | cycles_low0;
uint64_t end = ((uint64_t)cycles_high1 << 32) | cycles_low1;
return end-start;
}
extern uint64_t benchmarkFork(fun_ptr _ignore) {
uint32_t cycles_high0, cycles_low0, cycles_low1, cycles_high1;
pid_t parentId = getpid();
asm volatile (
"CPUID\n\t"
"RDTSC\n\t"
"mov %%edx, %0\n\t"
"mov %%eax, %1\n\t": "=r" (cycles_high0),
"=r" (cycles_low0)::"rax", "%rbx", "%rcx", "%rdx"
);
fork();
asm volatile (
"RDTSCP\n\t"
"mov %%edx, %0\n\t"
"mov %%eax, %1\n\t": "=r" (cycles_high1),
"=r" (cycles_low1)::"rax", "%rbx", "%rcx", "%rdx"
);
uint64_t start = ((uint64_t)cycles_high0 << 32) | cycles_low0;
uint64_t end = ((uint64_t)cycles_high1 << 32) | cycles_low1;
if (getpid() != parentId) {
exit(0);
}
else {
// this ensures that we minimize the total number of logical scheduling units low
// the more schedulable threads that exist, the larger the time-lapse may be from
// being context_switched out and being allocated the CPU again.
wait(NULL);
}
return end-start;
}
void* testThreadf(void * arg){ }
extern uint64_t benchmarkThread(fun_ptr _ignore) {
uint32_t cycles_high0, cycles_low0, cycles_low1, cycles_high1;
pthread_t tid;
asm volatile (
"CPUID\n\t"
"RDTSC\n\t"
"mov %%edx, %0\n\t"
"mov %%eax, %1\n\t": "=r" (cycles_high0),
"=r" (cycles_low0)::"rax", "%rbx", "%rcx", "%rdx"
);
pthread_create(&tid, NULL, &testThreadf, NULL);
asm volatile (
"RDTSCP\n\t"
"mov %%edx, %0\n\t"
"mov %%eax, %1\n\t": "=r" (cycles_high1),
"=r" (cycles_low1)::"rax", "%rbx", "%rcx", "%rdx"
);
uint64_t start = ((uint64_t)cycles_high0 << 32) | cycles_low0;
uint64_t end = ((uint64_t)cycles_high1 << 32) | cycles_low1;
// this ensures that we minimize the total number of logical scheduling units low
// the more schedulable threads that exist, the larger the time-lapse may be from
// being context_switched out and being allocated the CPU again.
pthread_join(tid, NULL);
return end-start;
}
void runTest(ben_ptr benchmark, fun_ptr test, const char* name, uint32_t iterations, uint32_t trials) {
uint64_t trial_results[trials];
uint64_t iteration_results[iterations];
// First, warm up I-Cache with 10,000,000 calls.
// MUST disable gcc optimizations for this to work
for (int i = 0; i < ICACHE_HITS; i++) {
benchmark(test);
}
// begin benchmark tests
for (int i=0; i < trials; i++) {
for (int j=0; j< iterations; j++) {
iteration_results[j] = benchmark(test);
}
// select median of all iteration tests per trial
trial_results[i] = median(iteration_results, iterations);
}
benchmark_stats stats = fill_stats(trial_results, trials);
printf("Testing %s\n", name);
printf("%d Trials of %d Iterations\n", trials, iterations);
printf("Mean: %f\n", stats.mean);
printf("Median: %ld\n", stats.median);
printf("StDev: %f\n", stats.stdev);
printf("Min: %ld\n", stats.min);
printf("Max: %ld\n", stats.max);
printf("\n");
}
extern void runTestQuarantine(ben_ptr benchmark, fun_ptr test, const char* name, uint32_t iterations, uint32_t trials) {
fprintf(stderr, "First run\n");
benchmark(test);
fprintf(stderr, "Second run\n");
benchmark(test);
fprintf(stderr, "tests completed correctly");
}
void runTestMod(ben_ptr benchmark, fun_ptr test, const char* name, uint32_t iterations, uint32_t trials, mod_ptr mod) {
uint64_t trial_results[trials];
uint64_t iteration_results[iterations];
// First, warm up I-Cache with 10,000,000 calls.
// MUST disable gcc optimizations for this to work
for (int i = 0; i < ICACHE_HITS; i++) {
benchmark(test);
}
// begin benchmark tests
for (int i=0; i < trials; i++) {
for (int j=0; j< iterations; j++) {
iteration_results[j] = mod(benchmark(test));
}
// select median of all iteration tests per trial
trial_results[i] = median(iteration_results, iterations);
}
benchmark_stats stats = fill_stats(trial_results, trials);
printf("Testing %s\n", name);
printf("%d Trials of %d Iterations\n", trials, iterations);
printf("Mean: %f\n", stats.mean);
printf("Median: %ld\n", stats.median);
printf("StDev: %f\n", stats.stdev);
printf("Min: %ld\n", stats.min);
printf("Max: %ld\n", stats.max);
printf("\n");
}
void runTestSetup(ben_ptr benchmark, fun_ptr test, const char* name, uint32_t iterations, uint32_t trials, fun_ptr setup) {
uint64_t trial_results[trials];
uint64_t iteration_results[iterations];
// First, warm up I-Cache with 10,000,000 calls.
// MUST disable gcc optimizations for this to work
for (int i = 0; i < ICACHE_HITS; i++) {
setup();
benchmark(test);
}
// begin benchmark tests
for (int i=0; i < trials; i++) {
for (int j=0; j< iterations; j++) {
setup();
iteration_results[j] = benchmark(test);
}
// select median of all iteration tests per trial
trial_results[i] = median(iteration_results, iterations);
}
benchmark_stats stats = fill_stats(trial_results, trials);
printf("Testing %s\n", name);
printf("%d Trials of %d Iterations\n", trials, iterations);
printf("Mean: %f\n", stats.mean);
printf("Median: %ld\n", stats.median);
printf("StDev: %f\n", stats.stdev);
printf("Min: %ld\n", stats.min);
printf("Max: %ld\n", stats.max);
printf("\n");
}