1 #include "test/jemalloc_test.h"
2
TEST_BEGIN(test_stats_summary)3 TEST_BEGIN(test_stats_summary)
4 {
5 size_t *cactive;
6 size_t sz, allocated, active, resident, mapped;
7 int expected = config_stats ? 0 : ENOENT;
8
9 sz = sizeof(cactive);
10 assert_d_eq(mallctl("stats.cactive", (void *)&cactive, &sz, NULL, 0),
11 expected, "Unexpected mallctl() result");
12
13 sz = sizeof(size_t);
14 assert_d_eq(mallctl("stats.allocated", (void *)&allocated, &sz, NULL,
15 0), expected, "Unexpected mallctl() result");
16 assert_d_eq(mallctl("stats.active", (void *)&active, &sz, NULL, 0),
17 expected, "Unexpected mallctl() result");
18 assert_d_eq(mallctl("stats.resident", (void *)&resident, &sz, NULL, 0),
19 expected, "Unexpected mallctl() result");
20 assert_d_eq(mallctl("stats.mapped", (void *)&mapped, &sz, NULL, 0),
21 expected, "Unexpected mallctl() result");
22
23 if (config_stats) {
24 assert_zu_le(active, *cactive,
25 "active should be no larger than cactive");
26 assert_zu_le(allocated, active,
27 "allocated should be no larger than active");
28 assert_zu_lt(active, resident,
29 "active should be less than resident");
30 assert_zu_lt(active, mapped,
31 "active should be less than mapped");
32 }
33 }
34 TEST_END
35
TEST_BEGIN(test_stats_huge)36 TEST_BEGIN(test_stats_huge)
37 {
38 void *p;
39 uint64_t epoch;
40 size_t allocated;
41 uint64_t nmalloc, ndalloc, nrequests;
42 size_t sz;
43 int expected = config_stats ? 0 : ENOENT;
44
45 p = mallocx(large_maxclass+1, 0);
46 assert_ptr_not_null(p, "Unexpected mallocx() failure");
47
48 assert_d_eq(mallctl("epoch", NULL, NULL, (void *)&epoch, sizeof(epoch)),
49 0, "Unexpected mallctl() failure");
50
51 sz = sizeof(size_t);
52 assert_d_eq(mallctl("stats.arenas.0.huge.allocated", (void *)&allocated,
53 &sz, NULL, 0), expected, "Unexpected mallctl() result");
54 sz = sizeof(uint64_t);
55 assert_d_eq(mallctl("stats.arenas.0.huge.nmalloc", (void *)&nmalloc,
56 &sz, NULL, 0), expected, "Unexpected mallctl() result");
57 assert_d_eq(mallctl("stats.arenas.0.huge.ndalloc", (void *)&ndalloc,
58 &sz, NULL, 0), expected, "Unexpected mallctl() result");
59 assert_d_eq(mallctl("stats.arenas.0.huge.nrequests", (void *)&nrequests,
60 &sz, NULL, 0), expected, "Unexpected mallctl() result");
61
62 if (config_stats) {
63 assert_zu_gt(allocated, 0,
64 "allocated should be greater than zero");
65 assert_u64_ge(nmalloc, ndalloc,
66 "nmalloc should be at least as large as ndalloc");
67 assert_u64_le(nmalloc, nrequests,
68 "nmalloc should no larger than nrequests");
69 }
70
71 dallocx(p, 0);
72 }
73 TEST_END
74
TEST_BEGIN(test_stats_arenas_summary)75 TEST_BEGIN(test_stats_arenas_summary)
76 {
77 unsigned arena;
78 void *little, *large, *huge;
79 uint64_t epoch;
80 size_t sz;
81 int expected = config_stats ? 0 : ENOENT;
82 size_t mapped;
83 uint64_t npurge, nmadvise, purged;
84
85 arena = 0;
86 assert_d_eq(mallctl("thread.arena", NULL, NULL, (void *)&arena,
87 sizeof(arena)), 0, "Unexpected mallctl() failure");
88
89 little = mallocx(SMALL_MAXCLASS, 0);
90 assert_ptr_not_null(little, "Unexpected mallocx() failure");
91 large = mallocx(large_maxclass, 0);
92 assert_ptr_not_null(large, "Unexpected mallocx() failure");
93 huge = mallocx(chunksize, 0);
94 assert_ptr_not_null(huge, "Unexpected mallocx() failure");
95
96 dallocx(little, 0);
97 dallocx(large, 0);
98 dallocx(huge, 0);
99
100 assert_d_eq(mallctl("arena.0.purge", NULL, NULL, NULL, 0), 0,
101 "Unexpected mallctl() failure");
102
103 assert_d_eq(mallctl("epoch", NULL, NULL, (void *)&epoch, sizeof(epoch)),
104 0, "Unexpected mallctl() failure");
105
106 sz = sizeof(size_t);
107 assert_d_eq(mallctl("stats.arenas.0.mapped", (void *)&mapped, &sz, NULL,
108 0), expected, "Unexepected mallctl() result");
109 sz = sizeof(uint64_t);
110 assert_d_eq(mallctl("stats.arenas.0.npurge", (void *)&npurge, &sz, NULL,
111 0), expected, "Unexepected mallctl() result");
112 assert_d_eq(mallctl("stats.arenas.0.nmadvise", (void *)&nmadvise, &sz,
113 NULL, 0), expected, "Unexepected mallctl() result");
114 assert_d_eq(mallctl("stats.arenas.0.purged", (void *)&purged, &sz, NULL,
115 0), expected, "Unexepected mallctl() result");
116
117 if (config_stats) {
118 assert_u64_gt(npurge, 0,
119 "At least one purge should have occurred");
120 assert_u64_le(nmadvise, purged,
121 "nmadvise should be no greater than purged");
122 }
123 }
124 TEST_END
125
126 void *
thd_start(void * arg)127 thd_start(void *arg)
128 {
129
130 return (NULL);
131 }
132
133 static void
no_lazy_lock(void)134 no_lazy_lock(void)
135 {
136 thd_t thd;
137
138 thd_create(&thd, thd_start, NULL);
139 thd_join(thd, NULL);
140 }
141
TEST_BEGIN(test_stats_arenas_small)142 TEST_BEGIN(test_stats_arenas_small)
143 {
144 unsigned arena;
145 void *p;
146 size_t sz, allocated;
147 uint64_t epoch, nmalloc, ndalloc, nrequests;
148 int expected = config_stats ? 0 : ENOENT;
149
150 no_lazy_lock(); /* Lazy locking would dodge tcache testing. */
151
152 arena = 0;
153 assert_d_eq(mallctl("thread.arena", NULL, NULL, (void *)&arena,
154 sizeof(arena)), 0, "Unexpected mallctl() failure");
155
156 p = mallocx(SMALL_MAXCLASS, 0);
157 assert_ptr_not_null(p, "Unexpected mallocx() failure");
158
159 assert_d_eq(mallctl("thread.tcache.flush", NULL, NULL, NULL, 0),
160 config_tcache ? 0 : ENOENT, "Unexpected mallctl() result");
161
162 assert_d_eq(mallctl("epoch", NULL, NULL, (void *)&epoch, sizeof(epoch)),
163 0, "Unexpected mallctl() failure");
164
165 sz = sizeof(size_t);
166 assert_d_eq(mallctl("stats.arenas.0.small.allocated",
167 (void *)&allocated, &sz, NULL, 0), expected,
168 "Unexpected mallctl() result");
169 sz = sizeof(uint64_t);
170 assert_d_eq(mallctl("stats.arenas.0.small.nmalloc", (void *)&nmalloc,
171 &sz, NULL, 0), expected, "Unexpected mallctl() result");
172 assert_d_eq(mallctl("stats.arenas.0.small.ndalloc", (void *)&ndalloc,
173 &sz, NULL, 0), expected, "Unexpected mallctl() result");
174 assert_d_eq(mallctl("stats.arenas.0.small.nrequests",
175 (void *)&nrequests, &sz, NULL, 0), expected,
176 "Unexpected mallctl() result");
177
178 if (config_stats) {
179 assert_zu_gt(allocated, 0,
180 "allocated should be greater than zero");
181 assert_u64_gt(nmalloc, 0,
182 "nmalloc should be no greater than zero");
183 assert_u64_ge(nmalloc, ndalloc,
184 "nmalloc should be at least as large as ndalloc");
185 assert_u64_gt(nrequests, 0,
186 "nrequests should be greater than zero");
187 }
188
189 dallocx(p, 0);
190 }
191 TEST_END
192
TEST_BEGIN(test_stats_arenas_large)193 TEST_BEGIN(test_stats_arenas_large)
194 {
195 unsigned arena;
196 void *p;
197 size_t sz, allocated;
198 uint64_t epoch, nmalloc, ndalloc, nrequests;
199 int expected = config_stats ? 0 : ENOENT;
200
201 arena = 0;
202 assert_d_eq(mallctl("thread.arena", NULL, NULL, (void *)&arena,
203 sizeof(arena)), 0, "Unexpected mallctl() failure");
204
205 p = mallocx(large_maxclass, 0);
206 assert_ptr_not_null(p, "Unexpected mallocx() failure");
207
208 assert_d_eq(mallctl("epoch", NULL, NULL, (void *)&epoch, sizeof(epoch)),
209 0, "Unexpected mallctl() failure");
210
211 sz = sizeof(size_t);
212 assert_d_eq(mallctl("stats.arenas.0.large.allocated",
213 (void *)&allocated, &sz, NULL, 0), expected,
214 "Unexpected mallctl() result");
215 sz = sizeof(uint64_t);
216 assert_d_eq(mallctl("stats.arenas.0.large.nmalloc", (void *)&nmalloc,
217 &sz, NULL, 0), expected, "Unexpected mallctl() result");
218 assert_d_eq(mallctl("stats.arenas.0.large.ndalloc", (void *)&ndalloc,
219 &sz, NULL, 0), expected, "Unexpected mallctl() result");
220 assert_d_eq(mallctl("stats.arenas.0.large.nrequests",
221 (void *)&nrequests, &sz, NULL, 0), expected,
222 "Unexpected mallctl() result");
223
224 if (config_stats) {
225 assert_zu_gt(allocated, 0,
226 "allocated should be greater than zero");
227 assert_u64_gt(nmalloc, 0,
228 "nmalloc should be greater than zero");
229 assert_u64_ge(nmalloc, ndalloc,
230 "nmalloc should be at least as large as ndalloc");
231 assert_u64_gt(nrequests, 0,
232 "nrequests should be greater than zero");
233 }
234
235 dallocx(p, 0);
236 }
237 TEST_END
238
TEST_BEGIN(test_stats_arenas_huge)239 TEST_BEGIN(test_stats_arenas_huge)
240 {
241 unsigned arena;
242 void *p;
243 size_t sz, allocated;
244 uint64_t epoch, nmalloc, ndalloc;
245 int expected = config_stats ? 0 : ENOENT;
246
247 arena = 0;
248 assert_d_eq(mallctl("thread.arena", NULL, NULL, (void *)&arena,
249 sizeof(arena)), 0, "Unexpected mallctl() failure");
250
251 p = mallocx(chunksize, 0);
252 assert_ptr_not_null(p, "Unexpected mallocx() failure");
253
254 assert_d_eq(mallctl("epoch", NULL, NULL, (void *)&epoch, sizeof(epoch)),
255 0, "Unexpected mallctl() failure");
256
257 sz = sizeof(size_t);
258 assert_d_eq(mallctl("stats.arenas.0.huge.allocated", (void *)&allocated,
259 &sz, NULL, 0), expected, "Unexpected mallctl() result");
260 sz = sizeof(uint64_t);
261 assert_d_eq(mallctl("stats.arenas.0.huge.nmalloc", (void *)&nmalloc,
262 &sz, NULL, 0), expected, "Unexpected mallctl() result");
263 assert_d_eq(mallctl("stats.arenas.0.huge.ndalloc", (void *)&ndalloc,
264 &sz, NULL, 0), expected, "Unexpected mallctl() result");
265
266 if (config_stats) {
267 assert_zu_gt(allocated, 0,
268 "allocated should be greater than zero");
269 assert_u64_gt(nmalloc, 0,
270 "nmalloc should be greater than zero");
271 assert_u64_ge(nmalloc, ndalloc,
272 "nmalloc should be at least as large as ndalloc");
273 }
274
275 dallocx(p, 0);
276 }
277 TEST_END
278
TEST_BEGIN(test_stats_arenas_bins)279 TEST_BEGIN(test_stats_arenas_bins)
280 {
281 unsigned arena;
282 void *p;
283 size_t sz, curruns, curregs;
284 uint64_t epoch, nmalloc, ndalloc, nrequests, nfills, nflushes;
285 uint64_t nruns, nreruns;
286 int expected = config_stats ? 0 : ENOENT;
287
288 arena = 0;
289 assert_d_eq(mallctl("thread.arena", NULL, NULL, (void *)&arena,
290 sizeof(arena)), 0, "Unexpected mallctl() failure");
291
292 p = mallocx(arena_bin_info[0].reg_size, 0);
293 assert_ptr_not_null(p, "Unexpected mallocx() failure");
294
295 assert_d_eq(mallctl("thread.tcache.flush", NULL, NULL, NULL, 0),
296 config_tcache ? 0 : ENOENT, "Unexpected mallctl() result");
297
298 assert_d_eq(mallctl("epoch", NULL, NULL, (void *)&epoch, sizeof(epoch)),
299 0, "Unexpected mallctl() failure");
300
301 sz = sizeof(uint64_t);
302 assert_d_eq(mallctl("stats.arenas.0.bins.0.nmalloc", (void *)&nmalloc,
303 &sz, NULL, 0), expected, "Unexpected mallctl() result");
304 assert_d_eq(mallctl("stats.arenas.0.bins.0.ndalloc", (void *)&ndalloc,
305 &sz, NULL, 0), expected, "Unexpected mallctl() result");
306 assert_d_eq(mallctl("stats.arenas.0.bins.0.nrequests",
307 (void *)&nrequests, &sz, NULL, 0), expected,
308 "Unexpected mallctl() result");
309 sz = sizeof(size_t);
310 assert_d_eq(mallctl("stats.arenas.0.bins.0.curregs", (void *)&curregs,
311 &sz, NULL, 0), expected, "Unexpected mallctl() result");
312
313 sz = sizeof(uint64_t);
314 assert_d_eq(mallctl("stats.arenas.0.bins.0.nfills", (void *)&nfills,
315 &sz, NULL, 0), config_tcache ? expected : ENOENT,
316 "Unexpected mallctl() result");
317 assert_d_eq(mallctl("stats.arenas.0.bins.0.nflushes", (void *)&nflushes,
318 &sz, NULL, 0), config_tcache ? expected : ENOENT,
319 "Unexpected mallctl() result");
320
321 assert_d_eq(mallctl("stats.arenas.0.bins.0.nruns", (void *)&nruns, &sz,
322 NULL, 0), expected, "Unexpected mallctl() result");
323 assert_d_eq(mallctl("stats.arenas.0.bins.0.nreruns", (void *)&nreruns,
324 &sz, NULL, 0), expected, "Unexpected mallctl() result");
325 sz = sizeof(size_t);
326 assert_d_eq(mallctl("stats.arenas.0.bins.0.curruns", (void *)&curruns,
327 &sz, NULL, 0), expected, "Unexpected mallctl() result");
328
329 if (config_stats) {
330 assert_u64_gt(nmalloc, 0,
331 "nmalloc should be greater than zero");
332 assert_u64_ge(nmalloc, ndalloc,
333 "nmalloc should be at least as large as ndalloc");
334 assert_u64_gt(nrequests, 0,
335 "nrequests should be greater than zero");
336 assert_zu_gt(curregs, 0,
337 "allocated should be greater than zero");
338 if (config_tcache) {
339 assert_u64_gt(nfills, 0,
340 "At least one fill should have occurred");
341 assert_u64_gt(nflushes, 0,
342 "At least one flush should have occurred");
343 }
344 assert_u64_gt(nruns, 0,
345 "At least one run should have been allocated");
346 assert_zu_gt(curruns, 0,
347 "At least one run should be currently allocated");
348 }
349
350 dallocx(p, 0);
351 }
352 TEST_END
353
TEST_BEGIN(test_stats_arenas_lruns)354 TEST_BEGIN(test_stats_arenas_lruns)
355 {
356 unsigned arena;
357 void *p;
358 uint64_t epoch, nmalloc, ndalloc, nrequests;
359 size_t curruns, sz;
360 int expected = config_stats ? 0 : ENOENT;
361
362 arena = 0;
363 assert_d_eq(mallctl("thread.arena", NULL, NULL, (void *)&arena,
364 sizeof(arena)), 0, "Unexpected mallctl() failure");
365
366 p = mallocx(LARGE_MINCLASS, 0);
367 assert_ptr_not_null(p, "Unexpected mallocx() failure");
368
369 assert_d_eq(mallctl("epoch", NULL, NULL, (void *)&epoch, sizeof(epoch)),
370 0, "Unexpected mallctl() failure");
371
372 sz = sizeof(uint64_t);
373 assert_d_eq(mallctl("stats.arenas.0.lruns.0.nmalloc", (void *)&nmalloc,
374 &sz, NULL, 0), expected, "Unexpected mallctl() result");
375 assert_d_eq(mallctl("stats.arenas.0.lruns.0.ndalloc", (void *)&ndalloc,
376 &sz, NULL, 0), expected, "Unexpected mallctl() result");
377 assert_d_eq(mallctl("stats.arenas.0.lruns.0.nrequests",
378 (void *)&nrequests, &sz, NULL, 0), expected,
379 "Unexpected mallctl() result");
380 sz = sizeof(size_t);
381 assert_d_eq(mallctl("stats.arenas.0.lruns.0.curruns", (void *)&curruns,
382 &sz, NULL, 0), expected, "Unexpected mallctl() result");
383
384 if (config_stats) {
385 assert_u64_gt(nmalloc, 0,
386 "nmalloc should be greater than zero");
387 assert_u64_ge(nmalloc, ndalloc,
388 "nmalloc should be at least as large as ndalloc");
389 assert_u64_gt(nrequests, 0,
390 "nrequests should be greater than zero");
391 assert_u64_gt(curruns, 0,
392 "At least one run should be currently allocated");
393 }
394
395 dallocx(p, 0);
396 }
397 TEST_END
398
TEST_BEGIN(test_stats_arenas_hchunks)399 TEST_BEGIN(test_stats_arenas_hchunks)
400 {
401 unsigned arena;
402 void *p;
403 uint64_t epoch, nmalloc, ndalloc;
404 size_t curhchunks, sz;
405 int expected = config_stats ? 0 : ENOENT;
406
407 arena = 0;
408 assert_d_eq(mallctl("thread.arena", NULL, NULL, (void *)&arena,
409 sizeof(arena)), 0, "Unexpected mallctl() failure");
410
411 p = mallocx(chunksize, 0);
412 assert_ptr_not_null(p, "Unexpected mallocx() failure");
413
414 assert_d_eq(mallctl("epoch", NULL, NULL, (void *)&epoch, sizeof(epoch)),
415 0, "Unexpected mallctl() failure");
416
417 sz = sizeof(uint64_t);
418 assert_d_eq(mallctl("stats.arenas.0.hchunks.0.nmalloc",
419 (void *)&nmalloc, &sz, NULL, 0), expected,
420 "Unexpected mallctl() result");
421 assert_d_eq(mallctl("stats.arenas.0.hchunks.0.ndalloc",
422 (void *)&ndalloc, &sz, NULL, 0), expected,
423 "Unexpected mallctl() result");
424 sz = sizeof(size_t);
425 assert_d_eq(mallctl("stats.arenas.0.hchunks.0.curhchunks",
426 (void *)&curhchunks, &sz, NULL, 0), expected,
427 "Unexpected mallctl() result");
428
429 if (config_stats) {
430 assert_u64_gt(nmalloc, 0,
431 "nmalloc should be greater than zero");
432 assert_u64_ge(nmalloc, ndalloc,
433 "nmalloc should be at least as large as ndalloc");
434 assert_u64_gt(curhchunks, 0,
435 "At least one chunk should be currently allocated");
436 }
437
438 dallocx(p, 0);
439 }
440 TEST_END
441
442 int
main(void)443 main(void)
444 {
445
446 return (test(
447 test_stats_summary,
448 test_stats_huge,
449 test_stats_arenas_summary,
450 test_stats_arenas_small,
451 test_stats_arenas_large,
452 test_stats_arenas_huge,
453 test_stats_arenas_bins,
454 test_stats_arenas_lruns,
455 test_stats_arenas_hchunks));
456 }
457