Home | History | Annotate | Line # | Download | only in integration
allocated.c revision 1.1
      1  1.1  christos #include "test/jemalloc_test.h"
      2  1.1  christos 
      3  1.1  christos static const bool config_stats =
      4  1.1  christos #ifdef JEMALLOC_STATS
      5  1.1  christos     true
      6  1.1  christos #else
      7  1.1  christos     false
      8  1.1  christos #endif
      9  1.1  christos     ;
     10  1.1  christos 
     11  1.1  christos void *
     12  1.1  christos thd_start(void *arg) {
     13  1.1  christos 	int err;
     14  1.1  christos 	void *p;
     15  1.1  christos 	uint64_t a0, a1, d0, d1;
     16  1.1  christos 	uint64_t *ap0, *ap1, *dp0, *dp1;
     17  1.1  christos 	size_t sz, usize;
     18  1.1  christos 
     19  1.1  christos 	sz = sizeof(a0);
     20  1.1  christos 	if ((err = mallctl("thread.allocated", (void *)&a0, &sz, NULL, 0))) {
     21  1.1  christos 		if (err == ENOENT) {
     22  1.1  christos 			goto label_ENOENT;
     23  1.1  christos 		}
     24  1.1  christos 		test_fail("%s(): Error in mallctl(): %s", __func__,
     25  1.1  christos 		    strerror(err));
     26  1.1  christos 	}
     27  1.1  christos 	sz = sizeof(ap0);
     28  1.1  christos 	if ((err = mallctl("thread.allocatedp", (void *)&ap0, &sz, NULL, 0))) {
     29  1.1  christos 		if (err == ENOENT) {
     30  1.1  christos 			goto label_ENOENT;
     31  1.1  christos 		}
     32  1.1  christos 		test_fail("%s(): Error in mallctl(): %s", __func__,
     33  1.1  christos 		    strerror(err));
     34  1.1  christos 	}
     35  1.1  christos 	assert_u64_eq(*ap0, a0,
     36  1.1  christos 	    "\"thread.allocatedp\" should provide a pointer to internal "
     37  1.1  christos 	    "storage");
     38  1.1  christos 
     39  1.1  christos 	sz = sizeof(d0);
     40  1.1  christos 	if ((err = mallctl("thread.deallocated", (void *)&d0, &sz, NULL, 0))) {
     41  1.1  christos 		if (err == ENOENT) {
     42  1.1  christos 			goto label_ENOENT;
     43  1.1  christos 		}
     44  1.1  christos 		test_fail("%s(): Error in mallctl(): %s", __func__,
     45  1.1  christos 		    strerror(err));
     46  1.1  christos 	}
     47  1.1  christos 	sz = sizeof(dp0);
     48  1.1  christos 	if ((err = mallctl("thread.deallocatedp", (void *)&dp0, &sz, NULL,
     49  1.1  christos 	    0))) {
     50  1.1  christos 		if (err == ENOENT) {
     51  1.1  christos 			goto label_ENOENT;
     52  1.1  christos 		}
     53  1.1  christos 		test_fail("%s(): Error in mallctl(): %s", __func__,
     54  1.1  christos 		    strerror(err));
     55  1.1  christos 	}
     56  1.1  christos 	assert_u64_eq(*dp0, d0,
     57  1.1  christos 	    "\"thread.deallocatedp\" should provide a pointer to internal "
     58  1.1  christos 	    "storage");
     59  1.1  christos 
     60  1.1  christos 	p = malloc(1);
     61  1.1  christos 	assert_ptr_not_null(p, "Unexpected malloc() error");
     62  1.1  christos 
     63  1.1  christos 	sz = sizeof(a1);
     64  1.1  christos 	mallctl("thread.allocated", (void *)&a1, &sz, NULL, 0);
     65  1.1  christos 	sz = sizeof(ap1);
     66  1.1  christos 	mallctl("thread.allocatedp", (void *)&ap1, &sz, NULL, 0);
     67  1.1  christos 	assert_u64_eq(*ap1, a1,
     68  1.1  christos 	    "Dereferenced \"thread.allocatedp\" value should equal "
     69  1.1  christos 	    "\"thread.allocated\" value");
     70  1.1  christos 	assert_ptr_eq(ap0, ap1,
     71  1.1  christos 	    "Pointer returned by \"thread.allocatedp\" should not change");
     72  1.1  christos 
     73  1.1  christos 	usize = malloc_usable_size(p);
     74  1.1  christos 	assert_u64_le(a0 + usize, a1,
     75  1.1  christos 	    "Allocated memory counter should increase by at least the amount "
     76  1.1  christos 	    "explicitly allocated");
     77  1.1  christos 
     78  1.1  christos 	free(p);
     79  1.1  christos 
     80  1.1  christos 	sz = sizeof(d1);
     81  1.1  christos 	mallctl("thread.deallocated", (void *)&d1, &sz, NULL, 0);
     82  1.1  christos 	sz = sizeof(dp1);
     83  1.1  christos 	mallctl("thread.deallocatedp", (void *)&dp1, &sz, NULL, 0);
     84  1.1  christos 	assert_u64_eq(*dp1, d1,
     85  1.1  christos 	    "Dereferenced \"thread.deallocatedp\" value should equal "
     86  1.1  christos 	    "\"thread.deallocated\" value");
     87  1.1  christos 	assert_ptr_eq(dp0, dp1,
     88  1.1  christos 	    "Pointer returned by \"thread.deallocatedp\" should not change");
     89  1.1  christos 
     90  1.1  christos 	assert_u64_le(d0 + usize, d1,
     91  1.1  christos 	    "Deallocated memory counter should increase by at least the amount "
     92  1.1  christos 	    "explicitly deallocated");
     93  1.1  christos 
     94  1.1  christos 	return NULL;
     95  1.1  christos label_ENOENT:
     96  1.1  christos 	assert_false(config_stats,
     97  1.1  christos 	    "ENOENT should only be returned if stats are disabled");
     98  1.1  christos 	test_skip("\"thread.allocated\" mallctl not available");
     99  1.1  christos 	return NULL;
    100  1.1  christos }
    101  1.1  christos 
    102  1.1  christos TEST_BEGIN(test_main_thread) {
    103  1.1  christos 	thd_start(NULL);
    104  1.1  christos }
    105  1.1  christos TEST_END
    106  1.1  christos 
    107  1.1  christos TEST_BEGIN(test_subthread) {
    108  1.1  christos 	thd_t thd;
    109  1.1  christos 
    110  1.1  christos 	thd_create(&thd, thd_start, NULL);
    111  1.1  christos 	thd_join(thd, NULL);
    112  1.1  christos }
    113  1.1  christos TEST_END
    114  1.1  christos 
    115  1.1  christos int
    116  1.1  christos main(void) {
    117  1.1  christos 	/* Run tests multiple times to check for bad interactions. */
    118  1.1  christos 	return test(
    119  1.1  christos 	    test_main_thread,
    120  1.1  christos 	    test_subthread,
    121  1.1  christos 	    test_main_thread,
    122  1.1  christos 	    test_subthread,
    123  1.1  christos 	    test_main_thread);
    124  1.1  christos }
    125