xref: /netbsd-src/external/bsd/jemalloc/dist/test/analyze/prof_bias.c (revision 4439cfd0acf9c7dc90625e5cd83b2317a9ab8967)
1 #include "test/jemalloc_test.h"
2 
3 /*
4  * This is a helper utility, only meant to be run manually (and, for example,
5  * doesn't check for failures, try to skip execution in non-prof modes, etc.).
6  * It runs, allocates objects of two different sizes from the same stack trace,
7  * and exits.
8  *
9  * The idea is that some human operator will run it like:
10  *     MALLOC_CONF="prof:true,prof_final:true" test/analyze/prof_bias
11  * and manually inspect the results.
12  *
13  * The results should be:
14  * jeprof --text test/analyze/prof_bias --inuse_space jeprof.<pid>.0.f.heap:
15  * 	around 1024 MB
16  * jeprof --text test/analyze/prof_bias --inuse_objects jeprof.<pid>.0.f.heap:
17  * 	around 33554448 = 16 + 32 * 1024 * 1024
18  *
19  * And, if prof_accum is on:
20  * jeprof --text test/analyze/prof_bias --alloc_space jeprof.<pid>.0.f.heap:
21  *     around 2048 MB
22  * jeprof --text test/analyze/prof_bias --alloc_objects jeprof.<pid>.0.f.heap:
23  * 	around 67108896 = 2 * (16 + 32 * 1024 * 1024)
24  */
25 
26 static void
27 mock_backtrace(void **vec, unsigned *len, unsigned max_len) {
28 	*len = 4;
29 	vec[0] = (void *)0x111;
30 	vec[1] = (void *)0x222;
31 	vec[2] = (void *)0x333;
32 	vec[3] = (void *)0x444;
33 }
34 
35 static void
36 do_allocs(size_t sz, size_t cnt, bool do_frees) {
37 	for (size_t i = 0; i < cnt; i++) {
38 		void *ptr = mallocx(sz, 0);
39 		assert_ptr_not_null(ptr, "Unexpected mallocx failure");
40 		if (do_frees) {
41 			dallocx(ptr, 0);
42 		}
43 	}
44 }
45 
46 int
47 main(void) {
48 	size_t lg_prof_sample_local = 19;
49 	int err = mallctl("prof.reset", NULL, NULL,
50 	    (void *)&lg_prof_sample_local, sizeof(lg_prof_sample_local));
51 	assert(err == 0);
52 
53 	prof_backtrace_hook_set(mock_backtrace);
54 	do_allocs(16, 32 * 1024 * 1024, /* do_frees */ true);
55 	do_allocs(32 * 1024* 1024, 16, /* do_frees */ true);
56 	do_allocs(16, 32 * 1024 * 1024, /* do_frees */ false);
57 	do_allocs(32 * 1024* 1024, 16, /* do_frees */ false);
58 
59 	return 0;
60 }
61