blob: f2ef4e606114692795616663346ef61b859efbae [file] [log] [blame]
Jason Evans3c234352010-01-27 13:10:55 -08001#define JEMALLOC_CTL_C_
Jason Evans376b1522010-02-11 14:45:59 -08002#include "jemalloc/internal/jemalloc_internal.h"
Jason Evans3c234352010-01-27 13:10:55 -08003
4/******************************************************************************/
5/* Data. */
6
Jason Evansfc4dcfa2010-11-24 15:44:21 -08007/*
8 * ctl_mtx protects the following:
9 * - ctl_stats.*
10 * - opt_prof_active
Jason Evansfc4dcfa2010-11-24 15:44:21 -080011 */
Jason Evans3c234352010-01-27 13:10:55 -080012static malloc_mutex_t ctl_mtx;
13static bool ctl_initialized;
14static uint64_t ctl_epoch;
15static ctl_stats_t ctl_stats;
16
17/******************************************************************************/
Mike Hommey461ad5c2012-04-20 08:38:42 +020018/* Helpers for named and indexed nodes. */
19
20static inline const ctl_named_node_t *
21ctl_named_node(const ctl_node_t *node)
22{
23
24 return ((node->named) ? (const ctl_named_node_t *)node : NULL);
25}
26
27static inline const ctl_named_node_t *
28ctl_named_children(const ctl_named_node_t *node, int index)
29{
30 const ctl_named_node_t *children = ctl_named_node(node->children);
31
32 return (children ? &children[index] : NULL);
33}
34
35static inline const ctl_indexed_node_t *
36ctl_indexed_node(const ctl_node_t *node)
37{
38
39 return ((node->named == false) ? (const ctl_indexed_node_t *)node :
40 NULL);
41}
42
43/******************************************************************************/
Jason Evans3c234352010-01-27 13:10:55 -080044/* Function prototypes for non-inline static functions. */
45
46#define CTL_PROTO(n) \
47static int n##_ctl(const size_t *mib, size_t miblen, void *oldp, \
48 size_t *oldlenp, void *newp, size_t newlen);
49
50#define INDEX_PROTO(n) \
Jason Evans609ae592012-10-11 13:53:15 -070051static const ctl_named_node_t *n##_index(const size_t *mib, \
52 size_t miblen, size_t i);
Jason Evans3c234352010-01-27 13:10:55 -080053
Jason Evans3c234352010-01-27 13:10:55 -080054static bool ctl_arena_init(ctl_arena_stats_t *astats);
Jason Evans3c234352010-01-27 13:10:55 -080055static void ctl_arena_clear(ctl_arena_stats_t *astats);
Jason Evans86815df2010-03-13 20:32:56 -080056static void ctl_arena_stats_amerge(ctl_arena_stats_t *cstats,
57 arena_t *arena);
58static void ctl_arena_stats_smerge(ctl_arena_stats_t *sstats,
59 ctl_arena_stats_t *astats);
Jason Evans3c234352010-01-27 13:10:55 -080060static void ctl_arena_refresh(arena_t *arena, unsigned i);
Jason Evans609ae592012-10-11 13:53:15 -070061static bool ctl_grow(void);
Jason Evans3c234352010-01-27 13:10:55 -080062static void ctl_refresh(void);
63static bool ctl_init(void);
64static int ctl_lookup(const char *name, ctl_node_t const **nodesp,
65 size_t *mibp, size_t *depthp);
66
Jason Evansa40bc7a2010-03-02 13:01:16 -080067CTL_PROTO(version)
Jason Evans3c234352010-01-27 13:10:55 -080068CTL_PROTO(epoch)
Jason Evansd4be8b72012-03-26 18:54:44 -070069CTL_PROTO(thread_tcache_enabled)
Jason Evanse7b8fa12012-03-16 17:09:32 -070070CTL_PROTO(thread_tcache_flush)
Jason Evansb267d0f2010-08-13 15:42:29 -070071CTL_PROTO(thread_arena)
Jason Evans93443682010-10-20 17:39:18 -070072CTL_PROTO(thread_allocated)
Jason Evansecf229a2010-12-03 15:55:47 -080073CTL_PROTO(thread_allocatedp)
Jason Evans93443682010-10-20 17:39:18 -070074CTL_PROTO(thread_deallocated)
Jason Evansecf229a2010-12-03 15:55:47 -080075CTL_PROTO(thread_deallocatedp)
Jason Evans3c234352010-01-27 13:10:55 -080076CTL_PROTO(config_debug)
77CTL_PROTO(config_dss)
Jason Evans3c234352010-01-27 13:10:55 -080078CTL_PROTO(config_fill)
79CTL_PROTO(config_lazy_lock)
Jason Evans2e671ff2012-05-09 16:12:00 -070080CTL_PROTO(config_mremap)
Jason Evans59ae2762012-04-16 17:52:27 -070081CTL_PROTO(config_munmap)
Jason Evansd34f9e72010-02-11 13:19:21 -080082CTL_PROTO(config_prof)
83CTL_PROTO(config_prof_libgcc)
84CTL_PROTO(config_prof_libunwind)
Jason Evans3c234352010-01-27 13:10:55 -080085CTL_PROTO(config_stats)
Jason Evans3c234352010-01-27 13:10:55 -080086CTL_PROTO(config_tcache)
Jason Evans3c234352010-01-27 13:10:55 -080087CTL_PROTO(config_tls)
Jason Evansb1476112012-04-05 13:36:17 -070088CTL_PROTO(config_utrace)
Jason Evans122449b2012-04-06 00:35:09 -070089CTL_PROTO(config_valgrind)
Jason Evans3c234352010-01-27 13:10:55 -080090CTL_PROTO(config_xmalloc)
91CTL_PROTO(opt_abort)
Jason Evans609ae592012-10-11 13:53:15 -070092CTL_PROTO(opt_dss)
Jason Evanse7339702010-10-23 18:37:06 -070093CTL_PROTO(opt_lg_chunk)
94CTL_PROTO(opt_narenas)
95CTL_PROTO(opt_lg_dirty_mult)
96CTL_PROTO(opt_stats_print)
Jason Evans3c234352010-01-27 13:10:55 -080097CTL_PROTO(opt_junk)
Jason Evanse7339702010-10-23 18:37:06 -070098CTL_PROTO(opt_zero)
Jason Evans122449b2012-04-06 00:35:09 -070099CTL_PROTO(opt_quarantine)
100CTL_PROTO(opt_redzone)
Jason Evansb1476112012-04-05 13:36:17 -0700101CTL_PROTO(opt_utrace)
Jason Evans122449b2012-04-06 00:35:09 -0700102CTL_PROTO(opt_valgrind)
Jason Evans3c234352010-01-27 13:10:55 -0800103CTL_PROTO(opt_xmalloc)
Jason Evans3fa9a2f2010-03-07 15:34:14 -0800104CTL_PROTO(opt_tcache)
Jason Evansf3ca7c82012-04-04 16:16:09 -0700105CTL_PROTO(opt_lg_tcache_max)
Jason Evansd34f9e72010-02-11 13:19:21 -0800106CTL_PROTO(opt_prof)
Jason Evanse7339702010-10-23 18:37:06 -0700107CTL_PROTO(opt_prof_prefix)
Jason Evansf18c9822010-03-31 18:43:24 -0700108CTL_PROTO(opt_prof_active)
Jason Evansb9477e72010-03-01 20:15:26 -0800109CTL_PROTO(opt_lg_prof_sample)
Jason Evansd34f9e72010-02-11 13:19:21 -0800110CTL_PROTO(opt_lg_prof_interval)
Jason Evanse7339702010-10-23 18:37:06 -0700111CTL_PROTO(opt_prof_gdump)
Jason Evans0b25fe72012-04-17 16:39:33 -0700112CTL_PROTO(opt_prof_final)
Jason Evansd34f9e72010-02-11 13:19:21 -0800113CTL_PROTO(opt_prof_leak)
Jason Evansa881cd22010-10-02 15:18:50 -0700114CTL_PROTO(opt_prof_accum)
Jason Evans609ae592012-10-11 13:53:15 -0700115CTL_PROTO(arena_i_purge)
Jason Evans34457f52012-11-03 21:18:28 -0700116static void arena_purge(unsigned arena_ind);
Jason Evans609ae592012-10-11 13:53:15 -0700117CTL_PROTO(arena_i_dss)
118INDEX_PROTO(arena_i)
Jason Evans3c234352010-01-27 13:10:55 -0800119CTL_PROTO(arenas_bin_i_size)
120CTL_PROTO(arenas_bin_i_nregs)
121CTL_PROTO(arenas_bin_i_run_size)
122INDEX_PROTO(arenas_bin_i)
123CTL_PROTO(arenas_lrun_i_size)
124INDEX_PROTO(arenas_lrun_i)
125CTL_PROTO(arenas_narenas)
126CTL_PROTO(arenas_initialized)
127CTL_PROTO(arenas_quantum)
Jason Evansae4c7b42012-04-02 07:04:34 -0700128CTL_PROTO(arenas_page)
Jason Evansdafde142010-03-17 16:27:39 -0700129CTL_PROTO(arenas_tcache_max)
Jason Evans3c234352010-01-27 13:10:55 -0800130CTL_PROTO(arenas_nbins)
Jason Evansdafde142010-03-17 16:27:39 -0700131CTL_PROTO(arenas_nhbins)
Jason Evans3c234352010-01-27 13:10:55 -0800132CTL_PROTO(arenas_nlruns)
Jason Evans6005f072010-09-30 16:55:08 -0700133CTL_PROTO(arenas_purge)
Jason Evans609ae592012-10-11 13:53:15 -0700134CTL_PROTO(arenas_extend)
Jason Evansf18c9822010-03-31 18:43:24 -0700135CTL_PROTO(prof_active)
Jason Evansd34f9e72010-02-11 13:19:21 -0800136CTL_PROTO(prof_dump)
137CTL_PROTO(prof_interval)
Jason Evans3c234352010-01-27 13:10:55 -0800138CTL_PROTO(stats_chunks_current)
139CTL_PROTO(stats_chunks_total)
140CTL_PROTO(stats_chunks_high)
141CTL_PROTO(stats_huge_allocated)
142CTL_PROTO(stats_huge_nmalloc)
143CTL_PROTO(stats_huge_ndalloc)
144CTL_PROTO(stats_arenas_i_small_allocated)
145CTL_PROTO(stats_arenas_i_small_nmalloc)
146CTL_PROTO(stats_arenas_i_small_ndalloc)
Jason Evans86815df2010-03-13 20:32:56 -0800147CTL_PROTO(stats_arenas_i_small_nrequests)
Jason Evans3c234352010-01-27 13:10:55 -0800148CTL_PROTO(stats_arenas_i_large_allocated)
149CTL_PROTO(stats_arenas_i_large_nmalloc)
150CTL_PROTO(stats_arenas_i_large_ndalloc)
Jason Evansdafde142010-03-17 16:27:39 -0700151CTL_PROTO(stats_arenas_i_large_nrequests)
Jason Evans86815df2010-03-13 20:32:56 -0800152CTL_PROTO(stats_arenas_i_bins_j_allocated)
153CTL_PROTO(stats_arenas_i_bins_j_nmalloc)
154CTL_PROTO(stats_arenas_i_bins_j_ndalloc)
Jason Evans3c234352010-01-27 13:10:55 -0800155CTL_PROTO(stats_arenas_i_bins_j_nrequests)
Jason Evans3c234352010-01-27 13:10:55 -0800156CTL_PROTO(stats_arenas_i_bins_j_nfills)
157CTL_PROTO(stats_arenas_i_bins_j_nflushes)
Jason Evans3c234352010-01-27 13:10:55 -0800158CTL_PROTO(stats_arenas_i_bins_j_nruns)
159CTL_PROTO(stats_arenas_i_bins_j_nreruns)
Jason Evans3c234352010-01-27 13:10:55 -0800160CTL_PROTO(stats_arenas_i_bins_j_curruns)
161INDEX_PROTO(stats_arenas_i_bins_j)
Jason Evansdafde142010-03-17 16:27:39 -0700162CTL_PROTO(stats_arenas_i_lruns_j_nmalloc)
163CTL_PROTO(stats_arenas_i_lruns_j_ndalloc)
Jason Evans3c234352010-01-27 13:10:55 -0800164CTL_PROTO(stats_arenas_i_lruns_j_nrequests)
Jason Evans3c234352010-01-27 13:10:55 -0800165CTL_PROTO(stats_arenas_i_lruns_j_curruns)
166INDEX_PROTO(stats_arenas_i_lruns_j)
Jason Evans597632b2011-03-18 13:41:33 -0700167CTL_PROTO(stats_arenas_i_nthreads)
Jason Evans609ae592012-10-11 13:53:15 -0700168CTL_PROTO(stats_arenas_i_dss)
Jason Evans3c234352010-01-27 13:10:55 -0800169CTL_PROTO(stats_arenas_i_pactive)
170CTL_PROTO(stats_arenas_i_pdirty)
Jason Evans3c234352010-01-27 13:10:55 -0800171CTL_PROTO(stats_arenas_i_mapped)
172CTL_PROTO(stats_arenas_i_npurge)
173CTL_PROTO(stats_arenas_i_nmadvise)
174CTL_PROTO(stats_arenas_i_purged)
Jason Evans3c234352010-01-27 13:10:55 -0800175INDEX_PROTO(stats_arenas_i)
Jason Evans0657f122011-03-18 17:56:14 -0700176CTL_PROTO(stats_cactive)
Jason Evans3c234352010-01-27 13:10:55 -0800177CTL_PROTO(stats_allocated)
178CTL_PROTO(stats_active)
179CTL_PROTO(stats_mapped)
Jason Evans3c234352010-01-27 13:10:55 -0800180
181/******************************************************************************/
182/* mallctl tree. */
183
184/* Maximum tree depth. */
185#define CTL_MAX_DEPTH 6
186
Mike Hommey461ad5c2012-04-20 08:38:42 +0200187#define NAME(n) {true}, n
Jason Evans65f343a2012-04-23 19:31:45 -0700188#define CHILD(t, c) \
189 sizeof(c##_node) / sizeof(ctl_##t##_node_t), \
190 (ctl_node_t *)c##_node, \
191 NULL
Mike Hommey461ad5c2012-04-20 08:38:42 +0200192#define CTL(c) 0, NULL, c##_ctl
Jason Evans3c234352010-01-27 13:10:55 -0800193
194/*
195 * Only handles internal indexed nodes, since there are currently no external
196 * ones.
197 */
Mike Hommey461ad5c2012-04-20 08:38:42 +0200198#define INDEX(i) {false}, i##_index
Jason Evans3c234352010-01-27 13:10:55 -0800199
Mike Hommey461ad5c2012-04-20 08:38:42 +0200200static const ctl_named_node_t tcache_node[] = {
Jason Evansd4be8b72012-03-26 18:54:44 -0700201 {NAME("enabled"), CTL(thread_tcache_enabled)},
Jason Evanse7b8fa12012-03-16 17:09:32 -0700202 {NAME("flush"), CTL(thread_tcache_flush)}
Jason Evans3c234352010-01-27 13:10:55 -0800203};
Jason Evans3c234352010-01-27 13:10:55 -0800204
Mike Hommey461ad5c2012-04-20 08:38:42 +0200205static const ctl_named_node_t thread_node[] = {
Jason Evans7372b152012-02-10 20:22:09 -0800206 {NAME("arena"), CTL(thread_arena)},
Jason Evans93443682010-10-20 17:39:18 -0700207 {NAME("allocated"), CTL(thread_allocated)},
Jason Evansecf229a2010-12-03 15:55:47 -0800208 {NAME("allocatedp"), CTL(thread_allocatedp)},
209 {NAME("deallocated"), CTL(thread_deallocated)},
Jason Evanse7b8fa12012-03-16 17:09:32 -0700210 {NAME("deallocatedp"), CTL(thread_deallocatedp)},
Jason Evans65f343a2012-04-23 19:31:45 -0700211 {NAME("tcache"), CHILD(named, tcache)}
Jason Evansb267d0f2010-08-13 15:42:29 -0700212};
Jason Evansb267d0f2010-08-13 15:42:29 -0700213
Mike Hommey461ad5c2012-04-20 08:38:42 +0200214static const ctl_named_node_t config_node[] = {
Jason Evans3c234352010-01-27 13:10:55 -0800215 {NAME("debug"), CTL(config_debug)},
216 {NAME("dss"), CTL(config_dss)},
Jason Evans3c234352010-01-27 13:10:55 -0800217 {NAME("fill"), CTL(config_fill)},
218 {NAME("lazy_lock"), CTL(config_lazy_lock)},
Jason Evans2e671ff2012-05-09 16:12:00 -0700219 {NAME("mremap"), CTL(config_mremap)},
Jason Evans59ae2762012-04-16 17:52:27 -0700220 {NAME("munmap"), CTL(config_munmap)},
Jason Evansd34f9e72010-02-11 13:19:21 -0800221 {NAME("prof"), CTL(config_prof)},
222 {NAME("prof_libgcc"), CTL(config_prof_libgcc)},
223 {NAME("prof_libunwind"), CTL(config_prof_libunwind)},
Jason Evans3c234352010-01-27 13:10:55 -0800224 {NAME("stats"), CTL(config_stats)},
Jason Evans3c234352010-01-27 13:10:55 -0800225 {NAME("tcache"), CTL(config_tcache)},
Jason Evans3c234352010-01-27 13:10:55 -0800226 {NAME("tls"), CTL(config_tls)},
Jason Evansb1476112012-04-05 13:36:17 -0700227 {NAME("utrace"), CTL(config_utrace)},
Jason Evans122449b2012-04-06 00:35:09 -0700228 {NAME("valgrind"), CTL(config_valgrind)},
Jason Evans3c234352010-01-27 13:10:55 -0800229 {NAME("xmalloc"), CTL(config_xmalloc)}
230};
231
Mike Hommey461ad5c2012-04-20 08:38:42 +0200232static const ctl_named_node_t opt_node[] = {
Jason Evans3c234352010-01-27 13:10:55 -0800233 {NAME("abort"), CTL(opt_abort)},
Jason Evans609ae592012-10-11 13:53:15 -0700234 {NAME("dss"), CTL(opt_dss)},
Jason Evanse7339702010-10-23 18:37:06 -0700235 {NAME("lg_chunk"), CTL(opt_lg_chunk)},
236 {NAME("narenas"), CTL(opt_narenas)},
237 {NAME("lg_dirty_mult"), CTL(opt_lg_dirty_mult)},
Jason Evans7372b152012-02-10 20:22:09 -0800238 {NAME("stats_print"), CTL(opt_stats_print)},
Jason Evans3c234352010-01-27 13:10:55 -0800239 {NAME("junk"), CTL(opt_junk)},
Jason Evans7372b152012-02-10 20:22:09 -0800240 {NAME("zero"), CTL(opt_zero)},
Jason Evans122449b2012-04-06 00:35:09 -0700241 {NAME("quarantine"), CTL(opt_quarantine)},
242 {NAME("redzone"), CTL(opt_redzone)},
Jason Evansb1476112012-04-05 13:36:17 -0700243 {NAME("utrace"), CTL(opt_utrace)},
Jason Evans122449b2012-04-06 00:35:09 -0700244 {NAME("valgrind"), CTL(opt_valgrind)},
Jason Evans7372b152012-02-10 20:22:09 -0800245 {NAME("xmalloc"), CTL(opt_xmalloc)},
Jason Evans3fa9a2f2010-03-07 15:34:14 -0800246 {NAME("tcache"), CTL(opt_tcache)},
Jason Evansf3ca7c82012-04-04 16:16:09 -0700247 {NAME("lg_tcache_max"), CTL(opt_lg_tcache_max)},
Jason Evansd34f9e72010-02-11 13:19:21 -0800248 {NAME("prof"), CTL(opt_prof)},
Jason Evanse7339702010-10-23 18:37:06 -0700249 {NAME("prof_prefix"), CTL(opt_prof_prefix)},
Jason Evansf18c9822010-03-31 18:43:24 -0700250 {NAME("prof_active"), CTL(opt_prof_active)},
Jason Evansb9477e72010-03-01 20:15:26 -0800251 {NAME("lg_prof_sample"), CTL(opt_lg_prof_sample)},
Jason Evansd34f9e72010-02-11 13:19:21 -0800252 {NAME("lg_prof_interval"), CTL(opt_lg_prof_interval)},
Jason Evanse7339702010-10-23 18:37:06 -0700253 {NAME("prof_gdump"), CTL(opt_prof_gdump)},
Jason Evans0b25fe72012-04-17 16:39:33 -0700254 {NAME("prof_final"), CTL(opt_prof_final)},
Jason Evansd34f9e72010-02-11 13:19:21 -0800255 {NAME("prof_leak"), CTL(opt_prof_leak)},
Jason Evans0b526ff2012-02-13 18:04:26 -0800256 {NAME("prof_accum"), CTL(opt_prof_accum)}
Jason Evans3c234352010-01-27 13:10:55 -0800257};
258
Jason Evans609ae592012-10-11 13:53:15 -0700259static const ctl_named_node_t arena_i_node[] = {
260 {NAME("purge"), CTL(arena_i_purge)},
261 {NAME("dss"), CTL(arena_i_dss)}
262};
263static const ctl_named_node_t super_arena_i_node[] = {
264 {NAME(""), CHILD(named, arena_i)}
265};
266
267static const ctl_indexed_node_t arena_node[] = {
268 {INDEX(arena_i)}
269};
270
Mike Hommey461ad5c2012-04-20 08:38:42 +0200271static const ctl_named_node_t arenas_bin_i_node[] = {
Jason Evans3c234352010-01-27 13:10:55 -0800272 {NAME("size"), CTL(arenas_bin_i_size)},
273 {NAME("nregs"), CTL(arenas_bin_i_nregs)},
274 {NAME("run_size"), CTL(arenas_bin_i_run_size)}
275};
Mike Hommey461ad5c2012-04-20 08:38:42 +0200276static const ctl_named_node_t super_arenas_bin_i_node[] = {
Jason Evans65f343a2012-04-23 19:31:45 -0700277 {NAME(""), CHILD(named, arenas_bin_i)}
Jason Evans3c234352010-01-27 13:10:55 -0800278};
279
Mike Hommey461ad5c2012-04-20 08:38:42 +0200280static const ctl_indexed_node_t arenas_bin_node[] = {
Jason Evans3c234352010-01-27 13:10:55 -0800281 {INDEX(arenas_bin_i)}
282};
283
Mike Hommey461ad5c2012-04-20 08:38:42 +0200284static const ctl_named_node_t arenas_lrun_i_node[] = {
Jason Evans3c234352010-01-27 13:10:55 -0800285 {NAME("size"), CTL(arenas_lrun_i_size)}
286};
Mike Hommey461ad5c2012-04-20 08:38:42 +0200287static const ctl_named_node_t super_arenas_lrun_i_node[] = {
Jason Evans65f343a2012-04-23 19:31:45 -0700288 {NAME(""), CHILD(named, arenas_lrun_i)}
Jason Evans3c234352010-01-27 13:10:55 -0800289};
290
Mike Hommey461ad5c2012-04-20 08:38:42 +0200291static const ctl_indexed_node_t arenas_lrun_node[] = {
Jason Evans3c234352010-01-27 13:10:55 -0800292 {INDEX(arenas_lrun_i)}
293};
294
Mike Hommey461ad5c2012-04-20 08:38:42 +0200295static const ctl_named_node_t arenas_node[] = {
Jason Evans3c234352010-01-27 13:10:55 -0800296 {NAME("narenas"), CTL(arenas_narenas)},
297 {NAME("initialized"), CTL(arenas_initialized)},
298 {NAME("quantum"), CTL(arenas_quantum)},
Jason Evansae4c7b42012-04-02 07:04:34 -0700299 {NAME("page"), CTL(arenas_page)},
Jason Evansdafde142010-03-17 16:27:39 -0700300 {NAME("tcache_max"), CTL(arenas_tcache_max)},
Jason Evans3c234352010-01-27 13:10:55 -0800301 {NAME("nbins"), CTL(arenas_nbins)},
Jason Evansdafde142010-03-17 16:27:39 -0700302 {NAME("nhbins"), CTL(arenas_nhbins)},
Jason Evans65f343a2012-04-23 19:31:45 -0700303 {NAME("bin"), CHILD(indexed, arenas_bin)},
Jason Evans3c234352010-01-27 13:10:55 -0800304 {NAME("nlruns"), CTL(arenas_nlruns)},
Jason Evans65f343a2012-04-23 19:31:45 -0700305 {NAME("lrun"), CHILD(indexed, arenas_lrun)},
Jason Evans609ae592012-10-11 13:53:15 -0700306 {NAME("purge"), CTL(arenas_purge)},
307 {NAME("extend"), CTL(arenas_extend)}
Jason Evans3c234352010-01-27 13:10:55 -0800308};
309
Mike Hommey461ad5c2012-04-20 08:38:42 +0200310static const ctl_named_node_t prof_node[] = {
Jason Evansf18c9822010-03-31 18:43:24 -0700311 {NAME("active"), CTL(prof_active)},
Jason Evansd34f9e72010-02-11 13:19:21 -0800312 {NAME("dump"), CTL(prof_dump)},
313 {NAME("interval"), CTL(prof_interval)}
314};
Jason Evansd34f9e72010-02-11 13:19:21 -0800315
Mike Hommey461ad5c2012-04-20 08:38:42 +0200316static const ctl_named_node_t stats_chunks_node[] = {
Jason Evans3c234352010-01-27 13:10:55 -0800317 {NAME("current"), CTL(stats_chunks_current)},
318 {NAME("total"), CTL(stats_chunks_total)},
319 {NAME("high"), CTL(stats_chunks_high)}
320};
321
Mike Hommey461ad5c2012-04-20 08:38:42 +0200322static const ctl_named_node_t stats_huge_node[] = {
Jason Evans3c234352010-01-27 13:10:55 -0800323 {NAME("allocated"), CTL(stats_huge_allocated)},
324 {NAME("nmalloc"), CTL(stats_huge_nmalloc)},
325 {NAME("ndalloc"), CTL(stats_huge_ndalloc)}
326};
327
Mike Hommey461ad5c2012-04-20 08:38:42 +0200328static const ctl_named_node_t stats_arenas_i_small_node[] = {
Jason Evans3c234352010-01-27 13:10:55 -0800329 {NAME("allocated"), CTL(stats_arenas_i_small_allocated)},
330 {NAME("nmalloc"), CTL(stats_arenas_i_small_nmalloc)},
Jason Evans86815df2010-03-13 20:32:56 -0800331 {NAME("ndalloc"), CTL(stats_arenas_i_small_ndalloc)},
332 {NAME("nrequests"), CTL(stats_arenas_i_small_nrequests)}
Jason Evans3c234352010-01-27 13:10:55 -0800333};
334
Mike Hommey461ad5c2012-04-20 08:38:42 +0200335static const ctl_named_node_t stats_arenas_i_large_node[] = {
Jason Evans3c234352010-01-27 13:10:55 -0800336 {NAME("allocated"), CTL(stats_arenas_i_large_allocated)},
337 {NAME("nmalloc"), CTL(stats_arenas_i_large_nmalloc)},
Jason Evansdafde142010-03-17 16:27:39 -0700338 {NAME("ndalloc"), CTL(stats_arenas_i_large_ndalloc)},
339 {NAME("nrequests"), CTL(stats_arenas_i_large_nrequests)}
Jason Evans3c234352010-01-27 13:10:55 -0800340};
341
Mike Hommey461ad5c2012-04-20 08:38:42 +0200342static const ctl_named_node_t stats_arenas_i_bins_j_node[] = {
Jason Evans86815df2010-03-13 20:32:56 -0800343 {NAME("allocated"), CTL(stats_arenas_i_bins_j_allocated)},
344 {NAME("nmalloc"), CTL(stats_arenas_i_bins_j_nmalloc)},
345 {NAME("ndalloc"), CTL(stats_arenas_i_bins_j_ndalloc)},
Jason Evans3c234352010-01-27 13:10:55 -0800346 {NAME("nrequests"), CTL(stats_arenas_i_bins_j_nrequests)},
Jason Evans3c234352010-01-27 13:10:55 -0800347 {NAME("nfills"), CTL(stats_arenas_i_bins_j_nfills)},
348 {NAME("nflushes"), CTL(stats_arenas_i_bins_j_nflushes)},
Jason Evans3c234352010-01-27 13:10:55 -0800349 {NAME("nruns"), CTL(stats_arenas_i_bins_j_nruns)},
350 {NAME("nreruns"), CTL(stats_arenas_i_bins_j_nreruns)},
Jason Evans3c234352010-01-27 13:10:55 -0800351 {NAME("curruns"), CTL(stats_arenas_i_bins_j_curruns)}
352};
Mike Hommey461ad5c2012-04-20 08:38:42 +0200353static const ctl_named_node_t super_stats_arenas_i_bins_j_node[] = {
Jason Evans65f343a2012-04-23 19:31:45 -0700354 {NAME(""), CHILD(named, stats_arenas_i_bins_j)}
Jason Evans3c234352010-01-27 13:10:55 -0800355};
356
Mike Hommey461ad5c2012-04-20 08:38:42 +0200357static const ctl_indexed_node_t stats_arenas_i_bins_node[] = {
Jason Evans3c234352010-01-27 13:10:55 -0800358 {INDEX(stats_arenas_i_bins_j)}
359};
360
Mike Hommey461ad5c2012-04-20 08:38:42 +0200361static const ctl_named_node_t stats_arenas_i_lruns_j_node[] = {
Jason Evansdafde142010-03-17 16:27:39 -0700362 {NAME("nmalloc"), CTL(stats_arenas_i_lruns_j_nmalloc)},
363 {NAME("ndalloc"), CTL(stats_arenas_i_lruns_j_ndalloc)},
Jason Evans3c234352010-01-27 13:10:55 -0800364 {NAME("nrequests"), CTL(stats_arenas_i_lruns_j_nrequests)},
Jason Evans3c234352010-01-27 13:10:55 -0800365 {NAME("curruns"), CTL(stats_arenas_i_lruns_j_curruns)}
366};
Mike Hommey461ad5c2012-04-20 08:38:42 +0200367static const ctl_named_node_t super_stats_arenas_i_lruns_j_node[] = {
Jason Evans65f343a2012-04-23 19:31:45 -0700368 {NAME(""), CHILD(named, stats_arenas_i_lruns_j)}
Jason Evans3c234352010-01-27 13:10:55 -0800369};
370
Mike Hommey461ad5c2012-04-20 08:38:42 +0200371static const ctl_indexed_node_t stats_arenas_i_lruns_node[] = {
Jason Evans3c234352010-01-27 13:10:55 -0800372 {INDEX(stats_arenas_i_lruns_j)}
373};
Jason Evans3c234352010-01-27 13:10:55 -0800374
Mike Hommey461ad5c2012-04-20 08:38:42 +0200375static const ctl_named_node_t stats_arenas_i_node[] = {
Jason Evans597632b2011-03-18 13:41:33 -0700376 {NAME("nthreads"), CTL(stats_arenas_i_nthreads)},
Jason Evans609ae592012-10-11 13:53:15 -0700377 {NAME("dss"), CTL(stats_arenas_i_dss)},
Jason Evans3c234352010-01-27 13:10:55 -0800378 {NAME("pactive"), CTL(stats_arenas_i_pactive)},
Jason Evans7372b152012-02-10 20:22:09 -0800379 {NAME("pdirty"), CTL(stats_arenas_i_pdirty)},
Jason Evans3c234352010-01-27 13:10:55 -0800380 {NAME("mapped"), CTL(stats_arenas_i_mapped)},
381 {NAME("npurge"), CTL(stats_arenas_i_npurge)},
382 {NAME("nmadvise"), CTL(stats_arenas_i_nmadvise)},
383 {NAME("purged"), CTL(stats_arenas_i_purged)},
Jason Evans65f343a2012-04-23 19:31:45 -0700384 {NAME("small"), CHILD(named, stats_arenas_i_small)},
385 {NAME("large"), CHILD(named, stats_arenas_i_large)},
Jason Evans87667a82012-04-23 19:54:15 -0700386 {NAME("bins"), CHILD(indexed, stats_arenas_i_bins)},
387 {NAME("lruns"), CHILD(indexed, stats_arenas_i_lruns)}
Jason Evans3c234352010-01-27 13:10:55 -0800388};
Mike Hommey461ad5c2012-04-20 08:38:42 +0200389static const ctl_named_node_t super_stats_arenas_i_node[] = {
Jason Evans65f343a2012-04-23 19:31:45 -0700390 {NAME(""), CHILD(named, stats_arenas_i)}
Jason Evans3c234352010-01-27 13:10:55 -0800391};
392
Mike Hommey461ad5c2012-04-20 08:38:42 +0200393static const ctl_indexed_node_t stats_arenas_node[] = {
Jason Evans3c234352010-01-27 13:10:55 -0800394 {INDEX(stats_arenas_i)}
395};
396
Mike Hommey461ad5c2012-04-20 08:38:42 +0200397static const ctl_named_node_t stats_node[] = {
Jason Evans0657f122011-03-18 17:56:14 -0700398 {NAME("cactive"), CTL(stats_cactive)},
Jason Evans3c234352010-01-27 13:10:55 -0800399 {NAME("allocated"), CTL(stats_allocated)},
400 {NAME("active"), CTL(stats_active)},
401 {NAME("mapped"), CTL(stats_mapped)},
Jason Evans65f343a2012-04-23 19:31:45 -0700402 {NAME("chunks"), CHILD(named, stats_chunks)},
403 {NAME("huge"), CHILD(named, stats_huge)},
404 {NAME("arenas"), CHILD(indexed, stats_arenas)}
Jason Evans3c234352010-01-27 13:10:55 -0800405};
406
Mike Hommey461ad5c2012-04-20 08:38:42 +0200407static const ctl_named_node_t root_node[] = {
Jason Evansa40bc7a2010-03-02 13:01:16 -0800408 {NAME("version"), CTL(version)},
Jason Evans3c234352010-01-27 13:10:55 -0800409 {NAME("epoch"), CTL(epoch)},
Jason Evans65f343a2012-04-23 19:31:45 -0700410 {NAME("thread"), CHILD(named, thread)},
411 {NAME("config"), CHILD(named, config)},
412 {NAME("opt"), CHILD(named, opt)},
Jason Evans609ae592012-10-11 13:53:15 -0700413 {NAME("arena"), CHILD(indexed, arena)},
Jason Evans65f343a2012-04-23 19:31:45 -0700414 {NAME("arenas"), CHILD(named, arenas)},
415 {NAME("prof"), CHILD(named, prof)},
416 {NAME("stats"), CHILD(named, stats)}
Jason Evans3c234352010-01-27 13:10:55 -0800417};
Mike Hommey461ad5c2012-04-20 08:38:42 +0200418static const ctl_named_node_t super_root_node[] = {
Jason Evans65f343a2012-04-23 19:31:45 -0700419 {NAME(""), CHILD(named, root)}
Jason Evans3c234352010-01-27 13:10:55 -0800420};
421
422#undef NAME
423#undef CHILD
424#undef CTL
425#undef INDEX
426
427/******************************************************************************/
428
Jason Evans3c234352010-01-27 13:10:55 -0800429static bool
430ctl_arena_init(ctl_arena_stats_t *astats)
431{
432
Jason Evans3c234352010-01-27 13:10:55 -0800433 if (astats->lstats == NULL) {
434 astats->lstats = (malloc_large_stats_t *)base_alloc(nlclasses *
435 sizeof(malloc_large_stats_t));
436 if (astats->lstats == NULL)
437 return (true);
438 }
439
440 return (false);
441}
Jason Evans3c234352010-01-27 13:10:55 -0800442
443static void
444ctl_arena_clear(ctl_arena_stats_t *astats)
445{
446
Jason Evans609ae592012-10-11 13:53:15 -0700447 astats->dss = dss_prec_names[dss_prec_limit];
Jason Evans3c234352010-01-27 13:10:55 -0800448 astats->pactive = 0;
449 astats->pdirty = 0;
Jason Evans7372b152012-02-10 20:22:09 -0800450 if (config_stats) {
451 memset(&astats->astats, 0, sizeof(arena_stats_t));
452 astats->allocated_small = 0;
453 astats->nmalloc_small = 0;
454 astats->ndalloc_small = 0;
455 astats->nrequests_small = 0;
Jason Evansb1726102012-02-28 16:50:47 -0800456 memset(astats->bstats, 0, NBINS * sizeof(malloc_bin_stats_t));
Jason Evans7372b152012-02-10 20:22:09 -0800457 memset(astats->lstats, 0, nlclasses *
458 sizeof(malloc_large_stats_t));
459 }
Jason Evans3c234352010-01-27 13:10:55 -0800460}
461
Jason Evans86815df2010-03-13 20:32:56 -0800462static void
463ctl_arena_stats_amerge(ctl_arena_stats_t *cstats, arena_t *arena)
464{
465 unsigned i;
466
Jason Evans609ae592012-10-11 13:53:15 -0700467 arena_stats_merge(arena, &cstats->dss, &cstats->pactive,
468 &cstats->pdirty, &cstats->astats, cstats->bstats, cstats->lstats);
Jason Evans86815df2010-03-13 20:32:56 -0800469
Jason Evansb1726102012-02-28 16:50:47 -0800470 for (i = 0; i < NBINS; i++) {
Jason Evans86815df2010-03-13 20:32:56 -0800471 cstats->allocated_small += cstats->bstats[i].allocated;
472 cstats->nmalloc_small += cstats->bstats[i].nmalloc;
473 cstats->ndalloc_small += cstats->bstats[i].ndalloc;
474 cstats->nrequests_small += cstats->bstats[i].nrequests;
475 }
Jason Evans86815df2010-03-13 20:32:56 -0800476}
477
478static void
479ctl_arena_stats_smerge(ctl_arena_stats_t *sstats, ctl_arena_stats_t *astats)
480{
481 unsigned i;
482
483 sstats->pactive += astats->pactive;
484 sstats->pdirty += astats->pdirty;
485
486 sstats->astats.mapped += astats->astats.mapped;
487 sstats->astats.npurge += astats->astats.npurge;
488 sstats->astats.nmadvise += astats->astats.nmadvise;
489 sstats->astats.purged += astats->astats.purged;
490
491 sstats->allocated_small += astats->allocated_small;
492 sstats->nmalloc_small += astats->nmalloc_small;
493 sstats->ndalloc_small += astats->ndalloc_small;
494 sstats->nrequests_small += astats->nrequests_small;
495
Jason Evans86815df2010-03-13 20:32:56 -0800496 sstats->astats.allocated_large += astats->astats.allocated_large;
497 sstats->astats.nmalloc_large += astats->astats.nmalloc_large;
498 sstats->astats.ndalloc_large += astats->astats.ndalloc_large;
Jason Evansdafde142010-03-17 16:27:39 -0700499 sstats->astats.nrequests_large += astats->astats.nrequests_large;
Jason Evans86815df2010-03-13 20:32:56 -0800500
501 for (i = 0; i < nlclasses; i++) {
Jason Evansdafde142010-03-17 16:27:39 -0700502 sstats->lstats[i].nmalloc += astats->lstats[i].nmalloc;
503 sstats->lstats[i].ndalloc += astats->lstats[i].ndalloc;
Jason Evans86815df2010-03-13 20:32:56 -0800504 sstats->lstats[i].nrequests += astats->lstats[i].nrequests;
Jason Evans86815df2010-03-13 20:32:56 -0800505 sstats->lstats[i].curruns += astats->lstats[i].curruns;
506 }
507
Jason Evansb1726102012-02-28 16:50:47 -0800508 for (i = 0; i < NBINS; i++) {
Jason Evans86815df2010-03-13 20:32:56 -0800509 sstats->bstats[i].allocated += astats->bstats[i].allocated;
510 sstats->bstats[i].nmalloc += astats->bstats[i].nmalloc;
511 sstats->bstats[i].ndalloc += astats->bstats[i].ndalloc;
512 sstats->bstats[i].nrequests += astats->bstats[i].nrequests;
Jason Evans7372b152012-02-10 20:22:09 -0800513 if (config_tcache) {
514 sstats->bstats[i].nfills += astats->bstats[i].nfills;
515 sstats->bstats[i].nflushes +=
516 astats->bstats[i].nflushes;
517 }
Jason Evans86815df2010-03-13 20:32:56 -0800518 sstats->bstats[i].nruns += astats->bstats[i].nruns;
519 sstats->bstats[i].reruns += astats->bstats[i].reruns;
Jason Evans86815df2010-03-13 20:32:56 -0800520 sstats->bstats[i].curruns += astats->bstats[i].curruns;
521 }
522}
Jason Evans86815df2010-03-13 20:32:56 -0800523
Jason Evans3c234352010-01-27 13:10:55 -0800524static void
525ctl_arena_refresh(arena_t *arena, unsigned i)
526{
527 ctl_arena_stats_t *astats = &ctl_stats.arenas[i];
Jason Evans609ae592012-10-11 13:53:15 -0700528 ctl_arena_stats_t *sstats = &ctl_stats.arenas[ctl_stats.narenas];
Jason Evans3c234352010-01-27 13:10:55 -0800529
530 ctl_arena_clear(astats);
531
Jason Evans597632b2011-03-18 13:41:33 -0700532 sstats->nthreads += astats->nthreads;
Jason Evans7372b152012-02-10 20:22:09 -0800533 if (config_stats) {
534 ctl_arena_stats_amerge(astats, arena);
535 /* Merge into sum stats as well. */
536 ctl_arena_stats_smerge(sstats, astats);
537 } else {
538 astats->pactive += arena->nactive;
539 astats->pdirty += arena->ndirty;
540 /* Merge into sum stats as well. */
541 sstats->pactive += arena->nactive;
542 sstats->pdirty += arena->ndirty;
543 }
Jason Evans3c234352010-01-27 13:10:55 -0800544}
545
Jason Evans609ae592012-10-11 13:53:15 -0700546static bool
547ctl_grow(void)
548{
549 size_t astats_size;
550 ctl_arena_stats_t *astats;
551 arena_t **tarenas;
552
553 /* Extend arena stats and arenas arrays. */
554 astats_size = (ctl_stats.narenas + 2) * sizeof(ctl_arena_stats_t);
555 if (ctl_stats.narenas == narenas_auto) {
556 /* ctl_stats.arenas and arenas came from base_alloc(). */
557 astats = (ctl_arena_stats_t *)imalloc(astats_size);
558 if (astats == NULL)
559 return (true);
560 memcpy(astats, ctl_stats.arenas, (ctl_stats.narenas + 1) *
561 sizeof(ctl_arena_stats_t));
562
563 tarenas = (arena_t **)imalloc((ctl_stats.narenas + 1) *
564 sizeof(arena_t *));
565 if (tarenas == NULL) {
566 idalloc(astats);
567 return (true);
568 }
569 memcpy(tarenas, arenas, ctl_stats.narenas * sizeof(arena_t *));
570 } else {
571 astats = (ctl_arena_stats_t *)iralloc(ctl_stats.arenas,
572 astats_size, 0, 0, false, false);
573 if (astats == NULL)
574 return (true);
575
576 tarenas = (arena_t **)iralloc(arenas, (ctl_stats.narenas + 1) *
577 sizeof(arena_t *), 0, 0, false, false);
578 if (tarenas == NULL)
579 return (true);
580 }
581 /* Initialize the new astats and arenas elements. */
582 memset(&astats[ctl_stats.narenas + 1], 0, sizeof(ctl_arena_stats_t));
583 if (ctl_arena_init(&astats[ctl_stats.narenas + 1]))
584 return (true);
585 tarenas[ctl_stats.narenas] = NULL;
586 /* Swap merged stats to their new location. */
587 {
588 ctl_arena_stats_t tstats;
589 memcpy(&tstats, &astats[ctl_stats.narenas],
590 sizeof(ctl_arena_stats_t));
591 memcpy(&astats[ctl_stats.narenas],
592 &astats[ctl_stats.narenas + 1], sizeof(ctl_arena_stats_t));
593 memcpy(&astats[ctl_stats.narenas + 1], &tstats,
594 sizeof(ctl_arena_stats_t));
595 }
596 ctl_stats.arenas = astats;
597 ctl_stats.narenas++;
598 malloc_mutex_lock(&arenas_lock);
599 arenas = tarenas;
600 narenas_total++;
601 arenas_extend(narenas_total - 1);
602 malloc_mutex_unlock(&arenas_lock);
603
604 return (false);
605}
606
Jason Evans3c234352010-01-27 13:10:55 -0800607static void
608ctl_refresh(void)
609{
610 unsigned i;
Jason Evans609ae592012-10-11 13:53:15 -0700611 VARIABLE_ARRAY(arena_t *, tarenas, ctl_stats.narenas);
Jason Evans3c234352010-01-27 13:10:55 -0800612
Jason Evans7372b152012-02-10 20:22:09 -0800613 if (config_stats) {
614 malloc_mutex_lock(&chunks_mtx);
615 ctl_stats.chunks.current = stats_chunks.curchunks;
616 ctl_stats.chunks.total = stats_chunks.nchunks;
617 ctl_stats.chunks.high = stats_chunks.highchunks;
618 malloc_mutex_unlock(&chunks_mtx);
Jason Evans3c234352010-01-27 13:10:55 -0800619
Jason Evans7372b152012-02-10 20:22:09 -0800620 malloc_mutex_lock(&huge_mtx);
621 ctl_stats.huge.allocated = huge_allocated;
622 ctl_stats.huge.nmalloc = huge_nmalloc;
623 ctl_stats.huge.ndalloc = huge_ndalloc;
624 malloc_mutex_unlock(&huge_mtx);
625 }
Jason Evans3c234352010-01-27 13:10:55 -0800626
627 /*
Jason Evans13668262010-01-31 03:57:29 -0800628 * Clear sum stats, since they will be merged into by
Jason Evans3c234352010-01-27 13:10:55 -0800629 * ctl_arena_refresh().
630 */
Jason Evans609ae592012-10-11 13:53:15 -0700631 ctl_stats.arenas[ctl_stats.narenas].nthreads = 0;
632 ctl_arena_clear(&ctl_stats.arenas[ctl_stats.narenas]);
Jason Evans3c234352010-01-27 13:10:55 -0800633
634 malloc_mutex_lock(&arenas_lock);
Jason Evans609ae592012-10-11 13:53:15 -0700635 memcpy(tarenas, arenas, sizeof(arena_t *) * ctl_stats.narenas);
636 for (i = 0; i < ctl_stats.narenas; i++) {
Jason Evans597632b2011-03-18 13:41:33 -0700637 if (arenas[i] != NULL)
638 ctl_stats.arenas[i].nthreads = arenas[i]->nthreads;
639 else
640 ctl_stats.arenas[i].nthreads = 0;
641 }
Jason Evans3c234352010-01-27 13:10:55 -0800642 malloc_mutex_unlock(&arenas_lock);
Jason Evans609ae592012-10-11 13:53:15 -0700643 for (i = 0; i < ctl_stats.narenas; i++) {
Jason Evans3c234352010-01-27 13:10:55 -0800644 bool initialized = (tarenas[i] != NULL);
645
646 ctl_stats.arenas[i].initialized = initialized;
647 if (initialized)
648 ctl_arena_refresh(tarenas[i], i);
649 }
650
Jason Evans7372b152012-02-10 20:22:09 -0800651 if (config_stats) {
Jason Evans609ae592012-10-11 13:53:15 -0700652 ctl_stats.allocated =
653 ctl_stats.arenas[ctl_stats.narenas].allocated_small
654 + ctl_stats.arenas[ctl_stats.narenas].astats.allocated_large
Jason Evans7372b152012-02-10 20:22:09 -0800655 + ctl_stats.huge.allocated;
Jason Evans609ae592012-10-11 13:53:15 -0700656 ctl_stats.active =
657 (ctl_stats.arenas[ctl_stats.narenas].pactive << LG_PAGE)
658 + ctl_stats.huge.allocated;
Jason Evans7372b152012-02-10 20:22:09 -0800659 ctl_stats.mapped = (ctl_stats.chunks.current << opt_lg_chunk);
Jason Evans7372b152012-02-10 20:22:09 -0800660 }
Jason Evans3c234352010-01-27 13:10:55 -0800661
662 ctl_epoch++;
663}
664
665static bool
666ctl_init(void)
667{
Jason Evansfc4dcfa2010-11-24 15:44:21 -0800668 bool ret;
Jason Evans3c234352010-01-27 13:10:55 -0800669
Jason Evansfc4dcfa2010-11-24 15:44:21 -0800670 malloc_mutex_lock(&ctl_mtx);
Jason Evans3c234352010-01-27 13:10:55 -0800671 if (ctl_initialized == false) {
Jason Evans3c234352010-01-27 13:10:55 -0800672 /*
673 * Allocate space for one extra arena stats element, which
674 * contains summed stats across all arenas.
675 */
Jason Evans609ae592012-10-11 13:53:15 -0700676 assert(narenas_auto == narenas_total_get());
677 ctl_stats.narenas = narenas_auto;
Jason Evans3c234352010-01-27 13:10:55 -0800678 ctl_stats.arenas = (ctl_arena_stats_t *)base_alloc(
Jason Evans609ae592012-10-11 13:53:15 -0700679 (ctl_stats.narenas + 1) * sizeof(ctl_arena_stats_t));
Jason Evansfc4dcfa2010-11-24 15:44:21 -0800680 if (ctl_stats.arenas == NULL) {
681 ret = true;
Jason Evansa1ee7832012-04-10 15:07:44 -0700682 goto label_return;
Jason Evansfc4dcfa2010-11-24 15:44:21 -0800683 }
Jason Evans609ae592012-10-11 13:53:15 -0700684 memset(ctl_stats.arenas, 0, (ctl_stats.narenas + 1) *
Jason Evans3c234352010-01-27 13:10:55 -0800685 sizeof(ctl_arena_stats_t));
686
687 /*
688 * Initialize all stats structures, regardless of whether they
689 * ever get used. Lazy initialization would allow errors to
690 * cause inconsistent state to be viewable by the application.
691 */
Jason Evans7372b152012-02-10 20:22:09 -0800692 if (config_stats) {
693 unsigned i;
Jason Evans609ae592012-10-11 13:53:15 -0700694 for (i = 0; i <= ctl_stats.narenas; i++) {
Jason Evans7372b152012-02-10 20:22:09 -0800695 if (ctl_arena_init(&ctl_stats.arenas[i])) {
696 ret = true;
Jason Evansa1ee7832012-04-10 15:07:44 -0700697 goto label_return;
Jason Evans7372b152012-02-10 20:22:09 -0800698 }
Jason Evansfc4dcfa2010-11-24 15:44:21 -0800699 }
Jason Evans3c234352010-01-27 13:10:55 -0800700 }
Jason Evans609ae592012-10-11 13:53:15 -0700701 ctl_stats.arenas[ctl_stats.narenas].initialized = true;
Jason Evans3c234352010-01-27 13:10:55 -0800702
703 ctl_epoch = 0;
704 ctl_refresh();
705 ctl_initialized = true;
706 }
707
Jason Evansfc4dcfa2010-11-24 15:44:21 -0800708 ret = false;
Jason Evansa1ee7832012-04-10 15:07:44 -0700709label_return:
Jason Evansfc4dcfa2010-11-24 15:44:21 -0800710 malloc_mutex_unlock(&ctl_mtx);
711 return (ret);
Jason Evans3c234352010-01-27 13:10:55 -0800712}
713
714static int
715ctl_lookup(const char *name, ctl_node_t const **nodesp, size_t *mibp,
716 size_t *depthp)
717{
718 int ret;
719 const char *elm, *tdot, *dot;
720 size_t elen, i, j;
Mike Hommey461ad5c2012-04-20 08:38:42 +0200721 const ctl_named_node_t *node;
Jason Evans3c234352010-01-27 13:10:55 -0800722
723 elm = name;
724 /* Equivalent to strchrnul(). */
725 dot = ((tdot = strchr(elm, '.')) != NULL) ? tdot : strchr(elm, '\0');
726 elen = (size_t)((uintptr_t)dot - (uintptr_t)elm);
727 if (elen == 0) {
728 ret = ENOENT;
Jason Evansa1ee7832012-04-10 15:07:44 -0700729 goto label_return;
Jason Evans3c234352010-01-27 13:10:55 -0800730 }
731 node = super_root_node;
732 for (i = 0; i < *depthp; i++) {
Mike Hommey461ad5c2012-04-20 08:38:42 +0200733 assert(node);
734 assert(node->nchildren > 0);
735 if (ctl_named_node(node->children) != NULL) {
736 const ctl_named_node_t *pnode = node;
Jason Evans3c234352010-01-27 13:10:55 -0800737
738 /* Children are named. */
Mike Hommey461ad5c2012-04-20 08:38:42 +0200739 for (j = 0; j < node->nchildren; j++) {
740 const ctl_named_node_t *child =
741 ctl_named_children(node, j);
742 if (strlen(child->name) == elen &&
743 strncmp(elm, child->name, elen) == 0) {
Jason Evans3c234352010-01-27 13:10:55 -0800744 node = child;
745 if (nodesp != NULL)
Mike Hommey461ad5c2012-04-20 08:38:42 +0200746 nodesp[i] =
747 (const ctl_node_t *)node;
Jason Evans3c234352010-01-27 13:10:55 -0800748 mibp[i] = j;
749 break;
750 }
751 }
752 if (node == pnode) {
753 ret = ENOENT;
Jason Evansa1ee7832012-04-10 15:07:44 -0700754 goto label_return;
Jason Evans3c234352010-01-27 13:10:55 -0800755 }
756 } else {
Jason Evans41b6afb2012-02-02 22:04:57 -0800757 uintmax_t index;
Mike Hommey461ad5c2012-04-20 08:38:42 +0200758 const ctl_indexed_node_t *inode;
Jason Evans3c234352010-01-27 13:10:55 -0800759
760 /* Children are indexed. */
Jason Evans41b6afb2012-02-02 22:04:57 -0800761 index = malloc_strtoumax(elm, NULL, 10);
762 if (index == UINTMAX_MAX || index > SIZE_T_MAX) {
Jason Evans3c234352010-01-27 13:10:55 -0800763 ret = ENOENT;
Jason Evansa1ee7832012-04-10 15:07:44 -0700764 goto label_return;
Jason Evans3c234352010-01-27 13:10:55 -0800765 }
766
Mike Hommey461ad5c2012-04-20 08:38:42 +0200767 inode = ctl_indexed_node(node->children);
768 node = inode->index(mibp, *depthp, (size_t)index);
Jason Evans3c234352010-01-27 13:10:55 -0800769 if (node == NULL) {
770 ret = ENOENT;
Jason Evansa1ee7832012-04-10 15:07:44 -0700771 goto label_return;
Jason Evans3c234352010-01-27 13:10:55 -0800772 }
773
774 if (nodesp != NULL)
Mike Hommey461ad5c2012-04-20 08:38:42 +0200775 nodesp[i] = (const ctl_node_t *)node;
Jason Evans3c234352010-01-27 13:10:55 -0800776 mibp[i] = (size_t)index;
777 }
778
779 if (node->ctl != NULL) {
780 /* Terminal node. */
781 if (*dot != '\0') {
782 /*
783 * The name contains more elements than are
784 * in this path through the tree.
785 */
786 ret = ENOENT;
Jason Evansa1ee7832012-04-10 15:07:44 -0700787 goto label_return;
Jason Evans3c234352010-01-27 13:10:55 -0800788 }
789 /* Complete lookup successful. */
790 *depthp = i + 1;
791 break;
792 }
793
794 /* Update elm. */
795 if (*dot == '\0') {
796 /* No more elements. */
797 ret = ENOENT;
Jason Evansa1ee7832012-04-10 15:07:44 -0700798 goto label_return;
Jason Evans3c234352010-01-27 13:10:55 -0800799 }
800 elm = &dot[1];
801 dot = ((tdot = strchr(elm, '.')) != NULL) ? tdot :
802 strchr(elm, '\0');
803 elen = (size_t)((uintptr_t)dot - (uintptr_t)elm);
804 }
805
806 ret = 0;
Jason Evansa1ee7832012-04-10 15:07:44 -0700807label_return:
Jason Evans3c234352010-01-27 13:10:55 -0800808 return (ret);
809}
810
811int
812ctl_byname(const char *name, void *oldp, size_t *oldlenp, void *newp,
813 size_t newlen)
814{
815 int ret;
816 size_t depth;
817 ctl_node_t const *nodes[CTL_MAX_DEPTH];
818 size_t mib[CTL_MAX_DEPTH];
Mike Hommey461ad5c2012-04-20 08:38:42 +0200819 const ctl_named_node_t *node;
Jason Evans3c234352010-01-27 13:10:55 -0800820
Jason Evansfc4dcfa2010-11-24 15:44:21 -0800821 if (ctl_initialized == false && ctl_init()) {
Jason Evans3c234352010-01-27 13:10:55 -0800822 ret = EAGAIN;
Jason Evansa1ee7832012-04-10 15:07:44 -0700823 goto label_return;
Jason Evans3c234352010-01-27 13:10:55 -0800824 }
825
826 depth = CTL_MAX_DEPTH;
827 ret = ctl_lookup(name, nodes, mib, &depth);
828 if (ret != 0)
Jason Evansa1ee7832012-04-10 15:07:44 -0700829 goto label_return;
Jason Evans3c234352010-01-27 13:10:55 -0800830
Mike Hommey461ad5c2012-04-20 08:38:42 +0200831 node = ctl_named_node(nodes[depth-1]);
832 if (node != NULL && node->ctl)
833 ret = node->ctl(mib, depth, oldp, oldlenp, newp, newlen);
834 else {
Jason Evans3c234352010-01-27 13:10:55 -0800835 /* The name refers to a partial path through the ctl tree. */
836 ret = ENOENT;
Jason Evans3c234352010-01-27 13:10:55 -0800837 }
Jason Evans3c234352010-01-27 13:10:55 -0800838
Jason Evansa1ee7832012-04-10 15:07:44 -0700839label_return:
Jason Evans3c234352010-01-27 13:10:55 -0800840 return(ret);
841}
842
843int
844ctl_nametomib(const char *name, size_t *mibp, size_t *miblenp)
845{
846 int ret;
847
Jason Evansfc4dcfa2010-11-24 15:44:21 -0800848 if (ctl_initialized == false && ctl_init()) {
Jason Evans3c234352010-01-27 13:10:55 -0800849 ret = EAGAIN;
Jason Evansa1ee7832012-04-10 15:07:44 -0700850 goto label_return;
Jason Evans3c234352010-01-27 13:10:55 -0800851 }
852
853 ret = ctl_lookup(name, NULL, mibp, miblenp);
Jason Evansa1ee7832012-04-10 15:07:44 -0700854label_return:
Jason Evans3c234352010-01-27 13:10:55 -0800855 return(ret);
856}
857
858int
859ctl_bymib(const size_t *mib, size_t miblen, void *oldp, size_t *oldlenp,
860 void *newp, size_t newlen)
861{
862 int ret;
Mike Hommey461ad5c2012-04-20 08:38:42 +0200863 const ctl_named_node_t *node;
Jason Evans3c234352010-01-27 13:10:55 -0800864 size_t i;
865
Jason Evansfc4dcfa2010-11-24 15:44:21 -0800866 if (ctl_initialized == false && ctl_init()) {
Jason Evans3c234352010-01-27 13:10:55 -0800867 ret = EAGAIN;
Jason Evansa1ee7832012-04-10 15:07:44 -0700868 goto label_return;
Jason Evans3c234352010-01-27 13:10:55 -0800869 }
870
871 /* Iterate down the tree. */
872 node = super_root_node;
873 for (i = 0; i < miblen; i++) {
Mike Hommey461ad5c2012-04-20 08:38:42 +0200874 assert(node);
875 assert(node->nchildren > 0);
876 if (ctl_named_node(node->children) != NULL) {
Jason Evans3c234352010-01-27 13:10:55 -0800877 /* Children are named. */
Mike Hommey461ad5c2012-04-20 08:38:42 +0200878 if (node->nchildren <= mib[i]) {
Jason Evans3c234352010-01-27 13:10:55 -0800879 ret = ENOENT;
Jason Evansa1ee7832012-04-10 15:07:44 -0700880 goto label_return;
Jason Evans3c234352010-01-27 13:10:55 -0800881 }
Mike Hommey461ad5c2012-04-20 08:38:42 +0200882 node = ctl_named_children(node, mib[i]);
Jason Evans3c234352010-01-27 13:10:55 -0800883 } else {
Mike Hommey461ad5c2012-04-20 08:38:42 +0200884 const ctl_indexed_node_t *inode;
Jason Evans3c234352010-01-27 13:10:55 -0800885
886 /* Indexed element. */
Mike Hommey461ad5c2012-04-20 08:38:42 +0200887 inode = ctl_indexed_node(node->children);
888 node = inode->index(mib, miblen, mib[i]);
Jason Evans3c234352010-01-27 13:10:55 -0800889 if (node == NULL) {
890 ret = ENOENT;
Jason Evansa1ee7832012-04-10 15:07:44 -0700891 goto label_return;
Jason Evans3c234352010-01-27 13:10:55 -0800892 }
893 }
894 }
895
896 /* Call the ctl function. */
Mike Hommey461ad5c2012-04-20 08:38:42 +0200897 if (node && node->ctl)
898 ret = node->ctl(mib, miblen, oldp, oldlenp, newp, newlen);
899 else {
Jason Evans3c234352010-01-27 13:10:55 -0800900 /* Partial MIB. */
901 ret = ENOENT;
Jason Evans3c234352010-01-27 13:10:55 -0800902 }
Jason Evans3c234352010-01-27 13:10:55 -0800903
Jason Evansa1ee7832012-04-10 15:07:44 -0700904label_return:
Jason Evans3c234352010-01-27 13:10:55 -0800905 return(ret);
906}
907
908bool
909ctl_boot(void)
910{
911
912 if (malloc_mutex_init(&ctl_mtx))
913 return (true);
914
915 ctl_initialized = false;
916
917 return (false);
918}
919
Jason Evans20f1fc92012-10-09 14:46:22 -0700920void
921ctl_prefork(void)
922{
923
924 malloc_mutex_lock(&ctl_mtx);
925}
926
927void
928ctl_postfork_parent(void)
929{
930
931 malloc_mutex_postfork_parent(&ctl_mtx);
932}
933
934void
935ctl_postfork_child(void)
936{
937
938 malloc_mutex_postfork_child(&ctl_mtx);
939}
940
Jason Evans3c234352010-01-27 13:10:55 -0800941/******************************************************************************/
942/* *_ctl() functions. */
943
944#define READONLY() do { \
945 if (newp != NULL || newlen != 0) { \
946 ret = EPERM; \
Jason Evans6b9ed672012-04-25 13:12:46 -0700947 goto label_return; \
Jason Evans3c234352010-01-27 13:10:55 -0800948 } \
949} while (0)
950
Jason Evans22ca8552010-03-02 11:57:30 -0800951#define WRITEONLY() do { \
Jason Evans3c234352010-01-27 13:10:55 -0800952 if (oldp != NULL || oldlenp != NULL) { \
953 ret = EPERM; \
Jason Evans6b9ed672012-04-25 13:12:46 -0700954 goto label_return; \
Jason Evans3c234352010-01-27 13:10:55 -0800955 } \
956} while (0)
957
958#define READ(v, t) do { \
959 if (oldp != NULL && oldlenp != NULL) { \
960 if (*oldlenp != sizeof(t)) { \
961 size_t copylen = (sizeof(t) <= *oldlenp) \
962 ? sizeof(t) : *oldlenp; \
Jason Evans6eb84fb2012-11-29 22:13:04 -0800963 memcpy(oldp, (void *)&(v), copylen); \
Jason Evans3c234352010-01-27 13:10:55 -0800964 ret = EINVAL; \
Jason Evans6b9ed672012-04-25 13:12:46 -0700965 goto label_return; \
Jason Evans3c234352010-01-27 13:10:55 -0800966 } else \
Jason Evans6eb84fb2012-11-29 22:13:04 -0800967 *(t *)oldp = (v); \
Jason Evans3c234352010-01-27 13:10:55 -0800968 } \
969} while (0)
970
971#define WRITE(v, t) do { \
972 if (newp != NULL) { \
973 if (newlen != sizeof(t)) { \
974 ret = EINVAL; \
Jason Evans6b9ed672012-04-25 13:12:46 -0700975 goto label_return; \
Jason Evans3c234352010-01-27 13:10:55 -0800976 } \
Jason Evans6eb84fb2012-11-29 22:13:04 -0800977 (v) = *(t *)newp; \
Jason Evans3c234352010-01-27 13:10:55 -0800978 } \
979} while (0)
980
Jason Evans7372b152012-02-10 20:22:09 -0800981/*
982 * There's a lot of code duplication in the following macros due to limitations
983 * in how nested cpp macros are expanded.
984 */
985#define CTL_RO_CLGEN(c, l, n, v, t) \
986static int \
987n##_ctl(const size_t *mib, size_t miblen, void *oldp, size_t *oldlenp, \
988 void *newp, size_t newlen) \
989{ \
990 int ret; \
991 t oldval; \
992 \
993 if ((c) == false) \
994 return (ENOENT); \
995 if (l) \
996 malloc_mutex_lock(&ctl_mtx); \
997 READONLY(); \
Jason Evans6eb84fb2012-11-29 22:13:04 -0800998 oldval = (v); \
Jason Evans7372b152012-02-10 20:22:09 -0800999 READ(oldval, t); \
1000 \
1001 ret = 0; \
Jason Evans6b9ed672012-04-25 13:12:46 -07001002label_return: \
Jason Evans7372b152012-02-10 20:22:09 -08001003 if (l) \
1004 malloc_mutex_unlock(&ctl_mtx); \
1005 return (ret); \
1006}
1007
1008#define CTL_RO_CGEN(c, n, v, t) \
1009static int \
1010n##_ctl(const size_t *mib, size_t miblen, void *oldp, size_t *oldlenp, \
1011 void *newp, size_t newlen) \
1012{ \
1013 int ret; \
1014 t oldval; \
1015 \
1016 if ((c) == false) \
1017 return (ENOENT); \
1018 malloc_mutex_lock(&ctl_mtx); \
1019 READONLY(); \
Jason Evans6eb84fb2012-11-29 22:13:04 -08001020 oldval = (v); \
Jason Evans7372b152012-02-10 20:22:09 -08001021 READ(oldval, t); \
1022 \
1023 ret = 0; \
Jason Evans6b9ed672012-04-25 13:12:46 -07001024label_return: \
Jason Evans7372b152012-02-10 20:22:09 -08001025 malloc_mutex_unlock(&ctl_mtx); \
1026 return (ret); \
1027}
1028
Jason Evans3c234352010-01-27 13:10:55 -08001029#define CTL_RO_GEN(n, v, t) \
1030static int \
1031n##_ctl(const size_t *mib, size_t miblen, void *oldp, size_t *oldlenp, \
1032 void *newp, size_t newlen) \
1033{ \
1034 int ret; \
1035 t oldval; \
1036 \
Jason Evansfc4dcfa2010-11-24 15:44:21 -08001037 malloc_mutex_lock(&ctl_mtx); \
1038 READONLY(); \
Jason Evans6eb84fb2012-11-29 22:13:04 -08001039 oldval = (v); \
Jason Evansfc4dcfa2010-11-24 15:44:21 -08001040 READ(oldval, t); \
1041 \
1042 ret = 0; \
Jason Evans6b9ed672012-04-25 13:12:46 -07001043label_return: \
Jason Evansfc4dcfa2010-11-24 15:44:21 -08001044 malloc_mutex_unlock(&ctl_mtx); \
1045 return (ret); \
1046}
1047
1048/*
1049 * ctl_mtx is not acquired, under the assumption that no pertinent data will
1050 * mutate during the call.
1051 */
Jason Evans7372b152012-02-10 20:22:09 -08001052#define CTL_RO_NL_CGEN(c, n, v, t) \
1053static int \
1054n##_ctl(const size_t *mib, size_t miblen, void *oldp, size_t *oldlenp, \
1055 void *newp, size_t newlen) \
1056{ \
1057 int ret; \
1058 t oldval; \
1059 \
1060 if ((c) == false) \
1061 return (ENOENT); \
1062 READONLY(); \
Jason Evans6eb84fb2012-11-29 22:13:04 -08001063 oldval = (v); \
Jason Evans7372b152012-02-10 20:22:09 -08001064 READ(oldval, t); \
1065 \
1066 ret = 0; \
Jason Evans6b9ed672012-04-25 13:12:46 -07001067label_return: \
Jason Evans7372b152012-02-10 20:22:09 -08001068 return (ret); \
1069}
1070
1071#define CTL_RO_NL_GEN(n, v, t) \
Jason Evansfc4dcfa2010-11-24 15:44:21 -08001072static int \
1073n##_ctl(const size_t *mib, size_t miblen, void *oldp, size_t *oldlenp, \
1074 void *newp, size_t newlen) \
1075{ \
1076 int ret; \
1077 t oldval; \
1078 \
Jason Evans3c234352010-01-27 13:10:55 -08001079 READONLY(); \
Jason Evans6eb84fb2012-11-29 22:13:04 -08001080 oldval = (v); \
Jason Evans3c234352010-01-27 13:10:55 -08001081 READ(oldval, t); \
1082 \
1083 ret = 0; \
Jason Evans6b9ed672012-04-25 13:12:46 -07001084label_return: \
Jason Evans3c234352010-01-27 13:10:55 -08001085 return (ret); \
1086}
1087
Jason Evans7372b152012-02-10 20:22:09 -08001088#define CTL_RO_BOOL_CONFIG_GEN(n) \
Jason Evans3c234352010-01-27 13:10:55 -08001089static int \
1090n##_ctl(const size_t *mib, size_t miblen, void *oldp, size_t *oldlenp, \
1091 void *newp, size_t newlen) \
1092{ \
1093 int ret; \
1094 bool oldval; \
1095 \
1096 READONLY(); \
Jason Evans7372b152012-02-10 20:22:09 -08001097 oldval = n; \
Jason Evans3c234352010-01-27 13:10:55 -08001098 READ(oldval, bool); \
1099 \
1100 ret = 0; \
Jason Evans6b9ed672012-04-25 13:12:46 -07001101label_return: \
Jason Evans3c234352010-01-27 13:10:55 -08001102 return (ret); \
1103}
1104
Jason Evansfc4dcfa2010-11-24 15:44:21 -08001105CTL_RO_NL_GEN(version, JEMALLOC_VERSION, const char *)
Jason Evansa40bc7a2010-03-02 13:01:16 -08001106
Jason Evans3c234352010-01-27 13:10:55 -08001107static int
1108epoch_ctl(const size_t *mib, size_t miblen, void *oldp, size_t *oldlenp,
1109 void *newp, size_t newlen)
1110{
1111 int ret;
1112 uint64_t newval;
1113
Jason Evansfc4dcfa2010-11-24 15:44:21 -08001114 malloc_mutex_lock(&ctl_mtx);
Jason Evans3c234352010-01-27 13:10:55 -08001115 WRITE(newval, uint64_t);
Jason Evans6b9ed672012-04-25 13:12:46 -07001116 if (newp != NULL)
Jason Evans3c234352010-01-27 13:10:55 -08001117 ctl_refresh();
1118 READ(ctl_epoch, uint64_t);
1119
1120 ret = 0;
Jason Evansa1ee7832012-04-10 15:07:44 -07001121label_return:
Jason Evansfc4dcfa2010-11-24 15:44:21 -08001122 malloc_mutex_unlock(&ctl_mtx);
Jason Evans3c234352010-01-27 13:10:55 -08001123 return (ret);
1124}
1125
Jason Evans3c234352010-01-27 13:10:55 -08001126static int
Jason Evansd4be8b72012-03-26 18:54:44 -07001127thread_tcache_enabled_ctl(const size_t *mib, size_t miblen, void *oldp,
1128 size_t *oldlenp, void *newp, size_t newlen)
1129{
1130 int ret;
1131 bool oldval;
1132
1133 if (config_tcache == false)
1134 return (ENOENT);
1135
1136 oldval = tcache_enabled_get();
1137 if (newp != NULL) {
1138 if (newlen != sizeof(bool)) {
1139 ret = EINVAL;
Jason Evansa1ee7832012-04-10 15:07:44 -07001140 goto label_return;
Jason Evansd4be8b72012-03-26 18:54:44 -07001141 }
1142 tcache_enabled_set(*(bool *)newp);
1143 }
1144 READ(oldval, bool);
1145
Jason Evansd4be8b72012-03-26 18:54:44 -07001146 ret = 0;
Jason Evansf4c3f852012-10-08 15:48:04 -07001147label_return:
Jason Evansd4be8b72012-03-26 18:54:44 -07001148 return (ret);
1149}
1150
1151static int
Jason Evanse7b8fa12012-03-16 17:09:32 -07001152thread_tcache_flush_ctl(const size_t *mib, size_t miblen, void *oldp,
1153 size_t *oldlenp, void *newp, size_t newlen)
Jason Evans3c234352010-01-27 13:10:55 -08001154{
1155 int ret;
Jason Evans3c234352010-01-27 13:10:55 -08001156
Jason Evans7372b152012-02-10 20:22:09 -08001157 if (config_tcache == false)
1158 return (ENOENT);
1159
Mike Hommeyaf04b742012-04-30 12:38:25 +02001160 READONLY();
1161 WRITEONLY();
Jason Evans3c234352010-01-27 13:10:55 -08001162
Jason Evansd4be8b72012-03-26 18:54:44 -07001163 tcache_flush();
Jason Evans3c234352010-01-27 13:10:55 -08001164
1165 ret = 0;
Jason Evansa1ee7832012-04-10 15:07:44 -07001166label_return:
Jason Evans3c234352010-01-27 13:10:55 -08001167 return (ret);
1168}
Jason Evans3c234352010-01-27 13:10:55 -08001169
Jason Evansb267d0f2010-08-13 15:42:29 -07001170static int
1171thread_arena_ctl(const size_t *mib, size_t miblen, void *oldp, size_t *oldlenp,
1172 void *newp, size_t newlen)
1173{
1174 int ret;
1175 unsigned newind, oldind;
1176
Jason Evans609ae592012-10-11 13:53:15 -07001177 malloc_mutex_lock(&ctl_mtx);
Jason Evans01b3fe52012-04-03 09:28:00 -07001178 newind = oldind = choose_arena(NULL)->ind;
Jason Evansa7153a02011-03-14 11:39:49 -07001179 WRITE(newind, unsigned);
1180 READ(oldind, unsigned);
Jason Evansb267d0f2010-08-13 15:42:29 -07001181 if (newind != oldind) {
1182 arena_t *arena;
1183
Jason Evans609ae592012-10-11 13:53:15 -07001184 if (newind >= ctl_stats.narenas) {
Jason Evansb267d0f2010-08-13 15:42:29 -07001185 /* New arena index is out of range. */
1186 ret = EFAULT;
Jason Evansa1ee7832012-04-10 15:07:44 -07001187 goto label_return;
Jason Evansb267d0f2010-08-13 15:42:29 -07001188 }
1189
1190 /* Initialize arena if necessary. */
1191 malloc_mutex_lock(&arenas_lock);
Jason Evanscd9a1342012-03-21 18:33:03 -07001192 if ((arena = arenas[newind]) == NULL && (arena =
1193 arenas_extend(newind)) == NULL) {
1194 malloc_mutex_unlock(&arenas_lock);
Jason Evansb267d0f2010-08-13 15:42:29 -07001195 ret = EAGAIN;
Jason Evansa1ee7832012-04-10 15:07:44 -07001196 goto label_return;
Jason Evansb267d0f2010-08-13 15:42:29 -07001197 }
Jason Evanscd9a1342012-03-21 18:33:03 -07001198 assert(arena == arenas[newind]);
1199 arenas[oldind]->nthreads--;
1200 arenas[newind]->nthreads++;
1201 malloc_mutex_unlock(&arenas_lock);
Jason Evansb267d0f2010-08-13 15:42:29 -07001202
1203 /* Set new arena association. */
Jason Evans7372b152012-02-10 20:22:09 -08001204 if (config_tcache) {
Jason Evanscd9a1342012-03-21 18:33:03 -07001205 tcache_t *tcache;
Jason Evans122449b2012-04-06 00:35:09 -07001206 if ((uintptr_t)(tcache = *tcache_tsd_get()) >
1207 (uintptr_t)TCACHE_STATE_MAX) {
Jason Evanscd9a1342012-03-21 18:33:03 -07001208 tcache_arena_dissociate(tcache);
1209 tcache_arena_associate(tcache, arena);
1210 }
Jason Evans624f2f32010-12-29 12:21:05 -08001211 }
Jason Evanscd9a1342012-03-21 18:33:03 -07001212 arenas_tsd_set(&arena);
Jason Evansb267d0f2010-08-13 15:42:29 -07001213 }
1214
1215 ret = 0;
Jason Evansa1ee7832012-04-10 15:07:44 -07001216label_return:
Jason Evans609ae592012-10-11 13:53:15 -07001217 malloc_mutex_unlock(&ctl_mtx);
Jason Evansb267d0f2010-08-13 15:42:29 -07001218 return (ret);
1219}
Jason Evansb267d0f2010-08-13 15:42:29 -07001220
Jason Evanscd9a1342012-03-21 18:33:03 -07001221CTL_RO_NL_CGEN(config_stats, thread_allocated,
1222 thread_allocated_tsd_get()->allocated, uint64_t)
1223CTL_RO_NL_CGEN(config_stats, thread_allocatedp,
1224 &thread_allocated_tsd_get()->allocated, uint64_t *)
1225CTL_RO_NL_CGEN(config_stats, thread_deallocated,
1226 thread_allocated_tsd_get()->deallocated, uint64_t)
1227CTL_RO_NL_CGEN(config_stats, thread_deallocatedp,
1228 &thread_allocated_tsd_get()->deallocated, uint64_t *)
Jason Evans93443682010-10-20 17:39:18 -07001229
Jason Evans3c234352010-01-27 13:10:55 -08001230/******************************************************************************/
1231
Jason Evans7372b152012-02-10 20:22:09 -08001232CTL_RO_BOOL_CONFIG_GEN(config_debug)
1233CTL_RO_BOOL_CONFIG_GEN(config_dss)
Jason Evans7372b152012-02-10 20:22:09 -08001234CTL_RO_BOOL_CONFIG_GEN(config_fill)
1235CTL_RO_BOOL_CONFIG_GEN(config_lazy_lock)
Jason Evans2e671ff2012-05-09 16:12:00 -07001236CTL_RO_BOOL_CONFIG_GEN(config_mremap)
Jason Evans59ae2762012-04-16 17:52:27 -07001237CTL_RO_BOOL_CONFIG_GEN(config_munmap)
Jason Evans7372b152012-02-10 20:22:09 -08001238CTL_RO_BOOL_CONFIG_GEN(config_prof)
1239CTL_RO_BOOL_CONFIG_GEN(config_prof_libgcc)
1240CTL_RO_BOOL_CONFIG_GEN(config_prof_libunwind)
1241CTL_RO_BOOL_CONFIG_GEN(config_stats)
Jason Evans7372b152012-02-10 20:22:09 -08001242CTL_RO_BOOL_CONFIG_GEN(config_tcache)
Jason Evans7372b152012-02-10 20:22:09 -08001243CTL_RO_BOOL_CONFIG_GEN(config_tls)
Jason Evansb1476112012-04-05 13:36:17 -07001244CTL_RO_BOOL_CONFIG_GEN(config_utrace)
Jason Evans122449b2012-04-06 00:35:09 -07001245CTL_RO_BOOL_CONFIG_GEN(config_valgrind)
Jason Evans7372b152012-02-10 20:22:09 -08001246CTL_RO_BOOL_CONFIG_GEN(config_xmalloc)
Jason Evans3c234352010-01-27 13:10:55 -08001247
1248/******************************************************************************/
1249
Jason Evansfc4dcfa2010-11-24 15:44:21 -08001250CTL_RO_NL_GEN(opt_abort, opt_abort, bool)
Jason Evans609ae592012-10-11 13:53:15 -07001251CTL_RO_NL_GEN(opt_dss, opt_dss, const char *)
Jason Evansfc4dcfa2010-11-24 15:44:21 -08001252CTL_RO_NL_GEN(opt_lg_chunk, opt_lg_chunk, size_t)
1253CTL_RO_NL_GEN(opt_narenas, opt_narenas, size_t)
1254CTL_RO_NL_GEN(opt_lg_dirty_mult, opt_lg_dirty_mult, ssize_t)
1255CTL_RO_NL_GEN(opt_stats_print, opt_stats_print, bool)
Jason Evans7372b152012-02-10 20:22:09 -08001256CTL_RO_NL_CGEN(config_fill, opt_junk, opt_junk, bool)
1257CTL_RO_NL_CGEN(config_fill, opt_zero, opt_zero, bool)
Jason Evans122449b2012-04-06 00:35:09 -07001258CTL_RO_NL_CGEN(config_fill, opt_quarantine, opt_quarantine, size_t)
1259CTL_RO_NL_CGEN(config_fill, opt_redzone, opt_redzone, bool)
Jason Evansb1476112012-04-05 13:36:17 -07001260CTL_RO_NL_CGEN(config_utrace, opt_utrace, opt_utrace, bool)
Jason Evans122449b2012-04-06 00:35:09 -07001261CTL_RO_NL_CGEN(config_valgrind, opt_valgrind, opt_valgrind, bool)
Jason Evans7372b152012-02-10 20:22:09 -08001262CTL_RO_NL_CGEN(config_xmalloc, opt_xmalloc, opt_xmalloc, bool)
1263CTL_RO_NL_CGEN(config_tcache, opt_tcache, opt_tcache, bool)
Jason Evansf3ca7c82012-04-04 16:16:09 -07001264CTL_RO_NL_CGEN(config_tcache, opt_lg_tcache_max, opt_lg_tcache_max, ssize_t)
Jason Evans7372b152012-02-10 20:22:09 -08001265CTL_RO_NL_CGEN(config_prof, opt_prof, opt_prof, bool)
1266CTL_RO_NL_CGEN(config_prof, opt_prof_prefix, opt_prof_prefix, const char *)
1267CTL_RO_CGEN(config_prof, opt_prof_active, opt_prof_active, bool) /* Mutable. */
Jason Evans7372b152012-02-10 20:22:09 -08001268CTL_RO_NL_CGEN(config_prof, opt_lg_prof_sample, opt_lg_prof_sample, size_t)
1269CTL_RO_NL_CGEN(config_prof, opt_lg_prof_interval, opt_lg_prof_interval, ssize_t)
1270CTL_RO_NL_CGEN(config_prof, opt_prof_gdump, opt_prof_gdump, bool)
Jason Evans0b25fe72012-04-17 16:39:33 -07001271CTL_RO_NL_CGEN(config_prof, opt_prof_final, opt_prof_final, bool)
Jason Evans7372b152012-02-10 20:22:09 -08001272CTL_RO_NL_CGEN(config_prof, opt_prof_leak, opt_prof_leak, bool)
1273CTL_RO_NL_CGEN(config_prof, opt_prof_accum, opt_prof_accum, bool)
Jason Evans3c234352010-01-27 13:10:55 -08001274
1275/******************************************************************************/
1276
Jason Evans34457f52012-11-03 21:18:28 -07001277/* ctl_mutex must be held during execution of this function. */
1278static void
Jason Evans609ae592012-10-11 13:53:15 -07001279arena_purge(unsigned arena_ind)
1280{
Jason Evans34457f52012-11-03 21:18:28 -07001281 VARIABLE_ARRAY(arena_t *, tarenas, ctl_stats.narenas);
Jason Evans609ae592012-10-11 13:53:15 -07001282
Jason Evans34457f52012-11-03 21:18:28 -07001283 malloc_mutex_lock(&arenas_lock);
1284 memcpy(tarenas, arenas, sizeof(arena_t *) * ctl_stats.narenas);
1285 malloc_mutex_unlock(&arenas_lock);
Jason Evans609ae592012-10-11 13:53:15 -07001286
Jason Evans34457f52012-11-03 21:18:28 -07001287 if (arena_ind == ctl_stats.narenas) {
1288 unsigned i;
1289 for (i = 0; i < ctl_stats.narenas; i++) {
1290 if (tarenas[i] != NULL)
1291 arena_purge_all(tarenas[i]);
Jason Evans609ae592012-10-11 13:53:15 -07001292 }
Jason Evans34457f52012-11-03 21:18:28 -07001293 } else {
1294 assert(arena_ind < ctl_stats.narenas);
1295 if (tarenas[arena_ind] != NULL)
1296 arena_purge_all(tarenas[arena_ind]);
Jason Evans609ae592012-10-11 13:53:15 -07001297 }
Jason Evans609ae592012-10-11 13:53:15 -07001298}
1299
1300static int
1301arena_i_purge_ctl(const size_t *mib, size_t miblen, void *oldp, size_t *oldlenp,
1302 void *newp, size_t newlen)
1303{
1304 int ret;
1305
1306 READONLY();
1307 WRITEONLY();
Jason Evans34457f52012-11-03 21:18:28 -07001308 malloc_mutex_lock(&ctl_mtx);
1309 arena_purge(mib[1]);
1310 malloc_mutex_unlock(&ctl_mtx);
Jason Evans609ae592012-10-11 13:53:15 -07001311
Jason Evans34457f52012-11-03 21:18:28 -07001312 ret = 0;
Jason Evans609ae592012-10-11 13:53:15 -07001313label_return:
1314 return (ret);
1315}
1316
1317static int
1318arena_i_dss_ctl(const size_t *mib, size_t miblen, void *oldp, size_t *oldlenp,
1319 void *newp, size_t newlen)
1320{
1321 int ret, i;
1322 bool match, err;
1323 const char *dss;
1324 unsigned arena_ind = mib[1];
1325 dss_prec_t dss_prec_old = dss_prec_limit;
1326 dss_prec_t dss_prec = dss_prec_limit;
1327
Jason Evansa5c80f82012-10-15 12:48:59 -07001328 malloc_mutex_lock(&ctl_mtx);
Jason Evans609ae592012-10-11 13:53:15 -07001329 WRITE(dss, const char *);
1330 match = false;
1331 for (i = 0; i < dss_prec_limit; i++) {
1332 if (strcmp(dss_prec_names[i], dss) == 0) {
1333 dss_prec = i;
1334 match = true;
1335 break;
1336 }
1337 }
1338 if (match == false) {
1339 ret = EINVAL;
1340 goto label_return;
1341 }
1342
1343 if (arena_ind < ctl_stats.narenas) {
1344 arena_t *arena = arenas[arena_ind];
1345 if (arena != NULL) {
1346 dss_prec_old = arena_dss_prec_get(arena);
1347 arena_dss_prec_set(arena, dss_prec);
1348 err = false;
1349 } else
1350 err = true;
1351 } else {
1352 dss_prec_old = chunk_dss_prec_get();
1353 err = chunk_dss_prec_set(dss_prec);
1354 }
1355 dss = dss_prec_names[dss_prec_old];
1356 READ(dss, const char *);
1357 if (err) {
1358 ret = EFAULT;
1359 goto label_return;
1360 }
1361
1362 ret = 0;
1363label_return:
Jason Evansa5c80f82012-10-15 12:48:59 -07001364 malloc_mutex_unlock(&ctl_mtx);
Jason Evans609ae592012-10-11 13:53:15 -07001365 return (ret);
1366}
1367
1368static const ctl_named_node_t *
1369arena_i_index(const size_t *mib, size_t miblen, size_t i)
1370{
1371 const ctl_named_node_t * ret;
1372
1373 malloc_mutex_lock(&ctl_mtx);
1374 if (i > ctl_stats.narenas) {
1375 ret = NULL;
1376 goto label_return;
1377 }
1378
1379 ret = super_arena_i_node;
1380label_return:
1381 malloc_mutex_unlock(&ctl_mtx);
1382 return (ret);
1383}
1384
1385
1386/******************************************************************************/
1387
Jason Evans49f7e8f2011-03-15 13:59:15 -07001388CTL_RO_NL_GEN(arenas_bin_i_size, arena_bin_info[mib[2]].reg_size, size_t)
1389CTL_RO_NL_GEN(arenas_bin_i_nregs, arena_bin_info[mib[2]].nregs, uint32_t)
1390CTL_RO_NL_GEN(arenas_bin_i_run_size, arena_bin_info[mib[2]].run_size, size_t)
Jason Evans609ae592012-10-11 13:53:15 -07001391static const ctl_named_node_t *
Jason Evans3c234352010-01-27 13:10:55 -08001392arenas_bin_i_index(const size_t *mib, size_t miblen, size_t i)
1393{
1394
Jason Evansb1726102012-02-28 16:50:47 -08001395 if (i > NBINS)
Jason Evans3c234352010-01-27 13:10:55 -08001396 return (NULL);
1397 return (super_arenas_bin_i_node);
1398}
1399
Jason Evansae4c7b42012-04-02 07:04:34 -07001400CTL_RO_NL_GEN(arenas_lrun_i_size, ((mib[2]+1) << LG_PAGE), size_t)
Jason Evans609ae592012-10-11 13:53:15 -07001401static const ctl_named_node_t *
Jason Evans3c234352010-01-27 13:10:55 -08001402arenas_lrun_i_index(const size_t *mib, size_t miblen, size_t i)
1403{
1404
1405 if (i > nlclasses)
1406 return (NULL);
1407 return (super_arenas_lrun_i_node);
1408}
1409
Jason Evans609ae592012-10-11 13:53:15 -07001410static int
1411arenas_narenas_ctl(const size_t *mib, size_t miblen, void *oldp,
1412 size_t *oldlenp, void *newp, size_t newlen)
1413{
1414 int ret;
1415 unsigned narenas;
1416
1417 malloc_mutex_lock(&ctl_mtx);
1418 READONLY();
1419 if (*oldlenp != sizeof(unsigned)) {
1420 ret = EINVAL;
1421 goto label_return;
1422 }
1423 narenas = ctl_stats.narenas;
1424 READ(narenas, unsigned);
1425
1426 ret = 0;
1427label_return:
1428 malloc_mutex_unlock(&ctl_mtx);
1429 return (ret);
1430}
Jason Evans3c234352010-01-27 13:10:55 -08001431
1432static int
1433arenas_initialized_ctl(const size_t *mib, size_t miblen, void *oldp,
1434 size_t *oldlenp, void *newp, size_t newlen)
1435{
1436 int ret;
1437 unsigned nread, i;
1438
Jason Evansfc4dcfa2010-11-24 15:44:21 -08001439 malloc_mutex_lock(&ctl_mtx);
Jason Evans3c234352010-01-27 13:10:55 -08001440 READONLY();
Jason Evans609ae592012-10-11 13:53:15 -07001441 if (*oldlenp != ctl_stats.narenas * sizeof(bool)) {
Jason Evans3c234352010-01-27 13:10:55 -08001442 ret = EINVAL;
Jason Evans609ae592012-10-11 13:53:15 -07001443 nread = (*oldlenp < ctl_stats.narenas * sizeof(bool))
1444 ? (*oldlenp / sizeof(bool)) : ctl_stats.narenas;
Jason Evans3c234352010-01-27 13:10:55 -08001445 } else {
1446 ret = 0;
Jason Evans609ae592012-10-11 13:53:15 -07001447 nread = ctl_stats.narenas;
Jason Evans3c234352010-01-27 13:10:55 -08001448 }
1449
1450 for (i = 0; i < nread; i++)
1451 ((bool *)oldp)[i] = ctl_stats.arenas[i].initialized;
1452
Jason Evansa1ee7832012-04-10 15:07:44 -07001453label_return:
Jason Evansfc4dcfa2010-11-24 15:44:21 -08001454 malloc_mutex_unlock(&ctl_mtx);
Jason Evans3c234352010-01-27 13:10:55 -08001455 return (ret);
1456}
1457
Jason Evansfc4dcfa2010-11-24 15:44:21 -08001458CTL_RO_NL_GEN(arenas_quantum, QUANTUM, size_t)
Jason Evansae4c7b42012-04-02 07:04:34 -07001459CTL_RO_NL_GEN(arenas_page, PAGE, size_t)
Jason Evans7372b152012-02-10 20:22:09 -08001460CTL_RO_NL_CGEN(config_tcache, arenas_tcache_max, tcache_maxclass, size_t)
Jason Evansb1726102012-02-28 16:50:47 -08001461CTL_RO_NL_GEN(arenas_nbins, NBINS, unsigned)
Jason Evans7372b152012-02-10 20:22:09 -08001462CTL_RO_NL_CGEN(config_tcache, arenas_nhbins, nhbins, unsigned)
Jason Evansfc4dcfa2010-11-24 15:44:21 -08001463CTL_RO_NL_GEN(arenas_nlruns, nlclasses, size_t)
Jason Evans3c234352010-01-27 13:10:55 -08001464
Jason Evans6005f072010-09-30 16:55:08 -07001465static int
1466arenas_purge_ctl(const size_t *mib, size_t miblen, void *oldp, size_t *oldlenp,
1467 void *newp, size_t newlen)
1468{
1469 int ret;
Jason Evans609ae592012-10-11 13:53:15 -07001470 unsigned arena_ind;
Jason Evans6005f072010-09-30 16:55:08 -07001471
Jason Evans609ae592012-10-11 13:53:15 -07001472 malloc_mutex_lock(&ctl_mtx);
Jason Evans6005f072010-09-30 16:55:08 -07001473 WRITEONLY();
Jason Evans609ae592012-10-11 13:53:15 -07001474 arena_ind = UINT_MAX;
1475 WRITE(arena_ind, unsigned);
1476 if (newp != NULL && arena_ind >= ctl_stats.narenas)
Jason Evans6005f072010-09-30 16:55:08 -07001477 ret = EFAULT;
Jason Evans609ae592012-10-11 13:53:15 -07001478 else {
1479 if (arena_ind == UINT_MAX)
1480 arena_ind = ctl_stats.narenas;
Jason Evans34457f52012-11-03 21:18:28 -07001481 arena_purge(arena_ind);
1482 ret = 0;
Jason Evans6005f072010-09-30 16:55:08 -07001483 }
1484
Jason Evans609ae592012-10-11 13:53:15 -07001485label_return:
1486 malloc_mutex_unlock(&ctl_mtx);
1487 return (ret);
1488}
1489
1490static int
1491arenas_extend_ctl(const size_t *mib, size_t miblen, void *oldp, size_t *oldlenp,
1492 void *newp, size_t newlen)
1493{
1494 int ret;
Jason Evans6eb84fb2012-11-29 22:13:04 -08001495 unsigned narenas;
Jason Evans609ae592012-10-11 13:53:15 -07001496
1497 malloc_mutex_lock(&ctl_mtx);
1498 READONLY();
1499 if (ctl_grow()) {
1500 ret = EAGAIN;
1501 goto label_return;
1502 }
Jason Evans6eb84fb2012-11-29 22:13:04 -08001503 narenas = ctl_stats.narenas - 1;
1504 READ(narenas, unsigned);
Jason Evans609ae592012-10-11 13:53:15 -07001505
Jason Evans6005f072010-09-30 16:55:08 -07001506 ret = 0;
Jason Evansa1ee7832012-04-10 15:07:44 -07001507label_return:
Jason Evans609ae592012-10-11 13:53:15 -07001508 malloc_mutex_unlock(&ctl_mtx);
Jason Evans6005f072010-09-30 16:55:08 -07001509 return (ret);
1510}
1511
Jason Evans3c234352010-01-27 13:10:55 -08001512/******************************************************************************/
1513
Jason Evansd34f9e72010-02-11 13:19:21 -08001514static int
Jason Evansf18c9822010-03-31 18:43:24 -07001515prof_active_ctl(const size_t *mib, size_t miblen, void *oldp, size_t *oldlenp,
1516 void *newp, size_t newlen)
1517{
1518 int ret;
1519 bool oldval;
1520
Jason Evans7372b152012-02-10 20:22:09 -08001521 if (config_prof == false)
1522 return (ENOENT);
1523
Jason Evansfc4dcfa2010-11-24 15:44:21 -08001524 malloc_mutex_lock(&ctl_mtx); /* Protect opt_prof_active. */
Jason Evansf18c9822010-03-31 18:43:24 -07001525 oldval = opt_prof_active;
1526 if (newp != NULL) {
1527 /*
1528 * The memory barriers will tend to make opt_prof_active
1529 * propagate faster on systems with weak memory ordering.
1530 */
1531 mb_write();
1532 WRITE(opt_prof_active, bool);
1533 mb_write();
1534 }
1535 READ(oldval, bool);
1536
1537 ret = 0;
Jason Evansa1ee7832012-04-10 15:07:44 -07001538label_return:
Jason Evansfc4dcfa2010-11-24 15:44:21 -08001539 malloc_mutex_unlock(&ctl_mtx);
Jason Evansf18c9822010-03-31 18:43:24 -07001540 return (ret);
1541}
1542
1543static int
Jason Evansd34f9e72010-02-11 13:19:21 -08001544prof_dump_ctl(const size_t *mib, size_t miblen, void *oldp, size_t *oldlenp,
1545 void *newp, size_t newlen)
1546{
1547 int ret;
Jason Evans22ca8552010-03-02 11:57:30 -08001548 const char *filename = NULL;
Jason Evansd34f9e72010-02-11 13:19:21 -08001549
Jason Evans7372b152012-02-10 20:22:09 -08001550 if (config_prof == false)
1551 return (ENOENT);
1552
Jason Evans22ca8552010-03-02 11:57:30 -08001553 WRITEONLY();
1554 WRITE(filename, const char *);
Jason Evansd34f9e72010-02-11 13:19:21 -08001555
Jason Evans22ca8552010-03-02 11:57:30 -08001556 if (prof_mdump(filename)) {
1557 ret = EFAULT;
Jason Evansa1ee7832012-04-10 15:07:44 -07001558 goto label_return;
Jason Evans22ca8552010-03-02 11:57:30 -08001559 }
Jason Evansd34f9e72010-02-11 13:19:21 -08001560
1561 ret = 0;
Jason Evansa1ee7832012-04-10 15:07:44 -07001562label_return:
Jason Evansd34f9e72010-02-11 13:19:21 -08001563 return (ret);
1564}
1565
Jason Evans7372b152012-02-10 20:22:09 -08001566CTL_RO_NL_CGEN(config_prof, prof_interval, prof_interval, uint64_t)
Jason Evansd34f9e72010-02-11 13:19:21 -08001567
1568/******************************************************************************/
1569
Jason Evans7372b152012-02-10 20:22:09 -08001570CTL_RO_CGEN(config_stats, stats_chunks_current, ctl_stats.chunks.current,
1571 size_t)
1572CTL_RO_CGEN(config_stats, stats_chunks_total, ctl_stats.chunks.total, uint64_t)
1573CTL_RO_CGEN(config_stats, stats_chunks_high, ctl_stats.chunks.high, size_t)
1574CTL_RO_CGEN(config_stats, stats_huge_allocated, huge_allocated, size_t)
1575CTL_RO_CGEN(config_stats, stats_huge_nmalloc, huge_nmalloc, uint64_t)
1576CTL_RO_CGEN(config_stats, stats_huge_ndalloc, huge_ndalloc, uint64_t)
1577CTL_RO_CGEN(config_stats, stats_arenas_i_small_allocated,
Jason Evans86815df2010-03-13 20:32:56 -08001578 ctl_stats.arenas[mib[2]].allocated_small, size_t)
Jason Evans7372b152012-02-10 20:22:09 -08001579CTL_RO_CGEN(config_stats, stats_arenas_i_small_nmalloc,
Jason Evans86815df2010-03-13 20:32:56 -08001580 ctl_stats.arenas[mib[2]].nmalloc_small, uint64_t)
Jason Evans7372b152012-02-10 20:22:09 -08001581CTL_RO_CGEN(config_stats, stats_arenas_i_small_ndalloc,
Jason Evans86815df2010-03-13 20:32:56 -08001582 ctl_stats.arenas[mib[2]].ndalloc_small, uint64_t)
Jason Evans7372b152012-02-10 20:22:09 -08001583CTL_RO_CGEN(config_stats, stats_arenas_i_small_nrequests,
Jason Evans86815df2010-03-13 20:32:56 -08001584 ctl_stats.arenas[mib[2]].nrequests_small, uint64_t)
Jason Evans7372b152012-02-10 20:22:09 -08001585CTL_RO_CGEN(config_stats, stats_arenas_i_large_allocated,
Jason Evans3c234352010-01-27 13:10:55 -08001586 ctl_stats.arenas[mib[2]].astats.allocated_large, size_t)
Jason Evans7372b152012-02-10 20:22:09 -08001587CTL_RO_CGEN(config_stats, stats_arenas_i_large_nmalloc,
Jason Evans3c234352010-01-27 13:10:55 -08001588 ctl_stats.arenas[mib[2]].astats.nmalloc_large, uint64_t)
Jason Evans7372b152012-02-10 20:22:09 -08001589CTL_RO_CGEN(config_stats, stats_arenas_i_large_ndalloc,
Jason Evans3c234352010-01-27 13:10:55 -08001590 ctl_stats.arenas[mib[2]].astats.ndalloc_large, uint64_t)
Jason Evans7372b152012-02-10 20:22:09 -08001591CTL_RO_CGEN(config_stats, stats_arenas_i_large_nrequests,
Jason Evansdafde142010-03-17 16:27:39 -07001592 ctl_stats.arenas[mib[2]].astats.nrequests_large, uint64_t)
Jason Evans3c234352010-01-27 13:10:55 -08001593
Jason Evans7372b152012-02-10 20:22:09 -08001594CTL_RO_CGEN(config_stats, stats_arenas_i_bins_j_allocated,
Jason Evans86815df2010-03-13 20:32:56 -08001595 ctl_stats.arenas[mib[2]].bstats[mib[4]].allocated, size_t)
Jason Evans7372b152012-02-10 20:22:09 -08001596CTL_RO_CGEN(config_stats, stats_arenas_i_bins_j_nmalloc,
Jason Evans86815df2010-03-13 20:32:56 -08001597 ctl_stats.arenas[mib[2]].bstats[mib[4]].nmalloc, uint64_t)
Jason Evans7372b152012-02-10 20:22:09 -08001598CTL_RO_CGEN(config_stats, stats_arenas_i_bins_j_ndalloc,
Jason Evans86815df2010-03-13 20:32:56 -08001599 ctl_stats.arenas[mib[2]].bstats[mib[4]].ndalloc, uint64_t)
Jason Evans7372b152012-02-10 20:22:09 -08001600CTL_RO_CGEN(config_stats, stats_arenas_i_bins_j_nrequests,
Jason Evans3c234352010-01-27 13:10:55 -08001601 ctl_stats.arenas[mib[2]].bstats[mib[4]].nrequests, uint64_t)
Jason Evans7372b152012-02-10 20:22:09 -08001602CTL_RO_CGEN(config_stats && config_tcache, stats_arenas_i_bins_j_nfills,
Jason Evans3c234352010-01-27 13:10:55 -08001603 ctl_stats.arenas[mib[2]].bstats[mib[4]].nfills, uint64_t)
Jason Evans7372b152012-02-10 20:22:09 -08001604CTL_RO_CGEN(config_stats && config_tcache, stats_arenas_i_bins_j_nflushes,
Jason Evans3c234352010-01-27 13:10:55 -08001605 ctl_stats.arenas[mib[2]].bstats[mib[4]].nflushes, uint64_t)
Jason Evans7372b152012-02-10 20:22:09 -08001606CTL_RO_CGEN(config_stats, stats_arenas_i_bins_j_nruns,
Jason Evans3c234352010-01-27 13:10:55 -08001607 ctl_stats.arenas[mib[2]].bstats[mib[4]].nruns, uint64_t)
Jason Evans7372b152012-02-10 20:22:09 -08001608CTL_RO_CGEN(config_stats, stats_arenas_i_bins_j_nreruns,
Jason Evans3c234352010-01-27 13:10:55 -08001609 ctl_stats.arenas[mib[2]].bstats[mib[4]].reruns, uint64_t)
Jason Evans7372b152012-02-10 20:22:09 -08001610CTL_RO_CGEN(config_stats, stats_arenas_i_bins_j_curruns,
Jason Evans3c234352010-01-27 13:10:55 -08001611 ctl_stats.arenas[mib[2]].bstats[mib[4]].curruns, size_t)
1612
Jason Evans609ae592012-10-11 13:53:15 -07001613static const ctl_named_node_t *
Jason Evans3c234352010-01-27 13:10:55 -08001614stats_arenas_i_bins_j_index(const size_t *mib, size_t miblen, size_t j)
1615{
1616
Jason Evansb1726102012-02-28 16:50:47 -08001617 if (j > NBINS)
Jason Evans3c234352010-01-27 13:10:55 -08001618 return (NULL);
1619 return (super_stats_arenas_i_bins_j_node);
1620}
1621
Jason Evans7372b152012-02-10 20:22:09 -08001622CTL_RO_CGEN(config_stats, stats_arenas_i_lruns_j_nmalloc,
Jason Evansdafde142010-03-17 16:27:39 -07001623 ctl_stats.arenas[mib[2]].lstats[mib[4]].nmalloc, uint64_t)
Jason Evans7372b152012-02-10 20:22:09 -08001624CTL_RO_CGEN(config_stats, stats_arenas_i_lruns_j_ndalloc,
Jason Evansdafde142010-03-17 16:27:39 -07001625 ctl_stats.arenas[mib[2]].lstats[mib[4]].ndalloc, uint64_t)
Jason Evans7372b152012-02-10 20:22:09 -08001626CTL_RO_CGEN(config_stats, stats_arenas_i_lruns_j_nrequests,
Jason Evans3c234352010-01-27 13:10:55 -08001627 ctl_stats.arenas[mib[2]].lstats[mib[4]].nrequests, uint64_t)
Jason Evans7372b152012-02-10 20:22:09 -08001628CTL_RO_CGEN(config_stats, stats_arenas_i_lruns_j_curruns,
Jason Evans3c234352010-01-27 13:10:55 -08001629 ctl_stats.arenas[mib[2]].lstats[mib[4]].curruns, size_t)
Jason Evans3c234352010-01-27 13:10:55 -08001630
Jason Evans609ae592012-10-11 13:53:15 -07001631static const ctl_named_node_t *
Jason Evans3c234352010-01-27 13:10:55 -08001632stats_arenas_i_lruns_j_index(const size_t *mib, size_t miblen, size_t j)
1633{
1634
1635 if (j > nlclasses)
1636 return (NULL);
1637 return (super_stats_arenas_i_lruns_j_node);
1638}
1639
Jason Evans597632b2011-03-18 13:41:33 -07001640CTL_RO_GEN(stats_arenas_i_nthreads, ctl_stats.arenas[mib[2]].nthreads, unsigned)
Jason Evans609ae592012-10-11 13:53:15 -07001641CTL_RO_GEN(stats_arenas_i_dss, ctl_stats.arenas[mib[2]].dss, const char *)
Jason Evans3c234352010-01-27 13:10:55 -08001642CTL_RO_GEN(stats_arenas_i_pactive, ctl_stats.arenas[mib[2]].pactive, size_t)
1643CTL_RO_GEN(stats_arenas_i_pdirty, ctl_stats.arenas[mib[2]].pdirty, size_t)
Jason Evans7372b152012-02-10 20:22:09 -08001644CTL_RO_CGEN(config_stats, stats_arenas_i_mapped,
1645 ctl_stats.arenas[mib[2]].astats.mapped, size_t)
1646CTL_RO_CGEN(config_stats, stats_arenas_i_npurge,
1647 ctl_stats.arenas[mib[2]].astats.npurge, uint64_t)
1648CTL_RO_CGEN(config_stats, stats_arenas_i_nmadvise,
1649 ctl_stats.arenas[mib[2]].astats.nmadvise, uint64_t)
1650CTL_RO_CGEN(config_stats, stats_arenas_i_purged,
1651 ctl_stats.arenas[mib[2]].astats.purged, uint64_t)
Jason Evans3c234352010-01-27 13:10:55 -08001652
Jason Evans609ae592012-10-11 13:53:15 -07001653static const ctl_named_node_t *
Jason Evans3c234352010-01-27 13:10:55 -08001654stats_arenas_i_index(const size_t *mib, size_t miblen, size_t i)
1655{
Mike Hommey461ad5c2012-04-20 08:38:42 +02001656 const ctl_named_node_t * ret;
Jason Evans3c234352010-01-27 13:10:55 -08001657
Jason Evansfc4dcfa2010-11-24 15:44:21 -08001658 malloc_mutex_lock(&ctl_mtx);
Jason Evans609ae592012-10-11 13:53:15 -07001659 if (i > ctl_stats.narenas || ctl_stats.arenas[i].initialized == false) {
Jason Evansfc4dcfa2010-11-24 15:44:21 -08001660 ret = NULL;
Jason Evansa1ee7832012-04-10 15:07:44 -07001661 goto label_return;
Jason Evansfc4dcfa2010-11-24 15:44:21 -08001662 }
1663
1664 ret = super_stats_arenas_i_node;
Jason Evansa1ee7832012-04-10 15:07:44 -07001665label_return:
Jason Evansfc4dcfa2010-11-24 15:44:21 -08001666 malloc_mutex_unlock(&ctl_mtx);
1667 return (ret);
Jason Evans3c234352010-01-27 13:10:55 -08001668}
1669
Jason Evans7372b152012-02-10 20:22:09 -08001670CTL_RO_CGEN(config_stats, stats_cactive, &stats_cactive, size_t *)
1671CTL_RO_CGEN(config_stats, stats_allocated, ctl_stats.allocated, size_t)
1672CTL_RO_CGEN(config_stats, stats_active, ctl_stats.active, size_t)
1673CTL_RO_CGEN(config_stats, stats_mapped, ctl_stats.mapped, size_t)