-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathbenchmark.c
More file actions
236 lines (192 loc) · 6.54 KB
/
benchmark.c
File metadata and controls
236 lines (192 loc) · 6.54 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
228
229
230
231
232
233
234
235
236
#define _GNU_SOURCE
#include "arena.h"
#include <malloc.h>
#include <math.h>
#include <sched.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#define TOTAL_ALLOCATIONS 1000000LL
#define SAMPLE_SIZE 20000
#define SAMPLE_INTERVAL (TOTAL_ALLOCATIONS / SAMPLE_SIZE)
#define BATCH_SIZE 1000 * 2
#define ZOMBIE_POOL_SIZE 6400 * 2
#define NUM_RUNS 5
typedef struct {
int zombie_idx[65536];
int should_zombie[65536];
} DecisionTable;
typedef struct {
double speed_m_ops;
double p50;
double p99;
double max;
double stddev;
} RunResult;
/* Pin to single CPU */
void pin_to_cpu(int cpu) {
cpu_set_t set;
CPU_ZERO(&set);
CPU_SET(cpu, &set);
sched_setaffinity(0, sizeof(set), &set);
}
/* Serialized RDTSC begin */
static inline uint64_t rdtsc_begin(void) {
unsigned int lo, hi;
__asm__ volatile("cpuid\n\t"
"rdtsc\n\t"
: "=a"(lo), "=d"(hi)
: "a"(0)
: "%rbx", "%rcx");
return ((uint64_t)hi << 32) | lo;
}
/* Serialized RDTSC end */
static inline uint64_t rdtsc_end(void) {
unsigned int lo, hi;
__asm__ volatile("rdtscp\n\t"
"mov %%eax, %0\n\t"
"mov %%edx, %1\n\t"
"cpuid\n\t"
: "=r"(lo), "=r"(hi)
:
: "%rax", "%rbx", "%rcx", "%rdx");
return ((uint64_t)hi << 32) | lo;
}
int compare_latencies(const void *a, const void *b) {
double d1 = *(const double *)a;
double d2 = *(const double *)b;
return (d1 > d2) - (d1 < d2);
}
RunResult run_benchmark(int use_arena, int is_warmup, DecisionTable *table) {
uint64_t total_done = 0;
void *ptrs[BATCH_SIZE] = {0};
void *zombies[ZOMBIE_POOL_SIZE] = {0};
double *latencies = malloc(sizeof(double) * SAMPLE_SIZE);
size_t sample_count = 0;
// const size_t sz = 8 * 1024 * 1024; // 8 MB allocations
const size_t sz = 256 * 1024; // 256KB per allocation
if (use_arena) {
arena_config(64 * 1024, 1024); // 64KB chunks, 1024 chunks
prealloc_arena();
}
struct timespec start_ts, end_ts;
clock_gettime(CLOCK_MONOTONIC, &start_ts);
while (total_done < TOTAL_ALLOCATIONS) {
uint32_t table_idx = (uint32_t)(total_done & 0xFFFF);
for (int i = 0; i < BATCH_SIZE; i++) {
uint64_t t1 = rdtsc_begin();
void *ptr = use_arena ? allocate(sz) : malloc(sz);
if (ptr) {
/* Random page touching to destroy locality */
size_t pages = sz / 4096;
for (int p = 0; p < 128; p++) {
size_t off = (size_t)(rand() % pages) * 4096;
((volatile char *)ptr)[off] = 1;
}
}
uint64_t t2 = rdtsc_end();
if (!is_warmup && (total_done % SAMPLE_INTERVAL == 0) && sample_count < SAMPLE_SIZE) {
latencies[sample_count++] = (double)(t2 - t1);
}
if (ptr) {
if (table->should_zombie[table_idx]) {
int z_idx = table->zombie_idx[table_idx];
if (zombies[z_idx]) {
if (use_arena)
deallocate(zombies[z_idx]);
else
free(zombies[z_idx]);
}
zombies[z_idx] = ptr;
} else {
ptrs[i] = ptr;
}
}
}
for (int i = 0; i < BATCH_SIZE; i++) {
if (ptrs[i]) {
if (use_arena)
deallocate(ptrs[i]);
else
free(ptrs[i]);
ptrs[i] = NULL;
}
}
total_done += BATCH_SIZE;
}
clock_gettime(CLOCK_MONOTONIC, &end_ts);
double elapsed = (end_ts.tv_sec - start_ts.tv_sec) + (end_ts.tv_nsec - start_ts.tv_nsec) * 1e-9;
RunResult res = {0};
if (!is_warmup) {
qsort(latencies, sample_count, sizeof(double), compare_latencies);
double sum = 0, sq_sum = 0;
for (int i = 0; i < sample_count; i++) {
sum += latencies[i];
sq_sum += latencies[i] * latencies[i];
}
double mean = sum / sample_count;
res.speed_m_ops = (total_done / 1000000.0) / elapsed;
res.p50 = latencies[(int)(sample_count * 0.5)];
res.p99 = latencies[(int)(sample_count * 0.99)];
res.max = latencies[sample_count - 1];
res.stddev = sqrt((sq_sum / sample_count) - (mean * mean));
}
for (int i = 0; i < ZOMBIE_POOL_SIZE; i++) {
if (zombies[i]) {
if (use_arena)
deallocate(zombies[i]);
else
free(zombies[i]);
}
}
if (use_arena)
teardown_arena();
free(latencies);
malloc_trim(0); // force release
return res;
}
void print_final_stats(const char *label, RunResult results[]) {
double avg_speed = 0, avg_p50 = 0, avg_p99 = 0, avg_std = 0;
for (int i = 0; i < NUM_RUNS; i++) {
avg_speed += results[i].speed_m_ops;
avg_p50 += results[i].p50;
avg_p99 += results[i].p99;
avg_std += results[i].stddev;
}
printf("\n=== %s FINAL AVERAGES (%d RUNS) ===\n", label, NUM_RUNS);
printf("Speed: %.2f M ops/sec\n", avg_speed / NUM_RUNS);
printf("P50: %.0f cycles\n", avg_p50 / NUM_RUNS);
printf("P99: %.0f cycles\n", avg_p99 / NUM_RUNS);
printf("StdDev: %.2f\n", avg_std / NUM_RUNS);
}
int main() {
pin_to_cpu(0);
srand(1337);
DecisionTable *table = malloc(sizeof(DecisionTable));
for (int i = 0; i < 65536; i++) {
table->zombie_idx[i] = rand() % ZOMBIE_POOL_SIZE;
table->should_zombie[i] = (rand() % 5 != 0); // 80% stay live
}
RunResult malloc_results[NUM_RUNS];
RunResult arena_results[NUM_RUNS];
printf("Starting Warmup Runs for heap...\n");
run_benchmark(0, 1, table);
run_benchmark(0, 1, table);
for (int i = 0; i < NUM_RUNS; i++) {
printf("Running Malloc iteration %d...\n", i + 1);
malloc_results[i] = run_benchmark(0, 0, table);
}
printf("Starting Warmup Runs for arena...\n");
run_benchmark(1, 1, table);
run_benchmark(1, 1, table);
for (int i = 0; i < NUM_RUNS; i++) {
printf("Running Arena iteration %d...\n", i + 1);
arena_results[i] = run_benchmark(1, 0, table);
}
print_final_stats("MALLOC", malloc_results);
print_final_stats("ARENA ", arena_results);
free(table);
return 0;
}