]> git.saurik.com Git - redis.git/blob - deps/jemalloc/src/stats.c
Redis 2.6.5
[redis.git] / deps / jemalloc / src / stats.c
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", &reg_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 (opts != NULL) {
299 unsigned i;
300
301 for (i = 0; opts[i] != '\0'; i++) {
302 switch (opts[i]) {
303 case 'g':
304 general = false;
305 break;
306 case 'm':
307 merged = false;
308 break;
309 case 'a':
310 unmerged = false;
311 break;
312 case 'b':
313 bins = false;
314 break;
315 case 'l':
316 large = false;
317 break;
318 default:;
319 }
320 }
321 }
322
323 malloc_cprintf(write_cb, cbopaque,
324 "___ Begin jemalloc statistics ___\n");
325 if (general) {
326 int err;
327 const char *cpv;
328 bool bv;
329 unsigned uv;
330 ssize_t ssv;
331 size_t sv, bsz, ssz, sssz, cpsz;
332
333 bsz = sizeof(bool);
334 ssz = sizeof(size_t);
335 sssz = sizeof(ssize_t);
336 cpsz = sizeof(const char *);
337
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");
343
344 #define OPT_WRITE_BOOL(n) \
345 if ((err = je_mallctl("opt."#n, &bv, &bsz, NULL, 0)) \
346 == 0) { \
347 malloc_cprintf(write_cb, cbopaque, \
348 " opt."#n": %s\n", bv ? "true" : "false"); \
349 }
350 #define OPT_WRITE_SIZE_T(n) \
351 if ((err = je_mallctl("opt."#n, &sv, &ssz, NULL, 0)) \
352 == 0) { \
353 malloc_cprintf(write_cb, cbopaque, \
354 " opt."#n": %zu\n", sv); \
355 }
356 #define OPT_WRITE_SSIZE_T(n) \
357 if ((err = je_mallctl("opt."#n, &ssv, &sssz, NULL, 0)) \
358 == 0) { \
359 malloc_cprintf(write_cb, cbopaque, \
360 " opt."#n": %zd\n", ssv); \
361 }
362 #define OPT_WRITE_CHAR_P(n) \
363 if ((err = je_mallctl("opt."#n, &cpv, &cpsz, NULL, 0)) \
364 == 0) { \
365 malloc_cprintf(write_cb, cbopaque, \
366 " opt."#n": \"%s\"\n", cpv); \
367 }
368
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)
376 OPT_WRITE_BOOL(junk)
377 OPT_WRITE_SIZE_T(quarantine)
378 OPT_WRITE_BOOL(redzone)
379 OPT_WRITE_BOOL(zero)
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)
385 OPT_WRITE_BOOL(prof)
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)
394
395 #undef OPT_WRITE_BOOL
396 #undef OPT_WRITE_SIZE_T
397 #undef OPT_WRITE_SSIZE_T
398 #undef OPT_WRITE_CHAR_P
399
400 malloc_cprintf(write_cb, cbopaque, "CPUs: %u\n", ncpus);
401
402 CTL_GET("arenas.narenas", &uv, unsigned);
403 malloc_cprintf(write_cb, cbopaque, "Max arenas: %u\n", uv);
404
405 malloc_cprintf(write_cb, cbopaque, "Pointer size: %zu\n",
406 sizeof(void *));
407
408 CTL_GET("arenas.quantum", &sv, size_t);
409 malloc_cprintf(write_cb, cbopaque, "Quantum size: %zu\n", sv);
410
411 CTL_GET("arenas.page", &sv, size_t);
412 malloc_cprintf(write_cb, cbopaque, "Page size: %zu\n", sv);
413
414 CTL_GET("opt.lg_dirty_mult", &ssv, ssize_t);
415 if (ssv >= 0) {
416 malloc_cprintf(write_cb, cbopaque,
417 "Min active:dirty page ratio per arena: %u:1\n",
418 (1U << ssv));
419 } else {
420 malloc_cprintf(write_cb, cbopaque,
421 "Min active:dirty page ratio per arena: N/A\n");
422 }
423 if ((err = je_mallctl("arenas.tcache_max", &sv, &ssz, NULL, 0))
424 == 0) {
425 malloc_cprintf(write_cb, cbopaque,
426 "Maximum thread-cached size class: %zu\n", sv);
427 }
428 if ((err = je_mallctl("opt.prof", &bv, &bsz, NULL, 0)) == 0 &&
429 bv) {
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);
434
435 CTL_GET("opt.lg_prof_interval", &ssv, ssize_t);
436 if (ssv >= 0) {
437 malloc_cprintf(write_cb, cbopaque,
438 "Average profile dump interval: %"PRIu64
439 " (2^%zd)\n",
440 (((uint64_t)1U) << ssv), ssv);
441 } else {
442 malloc_cprintf(write_cb, cbopaque,
443 "Average profile dump interval: N/A\n");
444 }
445 }
446 CTL_GET("opt.lg_chunk", &sv, size_t);
447 malloc_cprintf(write_cb, cbopaque, "Chunk size: %zu (2^%zu)\n",
448 (ZU(1) << sv), sv);
449 }
450
451 if (config_stats) {
452 size_t *cactive;
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;
458
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));
468
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);
477
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);
487
488 if (merged) {
489 unsigned narenas;
490
491 CTL_GET("arenas.narenas", &narenas, unsigned);
492 {
493 VARIABLE_ARRAY(bool, initialized, narenas);
494 size_t isz;
495 unsigned i, ninitialized;
496
497 isz = sizeof(bool) * narenas;
498 xmallctl("arenas.initialized", initialized,
499 &isz, NULL, 0);
500 for (i = ninitialized = 0; i < narenas; i++) {
501 if (initialized[i])
502 ninitialized++;
503 }
504
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);
511 }
512 }
513 }
514
515 if (unmerged) {
516 unsigned narenas;
517
518 /* Print stats for each arena. */
519
520 CTL_GET("arenas.narenas", &narenas, unsigned);
521 {
522 VARIABLE_ARRAY(bool, initialized, narenas);
523 size_t isz;
524 unsigned i;
525
526 isz = sizeof(bool) * narenas;
527 xmallctl("arenas.initialized", initialized,
528 &isz, NULL, 0);
529
530 for (i = 0; i < narenas; i++) {
531 if (initialized[i]) {
532 malloc_cprintf(write_cb,
533 cbopaque,
534 "\narenas[%u]:\n", i);
535 stats_arena_print(write_cb,
536 cbopaque, i, bins, large);
537 }
538 }
539 }
540 }
541 }
542 malloc_cprintf(write_cb, cbopaque, "--- End jemalloc statistics ---\n");
543 }