-
Notifications
You must be signed in to change notification settings - Fork 11
/
bench.c
70 lines (56 loc) · 1.99 KB
/
bench.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
/*
* Copyright 2023 Stanislav Paskalev <spaskalev@protonmail.com>
*/
#include <assert.h>
#include <errno.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#define BUDDY_ALLOC_ALIGN 64
#define BUDDY_ALLOC_IMPLEMENTATION
#include "buddy_alloc.h"
#undef BUDDY_ALLOC_IMPLEMENTATION
double test_malloc(struct buddy *buddy, size_t alloc_size);
double test_malloc_firstfit(size_t alloc_size);
void *freeing_callback(void *ctx, void *addr, size_t slot_size, size_t allocated);
int main() {
setvbuf(stdout, NULL, _IONBF, 0);
size_t arena_size = 1 << 30;
unsigned char *buddy_buf = (unsigned char *) malloc(buddy_sizeof_alignment(arena_size, 64));
unsigned char *data_buf = (unsigned char *) malloc(arena_size);
struct buddy *buddy = buddy_init_alignment(buddy_buf, data_buf, arena_size, 64);
double total = 0;
for (size_t i = 0; i <= 6; i++) {
total += test_malloc(buddy, 64 << i);
total += test_malloc(buddy, 64 << i);
total += test_malloc(buddy, 64 << i);
}
printf("Total malloc runtime was %f seconds.\n\n", total);
free(data_buf);
free(buddy_buf);
}
double test_malloc(struct buddy *buddy, size_t alloc_size) {
printf("Starting test with alloc size [%zu].\n", alloc_size);
time_t start_time = time(NULL);
while (buddy_malloc(buddy, alloc_size)) {
// fill it up
}
time_t alloc_time = time(NULL);
assert(buddy_is_full(buddy));
buddy_walk(buddy, freeing_callback, buddy);
assert(buddy_is_empty(buddy));
time_t end_time = time(NULL);
double delta = difftime(end_time, start_time);
printf("Test took %.f seconds in total. Allocation: %.f freeing: %.f\n", delta,
difftime(alloc_time, start_time), difftime(end_time, alloc_time));
return delta;
}
void *freeing_callback(void *ctx, void *addr, size_t slot_size, size_t allocated) {
if (! allocated) {
return NULL;
}
struct buddy *buddy = (struct buddy*) ctx;
buddy_free(buddy, addr);
return NULL;
}