]>
git.proxmox.com Git - rustc.git/blob - src/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
)
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
;
216 size_t huge_allocated
;
217 uint64_t huge_nmalloc
, huge_ndalloc
, huge_nrequests
;
219 CTL_GET("arenas.page", &page
, size_t);
221 CTL_I_GET("stats.arenas.0.nthreads", &nthreads
, unsigned);
222 malloc_cprintf(write_cb
, cbopaque
,
223 "assigned threads: %u\n", nthreads
);
224 CTL_I_GET("stats.arenas.0.dss", &dss
, const char *);
225 malloc_cprintf(write_cb
, cbopaque
, "dss allocation precedence: %s\n",
227 CTL_I_GET("stats.arenas.0.pactive", &pactive
, size_t);
228 CTL_I_GET("stats.arenas.0.pdirty", &pdirty
, size_t);
229 CTL_I_GET("stats.arenas.0.npurge", &npurge
, uint64_t);
230 CTL_I_GET("stats.arenas.0.nmadvise", &nmadvise
, uint64_t);
231 CTL_I_GET("stats.arenas.0.purged", &purged
, uint64_t);
232 malloc_cprintf(write_cb
, cbopaque
,
233 "dirty pages: %zu:%zu active:dirty, %"PRIu64
" sweep%s,"
234 " %"PRIu64
" madvise%s, %"PRIu64
" purged\n",
235 pactive
, pdirty
, npurge
, npurge
== 1 ? "" : "s",
236 nmadvise
, nmadvise
== 1 ? "" : "s", purged
);
238 malloc_cprintf(write_cb
, cbopaque
,
239 " allocated nmalloc ndalloc nrequests\n");
240 CTL_I_GET("stats.arenas.0.small.allocated", &small_allocated
, size_t);
241 CTL_I_GET("stats.arenas.0.small.nmalloc", &small_nmalloc
, uint64_t);
242 CTL_I_GET("stats.arenas.0.small.ndalloc", &small_ndalloc
, uint64_t);
243 CTL_I_GET("stats.arenas.0.small.nrequests", &small_nrequests
, uint64_t);
244 malloc_cprintf(write_cb
, cbopaque
,
245 "small: %12zu %12"PRIu64
" %12"PRIu64
" %12"PRIu64
"\n",
246 small_allocated
, small_nmalloc
, small_ndalloc
, small_nrequests
);
247 CTL_I_GET("stats.arenas.0.large.allocated", &large_allocated
, size_t);
248 CTL_I_GET("stats.arenas.0.large.nmalloc", &large_nmalloc
, uint64_t);
249 CTL_I_GET("stats.arenas.0.large.ndalloc", &large_ndalloc
, uint64_t);
250 CTL_I_GET("stats.arenas.0.large.nrequests", &large_nrequests
, uint64_t);
251 malloc_cprintf(write_cb
, cbopaque
,
252 "large: %12zu %12"PRIu64
" %12"PRIu64
" %12"PRIu64
"\n",
253 large_allocated
, large_nmalloc
, large_ndalloc
, large_nrequests
);
254 CTL_I_GET("stats.arenas.0.huge.allocated", &huge_allocated
, size_t);
255 CTL_I_GET("stats.arenas.0.huge.nmalloc", &huge_nmalloc
, uint64_t);
256 CTL_I_GET("stats.arenas.0.huge.ndalloc", &huge_ndalloc
, uint64_t);
257 CTL_I_GET("stats.arenas.0.huge.nrequests", &huge_nrequests
, uint64_t);
258 malloc_cprintf(write_cb
, cbopaque
,
259 "huge: %12zu %12"PRIu64
" %12"PRIu64
" %12"PRIu64
"\n",
260 huge_allocated
, huge_nmalloc
, huge_ndalloc
, huge_nrequests
);
261 malloc_cprintf(write_cb
, cbopaque
,
262 "total: %12zu %12"PRIu64
" %12"PRIu64
" %12"PRIu64
"\n",
263 small_allocated
+ large_allocated
+ huge_allocated
,
264 small_nmalloc
+ large_nmalloc
+ huge_nmalloc
,
265 small_ndalloc
+ large_ndalloc
+ huge_ndalloc
,
266 small_nrequests
+ large_nrequests
+ huge_nrequests
);
267 malloc_cprintf(write_cb
, cbopaque
, "active: %12zu\n", pactive
* page
);
268 CTL_I_GET("stats.arenas.0.mapped", &mapped
, size_t);
269 malloc_cprintf(write_cb
, cbopaque
, "mapped: %12zu\n", mapped
);
272 stats_arena_bins_print(write_cb
, cbopaque
, i
);
274 stats_arena_lruns_print(write_cb
, cbopaque
, i
);
278 stats_print(void (*write_cb
)(void *, const char *), void *cbopaque
,
286 bool unmerged
= true;
291 * Refresh stats, in case mallctl() was called by the application.
293 * Check for OOM here, since refreshing the ctl cache can trigger
294 * allocation. In practice, none of the subsequent mallctl()-related
295 * calls in this function will cause OOM if this one succeeds.
298 u64sz
= sizeof(uint64_t);
299 err
= je_mallctl("epoch", &epoch
, &u64sz
, &epoch
, sizeof(uint64_t));
302 malloc_write("<jemalloc>: Memory allocation failure in "
303 "mallctl(\"epoch\", ...)\n");
306 malloc_write("<jemalloc>: Failure in mallctl(\"epoch\", "
314 for (i
= 0; opts
[i
] != '\0'; i
++) {
336 malloc_cprintf(write_cb
, cbopaque
,
337 "___ Begin jemalloc statistics ___\n");
343 size_t sv
, bsz
, ssz
, sssz
, cpsz
;
346 ssz
= sizeof(size_t);
347 sssz
= sizeof(ssize_t
);
348 cpsz
= sizeof(const char *);
350 CTL_GET("version", &cpv
, const char *);
351 malloc_cprintf(write_cb
, cbopaque
, "Version: %s\n", cpv
);
352 CTL_GET("config.debug", &bv
, bool);
353 malloc_cprintf(write_cb
, cbopaque
, "Assertions %s\n",
354 bv
? "enabled" : "disabled");
356 #define OPT_WRITE_BOOL(n) \
357 if (je_mallctl("opt."#n, &bv, &bsz, NULL, 0) == 0) { \
358 malloc_cprintf(write_cb, cbopaque, \
359 " opt."#n": %s\n", bv ? "true" : "false"); \
361 #define OPT_WRITE_BOOL_MUTABLE(n, m) { \
363 if (je_mallctl("opt."#n, &bv, &bsz, NULL, 0) == 0 && \
364 je_mallctl(#m, &bv2, &bsz, NULL, 0) == 0) { \
365 malloc_cprintf(write_cb, cbopaque, \
366 " opt."#n": %s ("#m": %s)\n", bv ? "true" \
367 : "false", bv2 ? "true" : "false"); \
370 #define OPT_WRITE_SIZE_T(n) \
371 if (je_mallctl("opt."#n, &sv, &ssz, NULL, 0) == 0) { \
372 malloc_cprintf(write_cb, cbopaque, \
373 " opt."#n": %zu\n", sv); \
375 #define OPT_WRITE_SSIZE_T(n) \
376 if (je_mallctl("opt."#n, &ssv, &sssz, NULL, 0) == 0) { \
377 malloc_cprintf(write_cb, cbopaque, \
378 " opt."#n": %zd\n", ssv); \
380 #define OPT_WRITE_CHAR_P(n) \
381 if (je_mallctl("opt."#n, &cpv, &cpsz, NULL, 0) == 0) { \
382 malloc_cprintf(write_cb, cbopaque, \
383 " opt."#n": \"%s\"\n", cpv); \
386 malloc_cprintf(write_cb
, cbopaque
,
387 "Run-time option settings:\n");
388 OPT_WRITE_BOOL(abort
)
389 OPT_WRITE_SIZE_T(lg_chunk
)
390 OPT_WRITE_CHAR_P(dss
)
391 OPT_WRITE_SIZE_T(narenas
)
392 OPT_WRITE_SSIZE_T(lg_dirty_mult
)
393 OPT_WRITE_BOOL(stats_print
)
395 OPT_WRITE_SIZE_T(quarantine
)
396 OPT_WRITE_BOOL(redzone
)
398 OPT_WRITE_BOOL(utrace
)
399 OPT_WRITE_BOOL(valgrind
)
400 OPT_WRITE_BOOL(xmalloc
)
401 OPT_WRITE_BOOL(tcache
)
402 OPT_WRITE_SSIZE_T(lg_tcache_max
)
404 OPT_WRITE_CHAR_P(prof_prefix
)
405 OPT_WRITE_BOOL_MUTABLE(prof_active
, prof
.active
)
406 OPT_WRITE_BOOL_MUTABLE(prof_thread_active_init
,
407 prof
.thread_active_init
)
408 OPT_WRITE_SSIZE_T(lg_prof_sample
)
409 OPT_WRITE_BOOL(prof_accum
)
410 OPT_WRITE_SSIZE_T(lg_prof_interval
)
411 OPT_WRITE_BOOL(prof_gdump
)
412 OPT_WRITE_BOOL(prof_final
)
413 OPT_WRITE_BOOL(prof_leak
)
415 #undef OPT_WRITE_BOOL
416 #undef OPT_WRITE_BOOL_MUTABLE
417 #undef OPT_WRITE_SIZE_T
418 #undef OPT_WRITE_SSIZE_T
419 #undef OPT_WRITE_CHAR_P
421 malloc_cprintf(write_cb
, cbopaque
, "CPUs: %u\n", ncpus
);
423 CTL_GET("arenas.narenas", &uv
, unsigned);
424 malloc_cprintf(write_cb
, cbopaque
, "Arenas: %u\n", uv
);
426 malloc_cprintf(write_cb
, cbopaque
, "Pointer size: %zu\n",
429 CTL_GET("arenas.quantum", &sv
, size_t);
430 malloc_cprintf(write_cb
, cbopaque
, "Quantum size: %zu\n", sv
);
432 CTL_GET("arenas.page", &sv
, size_t);
433 malloc_cprintf(write_cb
, cbopaque
, "Page size: %zu\n", sv
);
435 CTL_GET("opt.lg_dirty_mult", &ssv
, ssize_t
);
437 malloc_cprintf(write_cb
, cbopaque
,
438 "Min active:dirty page ratio per arena: %u:1\n",
441 malloc_cprintf(write_cb
, cbopaque
,
442 "Min active:dirty page ratio per arena: N/A\n");
444 if (je_mallctl("arenas.tcache_max", &sv
, &ssz
, NULL
, 0) == 0) {
445 malloc_cprintf(write_cb
, cbopaque
,
446 "Maximum thread-cached size class: %zu\n", sv
);
448 if (je_mallctl("opt.prof", &bv
, &bsz
, NULL
, 0) == 0 && bv
) {
449 CTL_GET("prof.lg_sample", &sv
, size_t);
450 malloc_cprintf(write_cb
, cbopaque
,
451 "Average profile sample interval: %"PRIu64
452 " (2^%zu)\n", (((uint64_t)1U) << sv
), sv
);
454 CTL_GET("opt.lg_prof_interval", &ssv
, ssize_t
);
456 malloc_cprintf(write_cb
, cbopaque
,
457 "Average profile dump interval: %"PRIu64
459 (((uint64_t)1U) << ssv
), ssv
);
461 malloc_cprintf(write_cb
, cbopaque
,
462 "Average profile dump interval: N/A\n");
465 CTL_GET("opt.lg_chunk", &sv
, size_t);
466 malloc_cprintf(write_cb
, cbopaque
, "Chunk size: %zu (2^%zu)\n",
472 size_t allocated
, active
, mapped
;
473 size_t chunks_current
, chunks_high
;
474 uint64_t chunks_total
;
476 CTL_GET("stats.cactive", &cactive
, size_t *);
477 CTL_GET("stats.allocated", &allocated
, size_t);
478 CTL_GET("stats.active", &active
, size_t);
479 CTL_GET("stats.mapped", &mapped
, size_t);
480 malloc_cprintf(write_cb
, cbopaque
,
481 "Allocated: %zu, active: %zu, mapped: %zu\n",
482 allocated
, active
, mapped
);
483 malloc_cprintf(write_cb
, cbopaque
,
484 "Current active ceiling: %zu\n", atomic_read_z(cactive
));
486 /* Print chunk stats. */
487 CTL_GET("stats.chunks.total", &chunks_total
, uint64_t);
488 CTL_GET("stats.chunks.high", &chunks_high
, size_t);
489 CTL_GET("stats.chunks.current", &chunks_current
, size_t);
490 malloc_cprintf(write_cb
, cbopaque
, "chunks: nchunks "
491 "highchunks curchunks\n");
492 malloc_cprintf(write_cb
, cbopaque
,
493 " %13"PRIu64
" %12zu %12zu\n",
494 chunks_total
, chunks_high
, chunks_current
);
499 CTL_GET("arenas.narenas", &narenas
, unsigned);
501 VARIABLE_ARRAY(bool, initialized
, narenas
);
503 unsigned i
, ninitialized
;
505 isz
= sizeof(bool) * narenas
;
506 xmallctl("arenas.initialized", initialized
,
508 for (i
= ninitialized
= 0; i
< narenas
; i
++) {
513 if (ninitialized
> 1 || !unmerged
) {
514 /* Print merged arena stats. */
515 malloc_cprintf(write_cb
, cbopaque
,
516 "\nMerged arenas stats:\n");
517 stats_arena_print(write_cb
, cbopaque
,
518 narenas
, bins
, large
);
526 /* Print stats for each arena. */
528 CTL_GET("arenas.narenas", &narenas
, unsigned);
530 VARIABLE_ARRAY(bool, initialized
, narenas
);
534 isz
= sizeof(bool) * narenas
;
535 xmallctl("arenas.initialized", initialized
,
538 for (i
= 0; i
< narenas
; i
++) {
539 if (initialized
[i
]) {
540 malloc_cprintf(write_cb
,
542 "\narenas[%u]:\n", i
);
543 stats_arena_print(write_cb
,
544 cbopaque
, i
, bins
, large
);
550 malloc_cprintf(write_cb
, cbopaque
, "--- End jemalloc statistics ---\n");