]>
git.saurik.com Git - redis.git/blob - deps/jemalloc/src/stats.c
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
)
209 size_t page
, pactive
, pdirty
, mapped
;
210 uint64_t npurge
, nmadvise
, purged
;
211 size_t small_allocated
;
212 uint64_t small_nmalloc
, small_ndalloc
, small_nrequests
;
213 size_t large_allocated
;
214 uint64_t large_nmalloc
, large_ndalloc
, large_nrequests
;
216 CTL_GET("arenas.page", &page
, size_t);
218 CTL_I_GET("stats.arenas.0.nthreads", &nthreads
, unsigned);
219 malloc_cprintf(write_cb
, cbopaque
,
220 "assigned threads: %u\n", nthreads
);
221 CTL_I_GET("stats.arenas.0.pactive", &pactive
, size_t);
222 CTL_I_GET("stats.arenas.0.pdirty", &pdirty
, size_t);
223 CTL_I_GET("stats.arenas.0.npurge", &npurge
, uint64_t);
224 CTL_I_GET("stats.arenas.0.nmadvise", &nmadvise
, uint64_t);
225 CTL_I_GET("stats.arenas.0.purged", &purged
, uint64_t);
226 malloc_cprintf(write_cb
, cbopaque
,
227 "dirty pages: %zu:%zu active:dirty, %"PRIu64
" sweep%s,"
228 " %"PRIu64
" madvise%s, %"PRIu64
" purged\n",
229 pactive
, pdirty
, npurge
, npurge
== 1 ? "" : "s",
230 nmadvise
, nmadvise
== 1 ? "" : "s", purged
);
232 malloc_cprintf(write_cb
, cbopaque
,
233 " allocated nmalloc ndalloc nrequests\n");
234 CTL_I_GET("stats.arenas.0.small.allocated", &small_allocated
, size_t);
235 CTL_I_GET("stats.arenas.0.small.nmalloc", &small_nmalloc
, uint64_t);
236 CTL_I_GET("stats.arenas.0.small.ndalloc", &small_ndalloc
, uint64_t);
237 CTL_I_GET("stats.arenas.0.small.nrequests", &small_nrequests
, uint64_t);
238 malloc_cprintf(write_cb
, cbopaque
,
239 "small: %12zu %12"PRIu64
" %12"PRIu64
" %12"PRIu64
"\n",
240 small_allocated
, small_nmalloc
, small_ndalloc
, small_nrequests
);
241 CTL_I_GET("stats.arenas.0.large.allocated", &large_allocated
, size_t);
242 CTL_I_GET("stats.arenas.0.large.nmalloc", &large_nmalloc
, uint64_t);
243 CTL_I_GET("stats.arenas.0.large.ndalloc", &large_ndalloc
, uint64_t);
244 CTL_I_GET("stats.arenas.0.large.nrequests", &large_nrequests
, uint64_t);
245 malloc_cprintf(write_cb
, cbopaque
,
246 "large: %12zu %12"PRIu64
" %12"PRIu64
" %12"PRIu64
"\n",
247 large_allocated
, large_nmalloc
, large_ndalloc
, large_nrequests
);
248 malloc_cprintf(write_cb
, cbopaque
,
249 "total: %12zu %12"PRIu64
" %12"PRIu64
" %12"PRIu64
"\n",
250 small_allocated
+ large_allocated
,
251 small_nmalloc
+ large_nmalloc
,
252 small_ndalloc
+ large_ndalloc
,
253 small_nrequests
+ large_nrequests
);
254 malloc_cprintf(write_cb
, cbopaque
, "active: %12zu\n", pactive
* page
);
255 CTL_I_GET("stats.arenas.0.mapped", &mapped
, size_t);
256 malloc_cprintf(write_cb
, cbopaque
, "mapped: %12zu\n", mapped
);
259 stats_arena_bins_print(write_cb
, cbopaque
, i
);
261 stats_arena_lruns_print(write_cb
, cbopaque
, i
);
265 stats_print(void (*write_cb
)(void *, const char *), void *cbopaque
,
273 bool unmerged
= true;
278 * Refresh stats, in case mallctl() was called by the application.
280 * Check for OOM here, since refreshing the ctl cache can trigger
281 * allocation. In practice, none of the subsequent mallctl()-related
282 * calls in this function will cause OOM if this one succeeds.
285 u64sz
= sizeof(uint64_t);
286 err
= je_mallctl("epoch", &epoch
, &u64sz
, &epoch
, sizeof(uint64_t));
289 malloc_write("<jemalloc>: Memory allocation failure in "
290 "mallctl(\"epoch\", ...)\n");
293 malloc_write("<jemalloc>: Failure in mallctl(\"epoch\", "
301 for (i
= 0; opts
[i
] != '\0'; i
++) {
323 malloc_cprintf(write_cb
, cbopaque
,
324 "___ Begin jemalloc statistics ___\n");
331 size_t sv
, bsz
, ssz
, sssz
, cpsz
;
334 ssz
= sizeof(size_t);
335 sssz
= sizeof(ssize_t
);
336 cpsz
= sizeof(const char *);
338 CTL_GET("version", &cpv
, const char *);
339 malloc_cprintf(write_cb
, cbopaque
, "Version: %s\n", cpv
);
340 CTL_GET("config.debug", &bv
, bool);
341 malloc_cprintf(write_cb
, cbopaque
, "Assertions %s\n",
342 bv
? "enabled" : "disabled");
344 #define OPT_WRITE_BOOL(n) \
345 if ((err = je_mallctl("opt."#n, &bv, &bsz, NULL, 0)) \
347 malloc_cprintf(write_cb, cbopaque, \
348 " opt."#n": %s\n", bv ? "true" : "false"); \
350 #define OPT_WRITE_SIZE_T(n) \
351 if ((err = je_mallctl("opt."#n, &sv, &ssz, NULL, 0)) \
353 malloc_cprintf(write_cb, cbopaque, \
354 " opt."#n": %zu\n", sv); \
356 #define OPT_WRITE_SSIZE_T(n) \
357 if ((err = je_mallctl("opt."#n, &ssv, &sssz, NULL, 0)) \
359 malloc_cprintf(write_cb, cbopaque, \
360 " opt."#n": %zd\n", ssv); \
362 #define OPT_WRITE_CHAR_P(n) \
363 if ((err = je_mallctl("opt."#n, &cpv, &cpsz, NULL, 0)) \
365 malloc_cprintf(write_cb, cbopaque, \
366 " opt."#n": \"%s\"\n", cpv); \
369 malloc_cprintf(write_cb
, cbopaque
,
370 "Run-time option settings:\n");
371 OPT_WRITE_BOOL(abort
)
372 OPT_WRITE_SIZE_T(lg_chunk
)
373 OPT_WRITE_SIZE_T(narenas
)
374 OPT_WRITE_SSIZE_T(lg_dirty_mult
)
375 OPT_WRITE_BOOL(stats_print
)
377 OPT_WRITE_SIZE_T(quarantine
)
378 OPT_WRITE_BOOL(redzone
)
380 OPT_WRITE_BOOL(utrace
)
381 OPT_WRITE_BOOL(valgrind
)
382 OPT_WRITE_BOOL(xmalloc
)
383 OPT_WRITE_BOOL(tcache
)
384 OPT_WRITE_SSIZE_T(lg_tcache_max
)
386 OPT_WRITE_CHAR_P(prof_prefix
)
387 OPT_WRITE_BOOL(prof_active
)
388 OPT_WRITE_SSIZE_T(lg_prof_sample
)
389 OPT_WRITE_BOOL(prof_accum
)
390 OPT_WRITE_SSIZE_T(lg_prof_interval
)
391 OPT_WRITE_BOOL(prof_gdump
)
392 OPT_WRITE_BOOL(prof_final
)
393 OPT_WRITE_BOOL(prof_leak
)
395 #undef OPT_WRITE_BOOL
396 #undef OPT_WRITE_SIZE_T
397 #undef OPT_WRITE_SSIZE_T
398 #undef OPT_WRITE_CHAR_P
400 malloc_cprintf(write_cb
, cbopaque
, "CPUs: %u\n", ncpus
);
402 CTL_GET("arenas.narenas", &uv
, unsigned);
403 malloc_cprintf(write_cb
, cbopaque
, "Max arenas: %u\n", uv
);
405 malloc_cprintf(write_cb
, cbopaque
, "Pointer size: %zu\n",
408 CTL_GET("arenas.quantum", &sv
, size_t);
409 malloc_cprintf(write_cb
, cbopaque
, "Quantum size: %zu\n", sv
);
411 CTL_GET("arenas.page", &sv
, size_t);
412 malloc_cprintf(write_cb
, cbopaque
, "Page size: %zu\n", sv
);
414 CTL_GET("opt.lg_dirty_mult", &ssv
, ssize_t
);
416 malloc_cprintf(write_cb
, cbopaque
,
417 "Min active:dirty page ratio per arena: %u:1\n",
420 malloc_cprintf(write_cb
, cbopaque
,
421 "Min active:dirty page ratio per arena: N/A\n");
423 if ((err
= je_mallctl("arenas.tcache_max", &sv
, &ssz
, NULL
, 0))
425 malloc_cprintf(write_cb
, cbopaque
,
426 "Maximum thread-cached size class: %zu\n", sv
);
428 if ((err
= je_mallctl("opt.prof", &bv
, &bsz
, NULL
, 0)) == 0 &&
430 CTL_GET("opt.lg_prof_sample", &sv
, size_t);
431 malloc_cprintf(write_cb
, cbopaque
,
432 "Average profile sample interval: %"PRIu64
433 " (2^%zu)\n", (((uint64_t)1U) << sv
), sv
);
435 CTL_GET("opt.lg_prof_interval", &ssv
, ssize_t
);
437 malloc_cprintf(write_cb
, cbopaque
,
438 "Average profile dump interval: %"PRIu64
440 (((uint64_t)1U) << ssv
), ssv
);
442 malloc_cprintf(write_cb
, cbopaque
,
443 "Average profile dump interval: N/A\n");
446 CTL_GET("opt.lg_chunk", &sv
, size_t);
447 malloc_cprintf(write_cb
, cbopaque
, "Chunk size: %zu (2^%zu)\n",
453 size_t allocated
, active
, mapped
;
454 size_t chunks_current
, chunks_high
;
455 uint64_t chunks_total
;
456 size_t huge_allocated
;
457 uint64_t huge_nmalloc
, huge_ndalloc
;
459 CTL_GET("stats.cactive", &cactive
, size_t *);
460 CTL_GET("stats.allocated", &allocated
, size_t);
461 CTL_GET("stats.active", &active
, size_t);
462 CTL_GET("stats.mapped", &mapped
, size_t);
463 malloc_cprintf(write_cb
, cbopaque
,
464 "Allocated: %zu, active: %zu, mapped: %zu\n",
465 allocated
, active
, mapped
);
466 malloc_cprintf(write_cb
, cbopaque
,
467 "Current active ceiling: %zu\n", atomic_read_z(cactive
));
469 /* Print chunk stats. */
470 CTL_GET("stats.chunks.total", &chunks_total
, uint64_t);
471 CTL_GET("stats.chunks.high", &chunks_high
, size_t);
472 CTL_GET("stats.chunks.current", &chunks_current
, size_t);
473 malloc_cprintf(write_cb
, cbopaque
, "chunks: nchunks "
474 "highchunks curchunks\n");
475 malloc_cprintf(write_cb
, cbopaque
, " %13"PRIu64
"%13zu%13zu\n",
476 chunks_total
, chunks_high
, chunks_current
);
478 /* Print huge stats. */
479 CTL_GET("stats.huge.nmalloc", &huge_nmalloc
, uint64_t);
480 CTL_GET("stats.huge.ndalloc", &huge_ndalloc
, uint64_t);
481 CTL_GET("stats.huge.allocated", &huge_allocated
, size_t);
482 malloc_cprintf(write_cb
, cbopaque
,
483 "huge: nmalloc ndalloc allocated\n");
484 malloc_cprintf(write_cb
, cbopaque
,
485 " %12"PRIu64
" %12"PRIu64
" %12zu\n",
486 huge_nmalloc
, huge_ndalloc
, huge_allocated
);
491 CTL_GET("arenas.narenas", &narenas
, unsigned);
493 VARIABLE_ARRAY(bool, initialized
, narenas
);
495 unsigned i
, ninitialized
;
497 isz
= sizeof(bool) * narenas
;
498 xmallctl("arenas.initialized", initialized
,
500 for (i
= ninitialized
= 0; i
< narenas
; i
++) {
505 if (ninitialized
> 1 || unmerged
== false) {
506 /* Print merged arena stats. */
507 malloc_cprintf(write_cb
, cbopaque
,
508 "\nMerged arenas stats:\n");
509 stats_arena_print(write_cb
, cbopaque
,
510 narenas
, bins
, large
);
518 /* Print stats for each arena. */
520 CTL_GET("arenas.narenas", &narenas
, unsigned);
522 VARIABLE_ARRAY(bool, initialized
, narenas
);
526 isz
= sizeof(bool) * narenas
;
527 xmallctl("arenas.initialized", initialized
,
530 for (i
= 0; i
< narenas
; i
++) {
531 if (initialized
[i
]) {
532 malloc_cprintf(write_cb
,
534 "\narenas[%u]:\n", i
);
535 stats_arena_print(write_cb
,
536 cbopaque
, i
, bins
, large
);
542 malloc_cprintf(write_cb
, cbopaque
, "--- End jemalloc statistics ---\n");