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 *
thd_start(void * arg)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", &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", &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", &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", &dp0, &sz, NULL, 0))) {
47 		if (err == ENOENT)
48 			goto label_ENOENT;
49 		test_fail("%s(): Error in mallctl(): %s", __func__,
50 		    strerror(err));
51 	}
52 	assert_u64_eq(*dp0, d0,
53 	    "\"thread.deallocatedp\" should provide a pointer to internal "
54 	    "storage");
55 
56 	p = malloc(1);
57 	assert_ptr_not_null(p, "Unexpected malloc() error");
58 
59 	sz = sizeof(a1);
60 	mallctl("thread.allocated", &a1, &sz, NULL, 0);
61 	sz = sizeof(ap1);
62 	mallctl("thread.allocatedp", &ap1, &sz, NULL, 0);
63 	assert_u64_eq(*ap1, a1,
64 	    "Dereferenced \"thread.allocatedp\" value should equal "
65 	    "\"thread.allocated\" value");
66 	assert_ptr_eq(ap0, ap1,
67 	    "Pointer returned by \"thread.allocatedp\" should not change");
68 
69 	usize = malloc_usable_size(p);
70 	assert_u64_le(a0 + usize, a1,
71 	    "Allocated memory counter should increase by at least the amount "
72 	    "explicitly allocated");
73 
74 	free(p);
75 
76 	sz = sizeof(d1);
77 	mallctl("thread.deallocated", &d1, &sz, NULL, 0);
78 	sz = sizeof(dp1);
79 	mallctl("thread.deallocatedp", &dp1, &sz, NULL, 0);
80 	assert_u64_eq(*dp1, d1,
81 	    "Dereferenced \"thread.deallocatedp\" value should equal "
82 	    "\"thread.deallocated\" value");
83 	assert_ptr_eq(dp0, dp1,
84 	    "Pointer returned by \"thread.deallocatedp\" should not change");
85 
86 	assert_u64_le(d0 + usize, d1,
87 	    "Deallocated memory counter should increase by at least the amount "
88 	    "explicitly deallocated");
89 
90 	return (NULL);
91 label_ENOENT:
92 	assert_false(config_stats,
93 	    "ENOENT should only be returned if stats are disabled");
94 	test_skip("\"thread.allocated\" mallctl not available");
95 	return (NULL);
96 }
97 
TEST_BEGIN(test_main_thread)98 TEST_BEGIN(test_main_thread)
99 {
100 
101 	thd_start(NULL);
102 }
103 TEST_END
104 
TEST_BEGIN(test_subthread)105 TEST_BEGIN(test_subthread)
106 {
107 	thd_t thd;
108 
109 	thd_create(&thd, thd_start, NULL);
110 	thd_join(thd, NULL);
111 }
112 TEST_END
113 
114 int
main(void)115 main(void)
116 {
117 
118 	/* Run tests multiple times to check for bad interactions. */
119 	return (test(
120 	    test_main_thread,
121 	    test_subthread,
122 	    test_main_thread,
123 	    test_subthread,
124 	    test_main_thread));
125 }
126