1 #define JEMALLOC_STATS_C_ 2 #include "jemalloc/internal/jemalloc_internal.h" 3 4 #define CTL_GET(n, v, t) do { \ 5 size_t sz = sizeof(t); \ 6 xmallctl(n, v, &sz, NULL, 0); \ 7 } while (0) 8 9 #define CTL_I_GET(n, v, t) do { \ 10 size_t mib[6]; \ 11 size_t miblen = sizeof(mib) / sizeof(size_t); \ 12 size_t sz = sizeof(t); \ 13 xmallctlnametomib(n, mib, &miblen); \ 14 mib[2] = i; \ 15 xmallctlbymib(mib, miblen, v, &sz, NULL, 0); \ 16 } while (0) 17 18 #define CTL_J_GET(n, v, t) do { \ 19 size_t mib[6]; \ 20 size_t miblen = sizeof(mib) / sizeof(size_t); \ 21 size_t sz = sizeof(t); \ 22 xmallctlnametomib(n, mib, &miblen); \ 23 mib[2] = j; \ 24 xmallctlbymib(mib, miblen, v, &sz, NULL, 0); \ 25 } while (0) 26 27 #define CTL_IJ_GET(n, v, t) do { \ 28 size_t mib[6]; \ 29 size_t miblen = sizeof(mib) / sizeof(size_t); \ 30 size_t sz = sizeof(t); \ 31 xmallctlnametomib(n, mib, &miblen); \ 32 mib[2] = i; \ 33 mib[4] = j; \ 34 xmallctlbymib(mib, miblen, v, &sz, NULL, 0); \ 35 } while (0) 36 37 /******************************************************************************/ 38 /* Data. */ 39 40 bool opt_stats_print = false; 41 42 size_t stats_cactive = 0; 43 44 /******************************************************************************/ 45 /* Function prototypes for non-inline static functions. */ 46 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); 53 54 /******************************************************************************/ 55 56 static void 57 stats_arena_bins_print(void (*write_cb)(void *, const char *), void *cbopaque, 58 unsigned i) 59 { 60 size_t page; 61 bool config_tcache; 62 unsigned nbins, j, gap_start; 63 64 CTL_GET("arenas.page", &page, size_t); 65 66 CTL_GET("config.tcache", &config_tcache, bool); 67 if (config_tcache) { 68 malloc_cprintf(write_cb, cbopaque, 69 "bins: bin size regs pgs allocated nmalloc" 70 " ndalloc nrequests nfills nflushes" 71 " newruns reruns curruns\n"); 72 } else { 73 malloc_cprintf(write_cb, cbopaque, 74 "bins: bin size regs pgs allocated nmalloc" 75 " ndalloc newruns reruns curruns\n"); 76 } 77 CTL_GET("arenas.nbins", &nbins, unsigned); 78 for (j = 0, gap_start = UINT_MAX; j < nbins; j++) { 79 uint64_t nruns; 80 81 CTL_IJ_GET("stats.arenas.0.bins.0.nruns", &nruns, uint64_t); 82 if (nruns == 0) { 83 if (gap_start == UINT_MAX) 84 gap_start = j; 85 } else { 86 size_t reg_size, run_size, allocated; 87 uint32_t nregs; 88 uint64_t nmalloc, ndalloc, nrequests, nfills, nflushes; 89 uint64_t reruns; 90 size_t curruns; 91 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, 97 j - 1); 98 } else { 99 /* Gap of one size class. */ 100 malloc_cprintf(write_cb, cbopaque, 101 "[%u]\n", gap_start); 102 } 103 gap_start = UINT_MAX; 104 } 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", 109 &allocated, size_t); 110 CTL_IJ_GET("stats.arenas.0.bins.0.nmalloc", 111 &nmalloc, uint64_t); 112 CTL_IJ_GET("stats.arenas.0.bins.0.ndalloc", 113 &ndalloc, uint64_t); 114 if (config_tcache) { 115 CTL_IJ_GET("stats.arenas.0.bins.0.nrequests", 116 &nrequests, uint64_t); 117 CTL_IJ_GET("stats.arenas.0.bins.0.nfills", 118 &nfills, uint64_t); 119 CTL_IJ_GET("stats.arenas.0.bins.0.nflushes", 120 &nflushes, uint64_t); 121 } 122 CTL_IJ_GET("stats.arenas.0.bins.0.nreruns", &reruns, 123 uint64_t); 124 CTL_IJ_GET("stats.arenas.0.bins.0.curruns", &curruns, 125 size_t); 126 if (config_tcache) { 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 131 " %12zu\n", 132 j, reg_size, nregs, run_size / page, 133 allocated, nmalloc, ndalloc, nrequests, 134 nfills, nflushes, nruns, reruns, curruns); 135 } else { 136 malloc_cprintf(write_cb, cbopaque, 137 "%13u %5zu %4u %3zu %12zu %12"PRIu64 138 " %12"PRIu64" %12"PRIu64" %12"PRIu64 139 " %12zu\n", 140 j, reg_size, nregs, run_size / page, 141 allocated, nmalloc, ndalloc, nruns, reruns, 142 curruns); 143 } 144 } 145 } 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", 150 gap_start, j - 1); 151 } else { 152 /* Gap of one size class. */ 153 malloc_cprintf(write_cb, cbopaque, "[%u]\n", gap_start); 154 } 155 } 156 } 157 158 static void 159 stats_arena_lruns_print(void (*write_cb)(void *, const char *), void *cbopaque, 160 unsigned i) 161 { 162 size_t page, nlruns, j; 163 ssize_t gap_start; 164 165 CTL_GET("arenas.page", &page, size_t); 166 167 malloc_cprintf(write_cb, cbopaque, 168 "large: size pages nmalloc ndalloc nrequests" 169 " curruns\n"); 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; 174 175 CTL_IJ_GET("stats.arenas.0.lruns.0.nmalloc", &nmalloc, 176 uint64_t); 177 CTL_IJ_GET("stats.arenas.0.lruns.0.ndalloc", &ndalloc, 178 uint64_t); 179 CTL_IJ_GET("stats.arenas.0.lruns.0.nrequests", &nrequests, 180 uint64_t); 181 if (nrequests == 0) { 182 if (gap_start == -1) 183 gap_start = j; 184 } else { 185 CTL_J_GET("arenas.lrun.0.size", &run_size, size_t); 186 CTL_IJ_GET("stats.arenas.0.lruns.0.curruns", &curruns, 187 size_t); 188 if (gap_start != -1) { 189 malloc_cprintf(write_cb, cbopaque, "[%zu]\n", 190 j - gap_start); 191 gap_start = -1; 192 } 193 malloc_cprintf(write_cb, cbopaque, 194 "%13zu %5zu %12"PRIu64" %12"PRIu64" %12"PRIu64 195 " %12zu\n", 196 run_size, run_size / page, nmalloc, ndalloc, 197 nrequests, curruns); 198 } 199 } 200 if (gap_start != -1) 201 malloc_cprintf(write_cb, cbopaque, "[%zu]\n", j - gap_start); 202 } 203 204 static void 205 stats_arena_print(void (*write_cb)(void *, const char *), void *cbopaque, 206 unsigned i, bool bins, bool large) 207 { 208 unsigned nthreads; 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; 215 216 CTL_GET("arenas.page", &page, size_t); 217 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); 231 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); 257 258 if (bins) 259 stats_arena_bins_print(write_cb, cbopaque, i); 260 if (large) 261 stats_arena_lruns_print(write_cb, cbopaque, i); 262 } 263 264 void 265 stats_print(void (*write_cb)(void *, const char *), void *cbopaque, 266 const char *opts) 267 { 268 int err; 269 uint64_t epoch; 270 size_t u64sz; 271 bool general = true; 272 bool merged = true; 273 bool unmerged = true; 274 bool bins = true; 275 bool large = true; 276 277 /* 278 * Refresh stats, in case mallctl() was called by the application. 279 * 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. 283 * */ 284 epoch = 1; 285 u64sz = sizeof(uint64_t); 286 err = je_mallctl("epoch", &epoch, &u64sz, &epoch, sizeof(uint64_t)); 287 if (err != 0) { 288 if (err == EAGAIN) { 289 malloc_write("<jemalloc>: Memory allocation failure in " 290 "mallctl(\"epoch\", ...)\n"); 291 return; 292 } 293 malloc_write("<jemalloc>: Failure in mallctl(\"epoch\", " 294 "...)\n"); 295 abort(); 296 } 297 298 if (write_cb == NULL) { 299 /* 300 * The caller did not provide an alternate write_cb callback 301 * function, so use the default one. malloc_write() is an 302 * inline function, so use malloc_message() directly here. 303 */ 304 write_cb = je_malloc_message; 305 cbopaque = NULL; 306 } 307 308 if (opts != NULL) { 309 unsigned i; 310 311 for (i = 0; opts[i] != '\0'; i++) { 312 switch (opts[i]) { 313 case 'g': 314 general = false; 315 break; 316 case 'm': 317 merged = false; 318 break; 319 case 'a': 320 unmerged = false; 321 break; 322 case 'b': 323 bins = false; 324 break; 325 case 'l': 326 large = false; 327 break; 328 default:; 329 } 330 } 331 } 332 333 write_cb(cbopaque, "___ Begin jemalloc statistics ___\n"); 334 if (general) { 335 int err; 336 const char *cpv; 337 bool bv; 338 unsigned uv; 339 ssize_t ssv; 340 size_t sv, bsz, ssz, sssz, cpsz; 341 342 bsz = sizeof(bool); 343 ssz = sizeof(size_t); 344 sssz = sizeof(ssize_t); 345 cpsz = sizeof(const char *); 346 347 CTL_GET("version", &cpv, const char *); 348 malloc_cprintf(write_cb, cbopaque, "Version: %s\n", cpv); 349 CTL_GET("config.debug", &bv, bool); 350 malloc_cprintf(write_cb, cbopaque, "Assertions %s\n", 351 bv ? "enabled" : "disabled"); 352 353 #define OPT_WRITE_BOOL(n) \ 354 if ((err = je_mallctl("opt."#n, &bv, &bsz, NULL, 0)) \ 355 == 0) { \ 356 malloc_cprintf(write_cb, cbopaque, \ 357 " opt."#n": %s\n", bv ? "true" : "false"); \ 358 } 359 #define OPT_WRITE_SIZE_T(n) \ 360 if ((err = je_mallctl("opt."#n, &sv, &ssz, NULL, 0)) \ 361 == 0) { \ 362 malloc_cprintf(write_cb, cbopaque, \ 363 " opt."#n": %zu\n", sv); \ 364 } 365 #define OPT_WRITE_SSIZE_T(n) \ 366 if ((err = je_mallctl("opt."#n, &ssv, &sssz, NULL, 0)) \ 367 == 0) { \ 368 malloc_cprintf(write_cb, cbopaque, \ 369 " opt."#n": %zd\n", ssv); \ 370 } 371 #define OPT_WRITE_CHAR_P(n) \ 372 if ((err = je_mallctl("opt."#n, &cpv, &cpsz, NULL, 0)) \ 373 == 0) { \ 374 malloc_cprintf(write_cb, cbopaque, \ 375 " opt."#n": \"%s\"\n", cpv); \ 376 } 377 378 write_cb(cbopaque, "Run-time option settings:\n"); 379 OPT_WRITE_BOOL(abort) 380 OPT_WRITE_SIZE_T(lg_chunk) 381 OPT_WRITE_SIZE_T(narenas) 382 OPT_WRITE_SSIZE_T(lg_dirty_mult) 383 OPT_WRITE_BOOL(stats_print) 384 OPT_WRITE_BOOL(junk) 385 OPT_WRITE_SIZE_T(quarantine) 386 OPT_WRITE_BOOL(redzone) 387 OPT_WRITE_BOOL(zero) 388 OPT_WRITE_BOOL(utrace) 389 OPT_WRITE_BOOL(valgrind) 390 OPT_WRITE_BOOL(xmalloc) 391 OPT_WRITE_BOOL(tcache) 392 OPT_WRITE_SSIZE_T(lg_tcache_max) 393 OPT_WRITE_BOOL(prof) 394 OPT_WRITE_CHAR_P(prof_prefix) 395 OPT_WRITE_BOOL(prof_active) 396 OPT_WRITE_SSIZE_T(lg_prof_sample) 397 OPT_WRITE_BOOL(prof_accum) 398 OPT_WRITE_SSIZE_T(lg_prof_interval) 399 OPT_WRITE_BOOL(prof_gdump) 400 OPT_WRITE_BOOL(prof_final) 401 OPT_WRITE_BOOL(prof_leak) 402 403 #undef OPT_WRITE_BOOL 404 #undef OPT_WRITE_SIZE_T 405 #undef OPT_WRITE_SSIZE_T 406 #undef OPT_WRITE_CHAR_P 407 408 malloc_cprintf(write_cb, cbopaque, "CPUs: %u\n", ncpus); 409 410 CTL_GET("arenas.narenas", &uv, unsigned); 411 malloc_cprintf(write_cb, cbopaque, "Max arenas: %u\n", uv); 412 413 malloc_cprintf(write_cb, cbopaque, "Pointer size: %zu\n", 414 sizeof(void *)); 415 416 CTL_GET("arenas.quantum", &sv, size_t); 417 malloc_cprintf(write_cb, cbopaque, "Quantum size: %zu\n", sv); 418 419 CTL_GET("arenas.page", &sv, size_t); 420 malloc_cprintf(write_cb, cbopaque, "Page size: %zu\n", sv); 421 422 CTL_GET("opt.lg_dirty_mult", &ssv, ssize_t); 423 if (ssv >= 0) { 424 malloc_cprintf(write_cb, cbopaque, 425 "Min active:dirty page ratio per arena: %u:1\n", 426 (1U << ssv)); 427 } else { 428 write_cb(cbopaque, 429 "Min active:dirty page ratio per arena: N/A\n"); 430 } 431 if ((err = je_mallctl("arenas.tcache_max", &sv, &ssz, NULL, 0)) 432 == 0) { 433 malloc_cprintf(write_cb, cbopaque, 434 "Maximum thread-cached size class: %zu\n", sv); 435 } 436 if ((err = je_mallctl("opt.prof", &bv, &bsz, NULL, 0)) == 0 && 437 bv) { 438 CTL_GET("opt.lg_prof_sample", &sv, size_t); 439 malloc_cprintf(write_cb, cbopaque, 440 "Average profile sample interval: %"PRIu64 441 " (2^%zu)\n", (((uint64_t)1U) << sv), sv); 442 443 CTL_GET("opt.lg_prof_interval", &ssv, ssize_t); 444 if (ssv >= 0) { 445 malloc_cprintf(write_cb, cbopaque, 446 "Average profile dump interval: %"PRIu64 447 " (2^%zd)\n", 448 (((uint64_t)1U) << ssv), ssv); 449 } else { 450 write_cb(cbopaque, 451 "Average profile dump interval: N/A\n"); 452 } 453 } 454 CTL_GET("opt.lg_chunk", &sv, size_t); 455 malloc_cprintf(write_cb, cbopaque, "Chunk size: %zu (2^%zu)\n", 456 (ZU(1) << sv), sv); 457 } 458 459 if (config_stats) { 460 size_t *cactive; 461 size_t allocated, active, mapped; 462 size_t chunks_current, chunks_high; 463 uint64_t chunks_total; 464 size_t huge_allocated; 465 uint64_t huge_nmalloc, huge_ndalloc; 466 467 CTL_GET("stats.cactive", &cactive, size_t *); 468 CTL_GET("stats.allocated", &allocated, size_t); 469 CTL_GET("stats.active", &active, size_t); 470 CTL_GET("stats.mapped", &mapped, size_t); 471 malloc_cprintf(write_cb, cbopaque, 472 "Allocated: %zu, active: %zu, mapped: %zu\n", 473 allocated, active, mapped); 474 malloc_cprintf(write_cb, cbopaque, 475 "Current active ceiling: %zu\n", atomic_read_z(cactive)); 476 477 /* Print chunk stats. */ 478 CTL_GET("stats.chunks.total", &chunks_total, uint64_t); 479 CTL_GET("stats.chunks.high", &chunks_high, size_t); 480 CTL_GET("stats.chunks.current", &chunks_current, size_t); 481 malloc_cprintf(write_cb, cbopaque, "chunks: nchunks " 482 "highchunks curchunks\n"); 483 malloc_cprintf(write_cb, cbopaque, " %13"PRIu64"%13zu%13zu\n", 484 chunks_total, chunks_high, chunks_current); 485 486 /* Print huge stats. */ 487 CTL_GET("stats.huge.nmalloc", &huge_nmalloc, uint64_t); 488 CTL_GET("stats.huge.ndalloc", &huge_ndalloc, uint64_t); 489 CTL_GET("stats.huge.allocated", &huge_allocated, size_t); 490 malloc_cprintf(write_cb, cbopaque, 491 "huge: nmalloc ndalloc allocated\n"); 492 malloc_cprintf(write_cb, cbopaque, 493 " %12"PRIu64" %12"PRIu64" %12zu\n", 494 huge_nmalloc, huge_ndalloc, huge_allocated); 495 496 if (merged) { 497 unsigned narenas; 498 499 CTL_GET("arenas.narenas", &narenas, unsigned); 500 { 501 bool initialized[narenas]; 502 size_t isz; 503 unsigned i, ninitialized; 504 505 isz = sizeof(initialized); 506 xmallctl("arenas.initialized", initialized, 507 &isz, NULL, 0); 508 for (i = ninitialized = 0; i < narenas; i++) { 509 if (initialized[i]) 510 ninitialized++; 511 } 512 513 if (ninitialized > 1 || unmerged == false) { 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); 519 } 520 } 521 } 522 523 if (unmerged) { 524 unsigned narenas; 525 526 /* Print stats for each arena. */ 527 528 CTL_GET("arenas.narenas", &narenas, unsigned); 529 { 530 bool initialized[narenas]; 531 size_t isz; 532 unsigned i; 533 534 isz = sizeof(initialized); 535 xmallctl("arenas.initialized", initialized, 536 &isz, NULL, 0); 537 538 for (i = 0; i < narenas; i++) { 539 if (initialized[i]) { 540 malloc_cprintf(write_cb, 541 cbopaque, 542 "\narenas[%u]:\n", i); 543 stats_arena_print(write_cb, 544 cbopaque, i, bins, large); 545 } 546 } 547 } 548 } 549 } 550 write_cb(cbopaque, "--- End jemalloc statistics ---\n"); 551 } 552