Home | History | Annotate | Download | only in unit
      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
     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 
     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, &active, sizeof(active)),
     32 	    0, "Unexpected mallctl failure while activating profiling");
     33 
     34 	prof_dump_open = prof_dump_open_intercept;
     35 
     36 	did_prof_dump_open = false;
     37 	p = mallocx(chunksize, 0);
     38 	assert_ptr_not_null(p, "Unexpected mallocx() failure");
     39 	assert_true(did_prof_dump_open, "Expected a profile dump");
     40 
     41 	did_prof_dump_open = false;
     42 	q = mallocx(chunksize, 0);
     43 	assert_ptr_not_null(q, "Unexpected mallocx() failure");
     44 	assert_true(did_prof_dump_open, "Expected a profile dump");
     45 
     46 	gdump = false;
     47 	sz = sizeof(gdump_old);
     48 	assert_d_eq(mallctl("prof.gdump", &gdump_old, &sz, &gdump,
     49 	    sizeof(gdump)), 0,
     50 	    "Unexpected mallctl failure while disabling prof.gdump");
     51 	assert(gdump_old);
     52 	did_prof_dump_open = false;
     53 	r = mallocx(chunksize, 0);
     54 	assert_ptr_not_null(q, "Unexpected mallocx() failure");
     55 	assert_false(did_prof_dump_open, "Unexpected profile dump");
     56 
     57 	gdump = true;
     58 	sz = sizeof(gdump_old);
     59 	assert_d_eq(mallctl("prof.gdump", &gdump_old, &sz, &gdump,
     60 	    sizeof(gdump)), 0,
     61 	    "Unexpected mallctl failure while enabling prof.gdump");
     62 	assert(!gdump_old);
     63 	did_prof_dump_open = false;
     64 	s = mallocx(chunksize, 0);
     65 	assert_ptr_not_null(q, "Unexpected mallocx() failure");
     66 	assert_true(did_prof_dump_open, "Expected a profile dump");
     67 
     68 	dallocx(p, 0);
     69 	dallocx(q, 0);
     70 	dallocx(r, 0);
     71 	dallocx(s, 0);
     72 }
     73 TEST_END
     74 
     75 int
     76 main(void)
     77 {
     78 
     79 	return (test(
     80 	    test_gdump));
     81 }
     82