xref: /netbsd-src/external/bsd/jemalloc/dist/test/unit/prof_accum.c (revision 7bdf38e5b7a28439665f2fdeff81e36913eef7dd)
1 #include "test/jemalloc_test.h"
2 
3 #include "jemalloc/internal/prof_data.h"
4 #include "jemalloc/internal/prof_sys.h"
5 
6 #define NTHREADS		4
7 #define NALLOCS_PER_THREAD	50
8 #define DUMP_INTERVAL		1
9 #define BT_COUNT_CHECK_INTERVAL	5
10 
11 static int
12 prof_dump_open_file_intercept(const char *filename, int mode) {
13 	int fd;
14 
15 	fd = open("/dev/null", O_WRONLY);
16 	assert_d_ne(fd, -1, "Unexpected open() failure");
17 
18 	return fd;
19 }
20 
21 static void *
22 alloc_from_permuted_backtrace(unsigned thd_ind, unsigned iteration) {
23 	return btalloc(1, thd_ind*NALLOCS_PER_THREAD + iteration);
24 }
25 
26 static void *
27 thd_start(void *varg) {
28 	unsigned thd_ind = *(unsigned *)varg;
29 	size_t bt_count_prev, bt_count;
30 	unsigned i_prev, i;
31 
32 	i_prev = 0;
33 	bt_count_prev = 0;
34 	for (i = 0; i < NALLOCS_PER_THREAD; i++) {
35 		void *p = alloc_from_permuted_backtrace(thd_ind, i);
36 		dallocx(p, 0);
37 		if (i % DUMP_INTERVAL == 0) {
38 			expect_d_eq(mallctl("prof.dump", NULL, NULL, NULL, 0),
39 			    0, "Unexpected error while dumping heap profile");
40 		}
41 
42 		if (i % BT_COUNT_CHECK_INTERVAL == 0 ||
43 		    i+1 == NALLOCS_PER_THREAD) {
44 			bt_count = prof_bt_count();
45 			expect_zu_le(bt_count_prev+(i-i_prev), bt_count,
46 			    "Expected larger backtrace count increase");
47 			i_prev = i;
48 			bt_count_prev = bt_count;
49 		}
50 	}
51 
52 	return NULL;
53 }
54 
55 TEST_BEGIN(test_idump) {
56 	bool active;
57 	thd_t thds[NTHREADS];
58 	unsigned thd_args[NTHREADS];
59 	unsigned i;
60 
61 	test_skip_if(!config_prof);
62 
63 	active = true;
64 	expect_d_eq(mallctl("prof.active", NULL, NULL, (void *)&active,
65 	    sizeof(active)), 0,
66 	    "Unexpected mallctl failure while activating profiling");
67 
68 	prof_dump_open_file = prof_dump_open_file_intercept;
69 
70 	for (i = 0; i < NTHREADS; i++) {
71 		thd_args[i] = i;
72 		thd_create(&thds[i], thd_start, (void *)&thd_args[i]);
73 	}
74 	for (i = 0; i < NTHREADS; i++) {
75 		thd_join(thds[i], NULL);
76 	}
77 }
78 TEST_END
79 
80 int
81 main(void) {
82 	return test_no_reentrancy(
83 	    test_idump);
84 }
85