1 #include "test/jemalloc_test.h"
2
3 #ifdef JEMALLOC_PROF
4 const char *malloc_conf = "prof:true,prof_active:false,prof_gdump:true";
5 #endif
6
7 static bool did_prof_dump_open;
8
9 static int
prof_dump_open_intercept(bool propagate_err,const char * filename)10 prof_dump_open_intercept(bool propagate_err, const char *filename)
11 {
12 int fd;
13
14 did_prof_dump_open = true;
15
16 fd = open("/dev/null", O_WRONLY);
17 assert_d_ne(fd, -1, "Unexpected open() failure");
18
19 return (fd);
20 }
21
TEST_BEGIN(test_gdump)22 TEST_BEGIN(test_gdump)
23 {
24 bool active, gdump, gdump_old;
25 void *p, *q, *r, *s;
26 size_t sz;
27
28 test_skip_if(!config_prof);
29
30 active = true;
31 assert_d_eq(mallctl("prof.active", NULL, NULL, (void *)&active,
32 sizeof(active)), 0,
33 "Unexpected mallctl failure while activating profiling");
34
35 prof_dump_open = prof_dump_open_intercept;
36
37 did_prof_dump_open = false;
38 p = mallocx(chunksize, 0);
39 assert_ptr_not_null(p, "Unexpected mallocx() failure");
40 assert_true(did_prof_dump_open, "Expected a profile dump");
41
42 did_prof_dump_open = false;
43 q = mallocx(chunksize, 0);
44 assert_ptr_not_null(q, "Unexpected mallocx() failure");
45 assert_true(did_prof_dump_open, "Expected a profile dump");
46
47 gdump = false;
48 sz = sizeof(gdump_old);
49 assert_d_eq(mallctl("prof.gdump", (void *)&gdump_old, &sz,
50 (void *)&gdump, sizeof(gdump)), 0,
51 "Unexpected mallctl failure while disabling prof.gdump");
52 assert(gdump_old);
53 did_prof_dump_open = false;
54 r = mallocx(chunksize, 0);
55 assert_ptr_not_null(q, "Unexpected mallocx() failure");
56 assert_false(did_prof_dump_open, "Unexpected profile dump");
57
58 gdump = true;
59 sz = sizeof(gdump_old);
60 assert_d_eq(mallctl("prof.gdump", (void *)&gdump_old, &sz,
61 (void *)&gdump, sizeof(gdump)), 0,
62 "Unexpected mallctl failure while enabling prof.gdump");
63 assert(!gdump_old);
64 did_prof_dump_open = false;
65 s = mallocx(chunksize, 0);
66 assert_ptr_not_null(q, "Unexpected mallocx() failure");
67 assert_true(did_prof_dump_open, "Expected a profile dump");
68
69 dallocx(p, 0);
70 dallocx(q, 0);
71 dallocx(r, 0);
72 dallocx(s, 0);
73 }
74 TEST_END
75
76 int
main(void)77 main(void)
78 {
79
80 return (test(
81 test_gdump));
82 }
83