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