forked from jemalloc/jemalloc-experiments
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbasic.cpp
75 lines (58 loc) · 2.31 KB
/
basic.cpp
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
#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
#include <jemalloc/jemalloc.h>
#include <cstdlib>
#include <tuple>
#include <vector>
// Copied from
// https://github.com/jemalloc/jemalloc/wiki/Use-Case%3A-Introspection-Via-mallctl%2A%28%29
// BUILD_TYPE=Debug ./install-jemalloc-and-build.sh
// build in debug mode, or everything is optimized out.
// ./install/bin/jeprof --text build/basic my_prefix.63665.0.i0.heap
// Dump memory profile after every 2^22=4MB of allocation activity.
// MALLOC_CONF="prof:true,lg_prof_interval:22" ./build/basic
void
make_arr() {
int* ptr = new int[10000];
}
void
do_something(size_t i)
{
// Compare these two. They will have different totals.
// Leak some memory.
// std::ignore = malloc(i * 1000);
void* ptr = malloc(i * 1000);
free(ptr);
make_arr();
}
int
main(int argc, char **argv)
{
size_t i, sz;
// This can be an absolute path though.
const char* my_prefix = "my_prefix";
mallctl("prof.prefix", NULL, NULL, (void*)(&my_prefix), sizeof(const char*));
for (i = 0; i < 100; i++) {
do_something(i);
// Update the statistics cached by mallctl.
uint64_t epoch = 1;
sz = sizeof(epoch);
mallctl("epoch", &epoch, &sz, &epoch, sz);
// Get basic allocation statistics. Take care to check for
// errors, since --enable-stats must have been specified at
// build time for these statistics to be available.
size_t sz, allocated, active, metadata, resident, mapped;
sz = sizeof(size_t);
if (mallctl("stats.allocated", &allocated, &sz, NULL, 0) == 0
&& mallctl("stats.active", &active, &sz, NULL, 0) == 0
&& mallctl("stats.metadata", &metadata, &sz, NULL, 0) == 0
&& mallctl("stats.resident", &resident, &sz, NULL, 0) == 0
&& mallctl("stats.mapped", &mapped, &sz, NULL, 0) == 0) {
fprintf(stderr,
"Current allocated/active/metadata/resident/mapped: %zu/%zu/%zu/%zu/%zu\n",
allocated, active, metadata, resident, mapped);
}
}
return (0);
}