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, v, &sz, NULL, 0); \
9 #define CTL_I_GET(n, 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, v, &sz, NULL, 0); \
18 #define CTL_J_GET(n, v, t) do { \
20 size_t miblen = sizeof(mib) / sizeof(size_t); \
21 size_t sz = sizeof(t); \
22 xmallctlnametomib(n, mib, &miblen); \
24 xmallctlbymib(mib, miblen, v, &sz, NULL, 0); \
27 #define CTL_IJ_GET(n, v, t) do { \
29 size_t miblen = sizeof(mib) / sizeof(size_t); \
30 size_t sz = sizeof(t); \
31 xmallctlnametomib(n, mib, &miblen); \
34 xmallctlbymib(mib, miblen, v, &sz, NULL, 0); \
37 /******************************************************************************/
40 bool opt_stats_print = false;
42 size_t stats_cactive = 0;
44 /******************************************************************************/
45 /* Function prototypes for non-inline static functions. */
47 static void stats_arena_bins_print(void (*write_cb)(void *, const char *),
48 void *cbopaque, unsigned i);
49 static void stats_arena_lruns_print(void (*write_cb)(void *, const char *),
50 void *cbopaque, unsigned i);
51 static void stats_arena_print(void (*write_cb)(void *, const char *),
52 void *cbopaque, unsigned i, bool bins, bool large);
54 /******************************************************************************/
57 stats_arena_bins_print(void (*write_cb)(void *, const char *), void *cbopaque,
62 unsigned nbins, j, gap_start;
64 CTL_GET("arenas.page", &page, size_t);
66 CTL_GET("config.tcache", &config_tcache, bool);
68 malloc_cprintf(write_cb, cbopaque,
69 "bins: bin size regs pgs allocated nmalloc"
70 " ndalloc nrequests nfills nflushes"
71 " newruns reruns curruns\n");
73 malloc_cprintf(write_cb, cbopaque,
74 "bins: bin size regs pgs allocated nmalloc"
75 " ndalloc newruns reruns curruns\n");
77 CTL_GET("arenas.nbins", &nbins, unsigned);
78 for (j = 0, gap_start = UINT_MAX; j < nbins; j++) {
81 CTL_IJ_GET("stats.arenas.0.bins.0.nruns", &nruns, uint64_t);
83 if (gap_start == UINT_MAX)
86 size_t reg_size, run_size, allocated;
88 uint64_t nmalloc, ndalloc, nrequests, nfills, nflushes;
92 if (gap_start != UINT_MAX) {
93 if (j > gap_start + 1) {
94 /* Gap of more than one size class. */
95 malloc_cprintf(write_cb, cbopaque,
96 "[%u..%u]\n", gap_start,
99 /* Gap of one size class. */
100 malloc_cprintf(write_cb, cbopaque,
101 "[%u]\n", gap_start);
103 gap_start = UINT_MAX;
105 CTL_J_GET("arenas.bin.0.size", ®_size, size_t);
106 CTL_J_GET("arenas.bin.0.nregs", &nregs, uint32_t);
107 CTL_J_GET("arenas.bin.0.run_size", &run_size, size_t);
108 CTL_IJ_GET("stats.arenas.0.bins.0.allocated",
110 CTL_IJ_GET("stats.arenas.0.bins.0.nmalloc",
112 CTL_IJ_GET("stats.arenas.0.bins.0.ndalloc",
115 CTL_IJ_GET("stats.arenas.0.bins.0.nrequests",
116 &nrequests, uint64_t);
117 CTL_IJ_GET("stats.arenas.0.bins.0.nfills",
119 CTL_IJ_GET("stats.arenas.0.bins.0.nflushes",
120 &nflushes, uint64_t);
122 CTL_IJ_GET("stats.arenas.0.bins.0.nreruns", &reruns,
124 CTL_IJ_GET("stats.arenas.0.bins.0.curruns", &curruns,
127 malloc_cprintf(write_cb, cbopaque,
128 "%13u %5zu %4u %3zu %12zu %12"PRIu64
129 " %12"PRIu64" %12"PRIu64" %12"PRIu64
130 " %12"PRIu64" %12"PRIu64" %12"PRIu64
132 j, reg_size, nregs, run_size / page,
133 allocated, nmalloc, ndalloc, nrequests,
134 nfills, nflushes, nruns, reruns, curruns);
136 malloc_cprintf(write_cb, cbopaque,
137 "%13u %5zu %4u %3zu %12zu %12"PRIu64
138 " %12"PRIu64" %12"PRIu64" %12"PRIu64
140 j, reg_size, nregs, run_size / page,
141 allocated, nmalloc, ndalloc, nruns, reruns,
146 if (gap_start != UINT_MAX) {
147 if (j > gap_start + 1) {
148 /* Gap of more than one size class. */
149 malloc_cprintf(write_cb, cbopaque, "[%u..%u]\n",
152 /* Gap of one size class. */
153 malloc_cprintf(write_cb, cbopaque, "[%u]\n", gap_start);
159 stats_arena_lruns_print(void (*write_cb)(void *, const char *), void *cbopaque,
162 size_t page, nlruns, j;
165 CTL_GET("arenas.page", &page, size_t);
167 malloc_cprintf(write_cb, cbopaque,
168 "large: size pages nmalloc ndalloc nrequests"
170 CTL_GET("arenas.nlruns", &nlruns, size_t);
171 for (j = 0, gap_start = -1; j < nlruns; j++) {
172 uint64_t nmalloc, ndalloc, nrequests;
173 size_t run_size, curruns;
175 CTL_IJ_GET("stats.arenas.0.lruns.0.nmalloc", &nmalloc,
177 CTL_IJ_GET("stats.arenas.0.lruns.0.ndalloc", &ndalloc,
179 CTL_IJ_GET("stats.arenas.0.lruns.0.nrequests", &nrequests,
181 if (nrequests == 0) {
185 CTL_J_GET("arenas.lrun.0.size", &run_size, size_t);
186 CTL_IJ_GET("stats.arenas.0.lruns.0.curruns", &curruns,
188 if (gap_start != -1) {
189 malloc_cprintf(write_cb, cbopaque, "[%zu]\n",
193 malloc_cprintf(write_cb, cbopaque,
194 "%13zu %5zu %12"PRIu64" %12"PRIu64" %12"PRIu64
196 run_size, run_size / page, nmalloc, ndalloc,
201 malloc_cprintf(write_cb, cbopaque, "[%zu]\n", j - gap_start);
205 stats_arena_print(void (*write_cb)(void *, const char *), void *cbopaque,
206 unsigned i, bool bins, bool large)
210 size_t page, pactive, pdirty, mapped;
211 uint64_t npurge, nmadvise, purged;
212 size_t small_allocated;
213 uint64_t small_nmalloc, small_ndalloc, small_nrequests;
214 size_t large_allocated;
215 uint64_t large_nmalloc, large_ndalloc, large_nrequests;
217 CTL_GET("arenas.page", &page, size_t);
219 CTL_I_GET("stats.arenas.0.nthreads", &nthreads, unsigned);
220 malloc_cprintf(write_cb, cbopaque,
221 "assigned threads: %u\n", nthreads);
222 CTL_I_GET("stats.arenas.0.dss", &dss, const char *);
223 malloc_cprintf(write_cb, cbopaque, "dss allocation precedence: %s\n",
225 CTL_I_GET("stats.arenas.0.pactive", &pactive, size_t);
226 CTL_I_GET("stats.arenas.0.pdirty", &pdirty, size_t);
227 CTL_I_GET("stats.arenas.0.npurge", &npurge, uint64_t);
228 CTL_I_GET("stats.arenas.0.nmadvise", &nmadvise, uint64_t);
229 CTL_I_GET("stats.arenas.0.purged", &purged, uint64_t);
230 malloc_cprintf(write_cb, cbopaque,
231 "dirty pages: %zu:%zu active:dirty, %"PRIu64" sweep%s,"
232 " %"PRIu64" madvise%s, %"PRIu64" purged\n",
233 pactive, pdirty, npurge, npurge == 1 ? "" : "s",
234 nmadvise, nmadvise == 1 ? "" : "s", purged);
236 malloc_cprintf(write_cb, cbopaque,
237 " allocated nmalloc ndalloc nrequests\n");
238 CTL_I_GET("stats.arenas.0.small.allocated", &small_allocated, size_t);
239 CTL_I_GET("stats.arenas.0.small.nmalloc", &small_nmalloc, uint64_t);
240 CTL_I_GET("stats.arenas.0.small.ndalloc", &small_ndalloc, uint64_t);
241 CTL_I_GET("stats.arenas.0.small.nrequests", &small_nrequests, uint64_t);
242 malloc_cprintf(write_cb, cbopaque,
243 "small: %12zu %12"PRIu64" %12"PRIu64" %12"PRIu64"\n",
244 small_allocated, small_nmalloc, small_ndalloc, small_nrequests);
245 CTL_I_GET("stats.arenas.0.large.allocated", &large_allocated, size_t);
246 CTL_I_GET("stats.arenas.0.large.nmalloc", &large_nmalloc, uint64_t);
247 CTL_I_GET("stats.arenas.0.large.ndalloc", &large_ndalloc, uint64_t);
248 CTL_I_GET("stats.arenas.0.large.nrequests", &large_nrequests, uint64_t);
249 malloc_cprintf(write_cb, cbopaque,
250 "large: %12zu %12"PRIu64" %12"PRIu64" %12"PRIu64"\n",
251 large_allocated, large_nmalloc, large_ndalloc, large_nrequests);
252 malloc_cprintf(write_cb, cbopaque,
253 "total: %12zu %12"PRIu64" %12"PRIu64" %12"PRIu64"\n",
254 small_allocated + large_allocated,
255 small_nmalloc + large_nmalloc,
256 small_ndalloc + large_ndalloc,
257 small_nrequests + large_nrequests);
258 malloc_cprintf(write_cb, cbopaque, "active: %12zu\n", pactive * page);
259 CTL_I_GET("stats.arenas.0.mapped", &mapped, size_t);
260 malloc_cprintf(write_cb, cbopaque, "mapped: %12zu\n", mapped);
263 stats_arena_bins_print(write_cb, cbopaque, i);
265 stats_arena_lruns_print(write_cb, cbopaque, i);
269 stats_print(void (*write_cb)(void *, const char *), void *cbopaque,
277 bool unmerged = true;
282 * Refresh stats, in case mallctl() was called by the application.
284 * Check for OOM here, since refreshing the ctl cache can trigger
285 * allocation. In practice, none of the subsequent mallctl()-related
286 * calls in this function will cause OOM if this one succeeds.
289 u64sz = sizeof(uint64_t);
290 err = je_mallctl("epoch", &epoch, &u64sz, &epoch, sizeof(uint64_t));
293 malloc_write("<jemalloc>: Memory allocation failure in "
294 "mallctl(\"epoch\", ...)\n");
297 malloc_write("<jemalloc>: Failure in mallctl(\"epoch\", "
305 for (i = 0; opts[i] != '\0'; i++) {
327 malloc_cprintf(write_cb, cbopaque,
328 "___ Begin jemalloc statistics ___\n");
335 size_t sv, bsz, ssz, sssz, cpsz;
338 ssz = sizeof(size_t);
339 sssz = sizeof(ssize_t);
340 cpsz = sizeof(const char *);
342 CTL_GET("version", &cpv, const char *);
343 malloc_cprintf(write_cb, cbopaque, "Version: %s\n", cpv);
344 CTL_GET("config.debug", &bv, bool);
345 malloc_cprintf(write_cb, cbopaque, "Assertions %s\n",
346 bv ? "enabled" : "disabled");
348 #define OPT_WRITE_BOOL(n) \
349 if ((err = je_mallctl("opt."#n, &bv, &bsz, NULL, 0)) \
351 malloc_cprintf(write_cb, cbopaque, \
352 " opt."#n": %s\n", bv ? "true" : "false"); \
354 #define OPT_WRITE_SIZE_T(n) \
355 if ((err = je_mallctl("opt."#n, &sv, &ssz, NULL, 0)) \
357 malloc_cprintf(write_cb, cbopaque, \
358 " opt."#n": %zu\n", sv); \
360 #define OPT_WRITE_SSIZE_T(n) \
361 if ((err = je_mallctl("opt."#n, &ssv, &sssz, NULL, 0)) \
363 malloc_cprintf(write_cb, cbopaque, \
364 " opt."#n": %zd\n", ssv); \
366 #define OPT_WRITE_CHAR_P(n) \
367 if ((err = je_mallctl("opt."#n, &cpv, &cpsz, NULL, 0)) \
369 malloc_cprintf(write_cb, cbopaque, \
370 " opt."#n": \"%s\"\n", cpv); \
373 malloc_cprintf(write_cb, cbopaque,
374 "Run-time option settings:\n");
375 OPT_WRITE_BOOL(abort)
376 OPT_WRITE_SIZE_T(lg_chunk)
377 OPT_WRITE_CHAR_P(dss)
378 OPT_WRITE_SIZE_T(narenas)
379 OPT_WRITE_SSIZE_T(lg_dirty_mult)
380 OPT_WRITE_BOOL(stats_print)
382 OPT_WRITE_SIZE_T(quarantine)
383 OPT_WRITE_BOOL(redzone)
385 OPT_WRITE_BOOL(utrace)
386 OPT_WRITE_BOOL(valgrind)
387 OPT_WRITE_BOOL(xmalloc)
388 OPT_WRITE_BOOL(tcache)
389 OPT_WRITE_SSIZE_T(lg_tcache_max)
391 OPT_WRITE_CHAR_P(prof_prefix)
392 OPT_WRITE_BOOL(prof_active)
393 OPT_WRITE_SSIZE_T(lg_prof_sample)
394 OPT_WRITE_BOOL(prof_accum)
395 OPT_WRITE_SSIZE_T(lg_prof_interval)
396 OPT_WRITE_BOOL(prof_gdump)
397 OPT_WRITE_BOOL(prof_final)
398 OPT_WRITE_BOOL(prof_leak)
400 #undef OPT_WRITE_BOOL
401 #undef OPT_WRITE_SIZE_T
402 #undef OPT_WRITE_SSIZE_T
403 #undef OPT_WRITE_CHAR_P
405 malloc_cprintf(write_cb, cbopaque, "CPUs: %u\n", ncpus);
407 CTL_GET("arenas.narenas", &uv, unsigned);
408 malloc_cprintf(write_cb, cbopaque, "Arenas: %u\n", uv);
410 malloc_cprintf(write_cb, cbopaque, "Pointer size: %zu\n",
413 CTL_GET("arenas.quantum", &sv, size_t);
414 malloc_cprintf(write_cb, cbopaque, "Quantum size: %zu\n", sv);
416 CTL_GET("arenas.page", &sv, size_t);
417 malloc_cprintf(write_cb, cbopaque, "Page size: %zu\n", sv);
419 CTL_GET("opt.lg_dirty_mult", &ssv, ssize_t);
421 malloc_cprintf(write_cb, cbopaque,
422 "Min active:dirty page ratio per arena: %u:1\n",
425 malloc_cprintf(write_cb, cbopaque,
426 "Min active:dirty page ratio per arena: N/A\n");
428 if ((err = je_mallctl("arenas.tcache_max", &sv, &ssz, NULL, 0))
430 malloc_cprintf(write_cb, cbopaque,
431 "Maximum thread-cached size class: %zu\n", sv);
433 if ((err = je_mallctl("opt.prof", &bv, &bsz, NULL, 0)) == 0 &&
435 CTL_GET("opt.lg_prof_sample", &sv, size_t);
436 malloc_cprintf(write_cb, cbopaque,
437 "Average profile sample interval: %"PRIu64
438 " (2^%zu)\n", (((uint64_t)1U) << sv), sv);
440 CTL_GET("opt.lg_prof_interval", &ssv, ssize_t);
442 malloc_cprintf(write_cb, cbopaque,
443 "Average profile dump interval: %"PRIu64
445 (((uint64_t)1U) << ssv), ssv);
447 malloc_cprintf(write_cb, cbopaque,
448 "Average profile dump interval: N/A\n");
451 CTL_GET("opt.lg_chunk", &sv, size_t);
452 malloc_cprintf(write_cb, cbopaque, "Chunk size: %zu (2^%zu)\n",
458 size_t allocated, active, mapped;
459 size_t chunks_current, chunks_high;
460 uint64_t chunks_total;
461 size_t huge_allocated;
462 uint64_t huge_nmalloc, huge_ndalloc;
464 CTL_GET("stats.cactive", &cactive, size_t *);
465 CTL_GET("stats.allocated", &allocated, size_t);
466 CTL_GET("stats.active", &active, size_t);
467 CTL_GET("stats.mapped", &mapped, size_t);
468 malloc_cprintf(write_cb, cbopaque,
469 "Allocated: %zu, active: %zu, mapped: %zu\n",
470 allocated, active, mapped);
471 malloc_cprintf(write_cb, cbopaque,
472 "Current active ceiling: %zu\n", atomic_read_z(cactive));
474 /* Print chunk stats. */
475 CTL_GET("stats.chunks.total", &chunks_total, uint64_t);
476 CTL_GET("stats.chunks.high", &chunks_high, size_t);
477 CTL_GET("stats.chunks.current", &chunks_current, size_t);
478 malloc_cprintf(write_cb, cbopaque, "chunks: nchunks "
479 "highchunks curchunks\n");
480 malloc_cprintf(write_cb, cbopaque,
481 " %13"PRIu64" %12zu %12zu\n",
482 chunks_total, chunks_high, chunks_current);
484 /* Print huge stats. */
485 CTL_GET("stats.huge.nmalloc", &huge_nmalloc, uint64_t);
486 CTL_GET("stats.huge.ndalloc", &huge_ndalloc, uint64_t);
487 CTL_GET("stats.huge.allocated", &huge_allocated, size_t);
488 malloc_cprintf(write_cb, cbopaque,
489 "huge: nmalloc ndalloc allocated\n");
490 malloc_cprintf(write_cb, cbopaque,
491 " %12"PRIu64" %12"PRIu64" %12zu\n",
492 huge_nmalloc, huge_ndalloc, huge_allocated);
497 CTL_GET("arenas.narenas", &narenas, unsigned);
499 VARIABLE_ARRAY(bool, initialized, narenas);
501 unsigned i, ninitialized;
503 isz = sizeof(bool) * narenas;
504 xmallctl("arenas.initialized", initialized,
506 for (i = ninitialized = 0; i < narenas; i++) {
511 if (ninitialized > 1 || unmerged == false) {
512 /* Print merged arena stats. */
513 malloc_cprintf(write_cb, cbopaque,
514 "\nMerged arenas stats:\n");
515 stats_arena_print(write_cb, cbopaque,
516 narenas, bins, large);
524 /* Print stats for each arena. */
526 CTL_GET("arenas.narenas", &narenas, unsigned);
528 VARIABLE_ARRAY(bool, initialized, narenas);
532 isz = sizeof(bool) * narenas;
533 xmallctl("arenas.initialized", initialized,
536 for (i = 0; i < narenas; i++) {
537 if (initialized[i]) {
538 malloc_cprintf(write_cb,
540 "\narenas[%u]:\n", i);
541 stats_arena_print(write_cb,
542 cbopaque, i, bins, large);
548 malloc_cprintf(write_cb, cbopaque, "--- End jemalloc statistics ---\n");