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