forked from microsoft/snmalloc
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlarge_alloc.cc
More file actions
90 lines (73 loc) · 2.04 KB
/
large_alloc.cc
File metadata and controls
90 lines (73 loc) · 2.04 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
#include <test/measuretime.h>
#include <test/opt.h>
#include <test/setup.h>
#include <test/snmalloc_testlib.h>
using namespace snmalloc;
static constexpr size_t ALLOC_SIZE = 800 * 1024; // 800 KB
void test_alloc_dealloc_cycle(size_t iterations)
{
{
MeasureTime m;
m << "Alloc/dealloc 800KB x " << iterations;
for (size_t i = 0; i < iterations; i++)
{
void* p = snmalloc::alloc(ALLOC_SIZE);
SNMALLOC_CHECK(p != nullptr);
snmalloc::dealloc(p);
}
}
snmalloc::debug_check_empty();
}
void test_batch_alloc_then_dealloc(size_t iterations)
{
static constexpr size_t BATCH = 128;
void* ptrs[BATCH];
MeasureTime m;
m << "Batch alloc then dealloc 800KB x " << BATCH;
for (size_t j = 0; j < iterations / BATCH; j++)
{
for (size_t i = 0; i < BATCH; i++)
{
ptrs[i] = snmalloc::alloc(ALLOC_SIZE);
SNMALLOC_CHECK(ptrs[i] != nullptr);
}
for (size_t i = 0; i < BATCH; i++)
{
snmalloc::dealloc(ptrs[i]);
}
}
snmalloc::debug_check_empty();
}
void test_alloc_dealloc_with_touch(size_t iterations)
{
{
MeasureTime m;
m << "Alloc/touch/dealloc 800KB x " << iterations;
for (size_t i = 0; i < iterations; i++)
{
char* p = static_cast<char*>(snmalloc::alloc(ALLOC_SIZE));
SNMALLOC_CHECK(p != nullptr);
// Touch every 4KiB and last bytes to ensure pages are faulted in
for (size_t offset = 0; offset < ALLOC_SIZE; offset += 4096)
{
p[offset] = 1;
}
snmalloc::dealloc(p);
}
}
snmalloc::debug_check_empty();
}
int main(int argc, char** argv)
{
setup();
opt::Opt opt(argc, argv);
// Each test does alloc/dealloc cycles driven by `iterations`. The
// batch test divides by BATCH=128, so the smoke value is chosen so
// that `smoke / 128 >= 1` (i.e. the batch test still runs at least
// one full batch round).
size_t iterations = opt.has("--smoke") ? 8192 : 100000;
test_alloc_dealloc_cycle(iterations);
test_batch_alloc_then_dealloc(iterations);
test_alloc_dealloc_with_touch(iterations);
return 0;
}