Skip to content

Commit f9c8dab

Browse files
authored
Add repeated upsizing tests (#129)
* format change: drop tabs * update gitignore * add repeated upresizing tests * Use buddy_can_shrink when testing upsize to detect if any allocation has moved up
1 parent 236bba8 commit f9c8dab

File tree

3 files changed

+2065
-2028
lines changed

3 files changed

+2065
-2028
lines changed

.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,9 @@ dkms.conf
6060
# Perf
6161
bench
6262

63+
# Test
64+
tests
65+
6366
# cmake output dir
6467
out/*
6568

bench.c

Lines changed: 34 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -19,52 +19,52 @@ double test_malloc_firstfit(size_t alloc_size);
1919
void *freeing_callback(void *ctx, void *addr, size_t slot_size, size_t allocated);
2020

2121
int main() {
22-
setvbuf(stdout, NULL, _IONBF, 0);
22+
setvbuf(stdout, NULL, _IONBF, 0);
2323

24-
size_t arena_size = 1 << 30;
25-
unsigned char *buddy_buf = (unsigned char *) malloc(buddy_sizeof_alignment(arena_size, 64));
26-
unsigned char *data_buf = (unsigned char *) malloc(arena_size);
27-
struct buddy *buddy = buddy_init_alignment(buddy_buf, data_buf, arena_size, 64);
24+
size_t arena_size = 1 << 30;
25+
unsigned char *buddy_buf = (unsigned char *) malloc(buddy_sizeof_alignment(arena_size, 64));
26+
unsigned char *data_buf = (unsigned char *) malloc(arena_size);
27+
struct buddy *buddy = buddy_init_alignment(buddy_buf, data_buf, arena_size, 64);
2828

29-
double total = 0;
30-
for (size_t i = 0; i <= 6; i++) {
31-
total += test_malloc(buddy, 64 << i);
32-
total += test_malloc(buddy, 64 << i);
33-
total += test_malloc(buddy, 64 << i);
34-
}
35-
printf("Total malloc runtime was %f seconds.\n\n", total);
29+
double total = 0;
30+
for (size_t i = 0; i <= 6; i++) {
31+
total += test_malloc(buddy, 64 << i);
32+
total += test_malloc(buddy, 64 << i);
33+
total += test_malloc(buddy, 64 << i);
34+
}
35+
printf("Total malloc runtime was %f seconds.\n\n", total);
3636

37-
free(data_buf);
38-
free(buddy_buf);
37+
free(data_buf);
38+
free(buddy_buf);
3939
}
4040

4141
double test_malloc(struct buddy *buddy, size_t alloc_size) {
42-
printf("Starting test with alloc size [%zu].\n", alloc_size);
43-
time_t start_time = time(NULL);
42+
printf("Starting test with alloc size [%zu].\n", alloc_size);
43+
time_t start_time = time(NULL);
4444

45-
while (buddy_malloc(buddy, alloc_size)) {
46-
// fill it up
47-
}
48-
time_t alloc_time = time(NULL);
45+
while (buddy_malloc(buddy, alloc_size)) {
46+
// fill it up
47+
}
48+
time_t alloc_time = time(NULL);
4949

50-
assert(buddy_is_full(buddy));
50+
assert(buddy_is_full(buddy));
5151

52-
buddy_walk(buddy, freeing_callback, buddy);
53-
assert(buddy_is_empty(buddy));
52+
buddy_walk(buddy, freeing_callback, buddy);
53+
assert(buddy_is_empty(buddy));
5454

55-
time_t end_time = time(NULL);
56-
double delta = difftime(end_time, start_time);
57-
printf("Test took %.f seconds in total. Allocation: %.f freeing: %.f\n", delta,
58-
difftime(alloc_time, start_time), difftime(end_time, alloc_time));
55+
time_t end_time = time(NULL);
56+
double delta = difftime(end_time, start_time);
57+
printf("Test took %.f seconds in total. Allocation: %.f freeing: %.f\n", delta,
58+
difftime(alloc_time, start_time), difftime(end_time, alloc_time));
5959

60-
return delta;
60+
return delta;
6161
}
6262

6363
void *freeing_callback(void *ctx, void *addr, size_t slot_size, size_t allocated) {
64-
if (! allocated) {
65-
return NULL;
66-
}
67-
struct buddy *buddy = (struct buddy*) ctx;
68-
buddy_free(buddy, addr);
69-
return NULL;
64+
if (! allocated) {
65+
return NULL;
66+
}
67+
struct buddy *buddy = (struct buddy*) ctx;
68+
buddy_free(buddy, addr);
69+
return NULL;
7070
}

0 commit comments

Comments
 (0)