1 #define JEMALLOC_STATS_C_
2 #include "jemalloc/internal/jemalloc_internal.h"
4 #define CTL_GET(n, v, t) do { \
5 size_t sz = sizeof(t); \
6 xmallctl(n, (void *)v, &sz, NULL, 0); \
9 #define CTL_M2_GET(n, i, v, t) do { \
11 size_t miblen = sizeof(mib) / sizeof(size_t); \
12 size_t sz = sizeof(t); \
13 xmallctlnametomib(n, mib, &miblen); \
15 xmallctlbymib(mib, miblen, (void *)v, &sz, NULL, 0); \
18 #define CTL_M2_M4_GET(n, i, j, v, t) do { \
20 size_t miblen = sizeof(mib) / sizeof(size_t); \
21 size_t sz = sizeof(t); \
22 xmallctlnametomib(n, mib, &miblen); \
25 xmallctlbymib(mib, miblen, (void *)v, &sz, NULL, 0); \
28 /******************************************************************************/
31 bool opt_stats_print = false;
33 size_t stats_cactive = 0;
35 /******************************************************************************/
38 stats_arena_bins_print(void (*write_cb)(void *, const char *), void *cbopaque,
39 bool json, bool large, bool huge, unsigned i)
42 bool config_tcache, in_gap, in_gap_prev;
45 CTL_GET("arenas.page", &page, size_t);
47 CTL_GET("arenas.nbins", &nbins, unsigned);
49 malloc_cprintf(write_cb, cbopaque,
50 "\t\t\t\t\"bins\": [\n");
52 CTL_GET("config.tcache", &config_tcache, bool);
54 malloc_cprintf(write_cb, cbopaque,
55 "bins: size ind allocated nmalloc"
56 " ndalloc nrequests curregs"
57 " curruns regs pgs util nfills"
58 " nflushes newruns reruns\n");
60 malloc_cprintf(write_cb, cbopaque,
61 "bins: size ind allocated nmalloc"
62 " ndalloc nrequests curregs"
63 " curruns regs pgs util newruns"
67 for (j = 0, in_gap = false; j < nbins; j++) {
69 size_t reg_size, run_size, curregs;
72 uint64_t nmalloc, ndalloc, nrequests, nfills, nflushes;
75 CTL_M2_M4_GET("stats.arenas.0.bins.0.nruns", i, j, &nruns,
78 in_gap = (nruns == 0);
80 if (!json && in_gap_prev && !in_gap) {
81 malloc_cprintf(write_cb, cbopaque,
85 CTL_M2_GET("arenas.bin.0.size", j, ®_size, size_t);
86 CTL_M2_GET("arenas.bin.0.nregs", j, &nregs, uint32_t);
87 CTL_M2_GET("arenas.bin.0.run_size", j, &run_size, size_t);
89 CTL_M2_M4_GET("stats.arenas.0.bins.0.nmalloc", i, j, &nmalloc,
91 CTL_M2_M4_GET("stats.arenas.0.bins.0.ndalloc", i, j, &ndalloc,
93 CTL_M2_M4_GET("stats.arenas.0.bins.0.curregs", i, j, &curregs,
95 CTL_M2_M4_GET("stats.arenas.0.bins.0.nrequests", i, j,
96 &nrequests, uint64_t);
98 CTL_M2_M4_GET("stats.arenas.0.bins.0.nfills", i, j,
100 CTL_M2_M4_GET("stats.arenas.0.bins.0.nflushes", i, j,
101 &nflushes, uint64_t);
103 CTL_M2_M4_GET("stats.arenas.0.bins.0.nreruns", i, j, &nreruns,
105 CTL_M2_M4_GET("stats.arenas.0.bins.0.curruns", i, j, &curruns,
109 malloc_cprintf(write_cb, cbopaque,
111 "\t\t\t\t\t\t\"nmalloc\": %"FMTu64",\n"
112 "\t\t\t\t\t\t\"ndalloc\": %"FMTu64",\n"
113 "\t\t\t\t\t\t\"curregs\": %zu,\n"
114 "\t\t\t\t\t\t\"nrequests\": %"FMTu64",\n",
120 malloc_cprintf(write_cb, cbopaque,
121 "\t\t\t\t\t\t\"nfills\": %"FMTu64",\n"
122 "\t\t\t\t\t\t\"nflushes\": %"FMTu64",\n",
126 malloc_cprintf(write_cb, cbopaque,
127 "\t\t\t\t\t\t\"nreruns\": %"FMTu64",\n"
128 "\t\t\t\t\t\t\"curruns\": %zu\n"
132 (j + 1 < nbins) ? "," : "");
133 } else if (!in_gap) {
134 size_t availregs, milli;
135 char util[6]; /* "x.yyy". */
137 availregs = nregs * curruns;
138 milli = (availregs != 0) ? (1000 * curregs) / availregs
140 assert(milli <= 1000);
142 malloc_snprintf(util, sizeof(util),
144 } else if (milli < 100) {
145 malloc_snprintf(util, sizeof(util), "0.0%zu",
147 } else if (milli < 1000) {
148 malloc_snprintf(util, sizeof(util), "0.%zu",
151 malloc_snprintf(util, sizeof(util), "1");
154 malloc_cprintf(write_cb, cbopaque,
155 "%20zu %3u %12zu %12"FMTu64
156 " %12"FMTu64" %12"FMTu64" %12zu"
157 " %12zu %4u %3zu %-5s %12"FMTu64
158 " %12"FMTu64" %12"FMTu64" %12"FMTu64"\n",
159 reg_size, j, curregs * reg_size, nmalloc,
160 ndalloc, nrequests, curregs, curruns, nregs,
161 run_size / page, util, nfills, nflushes,
164 malloc_cprintf(write_cb, cbopaque,
165 "%20zu %3u %12zu %12"FMTu64
166 " %12"FMTu64" %12"FMTu64" %12zu"
167 " %12zu %4u %3zu %-5s %12"FMTu64
169 reg_size, j, curregs * reg_size, nmalloc,
170 ndalloc, nrequests, curregs, curruns, nregs,
171 run_size / page, util, nruns, nreruns);
176 malloc_cprintf(write_cb, cbopaque,
177 "\t\t\t\t]%s\n", (large || huge) ? "," : "");
180 malloc_cprintf(write_cb, cbopaque,
187 stats_arena_lruns_print(void (*write_cb)(void *, const char *), void *cbopaque,
188 bool json, bool huge, unsigned i)
190 unsigned nbins, nlruns, j;
191 bool in_gap, in_gap_prev;
193 CTL_GET("arenas.nbins", &nbins, unsigned);
194 CTL_GET("arenas.nlruns", &nlruns, unsigned);
196 malloc_cprintf(write_cb, cbopaque,
197 "\t\t\t\t\"lruns\": [\n");
199 malloc_cprintf(write_cb, cbopaque,
200 "large: size ind allocated nmalloc"
201 " ndalloc nrequests curruns\n");
203 for (j = 0, in_gap = false; j < nlruns; j++) {
204 uint64_t nmalloc, ndalloc, nrequests;
205 size_t run_size, curruns;
207 CTL_M2_M4_GET("stats.arenas.0.lruns.0.nmalloc", i, j, &nmalloc,
209 CTL_M2_M4_GET("stats.arenas.0.lruns.0.ndalloc", i, j, &ndalloc,
211 CTL_M2_M4_GET("stats.arenas.0.lruns.0.nrequests", i, j,
212 &nrequests, uint64_t);
213 in_gap_prev = in_gap;
214 in_gap = (nrequests == 0);
216 if (!json && in_gap_prev && !in_gap) {
217 malloc_cprintf(write_cb, cbopaque,
221 CTL_M2_GET("arenas.lrun.0.size", j, &run_size, size_t);
222 CTL_M2_M4_GET("stats.arenas.0.lruns.0.curruns", i, j, &curruns,
225 malloc_cprintf(write_cb, cbopaque,
227 "\t\t\t\t\t\t\"curruns\": %zu\n"
230 (j + 1 < nlruns) ? "," : "");
231 } else if (!in_gap) {
232 malloc_cprintf(write_cb, cbopaque,
233 "%20zu %3u %12zu %12"FMTu64" %12"FMTu64
234 " %12"FMTu64" %12zu\n",
235 run_size, nbins + j, curruns * run_size, nmalloc,
236 ndalloc, nrequests, curruns);
240 malloc_cprintf(write_cb, cbopaque,
241 "\t\t\t\t]%s\n", huge ? "," : "");
244 malloc_cprintf(write_cb, cbopaque,
251 stats_arena_hchunks_print(void (*write_cb)(void *, const char *),
252 void *cbopaque, bool json, unsigned i)
254 unsigned nbins, nlruns, nhchunks, j;
255 bool in_gap, in_gap_prev;
257 CTL_GET("arenas.nbins", &nbins, unsigned);
258 CTL_GET("arenas.nlruns", &nlruns, unsigned);
259 CTL_GET("arenas.nhchunks", &nhchunks, unsigned);
261 malloc_cprintf(write_cb, cbopaque,
262 "\t\t\t\t\"hchunks\": [\n");
264 malloc_cprintf(write_cb, cbopaque,
265 "huge: size ind allocated nmalloc"
266 " ndalloc nrequests curhchunks\n");
268 for (j = 0, in_gap = false; j < nhchunks; j++) {
269 uint64_t nmalloc, ndalloc, nrequests;
270 size_t hchunk_size, curhchunks;
272 CTL_M2_M4_GET("stats.arenas.0.hchunks.0.nmalloc", i, j,
274 CTL_M2_M4_GET("stats.arenas.0.hchunks.0.ndalloc", i, j,
276 CTL_M2_M4_GET("stats.arenas.0.hchunks.0.nrequests", i, j,
277 &nrequests, uint64_t);
278 in_gap_prev = in_gap;
279 in_gap = (nrequests == 0);
281 if (!json && in_gap_prev && !in_gap) {
282 malloc_cprintf(write_cb, cbopaque,
286 CTL_M2_GET("arenas.hchunk.0.size", j, &hchunk_size, size_t);
287 CTL_M2_M4_GET("stats.arenas.0.hchunks.0.curhchunks", i, j,
288 &curhchunks, size_t);
290 malloc_cprintf(write_cb, cbopaque,
292 "\t\t\t\t\t\t\"curhchunks\": %zu\n"
295 (j + 1 < nhchunks) ? "," : "");
296 } else if (!in_gap) {
297 malloc_cprintf(write_cb, cbopaque,
298 "%20zu %3u %12zu %12"FMTu64" %12"FMTu64
299 " %12"FMTu64" %12zu\n",
300 hchunk_size, nbins + nlruns + j,
301 curhchunks * hchunk_size, nmalloc, ndalloc,
302 nrequests, curhchunks);
306 malloc_cprintf(write_cb, cbopaque,
310 malloc_cprintf(write_cb, cbopaque,
317 stats_arena_print(void (*write_cb)(void *, const char *), void *cbopaque,
318 bool json, unsigned i, bool bins, bool large, bool huge)
322 ssize_t lg_dirty_mult, decay_time;
323 size_t page, pactive, pdirty, mapped, retained;
324 size_t metadata_mapped, metadata_allocated;
325 uint64_t npurge, nmadvise, purged;
326 size_t small_allocated;
327 uint64_t small_nmalloc, small_ndalloc, small_nrequests;
328 size_t large_allocated;
329 uint64_t large_nmalloc, large_ndalloc, large_nrequests;
330 size_t huge_allocated;
331 uint64_t huge_nmalloc, huge_ndalloc, huge_nrequests;
333 CTL_GET("arenas.page", &page, size_t);
335 CTL_M2_GET("stats.arenas.0.nthreads", i, &nthreads, unsigned);
337 malloc_cprintf(write_cb, cbopaque,
338 "\t\t\t\t\"nthreads\": %u,\n", nthreads);
340 malloc_cprintf(write_cb, cbopaque,
341 "assigned threads: %u\n", nthreads);
344 CTL_M2_GET("stats.arenas.0.dss", i, &dss, const char *);
346 malloc_cprintf(write_cb, cbopaque,
347 "\t\t\t\t\"dss\": \"%s\",\n", dss);
349 malloc_cprintf(write_cb, cbopaque,
350 "dss allocation precedence: %s\n", dss);
353 CTL_M2_GET("stats.arenas.0.lg_dirty_mult", i, &lg_dirty_mult, ssize_t);
355 malloc_cprintf(write_cb, cbopaque,
356 "\t\t\t\t\"lg_dirty_mult\": %zd,\n", lg_dirty_mult);
358 if (opt_purge == purge_mode_ratio) {
359 if (lg_dirty_mult >= 0) {
360 malloc_cprintf(write_cb, cbopaque,
361 "min active:dirty page ratio: %u:1\n",
362 (1U << lg_dirty_mult));
364 malloc_cprintf(write_cb, cbopaque,
365 "min active:dirty page ratio: N/A\n");
370 CTL_M2_GET("stats.arenas.0.decay_time", i, &decay_time, ssize_t);
372 malloc_cprintf(write_cb, cbopaque,
373 "\t\t\t\t\"decay_time\": %zd,\n", decay_time);
375 if (opt_purge == purge_mode_decay) {
376 if (decay_time >= 0) {
377 malloc_cprintf(write_cb, cbopaque,
378 "decay time: %zd\n", decay_time);
380 malloc_cprintf(write_cb, cbopaque,
381 "decay time: N/A\n");
386 CTL_M2_GET("stats.arenas.0.pactive", i, &pactive, size_t);
387 CTL_M2_GET("stats.arenas.0.pdirty", i, &pdirty, size_t);
388 CTL_M2_GET("stats.arenas.0.npurge", i, &npurge, uint64_t);
389 CTL_M2_GET("stats.arenas.0.nmadvise", i, &nmadvise, uint64_t);
390 CTL_M2_GET("stats.arenas.0.purged", i, &purged, uint64_t);
392 malloc_cprintf(write_cb, cbopaque,
393 "\t\t\t\t\"pactive\": %zu,\n", pactive);
394 malloc_cprintf(write_cb, cbopaque,
395 "\t\t\t\t\"pdirty\": %zu,\n", pdirty);
396 malloc_cprintf(write_cb, cbopaque,
397 "\t\t\t\t\"npurge\": %"FMTu64",\n", npurge);
398 malloc_cprintf(write_cb, cbopaque,
399 "\t\t\t\t\"nmadvise\": %"FMTu64",\n", nmadvise);
400 malloc_cprintf(write_cb, cbopaque,
401 "\t\t\t\t\"purged\": %"FMTu64",\n", purged);
403 malloc_cprintf(write_cb, cbopaque,
404 "purging: dirty: %zu, sweeps: %"FMTu64", madvises: %"FMTu64
405 ", purged: %"FMTu64"\n", pdirty, npurge, nmadvise, purged);
408 CTL_M2_GET("stats.arenas.0.small.allocated", i, &small_allocated,
410 CTL_M2_GET("stats.arenas.0.small.nmalloc", i, &small_nmalloc, uint64_t);
411 CTL_M2_GET("stats.arenas.0.small.ndalloc", i, &small_ndalloc, uint64_t);
412 CTL_M2_GET("stats.arenas.0.small.nrequests", i, &small_nrequests,
415 malloc_cprintf(write_cb, cbopaque,
416 "\t\t\t\t\"small\": {\n");
418 malloc_cprintf(write_cb, cbopaque,
419 "\t\t\t\t\t\"allocated\": %zu,\n", small_allocated);
420 malloc_cprintf(write_cb, cbopaque,
421 "\t\t\t\t\t\"nmalloc\": %"FMTu64",\n", small_nmalloc);
422 malloc_cprintf(write_cb, cbopaque,
423 "\t\t\t\t\t\"ndalloc\": %"FMTu64",\n", small_ndalloc);
424 malloc_cprintf(write_cb, cbopaque,
425 "\t\t\t\t\t\"nrequests\": %"FMTu64"\n", small_nrequests);
427 malloc_cprintf(write_cb, cbopaque,
430 malloc_cprintf(write_cb, cbopaque,
432 " ndalloc nrequests\n");
433 malloc_cprintf(write_cb, cbopaque,
434 "small: %12zu %12"FMTu64" %12"FMTu64
436 small_allocated, small_nmalloc, small_ndalloc,
440 CTL_M2_GET("stats.arenas.0.large.allocated", i, &large_allocated,
442 CTL_M2_GET("stats.arenas.0.large.nmalloc", i, &large_nmalloc, uint64_t);
443 CTL_M2_GET("stats.arenas.0.large.ndalloc", i, &large_ndalloc, uint64_t);
444 CTL_M2_GET("stats.arenas.0.large.nrequests", i, &large_nrequests,
447 malloc_cprintf(write_cb, cbopaque,
448 "\t\t\t\t\"large\": {\n");
450 malloc_cprintf(write_cb, cbopaque,
451 "\t\t\t\t\t\"allocated\": %zu,\n", large_allocated);
452 malloc_cprintf(write_cb, cbopaque,
453 "\t\t\t\t\t\"nmalloc\": %"FMTu64",\n", large_nmalloc);
454 malloc_cprintf(write_cb, cbopaque,
455 "\t\t\t\t\t\"ndalloc\": %"FMTu64",\n", large_ndalloc);
456 malloc_cprintf(write_cb, cbopaque,
457 "\t\t\t\t\t\"nrequests\": %"FMTu64"\n", large_nrequests);
459 malloc_cprintf(write_cb, cbopaque,
462 malloc_cprintf(write_cb, cbopaque,
463 "large: %12zu %12"FMTu64" %12"FMTu64
465 large_allocated, large_nmalloc, large_ndalloc,
469 CTL_M2_GET("stats.arenas.0.huge.allocated", i, &huge_allocated, size_t);
470 CTL_M2_GET("stats.arenas.0.huge.nmalloc", i, &huge_nmalloc, uint64_t);
471 CTL_M2_GET("stats.arenas.0.huge.ndalloc", i, &huge_ndalloc, uint64_t);
472 CTL_M2_GET("stats.arenas.0.huge.nrequests", i, &huge_nrequests,
475 malloc_cprintf(write_cb, cbopaque,
476 "\t\t\t\t\"huge\": {\n");
478 malloc_cprintf(write_cb, cbopaque,
479 "\t\t\t\t\t\"allocated\": %zu,\n", huge_allocated);
480 malloc_cprintf(write_cb, cbopaque,
481 "\t\t\t\t\t\"nmalloc\": %"FMTu64",\n", huge_nmalloc);
482 malloc_cprintf(write_cb, cbopaque,
483 "\t\t\t\t\t\"ndalloc\": %"FMTu64",\n", huge_ndalloc);
484 malloc_cprintf(write_cb, cbopaque,
485 "\t\t\t\t\t\"nrequests\": %"FMTu64"\n", huge_nrequests);
487 malloc_cprintf(write_cb, cbopaque,
490 malloc_cprintf(write_cb, cbopaque,
491 "huge: %12zu %12"FMTu64" %12"FMTu64
493 huge_allocated, huge_nmalloc, huge_ndalloc, huge_nrequests);
494 malloc_cprintf(write_cb, cbopaque,
495 "total: %12zu %12"FMTu64" %12"FMTu64
497 small_allocated + large_allocated + huge_allocated,
498 small_nmalloc + large_nmalloc + huge_nmalloc,
499 small_ndalloc + large_ndalloc + huge_ndalloc,
500 small_nrequests + large_nrequests + huge_nrequests);
503 malloc_cprintf(write_cb, cbopaque,
504 "active: %12zu\n", pactive * page);
507 CTL_M2_GET("stats.arenas.0.mapped", i, &mapped, size_t);
509 malloc_cprintf(write_cb, cbopaque,
510 "\t\t\t\t\"mapped\": %zu,\n", mapped);
512 malloc_cprintf(write_cb, cbopaque,
513 "mapped: %12zu\n", mapped);
516 CTL_M2_GET("stats.arenas.0.retained", i, &retained, size_t);
518 malloc_cprintf(write_cb, cbopaque,
519 "\t\t\t\t\"retained\": %zu,\n", retained);
521 malloc_cprintf(write_cb, cbopaque,
522 "retained: %12zu\n", retained);
525 CTL_M2_GET("stats.arenas.0.metadata.mapped", i, &metadata_mapped,
527 CTL_M2_GET("stats.arenas.0.metadata.allocated", i, &metadata_allocated,
530 malloc_cprintf(write_cb, cbopaque,
531 "\t\t\t\t\"metadata\": {\n");
533 malloc_cprintf(write_cb, cbopaque,
534 "\t\t\t\t\t\"mapped\": %zu,\n", metadata_mapped);
535 malloc_cprintf(write_cb, cbopaque,
536 "\t\t\t\t\t\"allocated\": %zu\n", metadata_allocated);
538 malloc_cprintf(write_cb, cbopaque,
541 malloc_cprintf(write_cb, cbopaque,
542 "metadata: mapped: %zu, allocated: %zu\n",
543 metadata_mapped, metadata_allocated);
547 stats_arena_bins_print(write_cb, cbopaque, json, large, huge,
551 stats_arena_lruns_print(write_cb, cbopaque, json, huge, i);
553 stats_arena_hchunks_print(write_cb, cbopaque, json, i);
557 stats_general_print(void (*write_cb)(void *, const char *), void *cbopaque,
558 bool json, bool merged, bool unmerged)
566 size_t sv, bsz, usz, ssz, sssz, cpsz;
569 usz = sizeof(unsigned);
570 ssz = sizeof(size_t);
571 sssz = sizeof(ssize_t);
572 cpsz = sizeof(const char *);
574 CTL_GET("version", &cpv, const char *);
576 malloc_cprintf(write_cb, cbopaque,
577 "\t\t\"version\": \"%s\",\n", cpv);
579 malloc_cprintf(write_cb, cbopaque, "Version: %s\n", cpv);
582 #define CONFIG_WRITE_BOOL_JSON(n, c) \
584 CTL_GET("config."#n, &bv, bool); \
585 malloc_cprintf(write_cb, cbopaque, \
586 "\t\t\t\""#n"\": %s%s\n", bv ? "true" : "false", \
591 malloc_cprintf(write_cb, cbopaque,
592 "\t\t\"config\": {\n");
595 CONFIG_WRITE_BOOL_JSON(cache_oblivious, ",")
597 CTL_GET("config.debug", &bv, bool);
599 malloc_cprintf(write_cb, cbopaque,
600 "\t\t\t\"debug\": %s,\n", bv ? "true" : "false");
602 malloc_cprintf(write_cb, cbopaque, "Assertions %s\n",
603 bv ? "enabled" : "disabled");
606 CONFIG_WRITE_BOOL_JSON(fill, ",")
607 CONFIG_WRITE_BOOL_JSON(lazy_lock, ",")
610 malloc_cprintf(write_cb, cbopaque,
611 "\t\t\t\"malloc_conf\": \"%s\",\n",
614 malloc_cprintf(write_cb, cbopaque,
615 "config.malloc_conf: \"%s\"\n", config_malloc_conf);
618 CONFIG_WRITE_BOOL_JSON(munmap, ",")
619 CONFIG_WRITE_BOOL_JSON(prof, ",")
620 CONFIG_WRITE_BOOL_JSON(prof_libgcc, ",")
621 CONFIG_WRITE_BOOL_JSON(prof_libunwind, ",")
622 CONFIG_WRITE_BOOL_JSON(stats, ",")
623 CONFIG_WRITE_BOOL_JSON(tcache, ",")
624 CONFIG_WRITE_BOOL_JSON(tls, ",")
625 CONFIG_WRITE_BOOL_JSON(utrace, ",")
626 CONFIG_WRITE_BOOL_JSON(valgrind, ",")
627 CONFIG_WRITE_BOOL_JSON(xmalloc, "")
630 malloc_cprintf(write_cb, cbopaque,
633 #undef CONFIG_WRITE_BOOL_JSON
636 #define OPT_WRITE_BOOL(n, c) \
637 if (je_mallctl("opt."#n, (void *)&bv, &bsz, NULL, 0) == 0) { \
639 malloc_cprintf(write_cb, cbopaque, \
640 "\t\t\t\""#n"\": %s%s\n", bv ? "true" : \
643 malloc_cprintf(write_cb, cbopaque, \
644 " opt."#n": %s\n", bv ? "true" : "false"); \
647 #define OPT_WRITE_BOOL_MUTABLE(n, m, c) { \
649 if (je_mallctl("opt."#n, (void *)&bv, &bsz, NULL, 0) == 0 && \
650 je_mallctl(#m, &bv2, (void *)&bsz, NULL, 0) == 0) { \
652 malloc_cprintf(write_cb, cbopaque, \
653 "\t\t\t\""#n"\": %s%s\n", bv ? "true" : \
656 malloc_cprintf(write_cb, cbopaque, \
657 " opt."#n": %s ("#m": %s)\n", bv ? "true" \
658 : "false", bv2 ? "true" : "false"); \
662 #define OPT_WRITE_UNSIGNED(n, c) \
663 if (je_mallctl("opt."#n, (void *)&uv, &usz, NULL, 0) == 0) { \
665 malloc_cprintf(write_cb, cbopaque, \
666 "\t\t\t\""#n"\": %u%s\n", uv, (c)); \
668 malloc_cprintf(write_cb, cbopaque, \
669 " opt."#n": %u\n", uv); \
672 #define OPT_WRITE_SIZE_T(n, c) \
673 if (je_mallctl("opt."#n, (void *)&sv, &ssz, NULL, 0) == 0) { \
675 malloc_cprintf(write_cb, cbopaque, \
676 "\t\t\t\""#n"\": %zu%s\n", sv, (c)); \
678 malloc_cprintf(write_cb, cbopaque, \
679 " opt."#n": %zu\n", sv); \
682 #define OPT_WRITE_SSIZE_T(n, c) \
683 if (je_mallctl("opt."#n, (void *)&ssv, &sssz, NULL, 0) == 0) { \
685 malloc_cprintf(write_cb, cbopaque, \
686 "\t\t\t\""#n"\": %zd%s\n", ssv, (c)); \
688 malloc_cprintf(write_cb, cbopaque, \
689 " opt."#n": %zd\n", ssv); \
692 #define OPT_WRITE_SSIZE_T_MUTABLE(n, m, c) { \
694 if (je_mallctl("opt."#n, (void *)&ssv, &sssz, NULL, 0) == 0 && \
695 je_mallctl(#m, (void *)&ssv2, &sssz, NULL, 0) == 0) { \
697 malloc_cprintf(write_cb, cbopaque, \
698 "\t\t\t\""#n"\": %zd%s\n", ssv, (c)); \
700 malloc_cprintf(write_cb, cbopaque, \
701 " opt."#n": %zd ("#m": %zd)\n", \
706 #define OPT_WRITE_CHAR_P(n, c) \
707 if (je_mallctl("opt."#n, (void *)&cpv, &cpsz, NULL, 0) == 0) { \
709 malloc_cprintf(write_cb, cbopaque, \
710 "\t\t\t\""#n"\": \"%s\"%s\n", cpv, (c)); \
712 malloc_cprintf(write_cb, cbopaque, \
713 " opt."#n": \"%s\"\n", cpv); \
718 malloc_cprintf(write_cb, cbopaque,
721 malloc_cprintf(write_cb, cbopaque,
722 "Run-time option settings:\n");
724 OPT_WRITE_BOOL(abort, ",")
725 OPT_WRITE_SIZE_T(lg_chunk, ",")
726 OPT_WRITE_CHAR_P(dss, ",")
727 OPT_WRITE_UNSIGNED(narenas, ",")
728 OPT_WRITE_CHAR_P(purge, ",")
729 if (json || opt_purge == purge_mode_ratio) {
730 OPT_WRITE_SSIZE_T_MUTABLE(lg_dirty_mult,
731 arenas.lg_dirty_mult, ",")
733 if (json || opt_purge == purge_mode_decay) {
734 OPT_WRITE_SSIZE_T_MUTABLE(decay_time, arenas.decay_time, ",")
736 OPT_WRITE_CHAR_P(junk, ",")
737 OPT_WRITE_SIZE_T(quarantine, ",")
738 OPT_WRITE_BOOL(redzone, ",")
739 OPT_WRITE_BOOL(zero, ",")
740 OPT_WRITE_BOOL(utrace, ",")
741 OPT_WRITE_BOOL(xmalloc, ",")
742 OPT_WRITE_BOOL(tcache, ",")
743 OPT_WRITE_SSIZE_T(lg_tcache_max, ",")
744 OPT_WRITE_BOOL(prof, ",")
745 OPT_WRITE_CHAR_P(prof_prefix, ",")
746 OPT_WRITE_BOOL_MUTABLE(prof_active, prof.active, ",")
747 OPT_WRITE_BOOL_MUTABLE(prof_thread_active_init, prof.thread_active_init,
749 OPT_WRITE_SSIZE_T_MUTABLE(lg_prof_sample, prof.lg_sample, ",")
750 OPT_WRITE_BOOL(prof_accum, ",")
751 OPT_WRITE_SSIZE_T(lg_prof_interval, ",")
752 OPT_WRITE_BOOL(prof_gdump, ",")
753 OPT_WRITE_BOOL(prof_final, ",")
754 OPT_WRITE_BOOL(prof_leak, ",")
756 * stats_print is always emitted, so as long as stats_print comes last
757 * it's safe to unconditionally omit the comma here (rather than having
758 * to conditionally omit it elsewhere depending on configuration).
760 OPT_WRITE_BOOL(stats_print, "")
762 malloc_cprintf(write_cb, cbopaque,
766 #undef OPT_WRITE_BOOL
767 #undef OPT_WRITE_BOOL_MUTABLE
768 #undef OPT_WRITE_SIZE_T
769 #undef OPT_WRITE_SSIZE_T
770 #undef OPT_WRITE_CHAR_P
774 malloc_cprintf(write_cb, cbopaque,
775 "\t\t\"arenas\": {\n");
778 CTL_GET("arenas.narenas", &uv, unsigned);
780 malloc_cprintf(write_cb, cbopaque,
781 "\t\t\t\"narenas\": %u,\n", uv);
783 malloc_cprintf(write_cb, cbopaque, "Arenas: %u\n", uv);
785 CTL_GET("arenas.lg_dirty_mult", &ssv, ssize_t);
787 malloc_cprintf(write_cb, cbopaque,
788 "\t\t\t\"lg_dirty_mult\": %zd,\n", ssv);
789 } else if (opt_purge == purge_mode_ratio) {
791 malloc_cprintf(write_cb, cbopaque,
792 "Min active:dirty page ratio per arena: "
793 "%u:1\n", (1U << ssv));
795 malloc_cprintf(write_cb, cbopaque,
796 "Min active:dirty page ratio per arena: "
800 CTL_GET("arenas.decay_time", &ssv, ssize_t);
802 malloc_cprintf(write_cb, cbopaque,
803 "\t\t\t\"decay_time\": %zd,\n", ssv);
804 } else if (opt_purge == purge_mode_decay) {
805 malloc_cprintf(write_cb, cbopaque,
806 "Unused dirty page decay time: %zd%s\n",
807 ssv, (ssv < 0) ? " (no decay)" : "");
810 CTL_GET("arenas.quantum", &sv, size_t);
812 malloc_cprintf(write_cb, cbopaque,
813 "\t\t\t\"quantum\": %zu,\n", sv);
815 malloc_cprintf(write_cb, cbopaque, "Quantum size: %zu\n", sv);
817 CTL_GET("arenas.page", &sv, size_t);
819 malloc_cprintf(write_cb, cbopaque,
820 "\t\t\t\"page\": %zu,\n", sv);
822 malloc_cprintf(write_cb, cbopaque, "Page size: %zu\n", sv);
824 if (je_mallctl("arenas.tcache_max", (void *)&sv, &ssz, NULL, 0) == 0) {
826 malloc_cprintf(write_cb, cbopaque,
827 "\t\t\t\"tcache_max\": %zu,\n", sv);
829 malloc_cprintf(write_cb, cbopaque,
830 "Maximum thread-cached size class: %zu\n", sv);
835 unsigned nbins, nlruns, nhchunks, i;
837 CTL_GET("arenas.nbins", &nbins, unsigned);
838 malloc_cprintf(write_cb, cbopaque,
839 "\t\t\t\"nbins\": %u,\n", nbins);
841 CTL_GET("arenas.nhbins", &uv, unsigned);
842 malloc_cprintf(write_cb, cbopaque,
843 "\t\t\t\"nhbins\": %u,\n", uv);
845 malloc_cprintf(write_cb, cbopaque,
846 "\t\t\t\"bin\": [\n");
847 for (i = 0; i < nbins; i++) {
848 malloc_cprintf(write_cb, cbopaque,
851 CTL_M2_GET("arenas.bin.0.size", i, &sv, size_t);
852 malloc_cprintf(write_cb, cbopaque,
853 "\t\t\t\t\t\"size\": %zu,\n", sv);
855 CTL_M2_GET("arenas.bin.0.nregs", i, &u32v, uint32_t);
856 malloc_cprintf(write_cb, cbopaque,
857 "\t\t\t\t\t\"nregs\": %"FMTu32",\n", u32v);
859 CTL_M2_GET("arenas.bin.0.run_size", i, &sv, size_t);
860 malloc_cprintf(write_cb, cbopaque,
861 "\t\t\t\t\t\"run_size\": %zu\n", sv);
863 malloc_cprintf(write_cb, cbopaque,
864 "\t\t\t\t}%s\n", (i + 1 < nbins) ? "," : "");
866 malloc_cprintf(write_cb, cbopaque,
869 CTL_GET("arenas.nlruns", &nlruns, unsigned);
870 malloc_cprintf(write_cb, cbopaque,
871 "\t\t\t\"nlruns\": %u,\n", nlruns);
873 malloc_cprintf(write_cb, cbopaque,
874 "\t\t\t\"lrun\": [\n");
875 for (i = 0; i < nlruns; i++) {
876 malloc_cprintf(write_cb, cbopaque,
879 CTL_M2_GET("arenas.lrun.0.size", i, &sv, size_t);
880 malloc_cprintf(write_cb, cbopaque,
881 "\t\t\t\t\t\"size\": %zu\n", sv);
883 malloc_cprintf(write_cb, cbopaque,
884 "\t\t\t\t}%s\n", (i + 1 < nlruns) ? "," : "");
886 malloc_cprintf(write_cb, cbopaque,
889 CTL_GET("arenas.nhchunks", &nhchunks, unsigned);
890 malloc_cprintf(write_cb, cbopaque,
891 "\t\t\t\"nhchunks\": %u,\n", nhchunks);
893 malloc_cprintf(write_cb, cbopaque,
894 "\t\t\t\"hchunk\": [\n");
895 for (i = 0; i < nhchunks; i++) {
896 malloc_cprintf(write_cb, cbopaque,
899 CTL_M2_GET("arenas.hchunk.0.size", i, &sv, size_t);
900 malloc_cprintf(write_cb, cbopaque,
901 "\t\t\t\t\t\"size\": %zu\n", sv);
903 malloc_cprintf(write_cb, cbopaque,
904 "\t\t\t\t}%s\n", (i + 1 < nhchunks) ? "," : "");
906 malloc_cprintf(write_cb, cbopaque,
909 malloc_cprintf(write_cb, cbopaque,
915 malloc_cprintf(write_cb, cbopaque,
916 "\t\t\"prof\": {\n");
918 CTL_GET("prof.thread_active_init", &bv, bool);
919 malloc_cprintf(write_cb, cbopaque,
920 "\t\t\t\"thread_active_init\": %s,\n", bv ? "true" :
923 CTL_GET("prof.active", &bv, bool);
924 malloc_cprintf(write_cb, cbopaque,
925 "\t\t\t\"active\": %s,\n", bv ? "true" : "false");
927 CTL_GET("prof.gdump", &bv, bool);
928 malloc_cprintf(write_cb, cbopaque,
929 "\t\t\t\"gdump\": %s,\n", bv ? "true" : "false");
931 CTL_GET("prof.interval", &u64v, uint64_t);
932 malloc_cprintf(write_cb, cbopaque,
933 "\t\t\t\"interval\": %"FMTu64",\n", u64v);
935 CTL_GET("prof.lg_sample", &ssv, ssize_t);
936 malloc_cprintf(write_cb, cbopaque,
937 "\t\t\t\"lg_sample\": %zd\n", ssv);
939 malloc_cprintf(write_cb, cbopaque,
940 "\t\t}%s\n", (config_stats || merged || unmerged) ? "," :
946 stats_print_helper(void (*write_cb)(void *, const char *), void *cbopaque,
947 bool json, bool merged, bool unmerged, bool bins, bool large, bool huge)
950 size_t allocated, active, metadata, resident, mapped, retained;
952 CTL_GET("stats.cactive", &cactive, size_t *);
953 CTL_GET("stats.allocated", &allocated, size_t);
954 CTL_GET("stats.active", &active, size_t);
955 CTL_GET("stats.metadata", &metadata, size_t);
956 CTL_GET("stats.resident", &resident, size_t);
957 CTL_GET("stats.mapped", &mapped, size_t);
958 CTL_GET("stats.retained", &retained, size_t);
960 malloc_cprintf(write_cb, cbopaque,
961 "\t\t\"stats\": {\n");
963 malloc_cprintf(write_cb, cbopaque,
964 "\t\t\t\"cactive\": %zu,\n", atomic_read_z(cactive));
965 malloc_cprintf(write_cb, cbopaque,
966 "\t\t\t\"allocated\": %zu,\n", allocated);
967 malloc_cprintf(write_cb, cbopaque,
968 "\t\t\t\"active\": %zu,\n", active);
969 malloc_cprintf(write_cb, cbopaque,
970 "\t\t\t\"metadata\": %zu,\n", metadata);
971 malloc_cprintf(write_cb, cbopaque,
972 "\t\t\t\"resident\": %zu,\n", resident);
973 malloc_cprintf(write_cb, cbopaque,
974 "\t\t\t\"mapped\": %zu,\n", mapped);
975 malloc_cprintf(write_cb, cbopaque,
976 "\t\t\t\"retained\": %zu\n", retained);
978 malloc_cprintf(write_cb, cbopaque,
979 "\t\t}%s\n", (merged || unmerged) ? "," : "");
981 malloc_cprintf(write_cb, cbopaque,
982 "Allocated: %zu, active: %zu, metadata: %zu,"
983 " resident: %zu, mapped: %zu, retained: %zu\n",
984 allocated, active, metadata, resident, mapped, retained);
985 malloc_cprintf(write_cb, cbopaque,
986 "Current active ceiling: %zu\n",
987 atomic_read_z(cactive));
990 if (merged || unmerged) {
994 malloc_cprintf(write_cb, cbopaque,
995 "\t\t\"stats.arenas\": {\n");
998 CTL_GET("arenas.narenas", &narenas, unsigned);
1000 VARIABLE_ARRAY(bool, initialized, narenas);
1002 unsigned i, j, ninitialized;
1004 isz = sizeof(bool) * narenas;
1005 xmallctl("arenas.initialized", (void *)initialized,
1007 for (i = ninitialized = 0; i < narenas; i++) {
1013 if (merged && (ninitialized > 1 || !unmerged)) {
1014 /* Print merged arena stats. */
1016 malloc_cprintf(write_cb, cbopaque,
1017 "\t\t\t\"merged\": {\n");
1019 malloc_cprintf(write_cb, cbopaque,
1020 "\nMerged arenas stats:\n");
1022 stats_arena_print(write_cb, cbopaque, json,
1023 narenas, bins, large, huge);
1025 malloc_cprintf(write_cb, cbopaque,
1026 "\t\t\t}%s\n", (ninitialized > 1) ?
1031 /* Unmerged stats. */
1032 for (i = j = 0; i < narenas; i++) {
1033 if (initialized[i]) {
1036 malloc_cprintf(write_cb,
1038 "\t\t\t\"%u\": {\n", i);
1040 malloc_cprintf(write_cb,
1041 cbopaque, "\narenas[%u]:\n",
1044 stats_arena_print(write_cb, cbopaque,
1045 json, i, bins, large, huge);
1047 malloc_cprintf(write_cb,
1050 ninitialized) ? "," : "");
1057 malloc_cprintf(write_cb, cbopaque,
1064 stats_print(void (*write_cb)(void *, const char *), void *cbopaque,
1071 bool general = true;
1073 bool unmerged = true;
1079 * Refresh stats, in case mallctl() was called by the application.
1081 * Check for OOM here, since refreshing the ctl cache can trigger
1082 * allocation. In practice, none of the subsequent mallctl()-related
1083 * calls in this function will cause OOM if this one succeeds.
1086 u64sz = sizeof(uint64_t);
1087 err = je_mallctl("epoch", (void *)&epoch, &u64sz, (void *)&epoch,
1090 if (err == EAGAIN) {
1091 malloc_write("<jemalloc>: Memory allocation failure in "
1092 "mallctl(\"epoch\", ...)\n");
1095 malloc_write("<jemalloc>: Failure in mallctl(\"epoch\", "
1103 for (i = 0; opts[i] != '\0'; i++) {
1132 malloc_cprintf(write_cb, cbopaque,
1134 "\t\"jemalloc\": {\n");
1136 malloc_cprintf(write_cb, cbopaque,
1137 "___ Begin jemalloc statistics ___\n");
1141 stats_general_print(write_cb, cbopaque, json, merged, unmerged);
1143 stats_print_helper(write_cb, cbopaque, json, merged, unmerged,
1147 malloc_cprintf(write_cb, cbopaque,
1151 malloc_cprintf(write_cb, cbopaque,
1152 "--- End jemalloc statistics ---\n");