blob: edbbb209cf8f239af43e314ed0c4fb0cd98270cf [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
7static malloc_mutex_t ctl_mtx;
8static bool ctl_initialized;
9static uint64_t ctl_epoch;
10static ctl_stats_t ctl_stats;
11
12/******************************************************************************/
13/* Function prototypes for non-inline static functions. */
14
15#define CTL_PROTO(n) \
16static int n##_ctl(const size_t *mib, size_t miblen, void *oldp, \
17 size_t *oldlenp, void *newp, size_t newlen);
18
19#define INDEX_PROTO(n) \
20const ctl_node_t *n##_index(const size_t *mib, size_t miblen, \
21 size_t i);
22
23#ifdef JEMALLOC_STATS
24static bool ctl_arena_init(ctl_arena_stats_t *astats);
25#endif
26static void ctl_arena_clear(ctl_arena_stats_t *astats);
Jason Evans86815df2010-03-13 20:32:56 -080027#ifdef JEMALLOC_STATS
28static void ctl_arena_stats_amerge(ctl_arena_stats_t *cstats,
29 arena_t *arena);
30static void ctl_arena_stats_smerge(ctl_arena_stats_t *sstats,
31 ctl_arena_stats_t *astats);
32#endif
Jason Evans3c234352010-01-27 13:10:55 -080033static void ctl_arena_refresh(arena_t *arena, unsigned i);
34static void ctl_refresh(void);
35static bool ctl_init(void);
36static int ctl_lookup(const char *name, ctl_node_t const **nodesp,
37 size_t *mibp, size_t *depthp);
38
Jason Evansa40bc7a2010-03-02 13:01:16 -080039CTL_PROTO(version)
Jason Evans3c234352010-01-27 13:10:55 -080040CTL_PROTO(epoch)
41#ifdef JEMALLOC_TCACHE
42CTL_PROTO(tcache_flush)
43#endif
Jason Evansb267d0f2010-08-13 15:42:29 -070044CTL_PROTO(thread_arena)
Jason Evans3c234352010-01-27 13:10:55 -080045CTL_PROTO(config_debug)
46CTL_PROTO(config_dss)
47CTL_PROTO(config_dynamic_page_shift)
48CTL_PROTO(config_fill)
49CTL_PROTO(config_lazy_lock)
Jason Evansd34f9e72010-02-11 13:19:21 -080050CTL_PROTO(config_prof)
51CTL_PROTO(config_prof_libgcc)
52CTL_PROTO(config_prof_libunwind)
Jason Evans3c234352010-01-27 13:10:55 -080053CTL_PROTO(config_stats)
54CTL_PROTO(config_swap)
55CTL_PROTO(config_sysv)
56CTL_PROTO(config_tcache)
57CTL_PROTO(config_tiny)
58CTL_PROTO(config_tls)
Jason Evans3c234352010-01-27 13:10:55 -080059CTL_PROTO(config_xmalloc)
60CTL_PROTO(opt_abort)
61#ifdef JEMALLOC_FILL
62CTL_PROTO(opt_junk)
63#endif
64#ifdef JEMALLOC_SYSV
65CTL_PROTO(opt_sysv)
66#endif
67#ifdef JEMALLOC_XMALLOC
68CTL_PROTO(opt_xmalloc)
69#endif
70#ifdef JEMALLOC_ZERO
71CTL_PROTO(opt_zero)
72#endif
73#ifdef JEMALLOC_TCACHE
Jason Evans3fa9a2f2010-03-07 15:34:14 -080074CTL_PROTO(opt_tcache)
Jason Evans3c234352010-01-27 13:10:55 -080075CTL_PROTO(opt_lg_tcache_gc_sweep)
Jason Evans3c234352010-01-27 13:10:55 -080076#endif
Jason Evansd34f9e72010-02-11 13:19:21 -080077#ifdef JEMALLOC_PROF
78CTL_PROTO(opt_prof)
Jason Evansf18c9822010-03-31 18:43:24 -070079CTL_PROTO(opt_prof_active)
Jason Evansd34f9e72010-02-11 13:19:21 -080080CTL_PROTO(opt_lg_prof_bt_max)
Jason Evansb9477e72010-03-01 20:15:26 -080081CTL_PROTO(opt_lg_prof_sample)
Jason Evansd34f9e72010-02-11 13:19:21 -080082CTL_PROTO(opt_lg_prof_interval)
83CTL_PROTO(opt_prof_udump)
84CTL_PROTO(opt_prof_leak)
Jason Evansa881cd22010-10-02 15:18:50 -070085CTL_PROTO(opt_prof_accum)
86CTL_PROTO(opt_lg_prof_tcmax)
Jason Evansd34f9e72010-02-11 13:19:21 -080087#endif
Jason Evans3c234352010-01-27 13:10:55 -080088CTL_PROTO(opt_stats_print)
Jason Evans3c234352010-01-27 13:10:55 -080089CTL_PROTO(opt_lg_qspace_max)
90CTL_PROTO(opt_lg_cspace_max)
Jason Evans3c234352010-01-27 13:10:55 -080091CTL_PROTO(opt_lg_dirty_mult)
92CTL_PROTO(opt_lg_chunk)
93#ifdef JEMALLOC_SWAP
94CTL_PROTO(opt_overcommit)
95#endif
96CTL_PROTO(arenas_bin_i_size)
97CTL_PROTO(arenas_bin_i_nregs)
98CTL_PROTO(arenas_bin_i_run_size)
99INDEX_PROTO(arenas_bin_i)
100CTL_PROTO(arenas_lrun_i_size)
101INDEX_PROTO(arenas_lrun_i)
102CTL_PROTO(arenas_narenas)
103CTL_PROTO(arenas_initialized)
104CTL_PROTO(arenas_quantum)
105CTL_PROTO(arenas_cacheline)
106CTL_PROTO(arenas_subpage)
107CTL_PROTO(arenas_pagesize)
Jason Evans3c234352010-01-27 13:10:55 -0800108CTL_PROTO(arenas_chunksize)
109#ifdef JEMALLOC_TINY
110CTL_PROTO(arenas_tspace_min)
111CTL_PROTO(arenas_tspace_max)
112#endif
113CTL_PROTO(arenas_qspace_min)
114CTL_PROTO(arenas_qspace_max)
115CTL_PROTO(arenas_cspace_min)
116CTL_PROTO(arenas_cspace_max)
117CTL_PROTO(arenas_sspace_min)
118CTL_PROTO(arenas_sspace_max)
Jason Evansdafde142010-03-17 16:27:39 -0700119#ifdef JEMALLOC_TCACHE
120CTL_PROTO(arenas_tcache_max)
121#endif
Jason Evans3c234352010-01-27 13:10:55 -0800122CTL_PROTO(arenas_ntbins)
123CTL_PROTO(arenas_nqbins)
124CTL_PROTO(arenas_ncbins)
125CTL_PROTO(arenas_nsbins)
Jason Evans3c234352010-01-27 13:10:55 -0800126CTL_PROTO(arenas_nbins)
Jason Evansdafde142010-03-17 16:27:39 -0700127#ifdef JEMALLOC_TCACHE
128CTL_PROTO(arenas_nhbins)
129#endif
Jason Evans3c234352010-01-27 13:10:55 -0800130CTL_PROTO(arenas_nlruns)
Jason Evans6005f072010-09-30 16:55:08 -0700131CTL_PROTO(arenas_purge)
Jason Evansd34f9e72010-02-11 13:19:21 -0800132#ifdef JEMALLOC_PROF
Jason Evansf18c9822010-03-31 18:43:24 -0700133CTL_PROTO(prof_active)
Jason Evansd34f9e72010-02-11 13:19:21 -0800134CTL_PROTO(prof_dump)
135CTL_PROTO(prof_interval)
136#endif
Jason Evans3c234352010-01-27 13:10:55 -0800137#ifdef JEMALLOC_STATS
138CTL_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)
156#ifdef JEMALLOC_TCACHE
157CTL_PROTO(stats_arenas_i_bins_j_nfills)
158CTL_PROTO(stats_arenas_i_bins_j_nflushes)
159#endif
160CTL_PROTO(stats_arenas_i_bins_j_nruns)
161CTL_PROTO(stats_arenas_i_bins_j_nreruns)
162CTL_PROTO(stats_arenas_i_bins_j_highruns)
163CTL_PROTO(stats_arenas_i_bins_j_curruns)
164INDEX_PROTO(stats_arenas_i_bins_j)
Jason Evansdafde142010-03-17 16:27:39 -0700165CTL_PROTO(stats_arenas_i_lruns_j_nmalloc)
166CTL_PROTO(stats_arenas_i_lruns_j_ndalloc)
Jason Evans3c234352010-01-27 13:10:55 -0800167CTL_PROTO(stats_arenas_i_lruns_j_nrequests)
168CTL_PROTO(stats_arenas_i_lruns_j_highruns)
169CTL_PROTO(stats_arenas_i_lruns_j_curruns)
170INDEX_PROTO(stats_arenas_i_lruns_j)
171#endif
172CTL_PROTO(stats_arenas_i_pactive)
173CTL_PROTO(stats_arenas_i_pdirty)
174#ifdef JEMALLOC_STATS
175CTL_PROTO(stats_arenas_i_mapped)
176CTL_PROTO(stats_arenas_i_npurge)
177CTL_PROTO(stats_arenas_i_nmadvise)
178CTL_PROTO(stats_arenas_i_purged)
179#endif
180INDEX_PROTO(stats_arenas_i)
181#ifdef JEMALLOC_STATS
182CTL_PROTO(stats_allocated)
183CTL_PROTO(stats_active)
184CTL_PROTO(stats_mapped)
185#endif
186#ifdef JEMALLOC_SWAP
187# ifdef JEMALLOC_STATS
188CTL_PROTO(swap_avail)
189# endif
190CTL_PROTO(swap_prezeroed)
191CTL_PROTO(swap_nfds)
192CTL_PROTO(swap_fds)
193#endif
194
195/******************************************************************************/
196/* mallctl tree. */
197
198/* Maximum tree depth. */
199#define CTL_MAX_DEPTH 6
200
201#define NAME(n) true, {.named = {n
202#define CHILD(c) sizeof(c##_node) / sizeof(ctl_node_t), c##_node}}, NULL
203#define CTL(c) 0, NULL}}, c##_ctl
204
205/*
206 * Only handles internal indexed nodes, since there are currently no external
207 * ones.
208 */
209#define INDEX(i) false, {.indexed = {i##_index}}, NULL
210
211#ifdef JEMALLOC_TCACHE
212static const ctl_node_t tcache_node[] = {
213 {NAME("flush"), CTL(tcache_flush)}
214};
215#endif
216
Jason Evansb267d0f2010-08-13 15:42:29 -0700217static const ctl_node_t thread_node[] = {
218 {NAME("arena"), CTL(thread_arena)}
219};
Jason Evansb267d0f2010-08-13 15:42:29 -0700220
Jason Evans3c234352010-01-27 13:10:55 -0800221static const ctl_node_t config_node[] = {
222 {NAME("debug"), CTL(config_debug)},
223 {NAME("dss"), CTL(config_dss)},
224 {NAME("dynamic_page_shift"), CTL(config_dynamic_page_shift)},
225 {NAME("fill"), CTL(config_fill)},
226 {NAME("lazy_lock"), CTL(config_lazy_lock)},
Jason Evansd34f9e72010-02-11 13:19:21 -0800227 {NAME("prof"), CTL(config_prof)},
228 {NAME("prof_libgcc"), CTL(config_prof_libgcc)},
229 {NAME("prof_libunwind"), CTL(config_prof_libunwind)},
Jason Evans3c234352010-01-27 13:10:55 -0800230 {NAME("stats"), CTL(config_stats)},
231 {NAME("swap"), CTL(config_swap)},
232 {NAME("sysv"), CTL(config_sysv)},
233 {NAME("tcache"), CTL(config_tcache)},
234 {NAME("tiny"), CTL(config_tiny)},
235 {NAME("tls"), CTL(config_tls)},
Jason Evans3c234352010-01-27 13:10:55 -0800236 {NAME("xmalloc"), CTL(config_xmalloc)}
237};
238
239static const ctl_node_t opt_node[] = {
240 {NAME("abort"), CTL(opt_abort)},
241#ifdef JEMALLOC_FILL
242 {NAME("junk"), CTL(opt_junk)},
243#endif
244#ifdef JEMALLOC_SYSV
245 {NAME("sysv"), CTL(opt_sysv)},
246#endif
247#ifdef JEMALLOC_XMALLOC
248 {NAME("xmalloc"), CTL(opt_xmalloc)},
249#endif
250#ifdef JEMALLOC_ZERO
251 {NAME("zero"), CTL(opt_zero)},
252#endif
253#ifdef JEMALLOC_TCACHE
Jason Evans3fa9a2f2010-03-07 15:34:14 -0800254 {NAME("tcache"), CTL(opt_tcache)},
Jason Evans3c234352010-01-27 13:10:55 -0800255 {NAME("lg_tcache_gc_sweep"), CTL(opt_lg_tcache_gc_sweep)},
Jason Evans3c234352010-01-27 13:10:55 -0800256#endif
Jason Evansd34f9e72010-02-11 13:19:21 -0800257#ifdef JEMALLOC_PROF
258 {NAME("prof"), CTL(opt_prof)},
Jason Evansf18c9822010-03-31 18:43:24 -0700259 {NAME("prof_active"), CTL(opt_prof_active)},
Jason Evansd34f9e72010-02-11 13:19:21 -0800260 {NAME("lg_prof_bt_max"), CTL(opt_lg_prof_bt_max)},
Jason Evansb9477e72010-03-01 20:15:26 -0800261 {NAME("lg_prof_sample"), CTL(opt_lg_prof_sample)},
Jason Evansd34f9e72010-02-11 13:19:21 -0800262 {NAME("lg_prof_interval"), CTL(opt_lg_prof_interval)},
263 {NAME("prof_udump"), CTL(opt_prof_udump)},
264 {NAME("prof_leak"), CTL(opt_prof_leak)},
Jason Evansa881cd22010-10-02 15:18:50 -0700265 {NAME("prof_accum"), CTL(opt_prof_accum)},
266 {NAME("lg_prof_tcmax"), CTL(opt_lg_prof_tcmax)},
Jason Evansd34f9e72010-02-11 13:19:21 -0800267#endif
Jason Evans3c234352010-01-27 13:10:55 -0800268 {NAME("stats_print"), CTL(opt_stats_print)},
Jason Evans3c234352010-01-27 13:10:55 -0800269 {NAME("lg_qspace_max"), CTL(opt_lg_qspace_max)},
270 {NAME("lg_cspace_max"), CTL(opt_lg_cspace_max)},
Jason Evans3c234352010-01-27 13:10:55 -0800271 {NAME("lg_dirty_mult"), CTL(opt_lg_dirty_mult)},
272 {NAME("lg_chunk"), CTL(opt_lg_chunk)}
273#ifdef JEMALLOC_SWAP
274 ,
275 {NAME("overcommit"), CTL(opt_overcommit)}
276#endif
277};
278
279static const ctl_node_t arenas_bin_i_node[] = {
280 {NAME("size"), CTL(arenas_bin_i_size)},
281 {NAME("nregs"), CTL(arenas_bin_i_nregs)},
282 {NAME("run_size"), CTL(arenas_bin_i_run_size)}
283};
284static const ctl_node_t super_arenas_bin_i_node[] = {
285 {NAME(""), CHILD(arenas_bin_i)}
286};
287
288static const ctl_node_t arenas_bin_node[] = {
289 {INDEX(arenas_bin_i)}
290};
291
292static const ctl_node_t arenas_lrun_i_node[] = {
293 {NAME("size"), CTL(arenas_lrun_i_size)}
294};
295static const ctl_node_t super_arenas_lrun_i_node[] = {
296 {NAME(""), CHILD(arenas_lrun_i)}
297};
298
299static const ctl_node_t arenas_lrun_node[] = {
300 {INDEX(arenas_lrun_i)}
301};
302
303static const ctl_node_t arenas_node[] = {
304 {NAME("narenas"), CTL(arenas_narenas)},
305 {NAME("initialized"), CTL(arenas_initialized)},
306 {NAME("quantum"), CTL(arenas_quantum)},
307 {NAME("cacheline"), CTL(arenas_cacheline)},
308 {NAME("subpage"), CTL(arenas_subpage)},
309 {NAME("pagesize"), CTL(arenas_pagesize)},
Jason Evans3c234352010-01-27 13:10:55 -0800310 {NAME("chunksize"), CTL(arenas_chunksize)},
311#ifdef JEMALLOC_TINY
312 {NAME("tspace_min"), CTL(arenas_tspace_min)},
313 {NAME("tspace_max"), CTL(arenas_tspace_max)},
314#endif
315 {NAME("qspace_min"), CTL(arenas_qspace_min)},
316 {NAME("qspace_max"), CTL(arenas_qspace_max)},
317 {NAME("cspace_min"), CTL(arenas_cspace_min)},
318 {NAME("cspace_max"), CTL(arenas_cspace_max)},
319 {NAME("sspace_min"), CTL(arenas_sspace_min)},
320 {NAME("sspace_max"), CTL(arenas_sspace_max)},
Jason Evansdafde142010-03-17 16:27:39 -0700321#ifdef JEMALLOC_TCACHE
322 {NAME("tcache_max"), CTL(arenas_tcache_max)},
323#endif
Jason Evans3c234352010-01-27 13:10:55 -0800324 {NAME("ntbins"), CTL(arenas_ntbins)},
325 {NAME("nqbins"), CTL(arenas_nqbins)},
326 {NAME("ncbins"), CTL(arenas_ncbins)},
327 {NAME("nsbins"), CTL(arenas_nsbins)},
Jason Evans3c234352010-01-27 13:10:55 -0800328 {NAME("nbins"), CTL(arenas_nbins)},
Jason Evansdafde142010-03-17 16:27:39 -0700329#ifdef JEMALLOC_TCACHE
330 {NAME("nhbins"), CTL(arenas_nhbins)},
331#endif
Jason Evans3c234352010-01-27 13:10:55 -0800332 {NAME("bin"), CHILD(arenas_bin)},
333 {NAME("nlruns"), CTL(arenas_nlruns)},
Jason Evans6005f072010-09-30 16:55:08 -0700334 {NAME("lrun"), CHILD(arenas_lrun)},
335 {NAME("purge"), CTL(arenas_purge)}
Jason Evans3c234352010-01-27 13:10:55 -0800336};
337
Jason Evansd34f9e72010-02-11 13:19:21 -0800338#ifdef JEMALLOC_PROF
339static const ctl_node_t prof_node[] = {
Jason Evansf18c9822010-03-31 18:43:24 -0700340 {NAME("active"), CTL(prof_active)},
Jason Evansd34f9e72010-02-11 13:19:21 -0800341 {NAME("dump"), CTL(prof_dump)},
342 {NAME("interval"), CTL(prof_interval)}
343};
344#endif
345
Jason Evans3c234352010-01-27 13:10:55 -0800346#ifdef JEMALLOC_STATS
347static const ctl_node_t stats_chunks_node[] = {
348 {NAME("current"), CTL(stats_chunks_current)},
349 {NAME("total"), CTL(stats_chunks_total)},
350 {NAME("high"), CTL(stats_chunks_high)}
351};
352
353static const ctl_node_t stats_huge_node[] = {
354 {NAME("allocated"), CTL(stats_huge_allocated)},
355 {NAME("nmalloc"), CTL(stats_huge_nmalloc)},
356 {NAME("ndalloc"), CTL(stats_huge_ndalloc)}
357};
358
359static const ctl_node_t stats_arenas_i_small_node[] = {
360 {NAME("allocated"), CTL(stats_arenas_i_small_allocated)},
361 {NAME("nmalloc"), CTL(stats_arenas_i_small_nmalloc)},
Jason Evans86815df2010-03-13 20:32:56 -0800362 {NAME("ndalloc"), CTL(stats_arenas_i_small_ndalloc)},
363 {NAME("nrequests"), CTL(stats_arenas_i_small_nrequests)}
Jason Evans3c234352010-01-27 13:10:55 -0800364};
365
Jason Evans3c234352010-01-27 13:10:55 -0800366static const ctl_node_t stats_arenas_i_large_node[] = {
367 {NAME("allocated"), CTL(stats_arenas_i_large_allocated)},
368 {NAME("nmalloc"), CTL(stats_arenas_i_large_nmalloc)},
Jason Evansdafde142010-03-17 16:27:39 -0700369 {NAME("ndalloc"), CTL(stats_arenas_i_large_ndalloc)},
370 {NAME("nrequests"), CTL(stats_arenas_i_large_nrequests)}
Jason Evans3c234352010-01-27 13:10:55 -0800371};
372
373static const ctl_node_t stats_arenas_i_bins_j_node[] = {
Jason Evans86815df2010-03-13 20:32:56 -0800374 {NAME("allocated"), CTL(stats_arenas_i_bins_j_allocated)},
375 {NAME("nmalloc"), CTL(stats_arenas_i_bins_j_nmalloc)},
376 {NAME("ndalloc"), CTL(stats_arenas_i_bins_j_ndalloc)},
Jason Evans3c234352010-01-27 13:10:55 -0800377 {NAME("nrequests"), CTL(stats_arenas_i_bins_j_nrequests)},
378#ifdef JEMALLOC_TCACHE
379 {NAME("nfills"), CTL(stats_arenas_i_bins_j_nfills)},
380 {NAME("nflushes"), CTL(stats_arenas_i_bins_j_nflushes)},
381#endif
382 {NAME("nruns"), CTL(stats_arenas_i_bins_j_nruns)},
383 {NAME("nreruns"), CTL(stats_arenas_i_bins_j_nreruns)},
384 {NAME("highruns"), CTL(stats_arenas_i_bins_j_highruns)},
385 {NAME("curruns"), CTL(stats_arenas_i_bins_j_curruns)}
386};
387static const ctl_node_t super_stats_arenas_i_bins_j_node[] = {
388 {NAME(""), CHILD(stats_arenas_i_bins_j)}
389};
390
391static const ctl_node_t stats_arenas_i_bins_node[] = {
392 {INDEX(stats_arenas_i_bins_j)}
393};
394
395static const ctl_node_t stats_arenas_i_lruns_j_node[] = {
Jason Evansdafde142010-03-17 16:27:39 -0700396 {NAME("nmalloc"), CTL(stats_arenas_i_lruns_j_nmalloc)},
397 {NAME("ndalloc"), CTL(stats_arenas_i_lruns_j_ndalloc)},
Jason Evans3c234352010-01-27 13:10:55 -0800398 {NAME("nrequests"), CTL(stats_arenas_i_lruns_j_nrequests)},
399 {NAME("highruns"), CTL(stats_arenas_i_lruns_j_highruns)},
400 {NAME("curruns"), CTL(stats_arenas_i_lruns_j_curruns)}
401};
402static const ctl_node_t super_stats_arenas_i_lruns_j_node[] = {
403 {NAME(""), CHILD(stats_arenas_i_lruns_j)}
404};
405
406static const ctl_node_t stats_arenas_i_lruns_node[] = {
407 {INDEX(stats_arenas_i_lruns_j)}
408};
409#endif
410
411static const ctl_node_t stats_arenas_i_node[] = {
412 {NAME("pactive"), CTL(stats_arenas_i_pactive)},
413 {NAME("pdirty"), CTL(stats_arenas_i_pdirty)}
414#ifdef JEMALLOC_STATS
415 ,
416 {NAME("mapped"), CTL(stats_arenas_i_mapped)},
417 {NAME("npurge"), CTL(stats_arenas_i_npurge)},
418 {NAME("nmadvise"), CTL(stats_arenas_i_nmadvise)},
419 {NAME("purged"), CTL(stats_arenas_i_purged)},
420 {NAME("small"), CHILD(stats_arenas_i_small)},
Jason Evans3c234352010-01-27 13:10:55 -0800421 {NAME("large"), CHILD(stats_arenas_i_large)},
422 {NAME("bins"), CHILD(stats_arenas_i_bins)},
423 {NAME("lruns"), CHILD(stats_arenas_i_lruns)}
424#endif
425};
426static const ctl_node_t super_stats_arenas_i_node[] = {
427 {NAME(""), CHILD(stats_arenas_i)}
428};
429
430static const ctl_node_t stats_arenas_node[] = {
431 {INDEX(stats_arenas_i)}
432};
433
434static const ctl_node_t stats_node[] = {
435#ifdef JEMALLOC_STATS
436 {NAME("allocated"), CTL(stats_allocated)},
437 {NAME("active"), CTL(stats_active)},
438 {NAME("mapped"), CTL(stats_mapped)},
439 {NAME("chunks"), CHILD(stats_chunks)},
440 {NAME("huge"), CHILD(stats_huge)},
441#endif
442 {NAME("arenas"), CHILD(stats_arenas)}
443};
444
445#ifdef JEMALLOC_SWAP
446static const ctl_node_t swap_node[] = {
447# ifdef JEMALLOC_STATS
448 {NAME("avail"), CTL(swap_avail)},
449# endif
450 {NAME("prezeroed"), CTL(swap_prezeroed)},
451 {NAME("nfds"), CTL(swap_nfds)},
452 {NAME("fds"), CTL(swap_fds)}
453};
454#endif
455
456static const ctl_node_t root_node[] = {
Jason Evansa40bc7a2010-03-02 13:01:16 -0800457 {NAME("version"), CTL(version)},
Jason Evans3c234352010-01-27 13:10:55 -0800458 {NAME("epoch"), CTL(epoch)},
459#ifdef JEMALLOC_TCACHE
460 {NAME("tcache"), CHILD(tcache)},
461#endif
Jason Evansb267d0f2010-08-13 15:42:29 -0700462 {NAME("thread"), CHILD(thread)},
Jason Evans3c234352010-01-27 13:10:55 -0800463 {NAME("config"), CHILD(config)},
464 {NAME("opt"), CHILD(opt)},
465 {NAME("arenas"), CHILD(arenas)},
Jason Evansd34f9e72010-02-11 13:19:21 -0800466#ifdef JEMALLOC_PROF
467 {NAME("prof"), CHILD(prof)},
468#endif
Jason Evans3c234352010-01-27 13:10:55 -0800469 {NAME("stats"), CHILD(stats)}
470#ifdef JEMALLOC_SWAP
471 ,
472 {NAME("swap"), CHILD(swap)}
473#endif
474};
475static const ctl_node_t super_root_node[] = {
476 {NAME(""), CHILD(root)}
477};
478
479#undef NAME
480#undef CHILD
481#undef CTL
482#undef INDEX
483
484/******************************************************************************/
485
486#ifdef JEMALLOC_STATS
487static bool
488ctl_arena_init(ctl_arena_stats_t *astats)
489{
490
491 if (astats->bstats == NULL) {
492 astats->bstats = (malloc_bin_stats_t *)base_alloc(nbins *
493 sizeof(malloc_bin_stats_t));
494 if (astats->bstats == NULL)
495 return (true);
496 }
497 if (astats->lstats == NULL) {
498 astats->lstats = (malloc_large_stats_t *)base_alloc(nlclasses *
499 sizeof(malloc_large_stats_t));
500 if (astats->lstats == NULL)
501 return (true);
502 }
503
504 return (false);
505}
506#endif
507
508static void
509ctl_arena_clear(ctl_arena_stats_t *astats)
510{
511
512 astats->pactive = 0;
513 astats->pdirty = 0;
514#ifdef JEMALLOC_STATS
515 memset(&astats->astats, 0, sizeof(arena_stats_t));
Jason Evans86815df2010-03-13 20:32:56 -0800516 astats->allocated_small = 0;
517 astats->nmalloc_small = 0;
518 astats->ndalloc_small = 0;
519 astats->nrequests_small = 0;
Jason Evans3c234352010-01-27 13:10:55 -0800520 memset(astats->bstats, 0, nbins * sizeof(malloc_bin_stats_t));
521 memset(astats->lstats, 0, nlclasses * sizeof(malloc_large_stats_t));
522#endif
523}
524
Jason Evans86815df2010-03-13 20:32:56 -0800525#ifdef JEMALLOC_STATS
526static void
527ctl_arena_stats_amerge(ctl_arena_stats_t *cstats, arena_t *arena)
528{
529 unsigned i;
530
531 arena_stats_merge(arena, &cstats->pactive, &cstats->pdirty,
532 &cstats->astats, cstats->bstats, cstats->lstats);
533
Jason Evansdafde142010-03-17 16:27:39 -0700534 for (i = 0; i < nbins; i++) {
Jason Evans86815df2010-03-13 20:32:56 -0800535 cstats->allocated_small += cstats->bstats[i].allocated;
536 cstats->nmalloc_small += cstats->bstats[i].nmalloc;
537 cstats->ndalloc_small += cstats->bstats[i].ndalloc;
538 cstats->nrequests_small += cstats->bstats[i].nrequests;
539 }
Jason Evans86815df2010-03-13 20:32:56 -0800540}
541
542static void
543ctl_arena_stats_smerge(ctl_arena_stats_t *sstats, ctl_arena_stats_t *astats)
544{
545 unsigned i;
546
547 sstats->pactive += astats->pactive;
548 sstats->pdirty += astats->pdirty;
549
550 sstats->astats.mapped += astats->astats.mapped;
551 sstats->astats.npurge += astats->astats.npurge;
552 sstats->astats.nmadvise += astats->astats.nmadvise;
553 sstats->astats.purged += astats->astats.purged;
554
555 sstats->allocated_small += astats->allocated_small;
556 sstats->nmalloc_small += astats->nmalloc_small;
557 sstats->ndalloc_small += astats->ndalloc_small;
558 sstats->nrequests_small += astats->nrequests_small;
559
Jason Evans86815df2010-03-13 20:32:56 -0800560 sstats->astats.allocated_large += astats->astats.allocated_large;
561 sstats->astats.nmalloc_large += astats->astats.nmalloc_large;
562 sstats->astats.ndalloc_large += astats->astats.ndalloc_large;
Jason Evansdafde142010-03-17 16:27:39 -0700563 sstats->astats.nrequests_large += astats->astats.nrequests_large;
Jason Evans86815df2010-03-13 20:32:56 -0800564
565 for (i = 0; i < nlclasses; i++) {
Jason Evansdafde142010-03-17 16:27:39 -0700566 sstats->lstats[i].nmalloc += astats->lstats[i].nmalloc;
567 sstats->lstats[i].ndalloc += astats->lstats[i].ndalloc;
Jason Evans86815df2010-03-13 20:32:56 -0800568 sstats->lstats[i].nrequests += astats->lstats[i].nrequests;
569 sstats->lstats[i].highruns += astats->lstats[i].highruns;
570 sstats->lstats[i].curruns += astats->lstats[i].curruns;
571 }
572
573 for (i = 0; i < nbins; i++) {
574 sstats->bstats[i].allocated += astats->bstats[i].allocated;
575 sstats->bstats[i].nmalloc += astats->bstats[i].nmalloc;
576 sstats->bstats[i].ndalloc += astats->bstats[i].ndalloc;
577 sstats->bstats[i].nrequests += astats->bstats[i].nrequests;
578#ifdef JEMALLOC_TCACHE
579 sstats->bstats[i].nfills += astats->bstats[i].nfills;
580 sstats->bstats[i].nflushes += astats->bstats[i].nflushes;
581#endif
582 sstats->bstats[i].nruns += astats->bstats[i].nruns;
583 sstats->bstats[i].reruns += astats->bstats[i].reruns;
584 sstats->bstats[i].highruns += astats->bstats[i].highruns;
585 sstats->bstats[i].curruns += astats->bstats[i].curruns;
586 }
587}
588#endif
589
Jason Evans3c234352010-01-27 13:10:55 -0800590static void
591ctl_arena_refresh(arena_t *arena, unsigned i)
592{
593 ctl_arena_stats_t *astats = &ctl_stats.arenas[i];
594 ctl_arena_stats_t *sstats = &ctl_stats.arenas[narenas];
595
596 ctl_arena_clear(astats);
597
Jason Evans3c234352010-01-27 13:10:55 -0800598#ifdef JEMALLOC_STATS
Jason Evans86815df2010-03-13 20:32:56 -0800599 ctl_arena_stats_amerge(astats, arena);
Jason Evans3c234352010-01-27 13:10:55 -0800600 /* Merge into sum stats as well. */
Jason Evans86815df2010-03-13 20:32:56 -0800601 ctl_arena_stats_smerge(sstats, astats);
Jason Evans3c234352010-01-27 13:10:55 -0800602#else
603 astats->pactive += arena->nactive;
604 astats->pdirty += arena->ndirty;
605 /* Merge into sum stats as well. */
606 sstats->pactive += arena->nactive;
607 sstats->pdirty += arena->ndirty;
608#endif
Jason Evans3c234352010-01-27 13:10:55 -0800609}
610
611static void
612ctl_refresh(void)
613{
614 unsigned i;
615 arena_t *tarenas[narenas];
616
617#ifdef JEMALLOC_STATS
618 malloc_mutex_lock(&chunks_mtx);
619 ctl_stats.chunks.current = stats_chunks.curchunks;
620 ctl_stats.chunks.total = stats_chunks.nchunks;
621 ctl_stats.chunks.high = stats_chunks.highchunks;
622 malloc_mutex_unlock(&chunks_mtx);
623
624 malloc_mutex_lock(&huge_mtx);
625 ctl_stats.huge.allocated = huge_allocated;
626 ctl_stats.huge.nmalloc = huge_nmalloc;
627 ctl_stats.huge.ndalloc = huge_ndalloc;
628 malloc_mutex_unlock(&huge_mtx);
629#endif
630
631 /*
Jason Evans13668262010-01-31 03:57:29 -0800632 * Clear sum stats, since they will be merged into by
Jason Evans3c234352010-01-27 13:10:55 -0800633 * ctl_arena_refresh().
634 */
635 ctl_arena_clear(&ctl_stats.arenas[narenas]);
636
637 malloc_mutex_lock(&arenas_lock);
638 memcpy(tarenas, arenas, sizeof(arena_t *) * narenas);
639 malloc_mutex_unlock(&arenas_lock);
640 for (i = 0; i < narenas; i++) {
641 bool initialized = (tarenas[i] != NULL);
642
643 ctl_stats.arenas[i].initialized = initialized;
644 if (initialized)
645 ctl_arena_refresh(tarenas[i], i);
646 }
647
648#ifdef JEMALLOC_STATS
Jason Evans86815df2010-03-13 20:32:56 -0800649 ctl_stats.allocated = ctl_stats.arenas[narenas].allocated_small
Jason Evans3c234352010-01-27 13:10:55 -0800650 + ctl_stats.arenas[narenas].astats.allocated_large
651 + ctl_stats.huge.allocated;
Jason Evansc66aaf12010-01-29 11:24:19 -0800652 ctl_stats.active = (ctl_stats.arenas[narenas].pactive << PAGE_SHIFT)
653 + ctl_stats.huge.allocated;
Jason Evans3c234352010-01-27 13:10:55 -0800654 ctl_stats.mapped = (ctl_stats.chunks.current << opt_lg_chunk);
655
656# ifdef JEMALLOC_SWAP
657 malloc_mutex_lock(&swap_mtx);
658 ctl_stats.swap_avail = swap_avail;
659 malloc_mutex_unlock(&swap_mtx);
660# endif
661#endif
662
663 ctl_epoch++;
664}
665
666static bool
667ctl_init(void)
668{
669
670 if (ctl_initialized == false) {
671#ifdef JEMALLOC_STATS
672 unsigned i;
673#endif
674
675 /*
676 * Allocate space for one extra arena stats element, which
677 * contains summed stats across all arenas.
678 */
679 ctl_stats.arenas = (ctl_arena_stats_t *)base_alloc(
680 (narenas + 1) * sizeof(ctl_arena_stats_t));
681 if (ctl_stats.arenas == NULL)
682 return (true);
683 memset(ctl_stats.arenas, 0, (narenas + 1) *
684 sizeof(ctl_arena_stats_t));
685
686 /*
687 * Initialize all stats structures, regardless of whether they
688 * ever get used. Lazy initialization would allow errors to
689 * cause inconsistent state to be viewable by the application.
690 */
691#ifdef JEMALLOC_STATS
692 for (i = 0; i <= narenas; i++) {
693 if (ctl_arena_init(&ctl_stats.arenas[i]))
694 return (true);
695 }
696#endif
697 ctl_stats.arenas[narenas].initialized = true;
698
699 ctl_epoch = 0;
700 ctl_refresh();
701 ctl_initialized = true;
702 }
703
704 return (false);
705}
706
707static int
708ctl_lookup(const char *name, ctl_node_t const **nodesp, size_t *mibp,
709 size_t *depthp)
710{
711 int ret;
712 const char *elm, *tdot, *dot;
713 size_t elen, i, j;
714 const ctl_node_t *node;
715
716 elm = name;
717 /* Equivalent to strchrnul(). */
718 dot = ((tdot = strchr(elm, '.')) != NULL) ? tdot : strchr(elm, '\0');
719 elen = (size_t)((uintptr_t)dot - (uintptr_t)elm);
720 if (elen == 0) {
721 ret = ENOENT;
722 goto RETURN;
723 }
724 node = super_root_node;
725 for (i = 0; i < *depthp; i++) {
726 assert(node->named);
727 assert(node->u.named.nchildren > 0);
728 if (node->u.named.children[0].named) {
729 const ctl_node_t *pnode = node;
730
731 /* Children are named. */
732 for (j = 0; j < node->u.named.nchildren; j++) {
733 const ctl_node_t *child =
734 &node->u.named.children[j];
735 if (strlen(child->u.named.name) == elen
736 && strncmp(elm, child->u.named.name,
737 elen) == 0) {
738 node = child;
739 if (nodesp != NULL)
740 nodesp[i] = node;
741 mibp[i] = j;
742 break;
743 }
744 }
745 if (node == pnode) {
746 ret = ENOENT;
747 goto RETURN;
748 }
749 } else {
750 unsigned long index;
751 const ctl_node_t *inode;
752
753 /* Children are indexed. */
754 index = strtoul(elm, NULL, 10);
755 if (index == ULONG_MAX) {
756 ret = ENOENT;
757 goto RETURN;
758 }
759
760 inode = &node->u.named.children[0];
761 node = inode->u.indexed.index(mibp, *depthp,
762 index);
763 if (node == NULL) {
764 ret = ENOENT;
765 goto RETURN;
766 }
767
768 if (nodesp != NULL)
769 nodesp[i] = node;
770 mibp[i] = (size_t)index;
771 }
772
773 if (node->ctl != NULL) {
774 /* Terminal node. */
775 if (*dot != '\0') {
776 /*
777 * The name contains more elements than are
778 * in this path through the tree.
779 */
780 ret = ENOENT;
781 goto RETURN;
782 }
783 /* Complete lookup successful. */
784 *depthp = i + 1;
785 break;
786 }
787
788 /* Update elm. */
789 if (*dot == '\0') {
790 /* No more elements. */
791 ret = ENOENT;
792 goto RETURN;
793 }
794 elm = &dot[1];
795 dot = ((tdot = strchr(elm, '.')) != NULL) ? tdot :
796 strchr(elm, '\0');
797 elen = (size_t)((uintptr_t)dot - (uintptr_t)elm);
798 }
799
800 ret = 0;
801RETURN:
802 return (ret);
803}
804
805int
806ctl_byname(const char *name, void *oldp, size_t *oldlenp, void *newp,
807 size_t newlen)
808{
809 int ret;
810 size_t depth;
811 ctl_node_t const *nodes[CTL_MAX_DEPTH];
812 size_t mib[CTL_MAX_DEPTH];
813
814 malloc_mutex_lock(&ctl_mtx);
815 if (ctl_init()) {
816 ret = EAGAIN;
817 goto RETURN;
818 }
819
820 depth = CTL_MAX_DEPTH;
821 ret = ctl_lookup(name, nodes, mib, &depth);
822 if (ret != 0)
823 goto RETURN;
824
825 if (nodes[depth-1]->ctl == NULL) {
826 /* The name refers to a partial path through the ctl tree. */
827 ret = ENOENT;
828 goto RETURN;
829 }
830 ret = nodes[depth-1]->ctl(mib, depth, oldp, oldlenp, newp, newlen);
831
832RETURN:
833 malloc_mutex_unlock(&ctl_mtx);
834 return(ret);
835}
836
837int
838ctl_nametomib(const char *name, size_t *mibp, size_t *miblenp)
839{
840 int ret;
841
842 malloc_mutex_lock(&ctl_mtx);
843 if (ctl_init()) {
844 ret = EAGAIN;
845 goto RETURN;
846 }
847
848 ret = ctl_lookup(name, NULL, mibp, miblenp);
849
850RETURN:
851 malloc_mutex_unlock(&ctl_mtx);
852 return(ret);
853}
854
855int
856ctl_bymib(const size_t *mib, size_t miblen, void *oldp, size_t *oldlenp,
857 void *newp, size_t newlen)
858{
859 int ret;
860 const ctl_node_t *node;
861 size_t i;
862
863 malloc_mutex_lock(&ctl_mtx);
864 if (ctl_init()) {
865 ret = EAGAIN;
866 goto RETURN;
867 }
868
869 /* Iterate down the tree. */
870 node = super_root_node;
871 for (i = 0; i < miblen; i++) {
872 if (node->u.named.children[0].named) {
873 /* Children are named. */
874 if (node->u.named.nchildren <= mib[i]) {
875 ret = ENOENT;
876 goto RETURN;
877 }
878 node = &node->u.named.children[mib[i]];
879 } else {
880 const ctl_node_t *inode;
881
882 /* Indexed element. */
883 inode = &node->u.named.children[0];
884 node = inode->u.indexed.index(mib, miblen, mib[i]);
885 if (node == NULL) {
886 ret = ENOENT;
887 goto RETURN;
888 }
889 }
890 }
891
892 /* Call the ctl function. */
893 if (node->ctl == NULL) {
894 /* Partial MIB. */
895 ret = ENOENT;
896 goto RETURN;
897 }
898 ret = node->ctl(mib, miblen, oldp, oldlenp, newp, newlen);
899
900RETURN:
901 malloc_mutex_unlock(&ctl_mtx);
902 return(ret);
903}
904
905bool
906ctl_boot(void)
907{
908
909 if (malloc_mutex_init(&ctl_mtx))
910 return (true);
911
912 ctl_initialized = false;
913
914 return (false);
915}
916
917/******************************************************************************/
918/* *_ctl() functions. */
919
920#define READONLY() do { \
921 if (newp != NULL || newlen != 0) { \
922 ret = EPERM; \
923 goto RETURN; \
924 } \
925} while (0)
926
Jason Evans22ca8552010-03-02 11:57:30 -0800927#define WRITEONLY() do { \
Jason Evans3c234352010-01-27 13:10:55 -0800928 if (oldp != NULL || oldlenp != NULL) { \
929 ret = EPERM; \
930 goto RETURN; \
931 } \
932} while (0)
933
Jason Evans22ca8552010-03-02 11:57:30 -0800934#define VOID() do { \
935 READONLY(); \
936 WRITEONLY(); \
937} while (0)
938
Jason Evans3c234352010-01-27 13:10:55 -0800939#define READ(v, t) do { \
940 if (oldp != NULL && oldlenp != NULL) { \
941 if (*oldlenp != sizeof(t)) { \
942 size_t copylen = (sizeof(t) <= *oldlenp) \
943 ? sizeof(t) : *oldlenp; \
944 memcpy(oldp, (void *)&v, copylen); \
945 ret = EINVAL; \
946 goto RETURN; \
947 } else \
948 *(t *)oldp = v; \
949 } \
950} while (0)
951
952#define WRITE(v, t) do { \
953 if (newp != NULL) { \
954 if (newlen != sizeof(t)) { \
955 ret = EINVAL; \
956 goto RETURN; \
957 } \
958 v = *(t *)newp; \
959 } \
960} while (0)
961
962#define CTL_RO_GEN(n, v, t) \
963static int \
964n##_ctl(const size_t *mib, size_t miblen, void *oldp, size_t *oldlenp, \
965 void *newp, size_t newlen) \
966{ \
967 int ret; \
968 t oldval; \
969 \
970 READONLY(); \
971 oldval = v; \
972 READ(oldval, t); \
973 \
974 ret = 0; \
975RETURN: \
976 return (ret); \
977}
978
979#define CTL_RO_TRUE_GEN(n) \
980static int \
981n##_ctl(const size_t *mib, size_t miblen, void *oldp, size_t *oldlenp, \
982 void *newp, size_t newlen) \
983{ \
984 int ret; \
985 bool oldval; \
986 \
987 READONLY(); \
988 oldval = true; \
989 READ(oldval, bool); \
990 \
991 ret = 0; \
992RETURN: \
993 return (ret); \
994}
995
996#define CTL_RO_FALSE_GEN(n) \
997static int \
998n##_ctl(const size_t *mib, size_t miblen, void *oldp, size_t *oldlenp, \
999 void *newp, size_t newlen) \
1000{ \
1001 int ret; \
1002 bool oldval; \
1003 \
1004 READONLY(); \
1005 oldval = false; \
1006 READ(oldval, bool); \
1007 \
1008 ret = 0; \
1009RETURN: \
1010 return (ret); \
1011}
1012
Jason Evansa40bc7a2010-03-02 13:01:16 -08001013CTL_RO_GEN(version, JEMALLOC_VERSION, const char *)
1014
Jason Evans3c234352010-01-27 13:10:55 -08001015static int
1016epoch_ctl(const size_t *mib, size_t miblen, void *oldp, size_t *oldlenp,
1017 void *newp, size_t newlen)
1018{
1019 int ret;
1020 uint64_t newval;
1021
1022 newval = 0;
1023 WRITE(newval, uint64_t);
1024 if (newval != 0)
1025 ctl_refresh();
1026 READ(ctl_epoch, uint64_t);
1027
1028 ret = 0;
1029RETURN:
1030 return (ret);
1031}
1032
1033#ifdef JEMALLOC_TCACHE
1034static int
1035tcache_flush_ctl(const size_t *mib, size_t miblen, void *oldp, size_t *oldlenp,
1036 void *newp, size_t newlen)
1037{
1038 int ret;
1039 tcache_t *tcache;
1040
1041 VOID();
1042
Jason Evans2dbecf12010-09-05 10:35:13 -07001043 tcache = TCACHE_GET();
Jason Evans3c234352010-01-27 13:10:55 -08001044 if (tcache == NULL) {
1045 ret = 0;
1046 goto RETURN;
1047 }
1048 tcache_destroy(tcache);
Jason Evans2dbecf12010-09-05 10:35:13 -07001049 TCACHE_SET(NULL);
Jason Evans3c234352010-01-27 13:10:55 -08001050
1051 ret = 0;
1052RETURN:
1053 return (ret);
1054}
1055#endif
1056
Jason Evansb267d0f2010-08-13 15:42:29 -07001057static int
1058thread_arena_ctl(const size_t *mib, size_t miblen, void *oldp, size_t *oldlenp,
1059 void *newp, size_t newlen)
1060{
1061 int ret;
1062 unsigned newind, oldind;
1063
1064 newind = oldind = choose_arena()->ind;
1065 WRITE(oldind, unsigned);
1066 READ(newind, unsigned);
1067 if (newind != oldind) {
1068 arena_t *arena;
1069
1070 if (newind >= narenas) {
1071 /* New arena index is out of range. */
1072 ret = EFAULT;
1073 goto RETURN;
1074 }
1075
1076 /* Initialize arena if necessary. */
1077 malloc_mutex_lock(&arenas_lock);
1078 if ((arena = arenas[newind]) == NULL)
1079 arena = arenas_extend(newind);
1080 malloc_mutex_unlock(&arenas_lock);
1081 if (arena == NULL) {
1082 ret = EAGAIN;
1083 goto RETURN;
1084 }
1085
1086 /* Set new arena association. */
Jason Evans2dbecf12010-09-05 10:35:13 -07001087 ARENA_SET(arena);
Jason Evansb267d0f2010-08-13 15:42:29 -07001088 }
1089
1090 ret = 0;
1091RETURN:
1092 return (ret);
1093}
Jason Evansb267d0f2010-08-13 15:42:29 -07001094
Jason Evans3c234352010-01-27 13:10:55 -08001095/******************************************************************************/
1096
1097#ifdef JEMALLOC_DEBUG
1098CTL_RO_TRUE_GEN(config_debug)
1099#else
1100CTL_RO_FALSE_GEN(config_debug)
1101#endif
1102
1103#ifdef JEMALLOC_DSS
1104CTL_RO_TRUE_GEN(config_dss)
1105#else
1106CTL_RO_FALSE_GEN(config_dss)
1107#endif
1108
1109#ifdef JEMALLOC_DYNAMIC_PAGE_SHIFT
1110CTL_RO_TRUE_GEN(config_dynamic_page_shift)
1111#else
1112CTL_RO_FALSE_GEN(config_dynamic_page_shift)
1113#endif
1114
1115#ifdef JEMALLOC_FILL
1116CTL_RO_TRUE_GEN(config_fill)
1117#else
1118CTL_RO_FALSE_GEN(config_fill)
1119#endif
1120
1121#ifdef JEMALLOC_LAZY_LOCK
1122CTL_RO_TRUE_GEN(config_lazy_lock)
1123#else
1124CTL_RO_FALSE_GEN(config_lazy_lock)
1125#endif
1126
Jason Evansd34f9e72010-02-11 13:19:21 -08001127#ifdef JEMALLOC_PROF
1128CTL_RO_TRUE_GEN(config_prof)
1129#else
1130CTL_RO_FALSE_GEN(config_prof)
1131#endif
1132
1133#ifdef JEMALLOC_PROF_LIBGCC
1134CTL_RO_TRUE_GEN(config_prof_libgcc)
1135#else
1136CTL_RO_FALSE_GEN(config_prof_libgcc)
1137#endif
1138
1139#ifdef JEMALLOC_PROF_LIBUNWIND
1140CTL_RO_TRUE_GEN(config_prof_libunwind)
1141#else
1142CTL_RO_FALSE_GEN(config_prof_libunwind)
1143#endif
1144
Jason Evans3c234352010-01-27 13:10:55 -08001145#ifdef JEMALLOC_STATS
1146CTL_RO_TRUE_GEN(config_stats)
1147#else
1148CTL_RO_FALSE_GEN(config_stats)
1149#endif
1150
1151#ifdef JEMALLOC_SWAP
1152CTL_RO_TRUE_GEN(config_swap)
1153#else
1154CTL_RO_FALSE_GEN(config_swap)
1155#endif
1156
1157#ifdef JEMALLOC_SYSV
1158CTL_RO_TRUE_GEN(config_sysv)
1159#else
1160CTL_RO_FALSE_GEN(config_sysv)
1161#endif
1162
1163#ifdef JEMALLOC_TCACHE
1164CTL_RO_TRUE_GEN(config_tcache)
1165#else
1166CTL_RO_FALSE_GEN(config_tcache)
1167#endif
1168
1169#ifdef JEMALLOC_TINY
1170CTL_RO_TRUE_GEN(config_tiny)
1171#else
1172CTL_RO_FALSE_GEN(config_tiny)
1173#endif
1174
1175#ifdef JEMALLOC_TLS
1176CTL_RO_TRUE_GEN(config_tls)
1177#else
1178CTL_RO_FALSE_GEN(config_tls)
1179#endif
1180
Jason Evans3c234352010-01-27 13:10:55 -08001181#ifdef JEMALLOC_XMALLOC
1182CTL_RO_TRUE_GEN(config_xmalloc)
1183#else
1184CTL_RO_FALSE_GEN(config_xmalloc)
1185#endif
1186
1187/******************************************************************************/
1188
1189CTL_RO_GEN(opt_abort, opt_abort, bool)
1190#ifdef JEMALLOC_FILL
1191CTL_RO_GEN(opt_junk, opt_junk, bool)
1192#endif
1193#ifdef JEMALLOC_SYSV
1194CTL_RO_GEN(opt_sysv, opt_sysv, bool)
1195#endif
1196#ifdef JEMALLOC_XMALLOC
1197CTL_RO_GEN(opt_xmalloc, opt_xmalloc, bool)
1198#endif
1199#ifdef JEMALLOC_ZERO
1200CTL_RO_GEN(opt_zero, opt_zero, bool)
1201#endif
1202#ifdef JEMALLOC_TCACHE
Jason Evans3fa9a2f2010-03-07 15:34:14 -08001203CTL_RO_GEN(opt_tcache, opt_tcache, bool)
Jason Evans3c234352010-01-27 13:10:55 -08001204CTL_RO_GEN(opt_lg_tcache_gc_sweep, opt_lg_tcache_gc_sweep, ssize_t)
Jason Evans3c234352010-01-27 13:10:55 -08001205#endif
Jason Evansd34f9e72010-02-11 13:19:21 -08001206#ifdef JEMALLOC_PROF
1207CTL_RO_GEN(opt_prof, opt_prof, bool)
Jason Evansf18c9822010-03-31 18:43:24 -07001208CTL_RO_GEN(opt_prof_active, opt_prof_active, bool)
Jason Evansd34f9e72010-02-11 13:19:21 -08001209CTL_RO_GEN(opt_lg_prof_bt_max, opt_lg_prof_bt_max, size_t)
Jason Evansb9477e72010-03-01 20:15:26 -08001210CTL_RO_GEN(opt_lg_prof_sample, opt_lg_prof_sample, size_t)
Jason Evansa02fc082010-03-31 17:35:51 -07001211CTL_RO_GEN(opt_lg_prof_interval, opt_lg_prof_interval, ssize_t)
Jason Evansd34f9e72010-02-11 13:19:21 -08001212CTL_RO_GEN(opt_prof_udump, opt_prof_udump, bool)
1213CTL_RO_GEN(opt_prof_leak, opt_prof_leak, bool)
Jason Evansa881cd22010-10-02 15:18:50 -07001214CTL_RO_GEN(opt_prof_accum, opt_prof_accum, bool)
1215CTL_RO_GEN(opt_lg_prof_tcmax, opt_lg_prof_tcmax, ssize_t)
Jason Evansd34f9e72010-02-11 13:19:21 -08001216#endif
Jason Evans3c234352010-01-27 13:10:55 -08001217CTL_RO_GEN(opt_stats_print, opt_stats_print, bool)
Jason Evans3c234352010-01-27 13:10:55 -08001218CTL_RO_GEN(opt_lg_qspace_max, opt_lg_qspace_max, size_t)
1219CTL_RO_GEN(opt_lg_cspace_max, opt_lg_cspace_max, size_t)
Jason Evans3c234352010-01-27 13:10:55 -08001220CTL_RO_GEN(opt_lg_dirty_mult, opt_lg_dirty_mult, ssize_t)
1221CTL_RO_GEN(opt_lg_chunk, opt_lg_chunk, size_t)
1222#ifdef JEMALLOC_SWAP
1223CTL_RO_GEN(opt_overcommit, opt_overcommit, bool)
1224#endif
1225
1226/******************************************************************************/
1227
1228CTL_RO_GEN(arenas_bin_i_size, arenas[0]->bins[mib[2]].reg_size, size_t)
1229CTL_RO_GEN(arenas_bin_i_nregs, arenas[0]->bins[mib[2]].nregs, uint32_t)
1230CTL_RO_GEN(arenas_bin_i_run_size, arenas[0]->bins[mib[2]].run_size, size_t)
1231const ctl_node_t *
1232arenas_bin_i_index(const size_t *mib, size_t miblen, size_t i)
1233{
1234
1235 if (i > nbins)
1236 return (NULL);
1237 return (super_arenas_bin_i_node);
1238}
1239
1240CTL_RO_GEN(arenas_lrun_i_size, ((mib[2]+1) << PAGE_SHIFT), size_t)
1241const ctl_node_t *
1242arenas_lrun_i_index(const size_t *mib, size_t miblen, size_t i)
1243{
1244
1245 if (i > nlclasses)
1246 return (NULL);
1247 return (super_arenas_lrun_i_node);
1248}
1249
1250CTL_RO_GEN(arenas_narenas, narenas, unsigned)
1251
1252static int
1253arenas_initialized_ctl(const size_t *mib, size_t miblen, void *oldp,
1254 size_t *oldlenp, void *newp, size_t newlen)
1255{
1256 int ret;
1257 unsigned nread, i;
1258
1259 READONLY();
1260 if (*oldlenp != narenas * sizeof(bool)) {
1261 ret = EINVAL;
1262 nread = (*oldlenp < narenas * sizeof(bool))
1263 ? (*oldlenp / sizeof(bool)) : narenas;
1264 } else {
1265 ret = 0;
1266 nread = narenas;
1267 }
1268
1269 for (i = 0; i < nread; i++)
1270 ((bool *)oldp)[i] = ctl_stats.arenas[i].initialized;
1271
1272RETURN:
1273 return (ret);
1274}
1275
1276CTL_RO_GEN(arenas_quantum, QUANTUM, size_t)
1277CTL_RO_GEN(arenas_cacheline, CACHELINE, size_t)
1278CTL_RO_GEN(arenas_subpage, SUBPAGE, size_t)
1279CTL_RO_GEN(arenas_pagesize, PAGE_SIZE, size_t)
Jason Evans3c234352010-01-27 13:10:55 -08001280CTL_RO_GEN(arenas_chunksize, chunksize, size_t)
1281#ifdef JEMALLOC_TINY
1282CTL_RO_GEN(arenas_tspace_min, (1U << LG_TINY_MIN), size_t)
1283CTL_RO_GEN(arenas_tspace_max, (qspace_min >> 1), size_t)
1284#endif
1285CTL_RO_GEN(arenas_qspace_min, qspace_min, size_t)
1286CTL_RO_GEN(arenas_qspace_max, qspace_max, size_t)
1287CTL_RO_GEN(arenas_cspace_min, cspace_min, size_t)
1288CTL_RO_GEN(arenas_cspace_max, cspace_max, size_t)
1289CTL_RO_GEN(arenas_sspace_min, sspace_min, size_t)
1290CTL_RO_GEN(arenas_sspace_max, sspace_max, size_t)
Jason Evansdafde142010-03-17 16:27:39 -07001291#ifdef JEMALLOC_TCACHE
1292CTL_RO_GEN(arenas_tcache_max, tcache_maxclass, size_t)
1293#endif
Jason Evans3c234352010-01-27 13:10:55 -08001294CTL_RO_GEN(arenas_ntbins, ntbins, unsigned)
1295CTL_RO_GEN(arenas_nqbins, nqbins, unsigned)
1296CTL_RO_GEN(arenas_ncbins, ncbins, unsigned)
1297CTL_RO_GEN(arenas_nsbins, nsbins, unsigned)
Jason Evans3c234352010-01-27 13:10:55 -08001298CTL_RO_GEN(arenas_nbins, nbins, unsigned)
Jason Evansdafde142010-03-17 16:27:39 -07001299#ifdef JEMALLOC_TCACHE
1300CTL_RO_GEN(arenas_nhbins, nhbins, unsigned)
1301#endif
Jason Evans3c234352010-01-27 13:10:55 -08001302CTL_RO_GEN(arenas_nlruns, nlclasses, size_t)
1303
Jason Evans6005f072010-09-30 16:55:08 -07001304static int
1305arenas_purge_ctl(const size_t *mib, size_t miblen, void *oldp, size_t *oldlenp,
1306 void *newp, size_t newlen)
1307{
1308 int ret;
1309 unsigned arena;
1310
1311 WRITEONLY();
1312 arena = UINT_MAX;
1313 WRITE(arena, unsigned);
1314 if (newp != NULL && arena >= narenas) {
1315 ret = EFAULT;
1316 goto RETURN;
1317 } else {
1318 arena_t *tarenas[narenas];
1319
1320 malloc_mutex_lock(&arenas_lock);
1321 memcpy(tarenas, arenas, sizeof(arena_t *) * narenas);
1322 malloc_mutex_unlock(&arenas_lock);
1323
1324 if (arena == UINT_MAX) {
Jason Evans1506a1b2010-10-07 08:52:32 -07001325 unsigned i;
1326 for (i = 0; i < narenas; i++) {
Jason Evans6005f072010-09-30 16:55:08 -07001327 if (tarenas[i] != NULL)
1328 arena_purge_all(tarenas[i]);
1329 }
1330 } else {
1331 assert(arena < narenas);
1332 if (tarenas[arena] != NULL)
1333 arena_purge_all(tarenas[arena]);
1334 }
1335 }
1336
1337 ret = 0;
1338RETURN:
1339 return (ret);
1340}
1341
Jason Evans3c234352010-01-27 13:10:55 -08001342/******************************************************************************/
1343
Jason Evansd34f9e72010-02-11 13:19:21 -08001344#ifdef JEMALLOC_PROF
1345static int
Jason Evansf18c9822010-03-31 18:43:24 -07001346prof_active_ctl(const size_t *mib, size_t miblen, void *oldp, size_t *oldlenp,
1347 void *newp, size_t newlen)
1348{
1349 int ret;
1350 bool oldval;
1351
1352 oldval = opt_prof_active;
1353 if (newp != NULL) {
1354 /*
1355 * The memory barriers will tend to make opt_prof_active
1356 * propagate faster on systems with weak memory ordering.
1357 */
1358 mb_write();
1359 WRITE(opt_prof_active, bool);
1360 mb_write();
1361 }
1362 READ(oldval, bool);
1363
1364 ret = 0;
1365RETURN:
1366 return (ret);
1367}
1368
1369static int
Jason Evansd34f9e72010-02-11 13:19:21 -08001370prof_dump_ctl(const size_t *mib, size_t miblen, void *oldp, size_t *oldlenp,
1371 void *newp, size_t newlen)
1372{
1373 int ret;
Jason Evans22ca8552010-03-02 11:57:30 -08001374 const char *filename = NULL;
Jason Evansd34f9e72010-02-11 13:19:21 -08001375
Jason Evans22ca8552010-03-02 11:57:30 -08001376 WRITEONLY();
1377 WRITE(filename, const char *);
Jason Evansd34f9e72010-02-11 13:19:21 -08001378
Jason Evans22ca8552010-03-02 11:57:30 -08001379 if (prof_mdump(filename)) {
1380 ret = EFAULT;
1381 goto RETURN;
1382 }
Jason Evansd34f9e72010-02-11 13:19:21 -08001383
1384 ret = 0;
1385RETURN:
1386 return (ret);
1387}
1388
1389CTL_RO_GEN(prof_interval, prof_interval, uint64_t)
1390#endif
1391
1392/******************************************************************************/
1393
Jason Evans3c234352010-01-27 13:10:55 -08001394#ifdef JEMALLOC_STATS
1395CTL_RO_GEN(stats_chunks_current, ctl_stats.chunks.current, size_t)
1396CTL_RO_GEN(stats_chunks_total, ctl_stats.chunks.total, uint64_t)
1397CTL_RO_GEN(stats_chunks_high, ctl_stats.chunks.high, size_t)
1398CTL_RO_GEN(stats_huge_allocated, huge_allocated, size_t)
1399CTL_RO_GEN(stats_huge_nmalloc, huge_nmalloc, uint64_t)
1400CTL_RO_GEN(stats_huge_ndalloc, huge_ndalloc, uint64_t)
1401CTL_RO_GEN(stats_arenas_i_small_allocated,
Jason Evans86815df2010-03-13 20:32:56 -08001402 ctl_stats.arenas[mib[2]].allocated_small, size_t)
Jason Evans3c234352010-01-27 13:10:55 -08001403CTL_RO_GEN(stats_arenas_i_small_nmalloc,
Jason Evans86815df2010-03-13 20:32:56 -08001404 ctl_stats.arenas[mib[2]].nmalloc_small, uint64_t)
Jason Evans3c234352010-01-27 13:10:55 -08001405CTL_RO_GEN(stats_arenas_i_small_ndalloc,
Jason Evans86815df2010-03-13 20:32:56 -08001406 ctl_stats.arenas[mib[2]].ndalloc_small, uint64_t)
1407CTL_RO_GEN(stats_arenas_i_small_nrequests,
1408 ctl_stats.arenas[mib[2]].nrequests_small, uint64_t)
Jason Evans3c234352010-01-27 13:10:55 -08001409CTL_RO_GEN(stats_arenas_i_large_allocated,
1410 ctl_stats.arenas[mib[2]].astats.allocated_large, size_t)
1411CTL_RO_GEN(stats_arenas_i_large_nmalloc,
1412 ctl_stats.arenas[mib[2]].astats.nmalloc_large, uint64_t)
1413CTL_RO_GEN(stats_arenas_i_large_ndalloc,
1414 ctl_stats.arenas[mib[2]].astats.ndalloc_large, uint64_t)
Jason Evansdafde142010-03-17 16:27:39 -07001415CTL_RO_GEN(stats_arenas_i_large_nrequests,
1416 ctl_stats.arenas[mib[2]].astats.nrequests_large, uint64_t)
Jason Evans3c234352010-01-27 13:10:55 -08001417
Jason Evans86815df2010-03-13 20:32:56 -08001418CTL_RO_GEN(stats_arenas_i_bins_j_allocated,
1419 ctl_stats.arenas[mib[2]].bstats[mib[4]].allocated, size_t)
1420CTL_RO_GEN(stats_arenas_i_bins_j_nmalloc,
1421 ctl_stats.arenas[mib[2]].bstats[mib[4]].nmalloc, uint64_t)
1422CTL_RO_GEN(stats_arenas_i_bins_j_ndalloc,
1423 ctl_stats.arenas[mib[2]].bstats[mib[4]].ndalloc, uint64_t)
Jason Evans3c234352010-01-27 13:10:55 -08001424CTL_RO_GEN(stats_arenas_i_bins_j_nrequests,
1425 ctl_stats.arenas[mib[2]].bstats[mib[4]].nrequests, uint64_t)
1426#ifdef JEMALLOC_TCACHE
1427CTL_RO_GEN(stats_arenas_i_bins_j_nfills,
1428 ctl_stats.arenas[mib[2]].bstats[mib[4]].nfills, uint64_t)
1429CTL_RO_GEN(stats_arenas_i_bins_j_nflushes,
1430 ctl_stats.arenas[mib[2]].bstats[mib[4]].nflushes, uint64_t)
1431#endif
1432CTL_RO_GEN(stats_arenas_i_bins_j_nruns,
1433 ctl_stats.arenas[mib[2]].bstats[mib[4]].nruns, uint64_t)
1434CTL_RO_GEN(stats_arenas_i_bins_j_nreruns,
1435 ctl_stats.arenas[mib[2]].bstats[mib[4]].reruns, uint64_t)
1436CTL_RO_GEN(stats_arenas_i_bins_j_highruns,
1437 ctl_stats.arenas[mib[2]].bstats[mib[4]].highruns, size_t)
1438CTL_RO_GEN(stats_arenas_i_bins_j_curruns,
1439 ctl_stats.arenas[mib[2]].bstats[mib[4]].curruns, size_t)
1440
1441const ctl_node_t *
1442stats_arenas_i_bins_j_index(const size_t *mib, size_t miblen, size_t j)
1443{
1444
1445 if (j > nbins)
1446 return (NULL);
1447 return (super_stats_arenas_i_bins_j_node);
1448}
1449
Jason Evansdafde142010-03-17 16:27:39 -07001450CTL_RO_GEN(stats_arenas_i_lruns_j_nmalloc,
1451 ctl_stats.arenas[mib[2]].lstats[mib[4]].nmalloc, uint64_t)
1452CTL_RO_GEN(stats_arenas_i_lruns_j_ndalloc,
1453 ctl_stats.arenas[mib[2]].lstats[mib[4]].ndalloc, uint64_t)
Jason Evans3c234352010-01-27 13:10:55 -08001454CTL_RO_GEN(stats_arenas_i_lruns_j_nrequests,
1455 ctl_stats.arenas[mib[2]].lstats[mib[4]].nrequests, uint64_t)
1456CTL_RO_GEN(stats_arenas_i_lruns_j_curruns,
1457 ctl_stats.arenas[mib[2]].lstats[mib[4]].curruns, size_t)
1458CTL_RO_GEN(stats_arenas_i_lruns_j_highruns,
1459 ctl_stats.arenas[mib[2]].lstats[mib[4]].highruns, size_t)
1460
1461const ctl_node_t *
1462stats_arenas_i_lruns_j_index(const size_t *mib, size_t miblen, size_t j)
1463{
1464
1465 if (j > nlclasses)
1466 return (NULL);
1467 return (super_stats_arenas_i_lruns_j_node);
1468}
1469
1470#endif
1471CTL_RO_GEN(stats_arenas_i_pactive, ctl_stats.arenas[mib[2]].pactive, size_t)
1472CTL_RO_GEN(stats_arenas_i_pdirty, ctl_stats.arenas[mib[2]].pdirty, size_t)
1473#ifdef JEMALLOC_STATS
1474CTL_RO_GEN(stats_arenas_i_mapped, ctl_stats.arenas[mib[2]].astats.mapped,
1475 size_t)
1476CTL_RO_GEN(stats_arenas_i_npurge, ctl_stats.arenas[mib[2]].astats.npurge,
1477 uint64_t)
1478CTL_RO_GEN(stats_arenas_i_nmadvise, ctl_stats.arenas[mib[2]].astats.nmadvise,
1479 uint64_t)
1480CTL_RO_GEN(stats_arenas_i_purged, ctl_stats.arenas[mib[2]].astats.purged,
1481 uint64_t)
1482#endif
1483
1484const ctl_node_t *
1485stats_arenas_i_index(const size_t *mib, size_t miblen, size_t i)
1486{
1487
1488 if (ctl_stats.arenas[i].initialized == false)
1489 return (NULL);
1490 return (super_stats_arenas_i_node);
1491}
1492
1493#ifdef JEMALLOC_STATS
1494CTL_RO_GEN(stats_allocated, ctl_stats.allocated, size_t)
1495CTL_RO_GEN(stats_active, ctl_stats.active, size_t)
1496CTL_RO_GEN(stats_mapped, ctl_stats.mapped, size_t)
1497#endif
1498
1499/******************************************************************************/
1500
1501#ifdef JEMALLOC_SWAP
1502# ifdef JEMALLOC_STATS
1503CTL_RO_GEN(swap_avail, ctl_stats.swap_avail, size_t)
1504# endif
1505
1506static int
1507swap_prezeroed_ctl(const size_t *mib, size_t miblen, void *oldp,
1508 size_t *oldlenp, void *newp, size_t newlen)
1509{
1510 int ret;
1511
1512 if (swap_enabled) {
1513 READONLY();
1514 } else {
1515 /*
1516 * swap_prezeroed isn't actually used by the swap code until it
Jason Evans13668262010-01-31 03:57:29 -08001517 * is set during a successful chunk_swap_enabled() call. We
Jason Evans3c234352010-01-27 13:10:55 -08001518 * use it here to store the value that we'll pass to
1519 * chunk_swap_enable() in a swap.fds mallctl(). This is not
1520 * very clean, but the obvious alternatives are even worse.
1521 */
1522 WRITE(swap_prezeroed, bool);
1523 }
1524
1525 READ(swap_prezeroed, bool);
1526
1527 ret = 0;
1528RETURN:
1529 return (ret);
1530}
1531
1532CTL_RO_GEN(swap_nfds, swap_nfds, size_t)
1533
1534static int
1535swap_fds_ctl(const size_t *mib, size_t miblen, void *oldp, size_t *oldlenp,
1536 void *newp, size_t newlen)
1537{
1538 int ret;
1539
1540 if (swap_enabled) {
1541 READONLY();
1542 } else if (newp != NULL) {
1543 size_t nfds = newlen / sizeof(int);
1544
1545 {
1546 int fds[nfds];
1547
1548 memcpy(fds, newp, nfds * sizeof(int));
1549 if (chunk_swap_enable(fds, nfds, swap_prezeroed)) {
1550 ret = EFAULT;
1551 goto RETURN;
1552 }
1553 }
1554 }
1555
1556 if (oldp != NULL && oldlenp != NULL) {
1557 if (*oldlenp != swap_nfds * sizeof(int)) {
1558 size_t copylen = (swap_nfds * sizeof(int) <= *oldlenp)
1559 ? swap_nfds * sizeof(int) : *oldlenp;
1560
1561 memcpy(oldp, swap_fds, copylen);
1562 ret = EINVAL;
1563 goto RETURN;
1564 } else
1565 memcpy(oldp, swap_fds, *oldlenp);
1566 }
1567
1568 ret = 0;
1569RETURN:
1570 return (ret);
1571}
1572#endif