-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmalloc_test.c
78 lines (65 loc) · 1.73 KB
/
malloc_test.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
71
72
73
74
75
76
77
78
// Copyright 2022 by [Chris Palmer](https://noncombatant.org)
// SPDX-License-Identifier: Apache-2.0
#include <errno.h>
#include <inttypes.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "get_utc_nanoseconds.h"
#if defined(ORIGINAL)
#include "original_kr_malloc.h"
#elif defined(MODERN)
#include "modern_kr_malloc.h"
#elif defined(ARENA)
#include "arena_malloc.h"
#else
#error No flavor specified
#endif
#define iterations 1000
// Touch every page to ensure the benchmark doesn’t get noisier than it already
// is due to lazy commitment/page faults.
static void touch_pages(void* p, size_t byte_count) {
char* pp = p;
for (size_t i = 0; i < byte_count; i += 4096) {
pp[i] = 0;
}
}
int main() {
const size_t iterations_size = iterations * sizeof(char*);
char** ps = malloc(iterations_size);
touch_pages(ps, iterations_size);
#if defined(ARENA)
Arena a;
arena_create(&a, default_minimum_chunk_units);
#endif
const int64_t start = GetUTCNanoseconds();
for (size_t i = 1; i < iterations; i++) {
#if defined(ORIGINAL)
char* p = kr_malloc(i * 1);
#elif defined(MODERN)
char* p = kr_malloc(i, 1);
#elif defined(ARENA)
char* p = arena_malloc(&a, i, 1);
#endif
ps[i] = p;
if (p == NULL) {
printf("%s\n", strerror(errno));
return errno;
}
}
const int64_t after_allocations = GetUTCNanoseconds();
for (size_t i = 1; i < iterations; i++) {
#if defined(ARENA)
arena_free(&a, ps[i]);
#else
kr_free(ps[i]);
#endif
}
const int64_t end = GetUTCNanoseconds();
printf("ns per malloc: %" PRId64 ", ns per free: %" PRId64 "\n",
(after_allocations - start) / iterations,
(end - after_allocations) / iterations);
#if defined(ARENA)
arena_destroy(&a);
#endif
}