xref: /netbsd-src/external/bsd/jemalloc/dist/test/unit/prof_gdump.c (revision 7bdf38e5b7a28439665f2fdeff81e36913eef7dd)
1 #include "test/jemalloc_test.h"
2 
3 #include "jemalloc/internal/prof_sys.h"
4 
5 static bool did_prof_dump_open;
6 
7 static int
8 prof_dump_open_file_intercept(const char *filename, int mode) {
9 	int fd;
10 
11 	did_prof_dump_open = true;
12 
13 	fd = open("/dev/null", O_WRONLY);
14 	assert_d_ne(fd, -1, "Unexpected open() failure");
15 
16 	return fd;
17 }
18 
19 TEST_BEGIN(test_gdump) {
20 	test_skip_if(opt_hpa);
21 	bool active, gdump, gdump_old;
22 	void *p, *q, *r, *s;
23 	size_t sz;
24 
25 	test_skip_if(!config_prof);
26 
27 	active = true;
28 	expect_d_eq(mallctl("prof.active", NULL, NULL, (void *)&active,
29 	    sizeof(active)), 0,
30 	    "Unexpected mallctl failure while activating profiling");
31 
32 	prof_dump_open_file = prof_dump_open_file_intercept;
33 
34 	did_prof_dump_open = false;
35 	p = mallocx((1U << SC_LG_LARGE_MINCLASS), 0);
36 	expect_ptr_not_null(p, "Unexpected mallocx() failure");
37 	expect_true(did_prof_dump_open, "Expected a profile dump");
38 
39 	did_prof_dump_open = false;
40 	q = mallocx((1U << SC_LG_LARGE_MINCLASS), 0);
41 	expect_ptr_not_null(q, "Unexpected mallocx() failure");
42 	expect_true(did_prof_dump_open, "Expected a profile dump");
43 
44 	gdump = false;
45 	sz = sizeof(gdump_old);
46 	expect_d_eq(mallctl("prof.gdump", (void *)&gdump_old, &sz,
47 	    (void *)&gdump, sizeof(gdump)), 0,
48 	    "Unexpected mallctl failure while disabling prof.gdump");
49 	assert(gdump_old);
50 	did_prof_dump_open = false;
51 	r = mallocx((1U << SC_LG_LARGE_MINCLASS), 0);
52 	expect_ptr_not_null(q, "Unexpected mallocx() failure");
53 	expect_false(did_prof_dump_open, "Unexpected profile dump");
54 
55 	gdump = true;
56 	sz = sizeof(gdump_old);
57 	expect_d_eq(mallctl("prof.gdump", (void *)&gdump_old, &sz,
58 	    (void *)&gdump, sizeof(gdump)), 0,
59 	    "Unexpected mallctl failure while enabling prof.gdump");
60 	assert(!gdump_old);
61 	did_prof_dump_open = false;
62 	s = mallocx((1U << SC_LG_LARGE_MINCLASS), 0);
63 	expect_ptr_not_null(q, "Unexpected mallocx() failure");
64 	expect_true(did_prof_dump_open, "Expected a profile dump");
65 
66 	dallocx(p, 0);
67 	dallocx(q, 0);
68 	dallocx(r, 0);
69 	dallocx(s, 0);
70 }
71 TEST_END
72 
73 int
74 main(void) {
75 	return test_no_reentrancy(
76 	    test_gdump);
77 }
78