base.h revision 1.1 1 1.1 christos #ifndef JEMALLOC_INTERNAL_BASE_H
2 1.1 christos #define JEMALLOC_INTERNAL_BASE_H
3 1.1 christos
4 1.1 christos #include "jemalloc/internal/edata.h"
5 1.1 christos #include "jemalloc/internal/ehooks.h"
6 1.1 christos #include "jemalloc/internal/mutex.h"
7 1.1 christos
8 1.1 christos enum metadata_thp_mode_e {
9 1.1 christos metadata_thp_disabled = 0,
10 1.1 christos /*
11 1.1 christos * Lazily enable hugepage for metadata. To avoid high RSS caused by THP
12 1.1 christos * + low usage arena (i.e. THP becomes a significant percentage), the
13 1.1 christos * "auto" option only starts using THP after a base allocator used up
14 1.1 christos * the first THP region. Starting from the second hugepage (in a single
15 1.1 christos * arena), "auto" behaves the same as "always", i.e. madvise hugepage
16 1.1 christos * right away.
17 1.1 christos */
18 1.1 christos metadata_thp_auto = 1,
19 1.1 christos metadata_thp_always = 2,
20 1.1 christos metadata_thp_mode_limit = 3
21 1.1 christos };
22 1.1 christos typedef enum metadata_thp_mode_e metadata_thp_mode_t;
23 1.1 christos
24 1.1 christos #define METADATA_THP_DEFAULT metadata_thp_disabled
25 1.1 christos extern metadata_thp_mode_t opt_metadata_thp;
26 1.1 christos extern const char *metadata_thp_mode_names[];
27 1.1 christos
28 1.1 christos
29 1.1 christos /* Embedded at the beginning of every block of base-managed virtual memory. */
30 1.1 christos typedef struct base_block_s base_block_t;
31 1.1 christos struct base_block_s {
32 1.1 christos /* Total size of block's virtual memory mapping. */
33 1.1 christos size_t size;
34 1.1 christos
35 1.1 christos /* Next block in list of base's blocks. */
36 1.1 christos base_block_t *next;
37 1.1 christos
38 1.1 christos /* Tracks unused trailing space. */
39 1.1 christos edata_t edata;
40 1.1 christos };
41 1.1 christos
42 1.1 christos typedef struct base_s base_t;
43 1.1 christos struct base_s {
44 1.1 christos /*
45 1.1 christos * User-configurable extent hook functions.
46 1.1 christos */
47 1.1 christos ehooks_t ehooks;
48 1.1 christos
49 1.1 christos /*
50 1.1 christos * User-configurable extent hook functions for metadata allocations.
51 1.1 christos */
52 1.1 christos ehooks_t ehooks_base;
53 1.1 christos
54 1.1 christos /* Protects base_alloc() and base_stats_get() operations. */
55 1.1 christos malloc_mutex_t mtx;
56 1.1 christos
57 1.1 christos /* Using THP when true (metadata_thp auto mode). */
58 1.1 christos bool auto_thp_switched;
59 1.1 christos /*
60 1.1 christos * Most recent size class in the series of increasingly large base
61 1.1 christos * extents. Logarithmic spacing between subsequent allocations ensures
62 1.1 christos * that the total number of distinct mappings remains small.
63 1.1 christos */
64 1.1 christos pszind_t pind_last;
65 1.1 christos
66 1.1 christos /* Serial number generation state. */
67 1.1 christos size_t extent_sn_next;
68 1.1 christos
69 1.1 christos /* Chain of all blocks associated with base. */
70 1.1 christos base_block_t *blocks;
71 1.1 christos
72 1.1 christos /* Heap of extents that track unused trailing space within blocks. */
73 1.1 christos edata_heap_t avail[SC_NSIZES];
74 1.1 christos
75 1.1 christos /* Stats, only maintained if config_stats. */
76 1.1 christos size_t allocated;
77 1.1 christos size_t resident;
78 1.1 christos size_t mapped;
79 1.1 christos /* Number of THP regions touched. */
80 1.1 christos size_t n_thp;
81 1.1 christos };
82 1.1 christos
83 1.1 christos static inline unsigned
84 1.1 christos base_ind_get(const base_t *base) {
85 1.1 christos return ehooks_ind_get(&base->ehooks);
86 1.1 christos }
87 1.1 christos
88 1.1 christos static inline bool
89 1.1 christos metadata_thp_enabled(void) {
90 1.1 christos return (opt_metadata_thp != metadata_thp_disabled);
91 1.1 christos }
92 1.1 christos
93 1.1 christos base_t *b0get(void);
94 1.1 christos base_t *base_new(tsdn_t *tsdn, unsigned ind,
95 1.1 christos const extent_hooks_t *extent_hooks, bool metadata_use_hooks);
96 1.1 christos void base_delete(tsdn_t *tsdn, base_t *base);
97 1.1 christos ehooks_t *base_ehooks_get(base_t *base);
98 1.1 christos ehooks_t *base_ehooks_get_for_metadata(base_t *base);
99 1.1 christos extent_hooks_t *base_extent_hooks_set(base_t *base,
100 1.1 christos extent_hooks_t *extent_hooks);
101 1.1 christos void *base_alloc(tsdn_t *tsdn, base_t *base, size_t size, size_t alignment);
102 1.1 christos edata_t *base_alloc_edata(tsdn_t *tsdn, base_t *base);
103 1.1 christos void base_stats_get(tsdn_t *tsdn, base_t *base, size_t *allocated,
104 1.1 christos size_t *resident, size_t *mapped, size_t *n_thp);
105 1.1 christos void base_prefork(tsdn_t *tsdn, base_t *base);
106 1.1 christos void base_postfork_parent(tsdn_t *tsdn, base_t *base);
107 1.1 christos void base_postfork_child(tsdn_t *tsdn, base_t *base);
108 1.1 christos bool base_boot(tsdn_t *tsdn);
109 1.1 christos
110 1.1 christos #endif /* JEMALLOC_INTERNAL_BASE_H */
111