xref: /netbsd-src/external/bsd/jemalloc/dist/test/unit/prof_idump.c (revision 7bdf38e5b7a28439665f2fdeff81e36913eef7dd)
1a0698ed9Schristos #include "test/jemalloc_test.h"
2a0698ed9Schristos 
3*7bdf38e5Schristos #include "jemalloc/internal/prof_sys.h"
4*7bdf38e5Schristos 
5*7bdf38e5Schristos #define TEST_PREFIX "test_prefix"
6*7bdf38e5Schristos 
7a0698ed9Schristos static bool did_prof_dump_open;
8a0698ed9Schristos 
9a0698ed9Schristos static int
10*7bdf38e5Schristos prof_dump_open_file_intercept(const char *filename, int mode) {
11a0698ed9Schristos 	int fd;
12a0698ed9Schristos 
13a0698ed9Schristos 	did_prof_dump_open = true;
14a0698ed9Schristos 
15*7bdf38e5Schristos 	const char filename_prefix[] = TEST_PREFIX ".";
16*7bdf38e5Schristos 	expect_d_eq(strncmp(filename_prefix, filename, sizeof(filename_prefix)
17*7bdf38e5Schristos 	    - 1), 0, "Dump file name should start with \"" TEST_PREFIX ".\"");
18*7bdf38e5Schristos 
19a0698ed9Schristos 	fd = open("/dev/null", O_WRONLY);
20a0698ed9Schristos 	assert_d_ne(fd, -1, "Unexpected open() failure");
21a0698ed9Schristos 
22a0698ed9Schristos 	return fd;
23a0698ed9Schristos }
24a0698ed9Schristos 
25a0698ed9Schristos TEST_BEGIN(test_idump) {
26a0698ed9Schristos 	bool active;
27a0698ed9Schristos 	void *p;
28a0698ed9Schristos 
29*7bdf38e5Schristos 	const char *test_prefix = TEST_PREFIX;
30*7bdf38e5Schristos 
31a0698ed9Schristos 	test_skip_if(!config_prof);
32a0698ed9Schristos 
33a0698ed9Schristos 	active = true;
34*7bdf38e5Schristos 
35*7bdf38e5Schristos 	expect_d_eq(mallctl("prof.prefix", NULL, NULL, (void *)&test_prefix,
36*7bdf38e5Schristos 	    sizeof(test_prefix)), 0,
37*7bdf38e5Schristos 	    "Unexpected mallctl failure while overwriting dump prefix");
38*7bdf38e5Schristos 
39*7bdf38e5Schristos 	expect_d_eq(mallctl("prof.active", NULL, NULL, (void *)&active,
40a0698ed9Schristos 	    sizeof(active)), 0,
41a0698ed9Schristos 	    "Unexpected mallctl failure while activating profiling");
42a0698ed9Schristos 
43*7bdf38e5Schristos 	prof_dump_open_file = prof_dump_open_file_intercept;
44a0698ed9Schristos 
45a0698ed9Schristos 	did_prof_dump_open = false;
46a0698ed9Schristos 	p = mallocx(1, 0);
47*7bdf38e5Schristos 	expect_ptr_not_null(p, "Unexpected mallocx() failure");
48a0698ed9Schristos 	dallocx(p, 0);
49*7bdf38e5Schristos 	expect_true(did_prof_dump_open, "Expected a profile dump");
50a0698ed9Schristos }
51a0698ed9Schristos TEST_END
52a0698ed9Schristos 
53a0698ed9Schristos int
54a0698ed9Schristos main(void) {
55a0698ed9Schristos 	return test(
56a0698ed9Schristos 	    test_idump);
57a0698ed9Schristos }
58