Home | History | Annotate | Line # | Download | only in stress
      1  1.1  christos #include "test/jemalloc_test.h"
      2  1.1  christos 
      3  1.1  christos static inline void
      4  1.1  christos time_func(timedelta_t *timer, uint64_t nwarmup, uint64_t niter,
      5  1.1  christos     void (*func)(void)) {
      6  1.1  christos 	uint64_t i;
      7  1.1  christos 
      8  1.1  christos 	for (i = 0; i < nwarmup; i++) {
      9  1.1  christos 		func();
     10  1.1  christos 	}
     11  1.1  christos 	timer_start(timer);
     12  1.1  christos 	for (i = 0; i < niter; i++) {
     13  1.1  christos 		func();
     14  1.1  christos 	}
     15  1.1  christos 	timer_stop(timer);
     16  1.1  christos }
     17  1.1  christos 
     18  1.1  christos void
     19  1.1  christos compare_funcs(uint64_t nwarmup, uint64_t niter, const char *name_a,
     20  1.1  christos     void (*func_a), const char *name_b, void (*func_b)) {
     21  1.1  christos 	timedelta_t timer_a, timer_b;
     22  1.1  christos 	char ratio_buf[6];
     23  1.1  christos 	void *p;
     24  1.1  christos 
     25  1.1  christos 	p = mallocx(1, 0);
     26  1.1  christos 	if (p == NULL) {
     27  1.1  christos 		test_fail("Unexpected mallocx() failure");
     28  1.1  christos 		return;
     29  1.1  christos 	}
     30  1.1  christos 
     31  1.1  christos 	time_func(&timer_a, nwarmup, niter, func_a);
     32  1.1  christos 	time_func(&timer_b, nwarmup, niter, func_b);
     33  1.1  christos 
     34  1.1  christos 	timer_ratio(&timer_a, &timer_b, ratio_buf, sizeof(ratio_buf));
     35  1.1  christos 	malloc_printf("%"FMTu64" iterations, %s=%"FMTu64"us, "
     36  1.1  christos 	    "%s=%"FMTu64"us, ratio=1:%s\n",
     37  1.1  christos 	    niter, name_a, timer_usec(&timer_a), name_b, timer_usec(&timer_b),
     38  1.1  christos 	    ratio_buf);
     39  1.1  christos 
     40  1.1  christos 	dallocx(p, 0);
     41  1.1  christos }
     42  1.1  christos 
     43  1.1  christos static void
     44  1.1  christos malloc_free(void) {
     45  1.1  christos 	/* The compiler can optimize away free(malloc(1))! */
     46  1.1  christos 	void *p = malloc(1);
     47  1.1  christos 	if (p == NULL) {
     48  1.1  christos 		test_fail("Unexpected malloc() failure");
     49  1.1  christos 		return;
     50  1.1  christos 	}
     51  1.1  christos 	free(p);
     52  1.1  christos }
     53  1.1  christos 
     54  1.1  christos static void
     55  1.1  christos mallocx_free(void) {
     56  1.1  christos 	void *p = mallocx(1, 0);
     57  1.1  christos 	if (p == NULL) {
     58  1.1  christos 		test_fail("Unexpected mallocx() failure");
     59  1.1  christos 		return;
     60  1.1  christos 	}
     61  1.1  christos 	free(p);
     62  1.1  christos }
     63  1.1  christos 
     64  1.1  christos TEST_BEGIN(test_malloc_vs_mallocx) {
     65  1.1  christos 	compare_funcs(10*1000*1000, 100*1000*1000, "malloc",
     66  1.1  christos 	    malloc_free, "mallocx", mallocx_free);
     67  1.1  christos }
     68  1.1  christos TEST_END
     69  1.1  christos 
     70  1.1  christos static void
     71  1.1  christos malloc_dallocx(void) {
     72  1.1  christos 	void *p = malloc(1);
     73  1.1  christos 	if (p == NULL) {
     74  1.1  christos 		test_fail("Unexpected malloc() failure");
     75  1.1  christos 		return;
     76  1.1  christos 	}
     77  1.1  christos 	dallocx(p, 0);
     78  1.1  christos }
     79  1.1  christos 
     80  1.1  christos static void
     81  1.1  christos malloc_sdallocx(void) {
     82  1.1  christos 	void *p = malloc(1);
     83  1.1  christos 	if (p == NULL) {
     84  1.1  christos 		test_fail("Unexpected malloc() failure");
     85  1.1  christos 		return;
     86  1.1  christos 	}
     87  1.1  christos 	sdallocx(p, 1, 0);
     88  1.1  christos }
     89  1.1  christos 
     90  1.1  christos TEST_BEGIN(test_free_vs_dallocx) {
     91  1.1  christos 	compare_funcs(10*1000*1000, 100*1000*1000, "free", malloc_free,
     92  1.1  christos 	    "dallocx", malloc_dallocx);
     93  1.1  christos }
     94  1.1  christos TEST_END
     95  1.1  christos 
     96  1.1  christos TEST_BEGIN(test_dallocx_vs_sdallocx) {
     97  1.1  christos 	compare_funcs(10*1000*1000, 100*1000*1000, "dallocx", malloc_dallocx,
     98  1.1  christos 	    "sdallocx", malloc_sdallocx);
     99  1.1  christos }
    100  1.1  christos TEST_END
    101  1.1  christos 
    102  1.1  christos static void
    103  1.1  christos malloc_mus_free(void) {
    104  1.1  christos 	void *p;
    105  1.1  christos 
    106  1.1  christos 	p = malloc(1);
    107  1.1  christos 	if (p == NULL) {
    108  1.1  christos 		test_fail("Unexpected malloc() failure");
    109  1.1  christos 		return;
    110  1.1  christos 	}
    111  1.1  christos 	malloc_usable_size(p);
    112  1.1  christos 	free(p);
    113  1.1  christos }
    114  1.1  christos 
    115  1.1  christos static void
    116  1.1  christos malloc_sallocx_free(void) {
    117  1.1  christos 	void *p;
    118  1.1  christos 
    119  1.1  christos 	p = malloc(1);
    120  1.1  christos 	if (p == NULL) {
    121  1.1  christos 		test_fail("Unexpected malloc() failure");
    122  1.1  christos 		return;
    123  1.1  christos 	}
    124  1.1  christos 	if (sallocx(p, 0) < 1) {
    125  1.1  christos 		test_fail("Unexpected sallocx() failure");
    126  1.1  christos 	}
    127  1.1  christos 	free(p);
    128  1.1  christos }
    129  1.1  christos 
    130  1.1  christos TEST_BEGIN(test_mus_vs_sallocx) {
    131  1.1  christos 	compare_funcs(10*1000*1000, 100*1000*1000, "malloc_usable_size",
    132  1.1  christos 	    malloc_mus_free, "sallocx", malloc_sallocx_free);
    133  1.1  christos }
    134  1.1  christos TEST_END
    135  1.1  christos 
    136  1.1  christos static void
    137  1.1  christos malloc_nallocx_free(void) {
    138  1.1  christos 	void *p;
    139  1.1  christos 
    140  1.1  christos 	p = malloc(1);
    141  1.1  christos 	if (p == NULL) {
    142  1.1  christos 		test_fail("Unexpected malloc() failure");
    143  1.1  christos 		return;
    144  1.1  christos 	}
    145  1.1  christos 	if (nallocx(1, 0) < 1) {
    146  1.1  christos 		test_fail("Unexpected nallocx() failure");
    147  1.1  christos 	}
    148  1.1  christos 	free(p);
    149  1.1  christos }
    150  1.1  christos 
    151  1.1  christos TEST_BEGIN(test_sallocx_vs_nallocx) {
    152  1.1  christos 	compare_funcs(10*1000*1000, 100*1000*1000, "sallocx",
    153  1.1  christos 	    malloc_sallocx_free, "nallocx", malloc_nallocx_free);
    154  1.1  christos }
    155  1.1  christos TEST_END
    156  1.1  christos 
    157  1.1  christos int
    158  1.1  christos main(void) {
    159  1.1  christos 	return test_no_reentrancy(
    160  1.1  christos 	    test_malloc_vs_mallocx,
    161  1.1  christos 	    test_free_vs_dallocx,
    162  1.1  christos 	    test_dallocx_vs_sdallocx,
    163  1.1  christos 	    test_mus_vs_sallocx,
    164  1.1  christos 	    test_sallocx_vs_nallocx);
    165  1.1  christos }
    166