xref: /aosp_15_r20/external/jemalloc_new/test/unit/stats.c (revision 1208bc7e437ced7eb82efac44ba17e3beba411da)
1*1208bc7eSAndroid Build Coastguard Worker #include "test/jemalloc_test.h"
2*1208bc7eSAndroid Build Coastguard Worker 
TEST_BEGIN(test_stats_summary)3*1208bc7eSAndroid Build Coastguard Worker TEST_BEGIN(test_stats_summary) {
4*1208bc7eSAndroid Build Coastguard Worker 	size_t sz, allocated, active, resident, mapped;
5*1208bc7eSAndroid Build Coastguard Worker 	int expected = config_stats ? 0 : ENOENT;
6*1208bc7eSAndroid Build Coastguard Worker 
7*1208bc7eSAndroid Build Coastguard Worker 	sz = sizeof(size_t);
8*1208bc7eSAndroid Build Coastguard Worker 	assert_d_eq(mallctl("stats.allocated", (void *)&allocated, &sz, NULL,
9*1208bc7eSAndroid Build Coastguard Worker 	    0), expected, "Unexpected mallctl() result");
10*1208bc7eSAndroid Build Coastguard Worker 	assert_d_eq(mallctl("stats.active", (void *)&active, &sz, NULL, 0),
11*1208bc7eSAndroid Build Coastguard Worker 	    expected, "Unexpected mallctl() result");
12*1208bc7eSAndroid Build Coastguard Worker 	assert_d_eq(mallctl("stats.resident", (void *)&resident, &sz, NULL, 0),
13*1208bc7eSAndroid Build Coastguard Worker 	    expected, "Unexpected mallctl() result");
14*1208bc7eSAndroid Build Coastguard Worker 	assert_d_eq(mallctl("stats.mapped", (void *)&mapped, &sz, NULL, 0),
15*1208bc7eSAndroid Build Coastguard Worker 	    expected, "Unexpected mallctl() result");
16*1208bc7eSAndroid Build Coastguard Worker 
17*1208bc7eSAndroid Build Coastguard Worker 	if (config_stats) {
18*1208bc7eSAndroid Build Coastguard Worker 		assert_zu_le(allocated, active,
19*1208bc7eSAndroid Build Coastguard Worker 		    "allocated should be no larger than active");
20*1208bc7eSAndroid Build Coastguard Worker 		assert_zu_lt(active, resident,
21*1208bc7eSAndroid Build Coastguard Worker 		    "active should be less than resident");
22*1208bc7eSAndroid Build Coastguard Worker 		assert_zu_lt(active, mapped,
23*1208bc7eSAndroid Build Coastguard Worker 		    "active should be less than mapped");
24*1208bc7eSAndroid Build Coastguard Worker 	}
25*1208bc7eSAndroid Build Coastguard Worker }
26*1208bc7eSAndroid Build Coastguard Worker TEST_END
27*1208bc7eSAndroid Build Coastguard Worker 
TEST_BEGIN(test_stats_large)28*1208bc7eSAndroid Build Coastguard Worker TEST_BEGIN(test_stats_large) {
29*1208bc7eSAndroid Build Coastguard Worker 	void *p;
30*1208bc7eSAndroid Build Coastguard Worker 	uint64_t epoch;
31*1208bc7eSAndroid Build Coastguard Worker 	size_t allocated;
32*1208bc7eSAndroid Build Coastguard Worker 	uint64_t nmalloc, ndalloc, nrequests;
33*1208bc7eSAndroid Build Coastguard Worker 	size_t sz;
34*1208bc7eSAndroid Build Coastguard Worker 	int expected = config_stats ? 0 : ENOENT;
35*1208bc7eSAndroid Build Coastguard Worker 
36*1208bc7eSAndroid Build Coastguard Worker 	p = mallocx(SMALL_MAXCLASS+1, MALLOCX_ARENA(0));
37*1208bc7eSAndroid Build Coastguard Worker 	assert_ptr_not_null(p, "Unexpected mallocx() failure");
38*1208bc7eSAndroid Build Coastguard Worker 
39*1208bc7eSAndroid Build Coastguard Worker 	assert_d_eq(mallctl("epoch", NULL, NULL, (void *)&epoch, sizeof(epoch)),
40*1208bc7eSAndroid Build Coastguard Worker 	    0, "Unexpected mallctl() failure");
41*1208bc7eSAndroid Build Coastguard Worker 
42*1208bc7eSAndroid Build Coastguard Worker 	sz = sizeof(size_t);
43*1208bc7eSAndroid Build Coastguard Worker 	assert_d_eq(mallctl("stats.arenas.0.large.allocated",
44*1208bc7eSAndroid Build Coastguard Worker 	    (void *)&allocated, &sz, NULL, 0), expected,
45*1208bc7eSAndroid Build Coastguard Worker 	    "Unexpected mallctl() result");
46*1208bc7eSAndroid Build Coastguard Worker 	sz = sizeof(uint64_t);
47*1208bc7eSAndroid Build Coastguard Worker 	assert_d_eq(mallctl("stats.arenas.0.large.nmalloc", (void *)&nmalloc,
48*1208bc7eSAndroid Build Coastguard Worker 	    &sz, NULL, 0), expected, "Unexpected mallctl() result");
49*1208bc7eSAndroid Build Coastguard Worker 	assert_d_eq(mallctl("stats.arenas.0.large.ndalloc", (void *)&ndalloc,
50*1208bc7eSAndroid Build Coastguard Worker 	    &sz, NULL, 0), expected, "Unexpected mallctl() result");
51*1208bc7eSAndroid Build Coastguard Worker 	assert_d_eq(mallctl("stats.arenas.0.large.nrequests",
52*1208bc7eSAndroid Build Coastguard Worker 	    (void *)&nrequests, &sz, NULL, 0), expected,
53*1208bc7eSAndroid Build Coastguard Worker 	    "Unexpected mallctl() result");
54*1208bc7eSAndroid Build Coastguard Worker 
55*1208bc7eSAndroid Build Coastguard Worker 	if (config_stats) {
56*1208bc7eSAndroid Build Coastguard Worker 		assert_zu_gt(allocated, 0,
57*1208bc7eSAndroid Build Coastguard Worker 		    "allocated should be greater than zero");
58*1208bc7eSAndroid Build Coastguard Worker 		assert_u64_ge(nmalloc, ndalloc,
59*1208bc7eSAndroid Build Coastguard Worker 		    "nmalloc should be at least as large as ndalloc");
60*1208bc7eSAndroid Build Coastguard Worker 		assert_u64_le(nmalloc, nrequests,
61*1208bc7eSAndroid Build Coastguard Worker 		    "nmalloc should no larger than nrequests");
62*1208bc7eSAndroid Build Coastguard Worker 	}
63*1208bc7eSAndroid Build Coastguard Worker 
64*1208bc7eSAndroid Build Coastguard Worker 	dallocx(p, 0);
65*1208bc7eSAndroid Build Coastguard Worker }
66*1208bc7eSAndroid Build Coastguard Worker TEST_END
67*1208bc7eSAndroid Build Coastguard Worker 
TEST_BEGIN(test_stats_arenas_summary)68*1208bc7eSAndroid Build Coastguard Worker TEST_BEGIN(test_stats_arenas_summary) {
69*1208bc7eSAndroid Build Coastguard Worker 	void *little, *large;
70*1208bc7eSAndroid Build Coastguard Worker 	uint64_t epoch;
71*1208bc7eSAndroid Build Coastguard Worker 	size_t sz;
72*1208bc7eSAndroid Build Coastguard Worker 	int expected = config_stats ? 0 : ENOENT;
73*1208bc7eSAndroid Build Coastguard Worker 	size_t mapped;
74*1208bc7eSAndroid Build Coastguard Worker 	uint64_t dirty_npurge, dirty_nmadvise, dirty_purged;
75*1208bc7eSAndroid Build Coastguard Worker 	uint64_t muzzy_npurge, muzzy_nmadvise, muzzy_purged;
76*1208bc7eSAndroid Build Coastguard Worker 
77*1208bc7eSAndroid Build Coastguard Worker 	little = mallocx(SMALL_MAXCLASS, MALLOCX_ARENA(0));
78*1208bc7eSAndroid Build Coastguard Worker 	assert_ptr_not_null(little, "Unexpected mallocx() failure");
79*1208bc7eSAndroid Build Coastguard Worker 	large = mallocx((1U << LG_LARGE_MINCLASS), MALLOCX_ARENA(0));
80*1208bc7eSAndroid Build Coastguard Worker 	assert_ptr_not_null(large, "Unexpected mallocx() failure");
81*1208bc7eSAndroid Build Coastguard Worker 
82*1208bc7eSAndroid Build Coastguard Worker 	dallocx(little, 0);
83*1208bc7eSAndroid Build Coastguard Worker 	dallocx(large, 0);
84*1208bc7eSAndroid Build Coastguard Worker 
85*1208bc7eSAndroid Build Coastguard Worker 	assert_d_eq(mallctl("thread.tcache.flush", NULL, NULL, NULL, 0),
86*1208bc7eSAndroid Build Coastguard Worker 	    opt_tcache ? 0 : EFAULT, "Unexpected mallctl() result");
87*1208bc7eSAndroid Build Coastguard Worker 	assert_d_eq(mallctl("arena.0.purge", NULL, NULL, NULL, 0), 0,
88*1208bc7eSAndroid Build Coastguard Worker 	    "Unexpected mallctl() failure");
89*1208bc7eSAndroid Build Coastguard Worker 
90*1208bc7eSAndroid Build Coastguard Worker 	assert_d_eq(mallctl("epoch", NULL, NULL, (void *)&epoch, sizeof(epoch)),
91*1208bc7eSAndroid Build Coastguard Worker 	    0, "Unexpected mallctl() failure");
92*1208bc7eSAndroid Build Coastguard Worker 
93*1208bc7eSAndroid Build Coastguard Worker 	sz = sizeof(size_t);
94*1208bc7eSAndroid Build Coastguard Worker 	assert_d_eq(mallctl("stats.arenas.0.mapped", (void *)&mapped, &sz, NULL,
95*1208bc7eSAndroid Build Coastguard Worker 	    0), expected, "Unexepected mallctl() result");
96*1208bc7eSAndroid Build Coastguard Worker 
97*1208bc7eSAndroid Build Coastguard Worker 	sz = sizeof(uint64_t);
98*1208bc7eSAndroid Build Coastguard Worker 	assert_d_eq(mallctl("stats.arenas.0.dirty_npurge",
99*1208bc7eSAndroid Build Coastguard Worker 	    (void *)&dirty_npurge, &sz, NULL, 0), expected,
100*1208bc7eSAndroid Build Coastguard Worker 	    "Unexepected mallctl() result");
101*1208bc7eSAndroid Build Coastguard Worker 	assert_d_eq(mallctl("stats.arenas.0.dirty_nmadvise",
102*1208bc7eSAndroid Build Coastguard Worker 	    (void *)&dirty_nmadvise, &sz, NULL, 0), expected,
103*1208bc7eSAndroid Build Coastguard Worker 	    "Unexepected mallctl() result");
104*1208bc7eSAndroid Build Coastguard Worker 	assert_d_eq(mallctl("stats.arenas.0.dirty_purged",
105*1208bc7eSAndroid Build Coastguard Worker 	    (void *)&dirty_purged, &sz, NULL, 0), expected,
106*1208bc7eSAndroid Build Coastguard Worker 	    "Unexepected mallctl() result");
107*1208bc7eSAndroid Build Coastguard Worker 	assert_d_eq(mallctl("stats.arenas.0.muzzy_npurge",
108*1208bc7eSAndroid Build Coastguard Worker 	    (void *)&muzzy_npurge, &sz, NULL, 0), expected,
109*1208bc7eSAndroid Build Coastguard Worker 	    "Unexepected mallctl() result");
110*1208bc7eSAndroid Build Coastguard Worker 	assert_d_eq(mallctl("stats.arenas.0.muzzy_nmadvise",
111*1208bc7eSAndroid Build Coastguard Worker 	    (void *)&muzzy_nmadvise, &sz, NULL, 0), expected,
112*1208bc7eSAndroid Build Coastguard Worker 	    "Unexepected mallctl() result");
113*1208bc7eSAndroid Build Coastguard Worker 	assert_d_eq(mallctl("stats.arenas.0.muzzy_purged",
114*1208bc7eSAndroid Build Coastguard Worker 	    (void *)&muzzy_purged, &sz, NULL, 0), expected,
115*1208bc7eSAndroid Build Coastguard Worker 	    "Unexepected mallctl() result");
116*1208bc7eSAndroid Build Coastguard Worker 
117*1208bc7eSAndroid Build Coastguard Worker 	if (config_stats) {
118*1208bc7eSAndroid Build Coastguard Worker 		if (!background_thread_enabled()) {
119*1208bc7eSAndroid Build Coastguard Worker 			assert_u64_gt(dirty_npurge + muzzy_npurge, 0,
120*1208bc7eSAndroid Build Coastguard Worker 			    "At least one purge should have occurred");
121*1208bc7eSAndroid Build Coastguard Worker 		}
122*1208bc7eSAndroid Build Coastguard Worker 		assert_u64_le(dirty_nmadvise, dirty_purged,
123*1208bc7eSAndroid Build Coastguard Worker 		    "dirty_nmadvise should be no greater than dirty_purged");
124*1208bc7eSAndroid Build Coastguard Worker 		assert_u64_le(muzzy_nmadvise, muzzy_purged,
125*1208bc7eSAndroid Build Coastguard Worker 		    "muzzy_nmadvise should be no greater than muzzy_purged");
126*1208bc7eSAndroid Build Coastguard Worker 	}
127*1208bc7eSAndroid Build Coastguard Worker }
128*1208bc7eSAndroid Build Coastguard Worker TEST_END
129*1208bc7eSAndroid Build Coastguard Worker 
130*1208bc7eSAndroid Build Coastguard Worker void *
thd_start(void * arg)131*1208bc7eSAndroid Build Coastguard Worker thd_start(void *arg) {
132*1208bc7eSAndroid Build Coastguard Worker 	return NULL;
133*1208bc7eSAndroid Build Coastguard Worker }
134*1208bc7eSAndroid Build Coastguard Worker 
135*1208bc7eSAndroid Build Coastguard Worker static void
no_lazy_lock(void)136*1208bc7eSAndroid Build Coastguard Worker no_lazy_lock(void) {
137*1208bc7eSAndroid Build Coastguard Worker 	thd_t thd;
138*1208bc7eSAndroid Build Coastguard Worker 
139*1208bc7eSAndroid Build Coastguard Worker 	thd_create(&thd, thd_start, NULL);
140*1208bc7eSAndroid Build Coastguard Worker 	thd_join(thd, NULL);
141*1208bc7eSAndroid Build Coastguard Worker }
142*1208bc7eSAndroid Build Coastguard Worker 
TEST_BEGIN(test_stats_arenas_small)143*1208bc7eSAndroid Build Coastguard Worker TEST_BEGIN(test_stats_arenas_small) {
144*1208bc7eSAndroid Build Coastguard Worker 	void *p;
145*1208bc7eSAndroid Build Coastguard Worker 	size_t sz, allocated;
146*1208bc7eSAndroid Build Coastguard Worker 	uint64_t epoch, nmalloc, ndalloc, nrequests;
147*1208bc7eSAndroid Build Coastguard Worker 	int expected = config_stats ? 0 : ENOENT;
148*1208bc7eSAndroid Build Coastguard Worker 
149*1208bc7eSAndroid Build Coastguard Worker 	no_lazy_lock(); /* Lazy locking would dodge tcache testing. */
150*1208bc7eSAndroid Build Coastguard Worker 
151*1208bc7eSAndroid Build Coastguard Worker 	p = mallocx(SMALL_MAXCLASS, MALLOCX_ARENA(0));
152*1208bc7eSAndroid Build Coastguard Worker 	assert_ptr_not_null(p, "Unexpected mallocx() failure");
153*1208bc7eSAndroid Build Coastguard Worker 
154*1208bc7eSAndroid Build Coastguard Worker 	assert_d_eq(mallctl("thread.tcache.flush", NULL, NULL, NULL, 0),
155*1208bc7eSAndroid Build Coastguard Worker 	    opt_tcache ? 0 : EFAULT, "Unexpected mallctl() result");
156*1208bc7eSAndroid Build Coastguard Worker 
157*1208bc7eSAndroid Build Coastguard Worker 	assert_d_eq(mallctl("epoch", NULL, NULL, (void *)&epoch, sizeof(epoch)),
158*1208bc7eSAndroid Build Coastguard Worker 	    0, "Unexpected mallctl() failure");
159*1208bc7eSAndroid Build Coastguard Worker 
160*1208bc7eSAndroid Build Coastguard Worker 	sz = sizeof(size_t);
161*1208bc7eSAndroid Build Coastguard Worker 	assert_d_eq(mallctl("stats.arenas.0.small.allocated",
162*1208bc7eSAndroid Build Coastguard Worker 	    (void *)&allocated, &sz, NULL, 0), expected,
163*1208bc7eSAndroid Build Coastguard Worker 	    "Unexpected mallctl() result");
164*1208bc7eSAndroid Build Coastguard Worker 	sz = sizeof(uint64_t);
165*1208bc7eSAndroid Build Coastguard Worker 	assert_d_eq(mallctl("stats.arenas.0.small.nmalloc", (void *)&nmalloc,
166*1208bc7eSAndroid Build Coastguard Worker 	    &sz, NULL, 0), expected, "Unexpected mallctl() result");
167*1208bc7eSAndroid Build Coastguard Worker 	assert_d_eq(mallctl("stats.arenas.0.small.ndalloc", (void *)&ndalloc,
168*1208bc7eSAndroid Build Coastguard Worker 	    &sz, NULL, 0), expected, "Unexpected mallctl() result");
169*1208bc7eSAndroid Build Coastguard Worker 	assert_d_eq(mallctl("stats.arenas.0.small.nrequests",
170*1208bc7eSAndroid Build Coastguard Worker 	    (void *)&nrequests, &sz, NULL, 0), expected,
171*1208bc7eSAndroid Build Coastguard Worker 	    "Unexpected mallctl() result");
172*1208bc7eSAndroid Build Coastguard Worker 
173*1208bc7eSAndroid Build Coastguard Worker 	if (config_stats) {
174*1208bc7eSAndroid Build Coastguard Worker 		assert_zu_gt(allocated, 0,
175*1208bc7eSAndroid Build Coastguard Worker 		    "allocated should be greater than zero");
176*1208bc7eSAndroid Build Coastguard Worker 		assert_u64_gt(nmalloc, 0,
177*1208bc7eSAndroid Build Coastguard Worker 		    "nmalloc should be no greater than zero");
178*1208bc7eSAndroid Build Coastguard Worker 		assert_u64_ge(nmalloc, ndalloc,
179*1208bc7eSAndroid Build Coastguard Worker 		    "nmalloc should be at least as large as ndalloc");
180*1208bc7eSAndroid Build Coastguard Worker 		assert_u64_gt(nrequests, 0,
181*1208bc7eSAndroid Build Coastguard Worker 		    "nrequests should be greater than zero");
182*1208bc7eSAndroid Build Coastguard Worker 	}
183*1208bc7eSAndroid Build Coastguard Worker 
184*1208bc7eSAndroid Build Coastguard Worker 	dallocx(p, 0);
185*1208bc7eSAndroid Build Coastguard Worker }
186*1208bc7eSAndroid Build Coastguard Worker TEST_END
187*1208bc7eSAndroid Build Coastguard Worker 
TEST_BEGIN(test_stats_arenas_large)188*1208bc7eSAndroid Build Coastguard Worker TEST_BEGIN(test_stats_arenas_large) {
189*1208bc7eSAndroid Build Coastguard Worker 	void *p;
190*1208bc7eSAndroid Build Coastguard Worker 	size_t sz, allocated;
191*1208bc7eSAndroid Build Coastguard Worker 	uint64_t epoch, nmalloc, ndalloc;
192*1208bc7eSAndroid Build Coastguard Worker 	int expected = config_stats ? 0 : ENOENT;
193*1208bc7eSAndroid Build Coastguard Worker 
194*1208bc7eSAndroid Build Coastguard Worker 	p = mallocx((1U << LG_LARGE_MINCLASS), MALLOCX_ARENA(0));
195*1208bc7eSAndroid Build Coastguard Worker 	assert_ptr_not_null(p, "Unexpected mallocx() failure");
196*1208bc7eSAndroid Build Coastguard Worker 
197*1208bc7eSAndroid Build Coastguard Worker 	assert_d_eq(mallctl("epoch", NULL, NULL, (void *)&epoch, sizeof(epoch)),
198*1208bc7eSAndroid Build Coastguard Worker 	    0, "Unexpected mallctl() failure");
199*1208bc7eSAndroid Build Coastguard Worker 
200*1208bc7eSAndroid Build Coastguard Worker 	sz = sizeof(size_t);
201*1208bc7eSAndroid Build Coastguard Worker 	assert_d_eq(mallctl("stats.arenas.0.large.allocated",
202*1208bc7eSAndroid Build Coastguard Worker 	    (void *)&allocated, &sz, NULL, 0), expected,
203*1208bc7eSAndroid Build Coastguard Worker 	    "Unexpected mallctl() result");
204*1208bc7eSAndroid Build Coastguard Worker 	sz = sizeof(uint64_t);
205*1208bc7eSAndroid Build Coastguard Worker 	assert_d_eq(mallctl("stats.arenas.0.large.nmalloc", (void *)&nmalloc,
206*1208bc7eSAndroid Build Coastguard Worker 	    &sz, NULL, 0), expected, "Unexpected mallctl() result");
207*1208bc7eSAndroid Build Coastguard Worker 	assert_d_eq(mallctl("stats.arenas.0.large.ndalloc", (void *)&ndalloc,
208*1208bc7eSAndroid Build Coastguard Worker 	    &sz, NULL, 0), expected, "Unexpected mallctl() result");
209*1208bc7eSAndroid Build Coastguard Worker 
210*1208bc7eSAndroid Build Coastguard Worker 	if (config_stats) {
211*1208bc7eSAndroid Build Coastguard Worker 		assert_zu_gt(allocated, 0,
212*1208bc7eSAndroid Build Coastguard Worker 		    "allocated should be greater than zero");
213*1208bc7eSAndroid Build Coastguard Worker 		assert_u64_gt(nmalloc, 0,
214*1208bc7eSAndroid Build Coastguard Worker 		    "nmalloc should be greater than zero");
215*1208bc7eSAndroid Build Coastguard Worker 		assert_u64_ge(nmalloc, ndalloc,
216*1208bc7eSAndroid Build Coastguard Worker 		    "nmalloc should be at least as large as ndalloc");
217*1208bc7eSAndroid Build Coastguard Worker 	}
218*1208bc7eSAndroid Build Coastguard Worker 
219*1208bc7eSAndroid Build Coastguard Worker 	dallocx(p, 0);
220*1208bc7eSAndroid Build Coastguard Worker }
221*1208bc7eSAndroid Build Coastguard Worker TEST_END
222*1208bc7eSAndroid Build Coastguard Worker 
223*1208bc7eSAndroid Build Coastguard Worker static void
gen_mallctl_str(char * cmd,char * name,unsigned arena_ind)224*1208bc7eSAndroid Build Coastguard Worker gen_mallctl_str(char *cmd, char *name, unsigned arena_ind) {
225*1208bc7eSAndroid Build Coastguard Worker 	sprintf(cmd, "stats.arenas.%u.bins.0.%s", arena_ind, name);
226*1208bc7eSAndroid Build Coastguard Worker }
227*1208bc7eSAndroid Build Coastguard Worker 
TEST_BEGIN(test_stats_arenas_bins)228*1208bc7eSAndroid Build Coastguard Worker TEST_BEGIN(test_stats_arenas_bins) {
229*1208bc7eSAndroid Build Coastguard Worker 	test_skip_if(known_failure_on_android);
230*1208bc7eSAndroid Build Coastguard Worker 	void *p;
231*1208bc7eSAndroid Build Coastguard Worker 	size_t sz, curslabs, curregs;
232*1208bc7eSAndroid Build Coastguard Worker 	uint64_t epoch, nmalloc, ndalloc, nrequests, nfills, nflushes;
233*1208bc7eSAndroid Build Coastguard Worker 	uint64_t nslabs, nreslabs;
234*1208bc7eSAndroid Build Coastguard Worker 	int expected = config_stats ? 0 : ENOENT;
235*1208bc7eSAndroid Build Coastguard Worker 
236*1208bc7eSAndroid Build Coastguard Worker 	/* Make sure allocation below isn't satisfied by tcache. */
237*1208bc7eSAndroid Build Coastguard Worker 	assert_d_eq(mallctl("thread.tcache.flush", NULL, NULL, NULL, 0),
238*1208bc7eSAndroid Build Coastguard Worker 	    opt_tcache ? 0 : EFAULT, "Unexpected mallctl() result");
239*1208bc7eSAndroid Build Coastguard Worker 
240*1208bc7eSAndroid Build Coastguard Worker 	unsigned arena_ind, old_arena_ind;
241*1208bc7eSAndroid Build Coastguard Worker 	sz = sizeof(unsigned);
242*1208bc7eSAndroid Build Coastguard Worker 	assert_d_eq(mallctl("arenas.create", (void *)&arena_ind, &sz, NULL, 0),
243*1208bc7eSAndroid Build Coastguard Worker 	    0, "Arena creation failure");
244*1208bc7eSAndroid Build Coastguard Worker 	sz = sizeof(arena_ind);
245*1208bc7eSAndroid Build Coastguard Worker 	assert_d_eq(mallctl("thread.arena", (void *)&old_arena_ind, &sz,
246*1208bc7eSAndroid Build Coastguard Worker 	    (void *)&arena_ind, sizeof(arena_ind)), 0,
247*1208bc7eSAndroid Build Coastguard Worker 	    "Unexpected mallctl() failure");
248*1208bc7eSAndroid Build Coastguard Worker 
249*1208bc7eSAndroid Build Coastguard Worker 	p = malloc(bin_infos[0].reg_size);
250*1208bc7eSAndroid Build Coastguard Worker 	assert_ptr_not_null(p, "Unexpected malloc() failure");
251*1208bc7eSAndroid Build Coastguard Worker 
252*1208bc7eSAndroid Build Coastguard Worker 	assert_d_eq(mallctl("thread.tcache.flush", NULL, NULL, NULL, 0),
253*1208bc7eSAndroid Build Coastguard Worker 	    opt_tcache ? 0 : EFAULT, "Unexpected mallctl() result");
254*1208bc7eSAndroid Build Coastguard Worker 
255*1208bc7eSAndroid Build Coastguard Worker 	assert_d_eq(mallctl("epoch", NULL, NULL, (void *)&epoch, sizeof(epoch)),
256*1208bc7eSAndroid Build Coastguard Worker 	    0, "Unexpected mallctl() failure");
257*1208bc7eSAndroid Build Coastguard Worker 
258*1208bc7eSAndroid Build Coastguard Worker 	char cmd[128];
259*1208bc7eSAndroid Build Coastguard Worker 	sz = sizeof(uint64_t);
260*1208bc7eSAndroid Build Coastguard Worker 	gen_mallctl_str(cmd, "nmalloc", arena_ind);
261*1208bc7eSAndroid Build Coastguard Worker 	assert_d_eq(mallctl(cmd, (void *)&nmalloc, &sz, NULL, 0), expected,
262*1208bc7eSAndroid Build Coastguard Worker 	    "Unexpected mallctl() result");
263*1208bc7eSAndroid Build Coastguard Worker 	gen_mallctl_str(cmd, "ndalloc", arena_ind);
264*1208bc7eSAndroid Build Coastguard Worker 	assert_d_eq(mallctl(cmd, (void *)&ndalloc, &sz, NULL, 0), expected,
265*1208bc7eSAndroid Build Coastguard Worker 	    "Unexpected mallctl() result");
266*1208bc7eSAndroid Build Coastguard Worker 	gen_mallctl_str(cmd, "nrequests", arena_ind);
267*1208bc7eSAndroid Build Coastguard Worker 	assert_d_eq(mallctl(cmd, (void *)&nrequests, &sz, NULL, 0), expected,
268*1208bc7eSAndroid Build Coastguard Worker 	    "Unexpected mallctl() result");
269*1208bc7eSAndroid Build Coastguard Worker 	sz = sizeof(size_t);
270*1208bc7eSAndroid Build Coastguard Worker 	gen_mallctl_str(cmd, "curregs", arena_ind);
271*1208bc7eSAndroid Build Coastguard Worker 	assert_d_eq(mallctl(cmd, (void *)&curregs, &sz, NULL, 0), expected,
272*1208bc7eSAndroid Build Coastguard Worker 	    "Unexpected mallctl() result");
273*1208bc7eSAndroid Build Coastguard Worker 
274*1208bc7eSAndroid Build Coastguard Worker 	sz = sizeof(uint64_t);
275*1208bc7eSAndroid Build Coastguard Worker 	gen_mallctl_str(cmd, "nfills", arena_ind);
276*1208bc7eSAndroid Build Coastguard Worker 	assert_d_eq(mallctl(cmd, (void *)&nfills, &sz, NULL, 0), expected,
277*1208bc7eSAndroid Build Coastguard Worker 	    "Unexpected mallctl() result");
278*1208bc7eSAndroid Build Coastguard Worker 	gen_mallctl_str(cmd, "nflushes", arena_ind);
279*1208bc7eSAndroid Build Coastguard Worker 	assert_d_eq(mallctl(cmd, (void *)&nflushes, &sz, NULL, 0), expected,
280*1208bc7eSAndroid Build Coastguard Worker 	    "Unexpected mallctl() result");
281*1208bc7eSAndroid Build Coastguard Worker 
282*1208bc7eSAndroid Build Coastguard Worker 	gen_mallctl_str(cmd, "nslabs", arena_ind);
283*1208bc7eSAndroid Build Coastguard Worker 	assert_d_eq(mallctl(cmd, (void *)&nslabs, &sz, NULL, 0), expected,
284*1208bc7eSAndroid Build Coastguard Worker 	    "Unexpected mallctl() result");
285*1208bc7eSAndroid Build Coastguard Worker 	gen_mallctl_str(cmd, "nreslabs", arena_ind);
286*1208bc7eSAndroid Build Coastguard Worker 	assert_d_eq(mallctl(cmd, (void *)&nreslabs, &sz, NULL, 0), expected,
287*1208bc7eSAndroid Build Coastguard Worker 	    "Unexpected mallctl() result");
288*1208bc7eSAndroid Build Coastguard Worker 	sz = sizeof(size_t);
289*1208bc7eSAndroid Build Coastguard Worker 	gen_mallctl_str(cmd, "curslabs", arena_ind);
290*1208bc7eSAndroid Build Coastguard Worker 	assert_d_eq(mallctl(cmd, (void *)&curslabs, &sz, NULL, 0), expected,
291*1208bc7eSAndroid Build Coastguard Worker 	    "Unexpected mallctl() result");
292*1208bc7eSAndroid Build Coastguard Worker 
293*1208bc7eSAndroid Build Coastguard Worker 	if (config_stats) {
294*1208bc7eSAndroid Build Coastguard Worker 		assert_u64_gt(nmalloc, 0,
295*1208bc7eSAndroid Build Coastguard Worker 		    "nmalloc should be greater than zero");
296*1208bc7eSAndroid Build Coastguard Worker 		assert_u64_ge(nmalloc, ndalloc,
297*1208bc7eSAndroid Build Coastguard Worker 		    "nmalloc should be at least as large as ndalloc");
298*1208bc7eSAndroid Build Coastguard Worker 		assert_u64_gt(nrequests, 0,
299*1208bc7eSAndroid Build Coastguard Worker 		    "nrequests should be greater than zero");
300*1208bc7eSAndroid Build Coastguard Worker 		assert_zu_gt(curregs, 0,
301*1208bc7eSAndroid Build Coastguard Worker 		    "allocated should be greater than zero");
302*1208bc7eSAndroid Build Coastguard Worker 		if (opt_tcache) {
303*1208bc7eSAndroid Build Coastguard Worker 			assert_u64_gt(nfills, 0,
304*1208bc7eSAndroid Build Coastguard Worker 			    "At least one fill should have occurred");
305*1208bc7eSAndroid Build Coastguard Worker 			assert_u64_gt(nflushes, 0,
306*1208bc7eSAndroid Build Coastguard Worker 			    "At least one flush should have occurred");
307*1208bc7eSAndroid Build Coastguard Worker 		}
308*1208bc7eSAndroid Build Coastguard Worker 		assert_u64_gt(nslabs, 0,
309*1208bc7eSAndroid Build Coastguard Worker 		    "At least one slab should have been allocated");
310*1208bc7eSAndroid Build Coastguard Worker 		assert_zu_gt(curslabs, 0,
311*1208bc7eSAndroid Build Coastguard Worker 		    "At least one slab should be currently allocated");
312*1208bc7eSAndroid Build Coastguard Worker 	}
313*1208bc7eSAndroid Build Coastguard Worker 
314*1208bc7eSAndroid Build Coastguard Worker 	dallocx(p, 0);
315*1208bc7eSAndroid Build Coastguard Worker }
316*1208bc7eSAndroid Build Coastguard Worker TEST_END
317*1208bc7eSAndroid Build Coastguard Worker 
TEST_BEGIN(test_stats_arenas_lextents)318*1208bc7eSAndroid Build Coastguard Worker TEST_BEGIN(test_stats_arenas_lextents) {
319*1208bc7eSAndroid Build Coastguard Worker 	void *p;
320*1208bc7eSAndroid Build Coastguard Worker 	uint64_t epoch, nmalloc, ndalloc;
321*1208bc7eSAndroid Build Coastguard Worker 	size_t curlextents, sz, hsize;
322*1208bc7eSAndroid Build Coastguard Worker 	int expected = config_stats ? 0 : ENOENT;
323*1208bc7eSAndroid Build Coastguard Worker 
324*1208bc7eSAndroid Build Coastguard Worker 	sz = sizeof(size_t);
325*1208bc7eSAndroid Build Coastguard Worker 	assert_d_eq(mallctl("arenas.lextent.0.size", (void *)&hsize, &sz, NULL,
326*1208bc7eSAndroid Build Coastguard Worker 	    0), 0, "Unexpected mallctl() failure");
327*1208bc7eSAndroid Build Coastguard Worker 
328*1208bc7eSAndroid Build Coastguard Worker 	p = mallocx(hsize, MALLOCX_ARENA(0));
329*1208bc7eSAndroid Build Coastguard Worker 	assert_ptr_not_null(p, "Unexpected mallocx() failure");
330*1208bc7eSAndroid Build Coastguard Worker 
331*1208bc7eSAndroid Build Coastguard Worker 	assert_d_eq(mallctl("epoch", NULL, NULL, (void *)&epoch, sizeof(epoch)),
332*1208bc7eSAndroid Build Coastguard Worker 	    0, "Unexpected mallctl() failure");
333*1208bc7eSAndroid Build Coastguard Worker 
334*1208bc7eSAndroid Build Coastguard Worker 	sz = sizeof(uint64_t);
335*1208bc7eSAndroid Build Coastguard Worker 	assert_d_eq(mallctl("stats.arenas.0.lextents.0.nmalloc",
336*1208bc7eSAndroid Build Coastguard Worker 	    (void *)&nmalloc, &sz, NULL, 0), expected,
337*1208bc7eSAndroid Build Coastguard Worker 	    "Unexpected mallctl() result");
338*1208bc7eSAndroid Build Coastguard Worker 	assert_d_eq(mallctl("stats.arenas.0.lextents.0.ndalloc",
339*1208bc7eSAndroid Build Coastguard Worker 	    (void *)&ndalloc, &sz, NULL, 0), expected,
340*1208bc7eSAndroid Build Coastguard Worker 	    "Unexpected mallctl() result");
341*1208bc7eSAndroid Build Coastguard Worker 	sz = sizeof(size_t);
342*1208bc7eSAndroid Build Coastguard Worker 	assert_d_eq(mallctl("stats.arenas.0.lextents.0.curlextents",
343*1208bc7eSAndroid Build Coastguard Worker 	    (void *)&curlextents, &sz, NULL, 0), expected,
344*1208bc7eSAndroid Build Coastguard Worker 	    "Unexpected mallctl() result");
345*1208bc7eSAndroid Build Coastguard Worker 
346*1208bc7eSAndroid Build Coastguard Worker 	if (config_stats) {
347*1208bc7eSAndroid Build Coastguard Worker 		assert_u64_gt(nmalloc, 0,
348*1208bc7eSAndroid Build Coastguard Worker 		    "nmalloc should be greater than zero");
349*1208bc7eSAndroid Build Coastguard Worker 		assert_u64_ge(nmalloc, ndalloc,
350*1208bc7eSAndroid Build Coastguard Worker 		    "nmalloc should be at least as large as ndalloc");
351*1208bc7eSAndroid Build Coastguard Worker 		assert_u64_gt(curlextents, 0,
352*1208bc7eSAndroid Build Coastguard Worker 		    "At least one extent should be currently allocated");
353*1208bc7eSAndroid Build Coastguard Worker 	}
354*1208bc7eSAndroid Build Coastguard Worker 
355*1208bc7eSAndroid Build Coastguard Worker 	dallocx(p, 0);
356*1208bc7eSAndroid Build Coastguard Worker }
357*1208bc7eSAndroid Build Coastguard Worker TEST_END
358*1208bc7eSAndroid Build Coastguard Worker 
359*1208bc7eSAndroid Build Coastguard Worker int
main(void)360*1208bc7eSAndroid Build Coastguard Worker main(void) {
361*1208bc7eSAndroid Build Coastguard Worker 	return test_no_reentrancy(
362*1208bc7eSAndroid Build Coastguard Worker 	    test_stats_summary,
363*1208bc7eSAndroid Build Coastguard Worker 	    test_stats_large,
364*1208bc7eSAndroid Build Coastguard Worker 	    test_stats_arenas_summary,
365*1208bc7eSAndroid Build Coastguard Worker 	    test_stats_arenas_small,
366*1208bc7eSAndroid Build Coastguard Worker 	    test_stats_arenas_large,
367*1208bc7eSAndroid Build Coastguard Worker 	    test_stats_arenas_bins,
368*1208bc7eSAndroid Build Coastguard Worker 	    test_stats_arenas_lextents);
369*1208bc7eSAndroid Build Coastguard Worker }
370