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