-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrjntest.c
More file actions
470 lines (427 loc) · 13.9 KB
/
rjntest.c
File metadata and controls
470 lines (427 loc) · 13.9 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
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
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
#include "rjnmalloc.h"
#define _XOPEN_SOURCE
#include <assert.h>
#include <inttypes.h>
#include <malloc.h>
#include <math.h>
#include <pthread.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
// The space used by the rjn allocator
char rjnbuf[1 << 30];
// A wrapper of malloc for benchmarking comparison. We limit the amount of
// space it will allocate at one time to make it more comparable to the rjn
// allocator.
typedef struct bounded_default_allocator {
size_t max_size;
size_t allocated;
} bounded_default_allocator;
void *bounded_default_alloc(void *state, size_t alignment, size_t size) {
bounded_default_allocator *a = (bounded_default_allocator *)state;
if (a->allocated + size > a->max_size) {
return NULL;
}
void *p = aligned_alloc(alignment, size);
if (p) {
a->allocated += malloc_usable_size(p);
}
return p;
}
void bounded_default_free(void *state, void *ptr) {
bounded_default_allocator *a = (bounded_default_allocator *)state;
a->allocated -= malloc_usable_size(ptr);
free(ptr);
}
void *bounded_default_realloc(void *state, void *ptr, size_t alignment,
size_t size) {
bounded_default_allocator *a = (bounded_default_allocator *)state;
size_t old_size = malloc_usable_size(ptr);
if (a->allocated + size + old_size > a->max_size) {
return NULL;
}
assert(a->allocated >= old_size);
a->allocated += size - old_size;
return realloc(ptr, size);
}
allocator_ops bounded_default_allocator_ops = {
bounded_default_alloc, bounded_default_free, bounded_default_realloc};
bounded_default_allocator bd_allocator = {sizeof(rjnbuf), 0};
//
// The test code itself
//
typedef struct allocation {
uint8_t *p;
uint64_t alignment;
size_t size;
int c;
} allocation;
typedef enum alignment_mode { NONE, ARBITRARY } alignment_mode;
typedef struct test_params {
void *state;
allocator_ops *ops;
uint64_t nrounds;
alignment_mode al;
int check_contents;
int64_t alloc_weight;
int64_t realloc_weight;
int64_t free_weight;
uint64_t min_alloc_size;
uint64_t max_alloc_size;
uint64_t max_allocations;
uint64_t num_allocations;
allocation *allocations;
uint64_t successful_allocations;
uint64_t failed_allocations;
uint64_t successful_reallocations;
uint64_t failed_reallocations;
} test_params;
#define MIN(a, b) ((a) < (b) ? (a) : (b))
void check_contents(test_params *params, int i) {
if (params->check_contents && params->allocations[i].p) {
for (unsigned int j = 0; j < params->allocations[i].size; j++) {
assert(params->allocations[i].p[j] == params->allocations[i].c);
}
}
}
void malloc_test(test_params *params) {
void *hdr = params->state;
allocator_ops *ops = params->ops;
allocation *allocations = params->allocations;
int total_weight =
params->alloc_weight + params->realloc_weight + params->free_weight;
assert(0 < total_weight);
double lsize;
if (params->max_alloc_size == params->min_alloc_size) {
lsize = 0;
} else {
lsize = log(params->max_alloc_size - params->min_alloc_size + 1);
}
for (uint64_t j = 0; j < params->nrounds; j++) {
// Choose our operation and some common parameters
int op;
if (params->num_allocations == 0) {
if (params->alloc_weight + params->realloc_weight == 0) {
op = -1; // Always alloc
} else {
op = rand() % (params->alloc_weight + params->realloc_weight);
}
} else if (params->num_allocations == params->max_allocations) {
if (params->realloc_weight + params->free_weight == 0) {
op = total_weight; // Always free
} else {
op = (rand() % (params->realloc_weight + params->free_weight)) +
params->alloc_weight;
}
} else {
op = rand() % total_weight;
}
uint64_t newsize = params->min_alloc_size + exp(lsize * drand48() - 1);
uint64_t newalignment =
params->al == ARBITRARY ? exp(lsize * drand48() - 1) : sizeof(void *);
// Pick an allocation to operate on and do the operation
uint64_t i;
if (op < params->alloc_weight) {
// Alloc
i = params->num_allocations;
assert(i < params->max_allocations);
allocations[i].p = ops->alloc(hdr, newalignment, newsize);
if (allocations[i].p) {
allocations[i].size = newsize;
allocations[i].alignment = newalignment;
params->successful_allocations++;
params->num_allocations++;
} else {
params->failed_allocations++;
}
} else if (op < params->alloc_weight + params->realloc_weight) {
// Realloc
i = (rand() % (params->num_allocations + 1)) % params->max_allocations;
assert(i < params->max_allocations);
check_contents(params, i);
uint8_t *newp =
ops->realloc(hdr, allocations[i].p, newalignment, newsize);
if (newp || newsize == 0) {
allocations[i].p = newp;
allocations[i].size = newsize;
allocations[i].alignment = newalignment;
params->successful_reallocations++;
} else {
params->failed_reallocations++;
}
if (i == params->num_allocations && allocations[i].p) {
params->num_allocations++;
} else if (i < params->num_allocations && allocations[i].p == NULL) {
allocation tmp = allocations[i];
allocations[i] = allocations[params->num_allocations - 1];
allocations[params->num_allocations - 1] = tmp;
params->num_allocations--;
i = params->num_allocations;
}
} else {
// Free
i = rand() % params->num_allocations;
assert(i < params->max_allocations);
check_contents(params, i);
ops->free(hdr, allocations[i].p);
allocations[i].p = NULL;
allocation tmp = allocations[i];
allocations[i] = allocations[params->num_allocations - 1];
allocations[params->num_allocations - 1] = tmp;
params->num_allocations--;
i = params->num_allocations;
}
// Fill the allocation with a known value
if (i < params->num_allocations) {
allocations[i].c = rand() % 256;
if (allocations[i].alignment) {
assert(((uintptr_t)allocations[i].p) % allocations[i].alignment == 0);
}
if (params->check_contents) {
memset(allocations[i].p, allocations[i].c, allocations[i].size);
}
}
}
}
void *malloc_test_thread(void *p) {
malloc_test((test_params *)p);
return NULL;
}
void cleanup_test(test_params *params) {
void *hdr = params->state;
allocator_ops *ops = params->ops;
allocation *allocations = params->allocations;
for (uint64_t i = 0; i < params->num_allocations; i++) {
if (params->check_contents) {
for (unsigned int j = 0; j < allocations[i].size; j++) {
assert(allocations[i].p[j] == allocations[i].c);
}
}
ops->free(hdr, allocations[i].p);
allocations[i].p = NULL;
}
}
// User interface and main driver
__attribute__((noreturn)) void usage(char *argv0) {
int argv0len = strlen(argv0);
printf(
"Usage: %1$s [-a allocator] [-c] [-n nallocs] [-r rounds] [-t nthreads]\n"
" %2$*3$s [-l alignment-mode] [-m min-allocation-size]\n"
" %2$*3$s [-M max-allocation-size] [-A alloc-weight]\n"
" %2$*3$s [-R realloc-weight] [-F free-weight] [-s seed]\n",
argv0, "", argv0len);
printf(" -a: allocator (\"malloc\", \"rjn\")\n");
printf(" -c: check contents of allocations\n");
printf(" -n: number of allocations\n");
printf(" -r: number of rounds\n");
printf(" -t: number of threads\n");
printf(" -l: alignment mode (\"none\", \"arbitrary\")\n");
printf(" -m: minimum allocation size\n");
printf(" -M: maximum allocation size\n");
printf(" -A: weight of allocations\n");
printf(" -R: weight of reallocations\n");
printf(" -F: weight of frees\n");
printf(" -s: seed\n");
exit(EXIT_FAILURE);
}
int main(int argc, char **argv) {
int nallocs = 1000;
int nrounds = 1000000;
int nthreads = 4;
int check_contents = 0;
uint64_t seed = 0;
char *allocator = "rjn";
alignment_mode alignment = NONE;
uint64_t min_alloc_size = 0;
uint64_t max_alloc_size = 0;
uint64_t alloc_weight = 1;
uint64_t realloc_weight = 1;
uint64_t free_weight = 1;
int opt;
char *endptr;
while ((opt = getopt(argc, argv, ":a:cn:r:t:l:m:M:A:R:F:s:")) != -1) {
switch (opt) {
case 'a':
allocator = optarg;
break;
case 'c':
check_contents = 1;
break;
case 'n':
nallocs = strtoull(optarg, &endptr, 0);
if (*endptr != '\0') {
printf("Invalid number of allocations: %s\n", optarg);
usage(argv[0]);
}
break;
case 'r':
nrounds = strtoull(optarg, &endptr, 0);
if (*endptr != '\0') {
printf("Invalid number of rounds: %s\n", optarg);
usage(argv[0]);
}
break;
case 't':
nthreads = strtoull(optarg, &endptr, 0);
if (*endptr != '\0') {
printf("Invalid number of threads: %s\n", optarg);
usage(argv[0]);
}
break;
case 'l':
if (strcmp(optarg, "none") == 0) {
alignment = NONE;
} else if (strcmp(optarg, "arbitrary") == 0) {
alignment = ARBITRARY;
} else {
printf("Invalid alignment mode: %s\n", optarg);
usage(argv[0]);
}
break;
case 'm':
min_alloc_size = strtoull(optarg, &endptr, 0);
if (*endptr != '\0') {
printf("Invalid min allocation size: %s\n", optarg);
usage(argv[0]);
}
break;
case 'M':
max_alloc_size = strtoull(optarg, &endptr, 0);
if (*endptr != '\0') {
printf("Invalid max allocation size: %s\n", optarg);
usage(argv[0]);
}
break;
case 's':
seed = strtoull(optarg, &endptr, 0);
if (*endptr != '\0') {
printf("Invalid seed: %s\n", optarg);
usage(argv[0]);
}
srand(seed);
srand48(rand());
break;
case 'A':
alloc_weight = strtoull(optarg, &endptr, 0);
if (*endptr != '\0') {
printf("Invalid allocation weight: %s\n", optarg);
usage(argv[0]);
}
break;
case 'R':
realloc_weight = strtoull(optarg, &endptr, 0);
if (*endptr != '\0') {
printf("Invalid reallocation weight: %s\n", optarg);
usage(argv[0]);
}
break;
case 'F':
free_weight = strtoull(optarg, &endptr, 0);
if (*endptr != '\0') {
printf("Invalid free weight: %s\n", optarg);
usage(argv[0]);
}
break;
case ':':
printf("Option -%c requires an operand\n", optopt);
usage(argv[0]);
default:
printf("Unknown option: -%c\n", optopt);
usage(argv[0]);
}
}
allocator_ops *ops = NULL;
void *state = NULL;
if (strcmp(allocator, "malloc") == 0) {
ops = &bounded_default_allocator_ops;
state = &bd_allocator;
} else if (strcmp(allocator, "rjn") == 0) {
ops = &rjn_allocator_ops;
rjn *hdr = (rjn *)rjnbuf;
int r = rjn_init(hdr, sizeof(rjnbuf), 1 << 6);
assert(r == 0);
state = hdr;
} else {
printf("Unknown allocator: %s\n", allocator);
usage(argv[0]);
}
if (max_alloc_size == 0) {
max_alloc_size = sizeof(rjnbuf);
}
if (max_alloc_size < min_alloc_size) {
printf(
"Max allocation size must be greater than or equal to min allocation "
"size\n");
usage(argv[0]);
}
if (alloc_weight + realloc_weight + free_weight == 0) {
printf("At least one of alloc_weight, realloc_weight, or free_weight must "
"be non-zero\n");
usage(argv[0]);
}
test_params *params = (test_params *)malloc(sizeof(test_params) * nthreads);
for (int i = 0; i < nthreads; i++) {
params[i].state = state;
params[i].ops = ops;
params[i].nrounds = nrounds;
params[i].al = alignment;
params[i].check_contents = check_contents;
params[i].min_alloc_size = min_alloc_size;
params[i].max_alloc_size = max_alloc_size;
params[i].alloc_weight = alloc_weight;
params[i].realloc_weight = realloc_weight;
params[i].free_weight = free_weight;
params[i].max_allocations = nallocs;
params[i].num_allocations = 0;
params[i].allocations = (allocation *)calloc(nallocs, sizeof(allocation));
params[i].successful_allocations = 0;
params[i].failed_allocations = 0;
params[i].successful_reallocations = 0;
params[i].failed_reallocations = 0;
}
printf("Running malloc_test with %d thread%s for %d round%s\n", nthreads,
nthreads == 1 ? "" : "s", nrounds, nrounds == 1 ? "" : "s");
if (1 < nthreads) {
pthread_t *threads = (pthread_t *)malloc(sizeof(pthread_t) * nthreads);
for (int i = 0; i < nthreads; i++) {
pthread_create(&threads[i], NULL, malloc_test_thread, ¶ms[i]);
}
for (int i = 0; i < nthreads; i++) {
pthread_join(threads[i], NULL);
}
} else {
malloc_test(¶ms[0]);
}
if (ops == &rjn_allocator_ops) {
printf("Allocation stats after test:\n");
rjn_print_allocation_stats((rjn *)state);
}
uint64_t total_successful_allocations = 0;
uint64_t total_failed_allocations = 0;
uint64_t total_successful_reallocations = 0;
uint64_t total_failed_reallocations = 0;
for (int i = 0; i < nthreads; i++) {
total_successful_allocations += params[i].successful_allocations;
total_failed_allocations += params[i].failed_allocations;
total_successful_reallocations += params[i].successful_reallocations;
total_failed_reallocations += params[i].failed_reallocations;
}
printf("Total successful allocations: %" PRIu64 "\n",
total_successful_allocations);
printf("Total failed allocations: %" PRIu64 "\n", total_failed_allocations);
printf("Total successful reallocations: %" PRIu64 "\n",
total_successful_reallocations);
printf("Total failed reallocations: %" PRIu64 "\n",
total_failed_reallocations);
printf("Cleaning up\n");
for (int i = 0; i < nthreads; i++) {
cleanup_test(¶ms[i]);
}
if (ops == &rjn_allocator_ops) {
printf("Allocation stats after cleanup:\n");
rjn_print_allocation_stats(state);
rjn_deinit(state);
}
return 0;
}