1 #ifndef JEMALLOC_INTERNAL_CTL_H
2 #define JEMALLOC_INTERNAL_CTL_H
3 
4 #include "jemalloc/internal/jemalloc_internal_types.h"
5 #include "jemalloc/internal/malloc_io.h"
6 #include "jemalloc/internal/mutex_prof.h"
7 #include "jemalloc/internal/ql.h"
8 #include "jemalloc/internal/sc.h"
9 #include "jemalloc/internal/stats.h"
10 
11 /* Maximum ctl tree depth. */
12 #define CTL_MAX_DEPTH	7
13 
14 typedef struct ctl_node_s {
15 	bool named;
16 } ctl_node_t;
17 
18 typedef struct ctl_named_node_s {
19 	ctl_node_t node;
20 	const char *name;
21 	/* If (nchildren == 0), this is a terminal node. */
22 	size_t nchildren;
23 	const ctl_node_t *children;
24 	int (*ctl)(tsd_t *, const size_t *, size_t, void *, size_t *, void *,
25 	    size_t);
26 } ctl_named_node_t;
27 
28 typedef struct ctl_indexed_node_s {
29 	struct ctl_node_s node;
30 	const ctl_named_node_t *(*index)(tsdn_t *, const size_t *, size_t,
31 	    size_t);
32 } ctl_indexed_node_t;
33 
34 typedef struct ctl_arena_stats_s {
35 	arena_stats_t astats;
36 
37 	/* Aggregate stats for small size classes, based on bin stats. */
38 	size_t allocated_small;
39 	uint64_t nmalloc_small;
40 	uint64_t ndalloc_small;
41 	uint64_t nrequests_small;
42 	uint64_t nfills_small;
43 	uint64_t nflushes_small;
44 
45 	bin_stats_t bstats[SC_NBINS];
46 	arena_stats_large_t lstats[SC_NSIZES - SC_NBINS];
47 	arena_stats_extents_t estats[SC_NPSIZES];
48 } ctl_arena_stats_t;
49 
50 typedef struct ctl_stats_s {
51 	size_t allocated;
52 	size_t active;
53 	size_t metadata;
54 	size_t metadata_thp;
55 	size_t resident;
56 	size_t mapped;
57 	size_t retained;
58 
59 	background_thread_stats_t background_thread;
60 	mutex_prof_data_t mutex_prof_data[mutex_prof_num_global_mutexes];
61 } ctl_stats_t;
62 
63 typedef struct ctl_arena_s ctl_arena_t;
64 struct ctl_arena_s {
65 	unsigned arena_ind;
66 	bool initialized;
67 	ql_elm(ctl_arena_t) destroyed_link;
68 
69 	/* Basic stats, supported even if !config_stats. */
70 	unsigned nthreads;
71 	const char *dss;
72 	ssize_t dirty_decay_ms;
73 	ssize_t muzzy_decay_ms;
74 	size_t pactive;
75 	size_t pdirty;
76 	size_t pmuzzy;
77 
78 	/* NULL if !config_stats. */
79 	ctl_arena_stats_t *astats;
80 };
81 
82 typedef struct ctl_arenas_s {
83 	uint64_t epoch;
84 	unsigned narenas;
85 	ql_head(ctl_arena_t) destroyed;
86 
87 	/*
88 	 * Element 0 corresponds to merged stats for extant arenas (accessed via
89 	 * MALLCTL_ARENAS_ALL), element 1 corresponds to merged stats for
90 	 * destroyed arenas (accessed via MALLCTL_ARENAS_DESTROYED), and the
91 	 * remaining MALLOCX_ARENA_LIMIT elements correspond to arenas.
92 	 */
93 	ctl_arena_t *arenas[2 + MALLOCX_ARENA_LIMIT];
94 } ctl_arenas_t;
95 
96 int ctl_byname(tsd_t *tsd, const char *name, void *oldp, size_t *oldlenp,
97     void *newp, size_t newlen);
98 int ctl_nametomib(tsd_t *tsd, const char *name, size_t *mibp, size_t *miblenp);
99 
100 int ctl_bymib(tsd_t *tsd, const size_t *mib, size_t miblen, void *oldp,
101     size_t *oldlenp, void *newp, size_t newlen);
102 bool ctl_boot(void);
103 void ctl_prefork(tsdn_t *tsdn);
104 void ctl_postfork_parent(tsdn_t *tsdn);
105 void ctl_postfork_child(tsdn_t *tsdn);
106 
107 #define xmallctl(name, oldp, oldlenp, newp, newlen) do {		\
108 	if (je_mallctl(name, oldp, oldlenp, newp, newlen)		\
109 	    != 0) {							\
110 		malloc_printf(						\
111 		    "<jemalloc>: Failure in xmallctl(\"%s\", ...)\n",	\
112 		    name);						\
113 		abort();						\
114 	}								\
115 } while (0)
116 
117 #define xmallctlnametomib(name, mibp, miblenp) do {			\
118 	if (je_mallctlnametomib(name, mibp, miblenp) != 0) {		\
119 		malloc_printf("<jemalloc>: Failure in "			\
120 		    "xmallctlnametomib(\"%s\", ...)\n", name);		\
121 		abort();						\
122 	}								\
123 } while (0)
124 
125 #define xmallctlbymib(mib, miblen, oldp, oldlenp, newp, newlen) do {	\
126 	if (je_mallctlbymib(mib, miblen, oldp, oldlenp, newp,		\
127 	    newlen) != 0) {						\
128 		malloc_write(						\
129 		    "<jemalloc>: Failure in xmallctlbymib()\n");	\
130 		abort();						\
131 	}								\
132 } while (0)
133 
134 #endif /* JEMALLOC_INTERNAL_CTL_H */
135