Jason Evans | e476f8a | 2010-01-16 09:53:50 -0800 | [diff] [blame] | 1 | #define JEMALLOC_ARENA_C_ |
Jason Evans | 376b152 | 2010-02-11 14:45:59 -0800 | [diff] [blame] | 2 | #include "jemalloc/internal/jemalloc_internal.h" |
Jason Evans | e476f8a | 2010-01-16 09:53:50 -0800 | [diff] [blame] | 3 | |
| 4 | /******************************************************************************/ |
| 5 | /* Data. */ |
| 6 | |
Jason Evans | 243f7a0 | 2016-02-19 20:09:31 -0800 | [diff] [blame] | 7 | purge_mode_t opt_purge = PURGE_DEFAULT; |
| 8 | const char *purge_mode_names[] = { |
| 9 | "ratio", |
| 10 | "decay", |
| 11 | "N/A" |
| 12 | }; |
Jason Evans | e476f8a | 2010-01-16 09:53:50 -0800 | [diff] [blame] | 13 | ssize_t opt_lg_dirty_mult = LG_DIRTY_MULT_DEFAULT; |
Jason Evans | 8d6a3e8 | 2015-03-18 18:55:33 -0700 | [diff] [blame] | 14 | static ssize_t lg_dirty_mult_default; |
Jason Evans | 243f7a0 | 2016-02-19 20:09:31 -0800 | [diff] [blame] | 15 | ssize_t opt_decay_time = DECAY_TIME_DEFAULT; |
| 16 | static ssize_t decay_time_default; |
| 17 | |
Jason Evans | b172610 | 2012-02-28 16:50:47 -0800 | [diff] [blame] | 18 | arena_bin_info_t arena_bin_info[NBINS]; |
Jason Evans | e476f8a | 2010-01-16 09:53:50 -0800 | [diff] [blame] | 19 | |
Jason Evans | 155bfa7 | 2014-10-05 17:54:10 -0700 | [diff] [blame] | 20 | size_t map_bias; |
| 21 | size_t map_misc_offset; |
| 22 | size_t arena_maxrun; /* Max run size for arenas. */ |
Jason Evans | 676df88 | 2015-09-11 20:50:20 -0700 | [diff] [blame] | 23 | size_t large_maxclass; /* Max large size class. */ |
Jason Evans | 0da8ce1 | 2016-02-22 16:20:56 -0800 | [diff] [blame] | 24 | size_t run_quantize_max; /* Max run_quantize_*() input. */ |
| 25 | static size_t small_maxrun; /* Max run size for small size classes. */ |
Jason Evans | 8a03cf0 | 2015-05-04 09:58:36 -0700 | [diff] [blame] | 26 | static bool *small_run_tab; /* Valid small run page multiples. */ |
Jason Evans | 0da8ce1 | 2016-02-22 16:20:56 -0800 | [diff] [blame] | 27 | static size_t *run_quantize_floor_tab; /* run_quantize_floor() memoization. */ |
| 28 | static size_t *run_quantize_ceil_tab; /* run_quantize_ceil() memoization. */ |
Jason Evans | 3c4d92e | 2014-10-12 22:53:59 -0700 | [diff] [blame] | 29 | unsigned nlclasses; /* Number of large size classes. */ |
| 30 | unsigned nhclasses; /* Number of huge size classes. */ |
Dave Watson | 3417a30 | 2016-02-23 12:06:21 -0800 | [diff] [blame] | 31 | static szind_t runs_avail_bias; /* Size index for first runs_avail tree. */ |
| 32 | static szind_t runs_avail_nclasses; /* Number of runs_avail trees. */ |
Jason Evans | e476f8a | 2010-01-16 09:53:50 -0800 | [diff] [blame] | 33 | |
| 34 | /******************************************************************************/ |
Jason Evans | aa5113b | 2014-01-14 16:23:03 -0800 | [diff] [blame] | 35 | /* |
| 36 | * Function prototypes for static functions that are referenced prior to |
| 37 | * definition. |
| 38 | */ |
Jason Evans | e476f8a | 2010-01-16 09:53:50 -0800 | [diff] [blame] | 39 | |
Jason Evans | b2c0d63 | 2016-04-13 23:36:15 -0700 | [diff] [blame] | 40 | static void arena_purge_to_limit(tsd_t *tsd, arena_t *arena, |
| 41 | size_t ndirty_limit); |
| 42 | static void arena_run_dalloc(tsd_t *tsd, arena_t *arena, arena_run_t *run, |
| 43 | bool dirty, bool cleaned, bool decommitted); |
| 44 | static void arena_dalloc_bin_run(tsd_t *tsd, arena_t *arena, |
| 45 | arena_chunk_t *chunk, arena_run_t *run, arena_bin_t *bin); |
Jason Evans | 940a2e0 | 2010-10-17 17:51:37 -0700 | [diff] [blame] | 46 | static void arena_bin_lower_run(arena_t *arena, arena_chunk_t *chunk, |
| 47 | arena_run_t *run, arena_bin_t *bin); |
Jason Evans | e476f8a | 2010-01-16 09:53:50 -0800 | [diff] [blame] | 48 | |
| 49 | /******************************************************************************/ |
| 50 | |
Jason Evans | 8fadb1a | 2015-08-04 10:49:46 -0700 | [diff] [blame] | 51 | JEMALLOC_INLINE_C size_t |
Joshua Kahn | 13b4015 | 2015-09-18 16:58:17 -0400 | [diff] [blame] | 52 | arena_miscelm_size_get(const arena_chunk_map_misc_t *miscelm) |
Jason Evans | 8fadb1a | 2015-08-04 10:49:46 -0700 | [diff] [blame] | 53 | { |
| 54 | arena_chunk_t *chunk; |
| 55 | size_t pageind, mapbits; |
| 56 | |
Jason Evans | 8fadb1a | 2015-08-04 10:49:46 -0700 | [diff] [blame] | 57 | chunk = (arena_chunk_t *)CHUNK_ADDR2BASE(miscelm); |
| 58 | pageind = arena_miscelm_to_pageind(miscelm); |
| 59 | mapbits = arena_mapbits_get(chunk, pageind); |
Jason Evans | 5ef33a9 | 2015-08-19 14:12:05 -0700 | [diff] [blame] | 60 | return (arena_mapbits_size_decode(mapbits)); |
Ben Maurer | f9ff603 | 2014-04-06 13:24:16 -0700 | [diff] [blame] | 61 | } |
| 62 | |
Jason Evans | c6a2c39 | 2016-03-26 17:30:37 -0700 | [diff] [blame] | 63 | JEMALLOC_INLINE_C int |
| 64 | arena_run_addr_comp(const arena_chunk_map_misc_t *a, |
| 65 | const arena_chunk_map_misc_t *b) |
| 66 | { |
| 67 | uintptr_t a_miscelm = (uintptr_t)a; |
| 68 | uintptr_t b_miscelm = (uintptr_t)b; |
| 69 | |
| 70 | assert(a != NULL); |
| 71 | assert(b != NULL); |
| 72 | |
| 73 | return ((a_miscelm > b_miscelm) - (a_miscelm < b_miscelm)); |
| 74 | } |
| 75 | |
| 76 | /* Generate pairing heap functions. */ |
| 77 | ph_gen(static UNUSED, arena_run_heap_, arena_run_heap_t, arena_chunk_map_misc_t, |
| 78 | ph_link, arena_run_addr_comp) |
| 79 | |
Jason Evans | 8a03cf0 | 2015-05-04 09:58:36 -0700 | [diff] [blame] | 80 | static size_t |
Jason Evans | 0da8ce1 | 2016-02-22 16:20:56 -0800 | [diff] [blame] | 81 | run_quantize_floor_compute(size_t size) |
Jason Evans | 8a03cf0 | 2015-05-04 09:58:36 -0700 | [diff] [blame] | 82 | { |
| 83 | size_t qsize; |
| 84 | |
| 85 | assert(size != 0); |
| 86 | assert(size == PAGE_CEILING(size)); |
| 87 | |
| 88 | /* Don't change sizes that are valid small run sizes. */ |
| 89 | if (size <= small_maxrun && small_run_tab[size >> LG_PAGE]) |
| 90 | return (size); |
| 91 | |
| 92 | /* |
| 93 | * Round down to the nearest run size that can actually be requested |
| 94 | * during normal large allocation. Add large_pad so that cache index |
| 95 | * randomization can offset the allocation from the page boundary. |
| 96 | */ |
| 97 | qsize = index2size(size2index(size - large_pad + 1) - 1) + large_pad; |
| 98 | if (qsize <= SMALL_MAXCLASS + large_pad) |
Jason Evans | 0da8ce1 | 2016-02-22 16:20:56 -0800 | [diff] [blame] | 99 | return (run_quantize_floor_compute(size - large_pad)); |
Jason Evans | 8a03cf0 | 2015-05-04 09:58:36 -0700 | [diff] [blame] | 100 | assert(qsize <= size); |
| 101 | return (qsize); |
| 102 | } |
| 103 | |
| 104 | static size_t |
Jason Evans | 0da8ce1 | 2016-02-22 16:20:56 -0800 | [diff] [blame] | 105 | run_quantize_ceil_compute_hard(size_t size) |
Jason Evans | 8a03cf0 | 2015-05-04 09:58:36 -0700 | [diff] [blame] | 106 | { |
| 107 | size_t large_run_size_next; |
| 108 | |
| 109 | assert(size != 0); |
| 110 | assert(size == PAGE_CEILING(size)); |
| 111 | |
| 112 | /* |
| 113 | * Return the next quantized size greater than the input size. |
| 114 | * Quantized sizes comprise the union of run sizes that back small |
| 115 | * region runs, and run sizes that back large regions with no explicit |
| 116 | * alignment constraints. |
| 117 | */ |
| 118 | |
| 119 | if (size > SMALL_MAXCLASS) { |
| 120 | large_run_size_next = PAGE_CEILING(index2size(size2index(size - |
| 121 | large_pad) + 1) + large_pad); |
| 122 | } else |
| 123 | large_run_size_next = SIZE_T_MAX; |
| 124 | if (size >= small_maxrun) |
| 125 | return (large_run_size_next); |
| 126 | |
| 127 | while (true) { |
| 128 | size += PAGE; |
| 129 | assert(size <= small_maxrun); |
| 130 | if (small_run_tab[size >> LG_PAGE]) { |
| 131 | if (large_run_size_next < size) |
| 132 | return (large_run_size_next); |
| 133 | return (size); |
| 134 | } |
| 135 | } |
| 136 | } |
| 137 | |
| 138 | static size_t |
Jason Evans | 0da8ce1 | 2016-02-22 16:20:56 -0800 | [diff] [blame] | 139 | run_quantize_ceil_compute(size_t size) |
Jason Evans | 8a03cf0 | 2015-05-04 09:58:36 -0700 | [diff] [blame] | 140 | { |
Jason Evans | 0da8ce1 | 2016-02-22 16:20:56 -0800 | [diff] [blame] | 141 | size_t qsize = run_quantize_floor_compute(size); |
Jason Evans | 8a03cf0 | 2015-05-04 09:58:36 -0700 | [diff] [blame] | 142 | |
| 143 | if (qsize < size) { |
| 144 | /* |
| 145 | * Skip a quantization that may have an adequately large run, |
| 146 | * because under-sized runs may be mixed in. This only happens |
| 147 | * when an unusual size is requested, i.e. for aligned |
| 148 | * allocation, and is just one of several places where linear |
| 149 | * search would potentially find sufficiently aligned available |
| 150 | * memory somewhere lower. |
| 151 | */ |
Jason Evans | 0da8ce1 | 2016-02-22 16:20:56 -0800 | [diff] [blame] | 152 | qsize = run_quantize_ceil_compute_hard(qsize); |
Jason Evans | 8a03cf0 | 2015-05-04 09:58:36 -0700 | [diff] [blame] | 153 | } |
| 154 | return (qsize); |
| 155 | } |
Jason Evans | 0da8ce1 | 2016-02-22 16:20:56 -0800 | [diff] [blame] | 156 | |
| 157 | #ifdef JEMALLOC_JET |
| 158 | #undef run_quantize_floor |
Jason Evans | ab0cfe0 | 2016-04-18 15:11:20 -0700 | [diff] [blame] | 159 | #define run_quantize_floor JEMALLOC_N(n_run_quantize_floor) |
Jason Evans | 0da8ce1 | 2016-02-22 16:20:56 -0800 | [diff] [blame] | 160 | #endif |
| 161 | static size_t |
| 162 | run_quantize_floor(size_t size) |
| 163 | { |
| 164 | size_t ret; |
| 165 | |
| 166 | assert(size > 0); |
| 167 | assert(size <= run_quantize_max); |
| 168 | assert((size & PAGE_MASK) == 0); |
| 169 | |
| 170 | ret = run_quantize_floor_tab[(size >> LG_PAGE) - 1]; |
| 171 | assert(ret == run_quantize_floor_compute(size)); |
| 172 | return (ret); |
| 173 | } |
| 174 | #ifdef JEMALLOC_JET |
| 175 | #undef run_quantize_floor |
| 176 | #define run_quantize_floor JEMALLOC_N(run_quantize_floor) |
Jason Evans | ab0cfe0 | 2016-04-18 15:11:20 -0700 | [diff] [blame] | 177 | run_quantize_t *run_quantize_floor = JEMALLOC_N(n_run_quantize_floor); |
Jason Evans | 0da8ce1 | 2016-02-22 16:20:56 -0800 | [diff] [blame] | 178 | #endif |
| 179 | |
| 180 | #ifdef JEMALLOC_JET |
| 181 | #undef run_quantize_ceil |
Jason Evans | ab0cfe0 | 2016-04-18 15:11:20 -0700 | [diff] [blame] | 182 | #define run_quantize_ceil JEMALLOC_N(n_run_quantize_ceil) |
Jason Evans | 0da8ce1 | 2016-02-22 16:20:56 -0800 | [diff] [blame] | 183 | #endif |
| 184 | static size_t |
| 185 | run_quantize_ceil(size_t size) |
| 186 | { |
| 187 | size_t ret; |
| 188 | |
| 189 | assert(size > 0); |
| 190 | assert(size <= run_quantize_max); |
| 191 | assert((size & PAGE_MASK) == 0); |
| 192 | |
| 193 | ret = run_quantize_ceil_tab[(size >> LG_PAGE) - 1]; |
| 194 | assert(ret == run_quantize_ceil_compute(size)); |
| 195 | return (ret); |
| 196 | } |
Jason Evans | a9a4684 | 2016-02-22 14:58:05 -0800 | [diff] [blame] | 197 | #ifdef JEMALLOC_JET |
| 198 | #undef run_quantize_ceil |
| 199 | #define run_quantize_ceil JEMALLOC_N(run_quantize_ceil) |
Jason Evans | ab0cfe0 | 2016-04-18 15:11:20 -0700 | [diff] [blame] | 200 | run_quantize_t *run_quantize_ceil = JEMALLOC_N(n_run_quantize_ceil); |
Jason Evans | a9a4684 | 2016-02-22 14:58:05 -0800 | [diff] [blame] | 201 | #endif |
Jason Evans | 8a03cf0 | 2015-05-04 09:58:36 -0700 | [diff] [blame] | 202 | |
Jason Evans | c6a2c39 | 2016-03-26 17:30:37 -0700 | [diff] [blame] | 203 | static arena_run_heap_t * |
Dave Watson | 3417a30 | 2016-02-23 12:06:21 -0800 | [diff] [blame] | 204 | arena_runs_avail_get(arena_t *arena, szind_t ind) |
| 205 | { |
| 206 | |
| 207 | assert(ind >= runs_avail_bias); |
| 208 | assert(ind - runs_avail_bias < runs_avail_nclasses); |
| 209 | |
| 210 | return (&arena->runs_avail[ind - runs_avail_bias]); |
| 211 | } |
Jason Evans | e476f8a | 2010-01-16 09:53:50 -0800 | [diff] [blame] | 212 | |
Jason Evans | e3d1306 | 2012-10-30 15:42:37 -0700 | [diff] [blame] | 213 | static void |
| 214 | arena_avail_insert(arena_t *arena, arena_chunk_t *chunk, size_t pageind, |
Qinfan Wu | 90737fc | 2014-07-21 19:39:20 -0700 | [diff] [blame] | 215 | size_t npages) |
Jason Evans | e3d1306 | 2012-10-30 15:42:37 -0700 | [diff] [blame] | 216 | { |
Dave Watson | 3417a30 | 2016-02-23 12:06:21 -0800 | [diff] [blame] | 217 | szind_t ind = size2index(run_quantize_floor(arena_miscelm_size_get( |
Jason Evans | 61a6dfc | 2016-03-23 16:04:38 -0700 | [diff] [blame] | 218 | arena_miscelm_get_const(chunk, pageind)))); |
Jason Evans | e3d1306 | 2012-10-30 15:42:37 -0700 | [diff] [blame] | 219 | assert(npages == (arena_mapbits_unallocated_size_get(chunk, pageind) >> |
| 220 | LG_PAGE)); |
Jason Evans | c6a2c39 | 2016-03-26 17:30:37 -0700 | [diff] [blame] | 221 | arena_run_heap_insert(arena_runs_avail_get(arena, ind), |
| 222 | arena_miscelm_get_mutable(chunk, pageind)); |
Jason Evans | e3d1306 | 2012-10-30 15:42:37 -0700 | [diff] [blame] | 223 | } |
| 224 | |
| 225 | static void |
| 226 | arena_avail_remove(arena_t *arena, arena_chunk_t *chunk, size_t pageind, |
Qinfan Wu | 90737fc | 2014-07-21 19:39:20 -0700 | [diff] [blame] | 227 | size_t npages) |
Jason Evans | e3d1306 | 2012-10-30 15:42:37 -0700 | [diff] [blame] | 228 | { |
Dave Watson | 3417a30 | 2016-02-23 12:06:21 -0800 | [diff] [blame] | 229 | szind_t ind = size2index(run_quantize_floor(arena_miscelm_size_get( |
Jason Evans | 61a6dfc | 2016-03-23 16:04:38 -0700 | [diff] [blame] | 230 | arena_miscelm_get_const(chunk, pageind)))); |
Jason Evans | e3d1306 | 2012-10-30 15:42:37 -0700 | [diff] [blame] | 231 | assert(npages == (arena_mapbits_unallocated_size_get(chunk, pageind) >> |
| 232 | LG_PAGE)); |
Jason Evans | c6a2c39 | 2016-03-26 17:30:37 -0700 | [diff] [blame] | 233 | arena_run_heap_remove(arena_runs_avail_get(arena, ind), |
| 234 | arena_miscelm_get_mutable(chunk, pageind)); |
Jason Evans | e3d1306 | 2012-10-30 15:42:37 -0700 | [diff] [blame] | 235 | } |
| 236 | |
Jason Evans | 070b3c3 | 2014-08-14 14:45:58 -0700 | [diff] [blame] | 237 | static void |
Jason Evans | ee41ad4 | 2015-02-15 18:04:46 -0800 | [diff] [blame] | 238 | arena_run_dirty_insert(arena_t *arena, arena_chunk_t *chunk, size_t pageind, |
Jason Evans | 070b3c3 | 2014-08-14 14:45:58 -0700 | [diff] [blame] | 239 | size_t npages) |
| 240 | { |
Jason Evans | 61a6dfc | 2016-03-23 16:04:38 -0700 | [diff] [blame] | 241 | arena_chunk_map_misc_t *miscelm = arena_miscelm_get_mutable(chunk, |
| 242 | pageind); |
Jason Evans | ee41ad4 | 2015-02-15 18:04:46 -0800 | [diff] [blame] | 243 | |
Jason Evans | 070b3c3 | 2014-08-14 14:45:58 -0700 | [diff] [blame] | 244 | assert(npages == (arena_mapbits_unallocated_size_get(chunk, pageind) >> |
| 245 | LG_PAGE)); |
| 246 | assert(arena_mapbits_dirty_get(chunk, pageind) == CHUNK_MAP_DIRTY); |
| 247 | assert(arena_mapbits_dirty_get(chunk, pageind+npages-1) == |
| 248 | CHUNK_MAP_DIRTY); |
Jason Evans | ee41ad4 | 2015-02-15 18:04:46 -0800 | [diff] [blame] | 249 | |
Jason Evans | 613cdc8 | 2016-03-08 01:04:48 -0800 | [diff] [blame] | 250 | qr_new(&miscelm->rd, rd_link); |
| 251 | qr_meld(&arena->runs_dirty, &miscelm->rd, rd_link); |
Jason Evans | 070b3c3 | 2014-08-14 14:45:58 -0700 | [diff] [blame] | 252 | arena->ndirty += npages; |
| 253 | } |
| 254 | |
| 255 | static void |
Jason Evans | ee41ad4 | 2015-02-15 18:04:46 -0800 | [diff] [blame] | 256 | arena_run_dirty_remove(arena_t *arena, arena_chunk_t *chunk, size_t pageind, |
Jason Evans | 070b3c3 | 2014-08-14 14:45:58 -0700 | [diff] [blame] | 257 | size_t npages) |
| 258 | { |
Jason Evans | 61a6dfc | 2016-03-23 16:04:38 -0700 | [diff] [blame] | 259 | arena_chunk_map_misc_t *miscelm = arena_miscelm_get_mutable(chunk, |
| 260 | pageind); |
Jason Evans | ee41ad4 | 2015-02-15 18:04:46 -0800 | [diff] [blame] | 261 | |
Jason Evans | 070b3c3 | 2014-08-14 14:45:58 -0700 | [diff] [blame] | 262 | assert(npages == (arena_mapbits_unallocated_size_get(chunk, pageind) >> |
| 263 | LG_PAGE)); |
| 264 | assert(arena_mapbits_dirty_get(chunk, pageind) == CHUNK_MAP_DIRTY); |
| 265 | assert(arena_mapbits_dirty_get(chunk, pageind+npages-1) == |
| 266 | CHUNK_MAP_DIRTY); |
Jason Evans | ee41ad4 | 2015-02-15 18:04:46 -0800 | [diff] [blame] | 267 | |
Jason Evans | 613cdc8 | 2016-03-08 01:04:48 -0800 | [diff] [blame] | 268 | qr_remove(&miscelm->rd, rd_link); |
Jason Evans | ee41ad4 | 2015-02-15 18:04:46 -0800 | [diff] [blame] | 269 | assert(arena->ndirty >= npages); |
Jason Evans | 070b3c3 | 2014-08-14 14:45:58 -0700 | [diff] [blame] | 270 | arena->ndirty -= npages; |
| 271 | } |
| 272 | |
Jason Evans | ee41ad4 | 2015-02-15 18:04:46 -0800 | [diff] [blame] | 273 | static size_t |
| 274 | arena_chunk_dirty_npages(const extent_node_t *node) |
| 275 | { |
| 276 | |
| 277 | return (extent_node_size_get(node) >> LG_PAGE); |
| 278 | } |
| 279 | |
Jason Evans | ee41ad4 | 2015-02-15 18:04:46 -0800 | [diff] [blame] | 280 | void |
Jason Evans | 738e089 | 2015-02-18 01:15:50 -0800 | [diff] [blame] | 281 | arena_chunk_cache_maybe_insert(arena_t *arena, extent_node_t *node, bool cache) |
Jason Evans | ee41ad4 | 2015-02-15 18:04:46 -0800 | [diff] [blame] | 282 | { |
| 283 | |
Jason Evans | 738e089 | 2015-02-18 01:15:50 -0800 | [diff] [blame] | 284 | if (cache) { |
Jason Evans | 47701b2 | 2015-02-17 22:23:10 -0800 | [diff] [blame] | 285 | extent_node_dirty_linkage_init(node); |
Jason Evans | 738e089 | 2015-02-18 01:15:50 -0800 | [diff] [blame] | 286 | extent_node_dirty_insert(node, &arena->runs_dirty, |
| 287 | &arena->chunks_cache); |
Jason Evans | ee41ad4 | 2015-02-15 18:04:46 -0800 | [diff] [blame] | 288 | arena->ndirty += arena_chunk_dirty_npages(node); |
| 289 | } |
| 290 | } |
| 291 | |
| 292 | void |
Jason Evans | 738e089 | 2015-02-18 01:15:50 -0800 | [diff] [blame] | 293 | arena_chunk_cache_maybe_remove(arena_t *arena, extent_node_t *node, bool dirty) |
Jason Evans | ee41ad4 | 2015-02-15 18:04:46 -0800 | [diff] [blame] | 294 | { |
| 295 | |
| 296 | if (dirty) { |
Jason Evans | 738e089 | 2015-02-18 01:15:50 -0800 | [diff] [blame] | 297 | extent_node_dirty_remove(node); |
Jason Evans | ee41ad4 | 2015-02-15 18:04:46 -0800 | [diff] [blame] | 298 | assert(arena->ndirty >= arena_chunk_dirty_npages(node)); |
| 299 | arena->ndirty -= arena_chunk_dirty_npages(node); |
| 300 | } |
| 301 | } |
| 302 | |
Jason Evans | af1f592 | 2014-10-30 16:38:08 -0700 | [diff] [blame] | 303 | JEMALLOC_INLINE_C void * |
Jason Evans | 49f7e8f | 2011-03-15 13:59:15 -0700 | [diff] [blame] | 304 | arena_run_reg_alloc(arena_run_t *run, arena_bin_info_t *bin_info) |
Jason Evans | e476f8a | 2010-01-16 09:53:50 -0800 | [diff] [blame] | 305 | { |
| 306 | void *ret; |
Jason Evans | 42ce80e | 2016-02-25 20:51:00 -0800 | [diff] [blame] | 307 | size_t regind; |
Jason Evans | 0c5dd03 | 2014-09-29 01:31:39 -0700 | [diff] [blame] | 308 | arena_chunk_map_misc_t *miscelm; |
| 309 | void *rpages; |
Jason Evans | e476f8a | 2010-01-16 09:53:50 -0800 | [diff] [blame] | 310 | |
Jason Evans | 1e0a636 | 2010-03-13 13:41:58 -0800 | [diff] [blame] | 311 | assert(run->nfree > 0); |
Jason Evans | 551ebc4 | 2014-10-03 10:16:09 -0700 | [diff] [blame] | 312 | assert(!bitmap_full(run->bitmap, &bin_info->bitmap_info)); |
Jason Evans | e476f8a | 2010-01-16 09:53:50 -0800 | [diff] [blame] | 313 | |
Jason Evans | 9e1810c | 2016-02-24 12:42:23 -0800 | [diff] [blame] | 314 | regind = (unsigned)bitmap_sfu(run->bitmap, &bin_info->bitmap_info); |
Jason Evans | 0c5dd03 | 2014-09-29 01:31:39 -0700 | [diff] [blame] | 315 | miscelm = arena_run_to_miscelm(run); |
| 316 | rpages = arena_miscelm_to_rpages(miscelm); |
| 317 | ret = (void *)((uintptr_t)rpages + (uintptr_t)bin_info->reg0_offset + |
Jason Evans | 122449b | 2012-04-06 00:35:09 -0700 | [diff] [blame] | 318 | (uintptr_t)(bin_info->reg_interval * regind)); |
Jason Evans | 1e0a636 | 2010-03-13 13:41:58 -0800 | [diff] [blame] | 319 | run->nfree--; |
Jason Evans | 1e0a636 | 2010-03-13 13:41:58 -0800 | [diff] [blame] | 320 | return (ret); |
Jason Evans | 6109fe0 | 2010-02-10 10:37:56 -0800 | [diff] [blame] | 321 | } |
| 322 | |
Jason Evans | af1f592 | 2014-10-30 16:38:08 -0700 | [diff] [blame] | 323 | JEMALLOC_INLINE_C void |
Jason Evans | 1e0a636 | 2010-03-13 13:41:58 -0800 | [diff] [blame] | 324 | arena_run_reg_dalloc(arena_run_t *run, void *ptr) |
Jason Evans | 6109fe0 | 2010-02-10 10:37:56 -0800 | [diff] [blame] | 325 | { |
Jason Evans | 49f7e8f | 2011-03-15 13:59:15 -0700 | [diff] [blame] | 326 | arena_chunk_t *chunk = (arena_chunk_t *)CHUNK_ADDR2BASE(run); |
Jason Evans | 203484e | 2012-05-02 00:30:36 -0700 | [diff] [blame] | 327 | size_t pageind = ((uintptr_t)ptr - (uintptr_t)chunk) >> LG_PAGE; |
| 328 | size_t mapbits = arena_mapbits_get(chunk, pageind); |
Jason Evans | d01fd19 | 2015-08-19 15:21:32 -0700 | [diff] [blame] | 329 | szind_t binind = arena_ptr_small_binind_get(ptr, mapbits); |
Jason Evans | 49f7e8f | 2011-03-15 13:59:15 -0700 | [diff] [blame] | 330 | arena_bin_info_t *bin_info = &arena_bin_info[binind]; |
Jason Evans | 42ce80e | 2016-02-25 20:51:00 -0800 | [diff] [blame] | 331 | size_t regind = arena_run_regind(run, bin_info, ptr); |
Jason Evans | 84c8eef | 2011-03-16 10:30:13 -0700 | [diff] [blame] | 332 | |
Jason Evans | 49f7e8f | 2011-03-15 13:59:15 -0700 | [diff] [blame] | 333 | assert(run->nfree < bin_info->nregs); |
Jason Evans | 1e0a636 | 2010-03-13 13:41:58 -0800 | [diff] [blame] | 334 | /* Freeing an interior pointer can cause assertion failure. */ |
Jason Evans | 0c5dd03 | 2014-09-29 01:31:39 -0700 | [diff] [blame] | 335 | assert(((uintptr_t)ptr - |
| 336 | ((uintptr_t)arena_miscelm_to_rpages(arena_run_to_miscelm(run)) + |
Jason Evans | 122449b | 2012-04-06 00:35:09 -0700 | [diff] [blame] | 337 | (uintptr_t)bin_info->reg0_offset)) % |
| 338 | (uintptr_t)bin_info->reg_interval == 0); |
Jason Evans | 0c5dd03 | 2014-09-29 01:31:39 -0700 | [diff] [blame] | 339 | assert((uintptr_t)ptr >= |
| 340 | (uintptr_t)arena_miscelm_to_rpages(arena_run_to_miscelm(run)) + |
Jason Evans | 49f7e8f | 2011-03-15 13:59:15 -0700 | [diff] [blame] | 341 | (uintptr_t)bin_info->reg0_offset); |
Jason Evans | 84c8eef | 2011-03-16 10:30:13 -0700 | [diff] [blame] | 342 | /* Freeing an unallocated pointer can cause assertion failure. */ |
Jason Evans | 0c5dd03 | 2014-09-29 01:31:39 -0700 | [diff] [blame] | 343 | assert(bitmap_get(run->bitmap, &bin_info->bitmap_info, regind)); |
Jason Evans | e476f8a | 2010-01-16 09:53:50 -0800 | [diff] [blame] | 344 | |
Jason Evans | 0c5dd03 | 2014-09-29 01:31:39 -0700 | [diff] [blame] | 345 | bitmap_unset(run->bitmap, &bin_info->bitmap_info, regind); |
Jason Evans | 1e0a636 | 2010-03-13 13:41:58 -0800 | [diff] [blame] | 346 | run->nfree++; |
Jason Evans | e476f8a | 2010-01-16 09:53:50 -0800 | [diff] [blame] | 347 | } |
| 348 | |
Jason Evans | af1f592 | 2014-10-30 16:38:08 -0700 | [diff] [blame] | 349 | JEMALLOC_INLINE_C void |
Jason Evans | 3806748 | 2013-01-21 20:04:42 -0800 | [diff] [blame] | 350 | arena_run_zero(arena_chunk_t *chunk, size_t run_ind, size_t npages) |
| 351 | { |
| 352 | |
Jason Evans | bd87b01 | 2014-04-15 16:35:08 -0700 | [diff] [blame] | 353 | JEMALLOC_VALGRIND_MAKE_MEM_UNDEFINED((void *)((uintptr_t)chunk + |
| 354 | (run_ind << LG_PAGE)), (npages << LG_PAGE)); |
Jason Evans | 3806748 | 2013-01-21 20:04:42 -0800 | [diff] [blame] | 355 | memset((void *)((uintptr_t)chunk + (run_ind << LG_PAGE)), 0, |
| 356 | (npages << LG_PAGE)); |
Jason Evans | 3806748 | 2013-01-21 20:04:42 -0800 | [diff] [blame] | 357 | } |
| 358 | |
Jason Evans | af1f592 | 2014-10-30 16:38:08 -0700 | [diff] [blame] | 359 | JEMALLOC_INLINE_C void |
Jason Evans | dda90f5 | 2013-10-19 23:48:40 -0700 | [diff] [blame] | 360 | arena_run_page_mark_zeroed(arena_chunk_t *chunk, size_t run_ind) |
| 361 | { |
| 362 | |
Jason Evans | bd87b01 | 2014-04-15 16:35:08 -0700 | [diff] [blame] | 363 | JEMALLOC_VALGRIND_MAKE_MEM_DEFINED((void *)((uintptr_t)chunk + (run_ind |
| 364 | << LG_PAGE)), PAGE); |
Jason Evans | dda90f5 | 2013-10-19 23:48:40 -0700 | [diff] [blame] | 365 | } |
| 366 | |
Jason Evans | af1f592 | 2014-10-30 16:38:08 -0700 | [diff] [blame] | 367 | JEMALLOC_INLINE_C void |
Jason Evans | 3806748 | 2013-01-21 20:04:42 -0800 | [diff] [blame] | 368 | arena_run_page_validate_zeroed(arena_chunk_t *chunk, size_t run_ind) |
Jason Evans | 21fb95b | 2010-10-18 17:45:40 -0700 | [diff] [blame] | 369 | { |
Jason Evans | d4bab21 | 2010-10-24 20:08:37 -0700 | [diff] [blame] | 370 | size_t i; |
Jason Evans | ae4c7b4 | 2012-04-02 07:04:34 -0700 | [diff] [blame] | 371 | UNUSED size_t *p = (size_t *)((uintptr_t)chunk + (run_ind << LG_PAGE)); |
Jason Evans | d4bab21 | 2010-10-24 20:08:37 -0700 | [diff] [blame] | 372 | |
Jason Evans | dda90f5 | 2013-10-19 23:48:40 -0700 | [diff] [blame] | 373 | arena_run_page_mark_zeroed(chunk, run_ind); |
Jason Evans | ae4c7b4 | 2012-04-02 07:04:34 -0700 | [diff] [blame] | 374 | for (i = 0; i < PAGE / sizeof(size_t); i++) |
Jason Evans | 21fb95b | 2010-10-18 17:45:40 -0700 | [diff] [blame] | 375 | assert(p[i] == 0); |
| 376 | } |
Jason Evans | 21fb95b | 2010-10-18 17:45:40 -0700 | [diff] [blame] | 377 | |
Jason Evans | e476f8a | 2010-01-16 09:53:50 -0800 | [diff] [blame] | 378 | static void |
Jason Evans | 40ee9aa | 2016-02-27 12:34:50 -0800 | [diff] [blame] | 379 | arena_nactive_add(arena_t *arena, size_t add_pages) |
Jason Evans | aa5113b | 2014-01-14 16:23:03 -0800 | [diff] [blame] | 380 | { |
| 381 | |
| 382 | if (config_stats) { |
Jason Evans | 3763d3b | 2016-02-26 17:29:35 -0800 | [diff] [blame] | 383 | size_t cactive_add = CHUNK_CEILING((arena->nactive + |
| 384 | add_pages) << LG_PAGE) - CHUNK_CEILING(arena->nactive << |
Jason Evans | 1522937 | 2014-08-06 23:38:39 -0700 | [diff] [blame] | 385 | LG_PAGE); |
Jason Evans | 3763d3b | 2016-02-26 17:29:35 -0800 | [diff] [blame] | 386 | if (cactive_add != 0) |
| 387 | stats_cactive_add(cactive_add); |
| 388 | } |
Jason Evans | 40ee9aa | 2016-02-27 12:34:50 -0800 | [diff] [blame] | 389 | arena->nactive += add_pages; |
Jason Evans | 3763d3b | 2016-02-26 17:29:35 -0800 | [diff] [blame] | 390 | } |
| 391 | |
| 392 | static void |
Jason Evans | 40ee9aa | 2016-02-27 12:34:50 -0800 | [diff] [blame] | 393 | arena_nactive_sub(arena_t *arena, size_t sub_pages) |
Jason Evans | 3763d3b | 2016-02-26 17:29:35 -0800 | [diff] [blame] | 394 | { |
| 395 | |
| 396 | if (config_stats) { |
| 397 | size_t cactive_sub = CHUNK_CEILING(arena->nactive << LG_PAGE) - |
| 398 | CHUNK_CEILING((arena->nactive - sub_pages) << LG_PAGE); |
| 399 | if (cactive_sub != 0) |
| 400 | stats_cactive_sub(cactive_sub); |
Jason Evans | aa5113b | 2014-01-14 16:23:03 -0800 | [diff] [blame] | 401 | } |
Jason Evans | 40ee9aa | 2016-02-27 12:34:50 -0800 | [diff] [blame] | 402 | arena->nactive -= sub_pages; |
Jason Evans | aa5113b | 2014-01-14 16:23:03 -0800 | [diff] [blame] | 403 | } |
| 404 | |
| 405 | static void |
| 406 | arena_run_split_remove(arena_t *arena, arena_chunk_t *chunk, size_t run_ind, |
Jason Evans | 8fadb1a | 2015-08-04 10:49:46 -0700 | [diff] [blame] | 407 | size_t flag_dirty, size_t flag_decommitted, size_t need_pages) |
Jason Evans | aa5113b | 2014-01-14 16:23:03 -0800 | [diff] [blame] | 408 | { |
| 409 | size_t total_pages, rem_pages; |
| 410 | |
Jason Evans | 8fadb1a | 2015-08-04 10:49:46 -0700 | [diff] [blame] | 411 | assert(flag_dirty == 0 || flag_decommitted == 0); |
| 412 | |
Jason Evans | aa5113b | 2014-01-14 16:23:03 -0800 | [diff] [blame] | 413 | total_pages = arena_mapbits_unallocated_size_get(chunk, run_ind) >> |
| 414 | LG_PAGE; |
| 415 | assert(arena_mapbits_dirty_get(chunk, run_ind+total_pages-1) == |
| 416 | flag_dirty); |
| 417 | assert(need_pages <= total_pages); |
| 418 | rem_pages = total_pages - need_pages; |
| 419 | |
Qinfan Wu | 90737fc | 2014-07-21 19:39:20 -0700 | [diff] [blame] | 420 | arena_avail_remove(arena, chunk, run_ind, total_pages); |
Jason Evans | 070b3c3 | 2014-08-14 14:45:58 -0700 | [diff] [blame] | 421 | if (flag_dirty != 0) |
Jason Evans | ee41ad4 | 2015-02-15 18:04:46 -0800 | [diff] [blame] | 422 | arena_run_dirty_remove(arena, chunk, run_ind, total_pages); |
Jason Evans | 40ee9aa | 2016-02-27 12:34:50 -0800 | [diff] [blame] | 423 | arena_nactive_add(arena, need_pages); |
Jason Evans | aa5113b | 2014-01-14 16:23:03 -0800 | [diff] [blame] | 424 | |
| 425 | /* Keep track of trailing unused pages for later use. */ |
| 426 | if (rem_pages > 0) { |
Jason Evans | 8fadb1a | 2015-08-04 10:49:46 -0700 | [diff] [blame] | 427 | size_t flags = flag_dirty | flag_decommitted; |
Jason Evans | 1f27abc | 2015-08-11 12:42:33 -0700 | [diff] [blame] | 428 | size_t flag_unzeroed_mask = (flags == 0) ? CHUNK_MAP_UNZEROED : |
| 429 | 0; |
Jason Evans | 8fadb1a | 2015-08-04 10:49:46 -0700 | [diff] [blame] | 430 | |
Jason Evans | 1f27abc | 2015-08-11 12:42:33 -0700 | [diff] [blame] | 431 | arena_mapbits_unallocated_set(chunk, run_ind+need_pages, |
| 432 | (rem_pages << LG_PAGE), flags | |
| 433 | (arena_mapbits_unzeroed_get(chunk, run_ind+need_pages) & |
| 434 | flag_unzeroed_mask)); |
| 435 | arena_mapbits_unallocated_set(chunk, run_ind+total_pages-1, |
| 436 | (rem_pages << LG_PAGE), flags | |
| 437 | (arena_mapbits_unzeroed_get(chunk, run_ind+total_pages-1) & |
| 438 | flag_unzeroed_mask)); |
| 439 | if (flag_dirty != 0) { |
| 440 | arena_run_dirty_insert(arena, chunk, run_ind+need_pages, |
| 441 | rem_pages); |
Jason Evans | aa5113b | 2014-01-14 16:23:03 -0800 | [diff] [blame] | 442 | } |
Qinfan Wu | 90737fc | 2014-07-21 19:39:20 -0700 | [diff] [blame] | 443 | arena_avail_insert(arena, chunk, run_ind+need_pages, rem_pages); |
Jason Evans | aa5113b | 2014-01-14 16:23:03 -0800 | [diff] [blame] | 444 | } |
| 445 | } |
| 446 | |
Jason Evans | 8fadb1a | 2015-08-04 10:49:46 -0700 | [diff] [blame] | 447 | static bool |
Jason Evans | aa5113b | 2014-01-14 16:23:03 -0800 | [diff] [blame] | 448 | arena_run_split_large_helper(arena_t *arena, arena_run_t *run, size_t size, |
| 449 | bool remove, bool zero) |
Jason Evans | e476f8a | 2010-01-16 09:53:50 -0800 | [diff] [blame] | 450 | { |
| 451 | arena_chunk_t *chunk; |
Jason Evans | 0c5dd03 | 2014-09-29 01:31:39 -0700 | [diff] [blame] | 452 | arena_chunk_map_misc_t *miscelm; |
Dmitry-Me | a306a60 | 2015-09-04 13:15:28 +0300 | [diff] [blame] | 453 | size_t flag_dirty, flag_decommitted, run_ind, need_pages; |
Jason Evans | 1f27abc | 2015-08-11 12:42:33 -0700 | [diff] [blame] | 454 | size_t flag_unzeroed_mask; |
Jason Evans | 203484e | 2012-05-02 00:30:36 -0700 | [diff] [blame] | 455 | |
Jason Evans | e476f8a | 2010-01-16 09:53:50 -0800 | [diff] [blame] | 456 | chunk = (arena_chunk_t *)CHUNK_ADDR2BASE(run); |
Jason Evans | 0c5dd03 | 2014-09-29 01:31:39 -0700 | [diff] [blame] | 457 | miscelm = arena_run_to_miscelm(run); |
| 458 | run_ind = arena_miscelm_to_pageind(miscelm); |
Jason Evans | 203484e | 2012-05-02 00:30:36 -0700 | [diff] [blame] | 459 | flag_dirty = arena_mapbits_dirty_get(chunk, run_ind); |
Jason Evans | 8fadb1a | 2015-08-04 10:49:46 -0700 | [diff] [blame] | 460 | flag_decommitted = arena_mapbits_decommitted_get(chunk, run_ind); |
Jason Evans | ae4c7b4 | 2012-04-02 07:04:34 -0700 | [diff] [blame] | 461 | need_pages = (size >> LG_PAGE); |
Jason Evans | e476f8a | 2010-01-16 09:53:50 -0800 | [diff] [blame] | 462 | assert(need_pages > 0); |
Jason Evans | e476f8a | 2010-01-16 09:53:50 -0800 | [diff] [blame] | 463 | |
Jason Evans | de249c8 | 2015-08-09 16:47:27 -0700 | [diff] [blame] | 464 | if (flag_decommitted != 0 && arena->chunk_hooks.commit(chunk, chunksize, |
| 465 | run_ind << LG_PAGE, size, arena->ind)) |
Jason Evans | 8fadb1a | 2015-08-04 10:49:46 -0700 | [diff] [blame] | 466 | return (true); |
| 467 | |
Jason Evans | c368f8c | 2013-10-29 18:17:42 -0700 | [diff] [blame] | 468 | if (remove) { |
Jason Evans | aa5113b | 2014-01-14 16:23:03 -0800 | [diff] [blame] | 469 | arena_run_split_remove(arena, chunk, run_ind, flag_dirty, |
Jason Evans | 8fadb1a | 2015-08-04 10:49:46 -0700 | [diff] [blame] | 470 | flag_decommitted, need_pages); |
Jason Evans | 19b3d61 | 2010-03-18 20:36:40 -0700 | [diff] [blame] | 471 | } |
| 472 | |
Jason Evans | aa5113b | 2014-01-14 16:23:03 -0800 | [diff] [blame] | 473 | if (zero) { |
Jason Evans | 1f27abc | 2015-08-11 12:42:33 -0700 | [diff] [blame] | 474 | if (flag_decommitted != 0) { |
| 475 | /* The run is untouched, and therefore zeroed. */ |
| 476 | JEMALLOC_VALGRIND_MAKE_MEM_DEFINED((void |
| 477 | *)((uintptr_t)chunk + (run_ind << LG_PAGE)), |
| 478 | (need_pages << LG_PAGE)); |
| 479 | } else if (flag_dirty != 0) { |
| 480 | /* The run is dirty, so all pages must be zeroed. */ |
| 481 | arena_run_zero(chunk, run_ind, need_pages); |
| 482 | } else { |
Jason Evans | aa5113b | 2014-01-14 16:23:03 -0800 | [diff] [blame] | 483 | /* |
| 484 | * The run is clean, so some pages may be zeroed (i.e. |
| 485 | * never before touched). |
| 486 | */ |
Dmitry-Me | a306a60 | 2015-09-04 13:15:28 +0300 | [diff] [blame] | 487 | size_t i; |
Jason Evans | aa5113b | 2014-01-14 16:23:03 -0800 | [diff] [blame] | 488 | for (i = 0; i < need_pages; i++) { |
| 489 | if (arena_mapbits_unzeroed_get(chunk, run_ind+i) |
| 490 | != 0) |
| 491 | arena_run_zero(chunk, run_ind+i, 1); |
| 492 | else if (config_debug) { |
| 493 | arena_run_page_validate_zeroed(chunk, |
| 494 | run_ind+i); |
| 495 | } else { |
| 496 | arena_run_page_mark_zeroed(chunk, |
| 497 | run_ind+i); |
Jason Evans | 19b3d61 | 2010-03-18 20:36:40 -0700 | [diff] [blame] | 498 | } |
Jason Evans | e476f8a | 2010-01-16 09:53:50 -0800 | [diff] [blame] | 499 | } |
| 500 | } |
Jason Evans | 19b3d61 | 2010-03-18 20:36:40 -0700 | [diff] [blame] | 501 | } else { |
Jason Evans | bd87b01 | 2014-04-15 16:35:08 -0700 | [diff] [blame] | 502 | JEMALLOC_VALGRIND_MAKE_MEM_UNDEFINED((void *)((uintptr_t)chunk + |
Jason Evans | dda90f5 | 2013-10-19 23:48:40 -0700 | [diff] [blame] | 503 | (run_ind << LG_PAGE)), (need_pages << LG_PAGE)); |
Jason Evans | e476f8a | 2010-01-16 09:53:50 -0800 | [diff] [blame] | 504 | } |
Jason Evans | aa5113b | 2014-01-14 16:23:03 -0800 | [diff] [blame] | 505 | |
| 506 | /* |
| 507 | * Set the last element first, in case the run only contains one page |
| 508 | * (i.e. both statements set the same element). |
| 509 | */ |
Jason Evans | 1f27abc | 2015-08-11 12:42:33 -0700 | [diff] [blame] | 510 | flag_unzeroed_mask = (flag_dirty | flag_decommitted) == 0 ? |
| 511 | CHUNK_MAP_UNZEROED : 0; |
| 512 | arena_mapbits_large_set(chunk, run_ind+need_pages-1, 0, flag_dirty | |
| 513 | (flag_unzeroed_mask & arena_mapbits_unzeroed_get(chunk, |
| 514 | run_ind+need_pages-1))); |
| 515 | arena_mapbits_large_set(chunk, run_ind, size, flag_dirty | |
| 516 | (flag_unzeroed_mask & arena_mapbits_unzeroed_get(chunk, run_ind))); |
Jason Evans | 8fadb1a | 2015-08-04 10:49:46 -0700 | [diff] [blame] | 517 | return (false); |
Jason Evans | e476f8a | 2010-01-16 09:53:50 -0800 | [diff] [blame] | 518 | } |
| 519 | |
Jason Evans | 8fadb1a | 2015-08-04 10:49:46 -0700 | [diff] [blame] | 520 | static bool |
Jason Evans | aa5113b | 2014-01-14 16:23:03 -0800 | [diff] [blame] | 521 | arena_run_split_large(arena_t *arena, arena_run_t *run, size_t size, bool zero) |
Jason Evans | c368f8c | 2013-10-29 18:17:42 -0700 | [diff] [blame] | 522 | { |
| 523 | |
Jason Evans | 8fadb1a | 2015-08-04 10:49:46 -0700 | [diff] [blame] | 524 | return (arena_run_split_large_helper(arena, run, size, true, zero)); |
Jason Evans | c368f8c | 2013-10-29 18:17:42 -0700 | [diff] [blame] | 525 | } |
| 526 | |
Jason Evans | 8fadb1a | 2015-08-04 10:49:46 -0700 | [diff] [blame] | 527 | static bool |
Jason Evans | aa5113b | 2014-01-14 16:23:03 -0800 | [diff] [blame] | 528 | arena_run_init_large(arena_t *arena, arena_run_t *run, size_t size, bool zero) |
Jason Evans | c368f8c | 2013-10-29 18:17:42 -0700 | [diff] [blame] | 529 | { |
| 530 | |
Jason Evans | 8fadb1a | 2015-08-04 10:49:46 -0700 | [diff] [blame] | 531 | return (arena_run_split_large_helper(arena, run, size, false, zero)); |
Jason Evans | aa5113b | 2014-01-14 16:23:03 -0800 | [diff] [blame] | 532 | } |
| 533 | |
Jason Evans | 8fadb1a | 2015-08-04 10:49:46 -0700 | [diff] [blame] | 534 | static bool |
Jason Evans | aa5113b | 2014-01-14 16:23:03 -0800 | [diff] [blame] | 535 | arena_run_split_small(arena_t *arena, arena_run_t *run, size_t size, |
Jason Evans | d01fd19 | 2015-08-19 15:21:32 -0700 | [diff] [blame] | 536 | szind_t binind) |
Jason Evans | aa5113b | 2014-01-14 16:23:03 -0800 | [diff] [blame] | 537 | { |
| 538 | arena_chunk_t *chunk; |
Jason Evans | 0c5dd03 | 2014-09-29 01:31:39 -0700 | [diff] [blame] | 539 | arena_chunk_map_misc_t *miscelm; |
Jason Evans | 8fadb1a | 2015-08-04 10:49:46 -0700 | [diff] [blame] | 540 | size_t flag_dirty, flag_decommitted, run_ind, need_pages, i; |
Jason Evans | aa5113b | 2014-01-14 16:23:03 -0800 | [diff] [blame] | 541 | |
| 542 | assert(binind != BININD_INVALID); |
| 543 | |
| 544 | chunk = (arena_chunk_t *)CHUNK_ADDR2BASE(run); |
Jason Evans | 0c5dd03 | 2014-09-29 01:31:39 -0700 | [diff] [blame] | 545 | miscelm = arena_run_to_miscelm(run); |
| 546 | run_ind = arena_miscelm_to_pageind(miscelm); |
Jason Evans | aa5113b | 2014-01-14 16:23:03 -0800 | [diff] [blame] | 547 | flag_dirty = arena_mapbits_dirty_get(chunk, run_ind); |
Jason Evans | 8fadb1a | 2015-08-04 10:49:46 -0700 | [diff] [blame] | 548 | flag_decommitted = arena_mapbits_decommitted_get(chunk, run_ind); |
Jason Evans | aa5113b | 2014-01-14 16:23:03 -0800 | [diff] [blame] | 549 | need_pages = (size >> LG_PAGE); |
| 550 | assert(need_pages > 0); |
| 551 | |
Jason Evans | 8fadb1a | 2015-08-04 10:49:46 -0700 | [diff] [blame] | 552 | if (flag_decommitted != 0 && arena->chunk_hooks.commit(chunk, chunksize, |
| 553 | run_ind << LG_PAGE, size, arena->ind)) |
| 554 | return (true); |
| 555 | |
| 556 | arena_run_split_remove(arena, chunk, run_ind, flag_dirty, |
| 557 | flag_decommitted, need_pages); |
Jason Evans | aa5113b | 2014-01-14 16:23:03 -0800 | [diff] [blame] | 558 | |
Jason Evans | 381c23d | 2014-10-10 23:01:03 -0700 | [diff] [blame] | 559 | for (i = 0; i < need_pages; i++) { |
Jason Evans | 1f27abc | 2015-08-11 12:42:33 -0700 | [diff] [blame] | 560 | size_t flag_unzeroed = arena_mapbits_unzeroed_get(chunk, |
| 561 | run_ind+i); |
| 562 | arena_mapbits_small_set(chunk, run_ind+i, i, binind, |
| 563 | flag_unzeroed); |
| 564 | if (config_debug && flag_dirty == 0 && flag_unzeroed == 0) |
Jason Evans | aa5113b | 2014-01-14 16:23:03 -0800 | [diff] [blame] | 565 | arena_run_page_validate_zeroed(chunk, run_ind+i); |
| 566 | } |
Jason Evans | bd87b01 | 2014-04-15 16:35:08 -0700 | [diff] [blame] | 567 | JEMALLOC_VALGRIND_MAKE_MEM_UNDEFINED((void *)((uintptr_t)chunk + |
Jason Evans | aa5113b | 2014-01-14 16:23:03 -0800 | [diff] [blame] | 568 | (run_ind << LG_PAGE)), (need_pages << LG_PAGE)); |
Jason Evans | 8fadb1a | 2015-08-04 10:49:46 -0700 | [diff] [blame] | 569 | return (false); |
Jason Evans | aa5113b | 2014-01-14 16:23:03 -0800 | [diff] [blame] | 570 | } |
| 571 | |
| 572 | static arena_chunk_t * |
| 573 | arena_chunk_init_spare(arena_t *arena) |
| 574 | { |
| 575 | arena_chunk_t *chunk; |
| 576 | |
| 577 | assert(arena->spare != NULL); |
| 578 | |
| 579 | chunk = arena->spare; |
| 580 | arena->spare = NULL; |
| 581 | |
| 582 | assert(arena_mapbits_allocated_get(chunk, map_bias) == 0); |
| 583 | assert(arena_mapbits_allocated_get(chunk, chunk_npages-1) == 0); |
| 584 | assert(arena_mapbits_unallocated_size_get(chunk, map_bias) == |
Jason Evans | 155bfa7 | 2014-10-05 17:54:10 -0700 | [diff] [blame] | 585 | arena_maxrun); |
Jason Evans | aa5113b | 2014-01-14 16:23:03 -0800 | [diff] [blame] | 586 | assert(arena_mapbits_unallocated_size_get(chunk, chunk_npages-1) == |
Jason Evans | 155bfa7 | 2014-10-05 17:54:10 -0700 | [diff] [blame] | 587 | arena_maxrun); |
Jason Evans | aa5113b | 2014-01-14 16:23:03 -0800 | [diff] [blame] | 588 | assert(arena_mapbits_dirty_get(chunk, map_bias) == |
| 589 | arena_mapbits_dirty_get(chunk, chunk_npages-1)); |
| 590 | |
| 591 | return (chunk); |
| 592 | } |
| 593 | |
Jason Evans | 99bd94f | 2015-02-18 16:40:53 -0800 | [diff] [blame] | 594 | static bool |
Jason Evans | b2c0d63 | 2016-04-13 23:36:15 -0700 | [diff] [blame] | 595 | arena_chunk_register(tsd_t *tsd, arena_t *arena, arena_chunk_t *chunk, |
| 596 | bool zero) |
Jason Evans | 99bd94f | 2015-02-18 16:40:53 -0800 | [diff] [blame] | 597 | { |
| 598 | |
Jason Evans | 8fadb1a | 2015-08-04 10:49:46 -0700 | [diff] [blame] | 599 | /* |
| 600 | * The extent node notion of "committed" doesn't directly apply to |
Jason Evans | de249c8 | 2015-08-09 16:47:27 -0700 | [diff] [blame] | 601 | * arena chunks. Arbitrarily mark them as committed. The commit state |
| 602 | * of runs is tracked individually, and upon chunk deallocation the |
| 603 | * entire chunk is in a consistent commit state. |
Jason Evans | 8fadb1a | 2015-08-04 10:49:46 -0700 | [diff] [blame] | 604 | */ |
| 605 | extent_node_init(&chunk->node, arena, chunk, chunksize, zero, true); |
Jason Evans | 99bd94f | 2015-02-18 16:40:53 -0800 | [diff] [blame] | 606 | extent_node_achunk_set(&chunk->node, true); |
Jason Evans | b2c0d63 | 2016-04-13 23:36:15 -0700 | [diff] [blame] | 607 | return (chunk_register(tsd, chunk, &chunk->node)); |
Jason Evans | 99bd94f | 2015-02-18 16:40:53 -0800 | [diff] [blame] | 608 | } |
| 609 | |
| 610 | static arena_chunk_t * |
Jason Evans | b2c0d63 | 2016-04-13 23:36:15 -0700 | [diff] [blame] | 611 | arena_chunk_alloc_internal_hard(tsd_t *tsd, arena_t *arena, |
| 612 | chunk_hooks_t *chunk_hooks, bool *zero, bool *commit) |
Jason Evans | 99bd94f | 2015-02-18 16:40:53 -0800 | [diff] [blame] | 613 | { |
| 614 | arena_chunk_t *chunk; |
Jason Evans | 99bd94f | 2015-02-18 16:40:53 -0800 | [diff] [blame] | 615 | |
Jason Evans | b2c0d63 | 2016-04-13 23:36:15 -0700 | [diff] [blame] | 616 | malloc_mutex_unlock(tsd, &arena->lock); |
Jason Evans | b49a334 | 2015-07-28 11:28:19 -0400 | [diff] [blame] | 617 | |
Jason Evans | b2c0d63 | 2016-04-13 23:36:15 -0700 | [diff] [blame] | 618 | chunk = (arena_chunk_t *)chunk_alloc_wrapper(tsd, arena, chunk_hooks, |
| 619 | NULL, chunksize, chunksize, zero, commit); |
Jason Evans | 8fadb1a | 2015-08-04 10:49:46 -0700 | [diff] [blame] | 620 | if (chunk != NULL && !*commit) { |
| 621 | /* Commit header. */ |
| 622 | if (chunk_hooks->commit(chunk, chunksize, 0, map_bias << |
| 623 | LG_PAGE, arena->ind)) { |
Jason Evans | b2c0d63 | 2016-04-13 23:36:15 -0700 | [diff] [blame] | 624 | chunk_dalloc_wrapper(tsd, arena, chunk_hooks, |
| 625 | (void *)chunk, chunksize, *zero, *commit); |
Jason Evans | 8fadb1a | 2015-08-04 10:49:46 -0700 | [diff] [blame] | 626 | chunk = NULL; |
| 627 | } |
| 628 | } |
Jason Evans | b2c0d63 | 2016-04-13 23:36:15 -0700 | [diff] [blame] | 629 | if (chunk != NULL && arena_chunk_register(tsd, arena, chunk, *zero)) { |
Jason Evans | 8fadb1a | 2015-08-04 10:49:46 -0700 | [diff] [blame] | 630 | if (!*commit) { |
| 631 | /* Undo commit of header. */ |
| 632 | chunk_hooks->decommit(chunk, chunksize, 0, map_bias << |
| 633 | LG_PAGE, arena->ind); |
| 634 | } |
Jason Evans | b2c0d63 | 2016-04-13 23:36:15 -0700 | [diff] [blame] | 635 | chunk_dalloc_wrapper(tsd, arena, chunk_hooks, (void *)chunk, |
Jason Evans | ce7c0f9 | 2016-03-30 18:36:04 -0700 | [diff] [blame] | 636 | chunksize, *zero, *commit); |
Jason Evans | 99bd94f | 2015-02-18 16:40:53 -0800 | [diff] [blame] | 637 | chunk = NULL; |
| 638 | } |
Jason Evans | 99bd94f | 2015-02-18 16:40:53 -0800 | [diff] [blame] | 639 | |
Jason Evans | b2c0d63 | 2016-04-13 23:36:15 -0700 | [diff] [blame] | 640 | malloc_mutex_lock(tsd, &arena->lock); |
Jason Evans | 99bd94f | 2015-02-18 16:40:53 -0800 | [diff] [blame] | 641 | return (chunk); |
| 642 | } |
| 643 | |
Jason Evans | aa5113b | 2014-01-14 16:23:03 -0800 | [diff] [blame] | 644 | static arena_chunk_t * |
Jason Evans | b2c0d63 | 2016-04-13 23:36:15 -0700 | [diff] [blame] | 645 | arena_chunk_alloc_internal(tsd_t *tsd, arena_t *arena, bool *zero, bool *commit) |
Jason Evans | e2deab7 | 2014-05-15 22:22:27 -0700 | [diff] [blame] | 646 | { |
| 647 | arena_chunk_t *chunk; |
Jason Evans | b49a334 | 2015-07-28 11:28:19 -0400 | [diff] [blame] | 648 | chunk_hooks_t chunk_hooks = CHUNK_HOOKS_INITIALIZER; |
Jason Evans | e2deab7 | 2014-05-15 22:22:27 -0700 | [diff] [blame] | 649 | |
Jason Evans | b2c0d63 | 2016-04-13 23:36:15 -0700 | [diff] [blame] | 650 | chunk = chunk_alloc_cache(tsd, arena, &chunk_hooks, NULL, chunksize, |
Jason Evans | b49a334 | 2015-07-28 11:28:19 -0400 | [diff] [blame] | 651 | chunksize, zero, true); |
Jason Evans | 8fadb1a | 2015-08-04 10:49:46 -0700 | [diff] [blame] | 652 | if (chunk != NULL) { |
Jason Evans | b2c0d63 | 2016-04-13 23:36:15 -0700 | [diff] [blame] | 653 | if (arena_chunk_register(tsd, arena, chunk, *zero)) { |
| 654 | chunk_dalloc_cache(tsd, arena, &chunk_hooks, chunk, |
Jason Evans | de249c8 | 2015-08-09 16:47:27 -0700 | [diff] [blame] | 655 | chunksize, true); |
Jason Evans | 8fadb1a | 2015-08-04 10:49:46 -0700 | [diff] [blame] | 656 | return (NULL); |
| 657 | } |
| 658 | *commit = true; |
Jason Evans | b49a334 | 2015-07-28 11:28:19 -0400 | [diff] [blame] | 659 | } |
| 660 | if (chunk == NULL) { |
Jason Evans | b2c0d63 | 2016-04-13 23:36:15 -0700 | [diff] [blame] | 661 | chunk = arena_chunk_alloc_internal_hard(tsd, arena, |
| 662 | &chunk_hooks, zero, commit); |
Jason Evans | b49a334 | 2015-07-28 11:28:19 -0400 | [diff] [blame] | 663 | } |
Jason Evans | 99bd94f | 2015-02-18 16:40:53 -0800 | [diff] [blame] | 664 | |
Jason Evans | 4581b97 | 2014-11-27 17:22:36 -0200 | [diff] [blame] | 665 | if (config_stats && chunk != NULL) { |
Jason Evans | e2deab7 | 2014-05-15 22:22:27 -0700 | [diff] [blame] | 666 | arena->stats.mapped += chunksize; |
Jason Evans | 4581b97 | 2014-11-27 17:22:36 -0200 | [diff] [blame] | 667 | arena->stats.metadata_mapped += (map_bias << LG_PAGE); |
| 668 | } |
Jason Evans | e2deab7 | 2014-05-15 22:22:27 -0700 | [diff] [blame] | 669 | |
| 670 | return (chunk); |
| 671 | } |
| 672 | |
Jason Evans | e2deab7 | 2014-05-15 22:22:27 -0700 | [diff] [blame] | 673 | static arena_chunk_t * |
Jason Evans | b2c0d63 | 2016-04-13 23:36:15 -0700 | [diff] [blame] | 674 | arena_chunk_init_hard(tsd_t *tsd, arena_t *arena) |
Jason Evans | aa5113b | 2014-01-14 16:23:03 -0800 | [diff] [blame] | 675 | { |
| 676 | arena_chunk_t *chunk; |
Jason Evans | 8fadb1a | 2015-08-04 10:49:46 -0700 | [diff] [blame] | 677 | bool zero, commit; |
Jason Evans | 45186f0 | 2015-08-10 23:03:34 -0700 | [diff] [blame] | 678 | size_t flag_unzeroed, flag_decommitted, i; |
Jason Evans | aa5113b | 2014-01-14 16:23:03 -0800 | [diff] [blame] | 679 | |
| 680 | assert(arena->spare == NULL); |
| 681 | |
| 682 | zero = false; |
Jason Evans | 8fadb1a | 2015-08-04 10:49:46 -0700 | [diff] [blame] | 683 | commit = false; |
Jason Evans | b2c0d63 | 2016-04-13 23:36:15 -0700 | [diff] [blame] | 684 | chunk = arena_chunk_alloc_internal(tsd, arena, &zero, &commit); |
Jason Evans | aa5113b | 2014-01-14 16:23:03 -0800 | [diff] [blame] | 685 | if (chunk == NULL) |
| 686 | return (NULL); |
Jason Evans | aa5113b | 2014-01-14 16:23:03 -0800 | [diff] [blame] | 687 | |
Jason Evans | aa5113b | 2014-01-14 16:23:03 -0800 | [diff] [blame] | 688 | /* |
Jason Evans | aa5113b | 2014-01-14 16:23:03 -0800 | [diff] [blame] | 689 | * Initialize the map to contain one maximal free untouched run. Mark |
Jason Evans | f86bc08 | 2016-03-31 11:19:46 -0700 | [diff] [blame] | 690 | * the pages as zeroed if arena_chunk_alloc_internal() returned a zeroed |
| 691 | * or decommitted chunk. |
Jason Evans | aa5113b | 2014-01-14 16:23:03 -0800 | [diff] [blame] | 692 | */ |
Jason Evans | 45186f0 | 2015-08-10 23:03:34 -0700 | [diff] [blame] | 693 | flag_unzeroed = (zero || !commit) ? 0 : CHUNK_MAP_UNZEROED; |
| 694 | flag_decommitted = commit ? 0 : CHUNK_MAP_DECOMMITTED; |
| 695 | arena_mapbits_unallocated_set(chunk, map_bias, arena_maxrun, |
| 696 | flag_unzeroed | flag_decommitted); |
Jason Evans | aa5113b | 2014-01-14 16:23:03 -0800 | [diff] [blame] | 697 | /* |
| 698 | * There is no need to initialize the internal page map entries unless |
| 699 | * the chunk is not zeroed. |
| 700 | */ |
Jason Evans | 551ebc4 | 2014-10-03 10:16:09 -0700 | [diff] [blame] | 701 | if (!zero) { |
Jason Evans | bd87b01 | 2014-04-15 16:35:08 -0700 | [diff] [blame] | 702 | JEMALLOC_VALGRIND_MAKE_MEM_UNDEFINED( |
Jason Evans | 61a6dfc | 2016-03-23 16:04:38 -0700 | [diff] [blame] | 703 | (void *)arena_bitselm_get_const(chunk, map_bias+1), |
| 704 | (size_t)((uintptr_t)arena_bitselm_get_const(chunk, |
| 705 | chunk_npages-1) - |
| 706 | (uintptr_t)arena_bitselm_get_const(chunk, map_bias+1))); |
Jason Evans | aa5113b | 2014-01-14 16:23:03 -0800 | [diff] [blame] | 707 | for (i = map_bias+1; i < chunk_npages-1; i++) |
Jason Evans | 45186f0 | 2015-08-10 23:03:34 -0700 | [diff] [blame] | 708 | arena_mapbits_internal_set(chunk, i, flag_unzeroed); |
Jason Evans | aa5113b | 2014-01-14 16:23:03 -0800 | [diff] [blame] | 709 | } else { |
Qinfan Wu | ff6a31d | 2014-08-29 13:34:40 -0700 | [diff] [blame] | 710 | JEMALLOC_VALGRIND_MAKE_MEM_DEFINED((void |
Jason Evans | 61a6dfc | 2016-03-23 16:04:38 -0700 | [diff] [blame] | 711 | *)arena_bitselm_get_const(chunk, map_bias+1), |
| 712 | (size_t)((uintptr_t)arena_bitselm_get_const(chunk, |
| 713 | chunk_npages-1) - |
| 714 | (uintptr_t)arena_bitselm_get_const(chunk, map_bias+1))); |
Jason Evans | aa5113b | 2014-01-14 16:23:03 -0800 | [diff] [blame] | 715 | if (config_debug) { |
| 716 | for (i = map_bias+1; i < chunk_npages-1; i++) { |
| 717 | assert(arena_mapbits_unzeroed_get(chunk, i) == |
Jason Evans | 45186f0 | 2015-08-10 23:03:34 -0700 | [diff] [blame] | 718 | flag_unzeroed); |
Jason Evans | aa5113b | 2014-01-14 16:23:03 -0800 | [diff] [blame] | 719 | } |
| 720 | } |
| 721 | } |
Jason Evans | 155bfa7 | 2014-10-05 17:54:10 -0700 | [diff] [blame] | 722 | arena_mapbits_unallocated_set(chunk, chunk_npages-1, arena_maxrun, |
Jason Evans | 45186f0 | 2015-08-10 23:03:34 -0700 | [diff] [blame] | 723 | flag_unzeroed); |
Jason Evans | aa5113b | 2014-01-14 16:23:03 -0800 | [diff] [blame] | 724 | |
| 725 | return (chunk); |
Jason Evans | c368f8c | 2013-10-29 18:17:42 -0700 | [diff] [blame] | 726 | } |
| 727 | |
Jason Evans | e476f8a | 2010-01-16 09:53:50 -0800 | [diff] [blame] | 728 | static arena_chunk_t * |
Jason Evans | b2c0d63 | 2016-04-13 23:36:15 -0700 | [diff] [blame] | 729 | arena_chunk_alloc(tsd_t *tsd, arena_t *arena) |
Jason Evans | e476f8a | 2010-01-16 09:53:50 -0800 | [diff] [blame] | 730 | { |
| 731 | arena_chunk_t *chunk; |
Jason Evans | e476f8a | 2010-01-16 09:53:50 -0800 | [diff] [blame] | 732 | |
Jason Evans | aa5113b | 2014-01-14 16:23:03 -0800 | [diff] [blame] | 733 | if (arena->spare != NULL) |
| 734 | chunk = arena_chunk_init_spare(arena); |
Chris Pride | 20a8c78 | 2014-03-25 22:36:05 -0700 | [diff] [blame] | 735 | else { |
Jason Evans | b2c0d63 | 2016-04-13 23:36:15 -0700 | [diff] [blame] | 736 | chunk = arena_chunk_init_hard(tsd, arena); |
Chris Pride | 20a8c78 | 2014-03-25 22:36:05 -0700 | [diff] [blame] | 737 | if (chunk == NULL) |
| 738 | return (NULL); |
| 739 | } |
Jason Evans | e476f8a | 2010-01-16 09:53:50 -0800 | [diff] [blame] | 740 | |
Jason Evans | 19ff2ce | 2016-04-22 14:37:17 -0700 | [diff] [blame] | 741 | ql_elm_new(&chunk->node, ql_link); |
| 742 | ql_tail_insert(&arena->achunks, &chunk->node, ql_link); |
Qinfan Wu | 90737fc | 2014-07-21 19:39:20 -0700 | [diff] [blame] | 743 | arena_avail_insert(arena, chunk, map_bias, chunk_npages-map_bias); |
Jason Evans | e3d1306 | 2012-10-30 15:42:37 -0700 | [diff] [blame] | 744 | |
Jason Evans | e476f8a | 2010-01-16 09:53:50 -0800 | [diff] [blame] | 745 | return (chunk); |
| 746 | } |
| 747 | |
| 748 | static void |
Jason Evans | 19ff2ce | 2016-04-22 14:37:17 -0700 | [diff] [blame] | 749 | arena_chunk_discard(tsd_t *tsd, arena_t *arena, arena_chunk_t *chunk) |
| 750 | { |
| 751 | bool committed; |
| 752 | chunk_hooks_t chunk_hooks = CHUNK_HOOKS_INITIALIZER; |
| 753 | |
| 754 | chunk_deregister(chunk, &chunk->node); |
| 755 | |
| 756 | committed = (arena_mapbits_decommitted_get(chunk, map_bias) == 0); |
| 757 | if (!committed) { |
| 758 | /* |
| 759 | * Decommit the header. Mark the chunk as decommitted even if |
| 760 | * header decommit fails, since treating a partially committed |
| 761 | * chunk as committed has a high potential for causing later |
| 762 | * access of decommitted memory. |
| 763 | */ |
| 764 | chunk_hooks = chunk_hooks_get(tsd, arena); |
| 765 | chunk_hooks.decommit(chunk, chunksize, 0, map_bias << LG_PAGE, |
| 766 | arena->ind); |
| 767 | } |
| 768 | |
| 769 | chunk_dalloc_cache(tsd, arena, &chunk_hooks, (void *)chunk, chunksize, |
| 770 | committed); |
| 771 | |
| 772 | if (config_stats) { |
| 773 | arena->stats.mapped -= chunksize; |
| 774 | arena->stats.metadata_mapped -= (map_bias << LG_PAGE); |
| 775 | } |
| 776 | } |
| 777 | |
| 778 | static void |
| 779 | arena_spare_discard(tsd_t *tsd, arena_t *arena, arena_chunk_t *spare) |
| 780 | { |
| 781 | |
| 782 | assert(arena->spare != spare); |
| 783 | |
| 784 | if (arena_mapbits_dirty_get(spare, map_bias) != 0) { |
| 785 | arena_run_dirty_remove(arena, spare, map_bias, |
| 786 | chunk_npages-map_bias); |
| 787 | } |
| 788 | |
| 789 | arena_chunk_discard(tsd, arena, spare); |
| 790 | } |
| 791 | |
| 792 | static void |
Jason Evans | b2c0d63 | 2016-04-13 23:36:15 -0700 | [diff] [blame] | 793 | arena_chunk_dalloc(tsd_t *tsd, arena_t *arena, arena_chunk_t *chunk) |
Jason Evans | e476f8a | 2010-01-16 09:53:50 -0800 | [diff] [blame] | 794 | { |
Jason Evans | 19ff2ce | 2016-04-22 14:37:17 -0700 | [diff] [blame] | 795 | arena_chunk_t *spare; |
Qinfan Wu | 04d60a1 | 2014-07-18 14:21:17 -0700 | [diff] [blame] | 796 | |
Jason Evans | 30fe12b | 2012-05-10 17:09:17 -0700 | [diff] [blame] | 797 | assert(arena_mapbits_allocated_get(chunk, map_bias) == 0); |
| 798 | assert(arena_mapbits_allocated_get(chunk, chunk_npages-1) == 0); |
| 799 | assert(arena_mapbits_unallocated_size_get(chunk, map_bias) == |
Jason Evans | 155bfa7 | 2014-10-05 17:54:10 -0700 | [diff] [blame] | 800 | arena_maxrun); |
Jason Evans | 30fe12b | 2012-05-10 17:09:17 -0700 | [diff] [blame] | 801 | assert(arena_mapbits_unallocated_size_get(chunk, chunk_npages-1) == |
Jason Evans | 155bfa7 | 2014-10-05 17:54:10 -0700 | [diff] [blame] | 802 | arena_maxrun); |
Jason Evans | 30fe12b | 2012-05-10 17:09:17 -0700 | [diff] [blame] | 803 | assert(arena_mapbits_dirty_get(chunk, map_bias) == |
| 804 | arena_mapbits_dirty_get(chunk, chunk_npages-1)); |
Jason Evans | de249c8 | 2015-08-09 16:47:27 -0700 | [diff] [blame] | 805 | assert(arena_mapbits_decommitted_get(chunk, map_bias) == |
| 806 | arena_mapbits_decommitted_get(chunk, chunk_npages-1)); |
Jason Evans | 30fe12b | 2012-05-10 17:09:17 -0700 | [diff] [blame] | 807 | |
Dave Watson | 3417a30 | 2016-02-23 12:06:21 -0800 | [diff] [blame] | 808 | /* Remove run from runs_avail, so that the arena does not use it. */ |
Qinfan Wu | 90737fc | 2014-07-21 19:39:20 -0700 | [diff] [blame] | 809 | arena_avail_remove(arena, chunk, map_bias, chunk_npages-map_bias); |
Jason Evans | e476f8a | 2010-01-16 09:53:50 -0800 | [diff] [blame] | 810 | |
Jason Evans | 19ff2ce | 2016-04-22 14:37:17 -0700 | [diff] [blame] | 811 | ql_remove(&arena->achunks, &chunk->node, ql_link); |
| 812 | spare = arena->spare; |
| 813 | arena->spare = chunk; |
| 814 | if (spare != NULL) |
| 815 | arena_spare_discard(tsd, arena, spare); |
Jason Evans | e476f8a | 2010-01-16 09:53:50 -0800 | [diff] [blame] | 816 | } |
| 817 | |
Jason Evans | 9b41ac9 | 2014-10-14 22:20:00 -0700 | [diff] [blame] | 818 | static void |
| 819 | arena_huge_malloc_stats_update(arena_t *arena, size_t usize) |
| 820 | { |
Jason Evans | d01fd19 | 2015-08-19 15:21:32 -0700 | [diff] [blame] | 821 | szind_t index = size2index(usize) - nlclasses - NBINS; |
Jason Evans | 9b41ac9 | 2014-10-14 22:20:00 -0700 | [diff] [blame] | 822 | |
| 823 | cassert(config_stats); |
| 824 | |
| 825 | arena->stats.nmalloc_huge++; |
| 826 | arena->stats.allocated_huge += usize; |
| 827 | arena->stats.hstats[index].nmalloc++; |
| 828 | arena->stats.hstats[index].curhchunks++; |
| 829 | } |
| 830 | |
| 831 | static void |
| 832 | arena_huge_malloc_stats_update_undo(arena_t *arena, size_t usize) |
| 833 | { |
Jason Evans | d01fd19 | 2015-08-19 15:21:32 -0700 | [diff] [blame] | 834 | szind_t index = size2index(usize) - nlclasses - NBINS; |
Jason Evans | 9b41ac9 | 2014-10-14 22:20:00 -0700 | [diff] [blame] | 835 | |
| 836 | cassert(config_stats); |
| 837 | |
| 838 | arena->stats.nmalloc_huge--; |
| 839 | arena->stats.allocated_huge -= usize; |
| 840 | arena->stats.hstats[index].nmalloc--; |
| 841 | arena->stats.hstats[index].curhchunks--; |
| 842 | } |
| 843 | |
| 844 | static void |
| 845 | arena_huge_dalloc_stats_update(arena_t *arena, size_t usize) |
| 846 | { |
Jason Evans | d01fd19 | 2015-08-19 15:21:32 -0700 | [diff] [blame] | 847 | szind_t index = size2index(usize) - nlclasses - NBINS; |
Jason Evans | 9b41ac9 | 2014-10-14 22:20:00 -0700 | [diff] [blame] | 848 | |
| 849 | cassert(config_stats); |
| 850 | |
| 851 | arena->stats.ndalloc_huge++; |
| 852 | arena->stats.allocated_huge -= usize; |
| 853 | arena->stats.hstats[index].ndalloc++; |
| 854 | arena->stats.hstats[index].curhchunks--; |
| 855 | } |
| 856 | |
| 857 | static void |
Jason Evans | 7e67495 | 2016-04-25 13:26:54 -0700 | [diff] [blame] | 858 | arena_huge_reset_stats_cancel(arena_t *arena, size_t usize) |
| 859 | { |
| 860 | szind_t index = size2index(usize) - nlclasses - NBINS; |
| 861 | |
| 862 | cassert(config_stats); |
| 863 | |
| 864 | arena->stats.ndalloc_huge++; |
| 865 | arena->stats.hstats[index].ndalloc--; |
| 866 | } |
| 867 | |
| 868 | static void |
Jason Evans | 9b41ac9 | 2014-10-14 22:20:00 -0700 | [diff] [blame] | 869 | arena_huge_dalloc_stats_update_undo(arena_t *arena, size_t usize) |
| 870 | { |
Jason Evans | d01fd19 | 2015-08-19 15:21:32 -0700 | [diff] [blame] | 871 | szind_t index = size2index(usize) - nlclasses - NBINS; |
Jason Evans | 9b41ac9 | 2014-10-14 22:20:00 -0700 | [diff] [blame] | 872 | |
| 873 | cassert(config_stats); |
| 874 | |
| 875 | arena->stats.ndalloc_huge--; |
| 876 | arena->stats.allocated_huge += usize; |
| 877 | arena->stats.hstats[index].ndalloc--; |
| 878 | arena->stats.hstats[index].curhchunks++; |
| 879 | } |
| 880 | |
| 881 | static void |
| 882 | arena_huge_ralloc_stats_update(arena_t *arena, size_t oldsize, size_t usize) |
| 883 | { |
| 884 | |
| 885 | arena_huge_dalloc_stats_update(arena, oldsize); |
| 886 | arena_huge_malloc_stats_update(arena, usize); |
| 887 | } |
| 888 | |
| 889 | static void |
| 890 | arena_huge_ralloc_stats_update_undo(arena_t *arena, size_t oldsize, |
| 891 | size_t usize) |
| 892 | { |
| 893 | |
| 894 | arena_huge_dalloc_stats_update_undo(arena, oldsize); |
| 895 | arena_huge_malloc_stats_update_undo(arena, usize); |
| 896 | } |
| 897 | |
Jason Evans | cbf3a6d | 2015-02-11 12:24:27 -0800 | [diff] [blame] | 898 | extent_node_t * |
Jason Evans | b2c0d63 | 2016-04-13 23:36:15 -0700 | [diff] [blame] | 899 | arena_node_alloc(tsd_t *tsd, arena_t *arena) |
Jason Evans | cbf3a6d | 2015-02-11 12:24:27 -0800 | [diff] [blame] | 900 | { |
| 901 | extent_node_t *node; |
| 902 | |
Jason Evans | b2c0d63 | 2016-04-13 23:36:15 -0700 | [diff] [blame] | 903 | malloc_mutex_lock(tsd, &arena->node_cache_mtx); |
Jason Evans | 2195ba4 | 2015-02-15 16:43:52 -0800 | [diff] [blame] | 904 | node = ql_last(&arena->node_cache, ql_link); |
Jason Evans | cbf3a6d | 2015-02-11 12:24:27 -0800 | [diff] [blame] | 905 | if (node == NULL) { |
Jason Evans | b2c0d63 | 2016-04-13 23:36:15 -0700 | [diff] [blame] | 906 | malloc_mutex_unlock(tsd, &arena->node_cache_mtx); |
| 907 | return (base_alloc(tsd, sizeof(extent_node_t))); |
Jason Evans | cbf3a6d | 2015-02-11 12:24:27 -0800 | [diff] [blame] | 908 | } |
Jason Evans | 2195ba4 | 2015-02-15 16:43:52 -0800 | [diff] [blame] | 909 | ql_tail_remove(&arena->node_cache, extent_node_t, ql_link); |
Jason Evans | b2c0d63 | 2016-04-13 23:36:15 -0700 | [diff] [blame] | 910 | malloc_mutex_unlock(tsd, &arena->node_cache_mtx); |
Jason Evans | cbf3a6d | 2015-02-11 12:24:27 -0800 | [diff] [blame] | 911 | return (node); |
| 912 | } |
| 913 | |
| 914 | void |
Jason Evans | b2c0d63 | 2016-04-13 23:36:15 -0700 | [diff] [blame] | 915 | arena_node_dalloc(tsd_t *tsd, arena_t *arena, extent_node_t *node) |
Jason Evans | cbf3a6d | 2015-02-11 12:24:27 -0800 | [diff] [blame] | 916 | { |
| 917 | |
Jason Evans | b2c0d63 | 2016-04-13 23:36:15 -0700 | [diff] [blame] | 918 | malloc_mutex_lock(tsd, &arena->node_cache_mtx); |
Jason Evans | 2195ba4 | 2015-02-15 16:43:52 -0800 | [diff] [blame] | 919 | ql_elm_new(node, ql_link); |
| 920 | ql_tail_insert(&arena->node_cache, node, ql_link); |
Jason Evans | b2c0d63 | 2016-04-13 23:36:15 -0700 | [diff] [blame] | 921 | malloc_mutex_unlock(tsd, &arena->node_cache_mtx); |
Jason Evans | cbf3a6d | 2015-02-11 12:24:27 -0800 | [diff] [blame] | 922 | } |
| 923 | |
Jason Evans | 99bd94f | 2015-02-18 16:40:53 -0800 | [diff] [blame] | 924 | static void * |
Jason Evans | b2c0d63 | 2016-04-13 23:36:15 -0700 | [diff] [blame] | 925 | arena_chunk_alloc_huge_hard(tsd_t *tsd, arena_t *arena, |
| 926 | chunk_hooks_t *chunk_hooks, size_t usize, size_t alignment, bool *zero, |
| 927 | size_t csize) |
Jason Evans | 9b41ac9 | 2014-10-14 22:20:00 -0700 | [diff] [blame] | 928 | { |
| 929 | void *ret; |
Jason Evans | 8fadb1a | 2015-08-04 10:49:46 -0700 | [diff] [blame] | 930 | bool commit = true; |
Jason Evans | 9b41ac9 | 2014-10-14 22:20:00 -0700 | [diff] [blame] | 931 | |
Jason Evans | b2c0d63 | 2016-04-13 23:36:15 -0700 | [diff] [blame] | 932 | ret = chunk_alloc_wrapper(tsd, arena, chunk_hooks, NULL, csize, |
| 933 | alignment, zero, &commit); |
Jason Evans | 9b41ac9 | 2014-10-14 22:20:00 -0700 | [diff] [blame] | 934 | if (ret == NULL) { |
| 935 | /* Revert optimistic stats updates. */ |
Jason Evans | b2c0d63 | 2016-04-13 23:36:15 -0700 | [diff] [blame] | 936 | malloc_mutex_lock(tsd, &arena->lock); |
Jason Evans | 9b41ac9 | 2014-10-14 22:20:00 -0700 | [diff] [blame] | 937 | if (config_stats) { |
| 938 | arena_huge_malloc_stats_update_undo(arena, usize); |
| 939 | arena->stats.mapped -= usize; |
| 940 | } |
Jason Evans | 40ee9aa | 2016-02-27 12:34:50 -0800 | [diff] [blame] | 941 | arena_nactive_sub(arena, usize >> LG_PAGE); |
Jason Evans | b2c0d63 | 2016-04-13 23:36:15 -0700 | [diff] [blame] | 942 | malloc_mutex_unlock(tsd, &arena->lock); |
Jason Evans | 9b41ac9 | 2014-10-14 22:20:00 -0700 | [diff] [blame] | 943 | } |
| 944 | |
Jason Evans | 99bd94f | 2015-02-18 16:40:53 -0800 | [diff] [blame] | 945 | return (ret); |
| 946 | } |
Jason Evans | 9b41ac9 | 2014-10-14 22:20:00 -0700 | [diff] [blame] | 947 | |
Jason Evans | 99bd94f | 2015-02-18 16:40:53 -0800 | [diff] [blame] | 948 | void * |
Jason Evans | b2c0d63 | 2016-04-13 23:36:15 -0700 | [diff] [blame] | 949 | arena_chunk_alloc_huge(tsd_t *tsd, arena_t *arena, size_t usize, |
| 950 | size_t alignment, bool *zero) |
Jason Evans | 99bd94f | 2015-02-18 16:40:53 -0800 | [diff] [blame] | 951 | { |
| 952 | void *ret; |
Jason Evans | b49a334 | 2015-07-28 11:28:19 -0400 | [diff] [blame] | 953 | chunk_hooks_t chunk_hooks = CHUNK_HOOKS_INITIALIZER; |
Jason Evans | 99bd94f | 2015-02-18 16:40:53 -0800 | [diff] [blame] | 954 | size_t csize = CHUNK_CEILING(usize); |
| 955 | |
Jason Evans | b2c0d63 | 2016-04-13 23:36:15 -0700 | [diff] [blame] | 956 | malloc_mutex_lock(tsd, &arena->lock); |
Jason Evans | 99bd94f | 2015-02-18 16:40:53 -0800 | [diff] [blame] | 957 | |
| 958 | /* Optimistically update stats. */ |
| 959 | if (config_stats) { |
| 960 | arena_huge_malloc_stats_update(arena, usize); |
| 961 | arena->stats.mapped += usize; |
| 962 | } |
Jason Evans | 40ee9aa | 2016-02-27 12:34:50 -0800 | [diff] [blame] | 963 | arena_nactive_add(arena, usize >> LG_PAGE); |
Jason Evans | 99bd94f | 2015-02-18 16:40:53 -0800 | [diff] [blame] | 964 | |
Jason Evans | b2c0d63 | 2016-04-13 23:36:15 -0700 | [diff] [blame] | 965 | ret = chunk_alloc_cache(tsd, arena, &chunk_hooks, NULL, csize, |
| 966 | alignment, zero, true); |
| 967 | malloc_mutex_unlock(tsd, &arena->lock); |
Jason Evans | 99bd94f | 2015-02-18 16:40:53 -0800 | [diff] [blame] | 968 | if (ret == NULL) { |
Jason Evans | b2c0d63 | 2016-04-13 23:36:15 -0700 | [diff] [blame] | 969 | ret = arena_chunk_alloc_huge_hard(tsd, arena, &chunk_hooks, |
| 970 | usize, alignment, zero, csize); |
Jason Evans | 99bd94f | 2015-02-18 16:40:53 -0800 | [diff] [blame] | 971 | } |
| 972 | |
Jason Evans | 9b41ac9 | 2014-10-14 22:20:00 -0700 | [diff] [blame] | 973 | return (ret); |
| 974 | } |
| 975 | |
| 976 | void |
Jason Evans | b2c0d63 | 2016-04-13 23:36:15 -0700 | [diff] [blame] | 977 | arena_chunk_dalloc_huge(tsd_t *tsd, arena_t *arena, void *chunk, size_t usize) |
Jason Evans | 9b41ac9 | 2014-10-14 22:20:00 -0700 | [diff] [blame] | 978 | { |
Jason Evans | b49a334 | 2015-07-28 11:28:19 -0400 | [diff] [blame] | 979 | chunk_hooks_t chunk_hooks = CHUNK_HOOKS_INITIALIZER; |
Jason Evans | 99bd94f | 2015-02-18 16:40:53 -0800 | [diff] [blame] | 980 | size_t csize; |
Jason Evans | 9b41ac9 | 2014-10-14 22:20:00 -0700 | [diff] [blame] | 981 | |
Jason Evans | 99bd94f | 2015-02-18 16:40:53 -0800 | [diff] [blame] | 982 | csize = CHUNK_CEILING(usize); |
Jason Evans | b2c0d63 | 2016-04-13 23:36:15 -0700 | [diff] [blame] | 983 | malloc_mutex_lock(tsd, &arena->lock); |
Jason Evans | 9b41ac9 | 2014-10-14 22:20:00 -0700 | [diff] [blame] | 984 | if (config_stats) { |
| 985 | arena_huge_dalloc_stats_update(arena, usize); |
| 986 | arena->stats.mapped -= usize; |
Jason Evans | 9b41ac9 | 2014-10-14 22:20:00 -0700 | [diff] [blame] | 987 | } |
Jason Evans | 40ee9aa | 2016-02-27 12:34:50 -0800 | [diff] [blame] | 988 | arena_nactive_sub(arena, usize >> LG_PAGE); |
Jason Evans | 99bd94f | 2015-02-18 16:40:53 -0800 | [diff] [blame] | 989 | |
Jason Evans | b2c0d63 | 2016-04-13 23:36:15 -0700 | [diff] [blame] | 990 | chunk_dalloc_cache(tsd, arena, &chunk_hooks, chunk, csize, true); |
| 991 | malloc_mutex_unlock(tsd, &arena->lock); |
Jason Evans | 9b41ac9 | 2014-10-14 22:20:00 -0700 | [diff] [blame] | 992 | } |
| 993 | |
| 994 | void |
Jason Evans | b2c0d63 | 2016-04-13 23:36:15 -0700 | [diff] [blame] | 995 | arena_chunk_ralloc_huge_similar(tsd_t *tsd, arena_t *arena, void *chunk, |
| 996 | size_t oldsize, size_t usize) |
Jason Evans | 9b41ac9 | 2014-10-14 22:20:00 -0700 | [diff] [blame] | 997 | { |
| 998 | |
| 999 | assert(CHUNK_CEILING(oldsize) == CHUNK_CEILING(usize)); |
| 1000 | assert(oldsize != usize); |
| 1001 | |
Jason Evans | b2c0d63 | 2016-04-13 23:36:15 -0700 | [diff] [blame] | 1002 | malloc_mutex_lock(tsd, &arena->lock); |
Jason Evans | 9b41ac9 | 2014-10-14 22:20:00 -0700 | [diff] [blame] | 1003 | if (config_stats) |
| 1004 | arena_huge_ralloc_stats_update(arena, oldsize, usize); |
Jason Evans | 40ee9aa | 2016-02-27 12:34:50 -0800 | [diff] [blame] | 1005 | if (oldsize < usize) |
| 1006 | arena_nactive_add(arena, (usize - oldsize) >> LG_PAGE); |
| 1007 | else |
| 1008 | arena_nactive_sub(arena, (oldsize - usize) >> LG_PAGE); |
Jason Evans | b2c0d63 | 2016-04-13 23:36:15 -0700 | [diff] [blame] | 1009 | malloc_mutex_unlock(tsd, &arena->lock); |
Jason Evans | 9b41ac9 | 2014-10-14 22:20:00 -0700 | [diff] [blame] | 1010 | } |
| 1011 | |
| 1012 | void |
Jason Evans | b2c0d63 | 2016-04-13 23:36:15 -0700 | [diff] [blame] | 1013 | arena_chunk_ralloc_huge_shrink(tsd_t *tsd, arena_t *arena, void *chunk, |
| 1014 | size_t oldsize, size_t usize) |
Jason Evans | 9b41ac9 | 2014-10-14 22:20:00 -0700 | [diff] [blame] | 1015 | { |
Jason Evans | 9b41ac9 | 2014-10-14 22:20:00 -0700 | [diff] [blame] | 1016 | size_t udiff = oldsize - usize; |
| 1017 | size_t cdiff = CHUNK_CEILING(oldsize) - CHUNK_CEILING(usize); |
| 1018 | |
Jason Evans | b2c0d63 | 2016-04-13 23:36:15 -0700 | [diff] [blame] | 1019 | malloc_mutex_lock(tsd, &arena->lock); |
Jason Evans | 9b41ac9 | 2014-10-14 22:20:00 -0700 | [diff] [blame] | 1020 | if (config_stats) { |
| 1021 | arena_huge_ralloc_stats_update(arena, oldsize, usize); |
Jason Evans | 40ee9aa | 2016-02-27 12:34:50 -0800 | [diff] [blame] | 1022 | if (cdiff != 0) |
Jason Evans | 9b41ac9 | 2014-10-14 22:20:00 -0700 | [diff] [blame] | 1023 | arena->stats.mapped -= cdiff; |
Jason Evans | 9b41ac9 | 2014-10-14 22:20:00 -0700 | [diff] [blame] | 1024 | } |
Jason Evans | 40ee9aa | 2016-02-27 12:34:50 -0800 | [diff] [blame] | 1025 | arena_nactive_sub(arena, udiff >> LG_PAGE); |
Jason Evans | 99bd94f | 2015-02-18 16:40:53 -0800 | [diff] [blame] | 1026 | |
Jason Evans | 2012d5a | 2014-11-17 09:54:49 -0800 | [diff] [blame] | 1027 | if (cdiff != 0) { |
Jason Evans | b49a334 | 2015-07-28 11:28:19 -0400 | [diff] [blame] | 1028 | chunk_hooks_t chunk_hooks = CHUNK_HOOKS_INITIALIZER; |
Jason Evans | 99bd94f | 2015-02-18 16:40:53 -0800 | [diff] [blame] | 1029 | void *nchunk = (void *)((uintptr_t)chunk + |
| 1030 | CHUNK_CEILING(usize)); |
| 1031 | |
Jason Evans | b2c0d63 | 2016-04-13 23:36:15 -0700 | [diff] [blame] | 1032 | chunk_dalloc_cache(tsd, arena, &chunk_hooks, nchunk, cdiff, |
| 1033 | true); |
Jason Evans | b49a334 | 2015-07-28 11:28:19 -0400 | [diff] [blame] | 1034 | } |
Jason Evans | b2c0d63 | 2016-04-13 23:36:15 -0700 | [diff] [blame] | 1035 | malloc_mutex_unlock(tsd, &arena->lock); |
Jason Evans | 99bd94f | 2015-02-18 16:40:53 -0800 | [diff] [blame] | 1036 | } |
| 1037 | |
Jason Evans | b49a334 | 2015-07-28 11:28:19 -0400 | [diff] [blame] | 1038 | static bool |
Jason Evans | b2c0d63 | 2016-04-13 23:36:15 -0700 | [diff] [blame] | 1039 | arena_chunk_ralloc_huge_expand_hard(tsd_t *tsd, arena_t *arena, |
| 1040 | chunk_hooks_t *chunk_hooks, void *chunk, size_t oldsize, size_t usize, |
| 1041 | bool *zero, void *nchunk, size_t udiff, size_t cdiff) |
Jason Evans | 99bd94f | 2015-02-18 16:40:53 -0800 | [diff] [blame] | 1042 | { |
| 1043 | bool err; |
Jason Evans | 8fadb1a | 2015-08-04 10:49:46 -0700 | [diff] [blame] | 1044 | bool commit = true; |
Jason Evans | 99bd94f | 2015-02-18 16:40:53 -0800 | [diff] [blame] | 1045 | |
Jason Evans | b2c0d63 | 2016-04-13 23:36:15 -0700 | [diff] [blame] | 1046 | err = (chunk_alloc_wrapper(tsd, arena, chunk_hooks, nchunk, cdiff, |
| 1047 | chunksize, zero, &commit) == NULL); |
Jason Evans | 99bd94f | 2015-02-18 16:40:53 -0800 | [diff] [blame] | 1048 | if (err) { |
| 1049 | /* Revert optimistic stats updates. */ |
Jason Evans | b2c0d63 | 2016-04-13 23:36:15 -0700 | [diff] [blame] | 1050 | malloc_mutex_lock(tsd, &arena->lock); |
Jason Evans | 99bd94f | 2015-02-18 16:40:53 -0800 | [diff] [blame] | 1051 | if (config_stats) { |
| 1052 | arena_huge_ralloc_stats_update_undo(arena, oldsize, |
| 1053 | usize); |
| 1054 | arena->stats.mapped -= cdiff; |
| 1055 | } |
Jason Evans | 40ee9aa | 2016-02-27 12:34:50 -0800 | [diff] [blame] | 1056 | arena_nactive_sub(arena, udiff >> LG_PAGE); |
Jason Evans | b2c0d63 | 2016-04-13 23:36:15 -0700 | [diff] [blame] | 1057 | malloc_mutex_unlock(tsd, &arena->lock); |
Jason Evans | b49a334 | 2015-07-28 11:28:19 -0400 | [diff] [blame] | 1058 | } else if (chunk_hooks->merge(chunk, CHUNK_CEILING(oldsize), nchunk, |
| 1059 | cdiff, true, arena->ind)) { |
Jason Evans | b2c0d63 | 2016-04-13 23:36:15 -0700 | [diff] [blame] | 1060 | chunk_dalloc_wrapper(tsd, arena, chunk_hooks, nchunk, cdiff, |
| 1061 | *zero, true); |
Jason Evans | b49a334 | 2015-07-28 11:28:19 -0400 | [diff] [blame] | 1062 | err = true; |
Jason Evans | 2012d5a | 2014-11-17 09:54:49 -0800 | [diff] [blame] | 1063 | } |
Jason Evans | 99bd94f | 2015-02-18 16:40:53 -0800 | [diff] [blame] | 1064 | return (err); |
Jason Evans | 9b41ac9 | 2014-10-14 22:20:00 -0700 | [diff] [blame] | 1065 | } |
| 1066 | |
| 1067 | bool |
Jason Evans | b2c0d63 | 2016-04-13 23:36:15 -0700 | [diff] [blame] | 1068 | arena_chunk_ralloc_huge_expand(tsd_t *tsd, arena_t *arena, void *chunk, |
| 1069 | size_t oldsize, size_t usize, bool *zero) |
Jason Evans | 9b41ac9 | 2014-10-14 22:20:00 -0700 | [diff] [blame] | 1070 | { |
Jason Evans | 99bd94f | 2015-02-18 16:40:53 -0800 | [diff] [blame] | 1071 | bool err; |
Jason Evans | b2c0d63 | 2016-04-13 23:36:15 -0700 | [diff] [blame] | 1072 | chunk_hooks_t chunk_hooks = chunk_hooks_get(tsd, arena); |
Jason Evans | 99bd94f | 2015-02-18 16:40:53 -0800 | [diff] [blame] | 1073 | void *nchunk = (void *)((uintptr_t)chunk + CHUNK_CEILING(oldsize)); |
Jason Evans | 9b41ac9 | 2014-10-14 22:20:00 -0700 | [diff] [blame] | 1074 | size_t udiff = usize - oldsize; |
| 1075 | size_t cdiff = CHUNK_CEILING(usize) - CHUNK_CEILING(oldsize); |
| 1076 | |
Jason Evans | b2c0d63 | 2016-04-13 23:36:15 -0700 | [diff] [blame] | 1077 | malloc_mutex_lock(tsd, &arena->lock); |
Jason Evans | 99bd94f | 2015-02-18 16:40:53 -0800 | [diff] [blame] | 1078 | |
| 1079 | /* Optimistically update stats. */ |
Jason Evans | 9b41ac9 | 2014-10-14 22:20:00 -0700 | [diff] [blame] | 1080 | if (config_stats) { |
Jason Evans | 9b41ac9 | 2014-10-14 22:20:00 -0700 | [diff] [blame] | 1081 | arena_huge_ralloc_stats_update(arena, oldsize, usize); |
| 1082 | arena->stats.mapped += cdiff; |
| 1083 | } |
Jason Evans | 40ee9aa | 2016-02-27 12:34:50 -0800 | [diff] [blame] | 1084 | arena_nactive_add(arena, udiff >> LG_PAGE); |
Jason Evans | 9b41ac9 | 2014-10-14 22:20:00 -0700 | [diff] [blame] | 1085 | |
Jason Evans | b2c0d63 | 2016-04-13 23:36:15 -0700 | [diff] [blame] | 1086 | err = (chunk_alloc_cache(tsd, arena, &chunk_hooks, nchunk, cdiff, |
| 1087 | chunksize, zero, true) == NULL); |
| 1088 | malloc_mutex_unlock(tsd, &arena->lock); |
Jason Evans | 99bd94f | 2015-02-18 16:40:53 -0800 | [diff] [blame] | 1089 | if (err) { |
Jason Evans | b2c0d63 | 2016-04-13 23:36:15 -0700 | [diff] [blame] | 1090 | err = arena_chunk_ralloc_huge_expand_hard(tsd, arena, |
| 1091 | &chunk_hooks, chunk, oldsize, usize, zero, nchunk, udiff, |
Jason Evans | b49a334 | 2015-07-28 11:28:19 -0400 | [diff] [blame] | 1092 | cdiff); |
| 1093 | } else if (chunk_hooks.merge(chunk, CHUNK_CEILING(oldsize), nchunk, |
| 1094 | cdiff, true, arena->ind)) { |
Jason Evans | b2c0d63 | 2016-04-13 23:36:15 -0700 | [diff] [blame] | 1095 | chunk_dalloc_wrapper(tsd, arena, &chunk_hooks, nchunk, cdiff, |
| 1096 | *zero, true); |
Jason Evans | b49a334 | 2015-07-28 11:28:19 -0400 | [diff] [blame] | 1097 | err = true; |
Jason Evans | 9b41ac9 | 2014-10-14 22:20:00 -0700 | [diff] [blame] | 1098 | } |
| 1099 | |
Jason Evans | 99bd94f | 2015-02-18 16:40:53 -0800 | [diff] [blame] | 1100 | return (err); |
Jason Evans | 9b41ac9 | 2014-10-14 22:20:00 -0700 | [diff] [blame] | 1101 | } |
| 1102 | |
Jason Evans | aa28266 | 2015-07-15 16:02:21 -0700 | [diff] [blame] | 1103 | /* |
| 1104 | * Do first-best-fit run selection, i.e. select the lowest run that best fits. |
Dave Watson | 3417a30 | 2016-02-23 12:06:21 -0800 | [diff] [blame] | 1105 | * Run sizes are indexed, so not all candidate runs are necessarily exactly the |
| 1106 | * same size. |
Jason Evans | aa28266 | 2015-07-15 16:02:21 -0700 | [diff] [blame] | 1107 | */ |
Jason Evans | 97c04a9 | 2015-03-06 19:57:36 -0800 | [diff] [blame] | 1108 | static arena_run_t * |
Jason Evans | aa28266 | 2015-07-15 16:02:21 -0700 | [diff] [blame] | 1109 | arena_run_first_best_fit(arena_t *arena, size_t size) |
Jason Evans | 97c04a9 | 2015-03-06 19:57:36 -0800 | [diff] [blame] | 1110 | { |
Dave Watson | 3417a30 | 2016-02-23 12:06:21 -0800 | [diff] [blame] | 1111 | szind_t ind, i; |
| 1112 | |
| 1113 | ind = size2index(run_quantize_ceil(size)); |
Dave Watson | cd86c14 | 2016-02-24 11:02:49 -0800 | [diff] [blame] | 1114 | for (i = ind; i < runs_avail_nclasses + runs_avail_bias; i++) { |
Jason Evans | c6a2c39 | 2016-03-26 17:30:37 -0700 | [diff] [blame] | 1115 | arena_chunk_map_misc_t *miscelm = arena_run_heap_first( |
| 1116 | arena_runs_avail_get(arena, i)); |
| 1117 | if (miscelm != NULL) |
Dave Watson | 3417a30 | 2016-02-23 12:06:21 -0800 | [diff] [blame] | 1118 | return (&miscelm->run); |
| 1119 | } |
| 1120 | |
| 1121 | return (NULL); |
Jason Evans | 97c04a9 | 2015-03-06 19:57:36 -0800 | [diff] [blame] | 1122 | } |
| 1123 | |
Jason Evans | e476f8a | 2010-01-16 09:53:50 -0800 | [diff] [blame] | 1124 | static arena_run_t * |
Jason Evans | aa5113b | 2014-01-14 16:23:03 -0800 | [diff] [blame] | 1125 | arena_run_alloc_large_helper(arena_t *arena, size_t size, bool zero) |
Jason Evans | e476f8a | 2010-01-16 09:53:50 -0800 | [diff] [blame] | 1126 | { |
Jason Evans | aa28266 | 2015-07-15 16:02:21 -0700 | [diff] [blame] | 1127 | arena_run_t *run = arena_run_first_best_fit(arena, s2u(size)); |
Jason Evans | 8fadb1a | 2015-08-04 10:49:46 -0700 | [diff] [blame] | 1128 | if (run != NULL) { |
| 1129 | if (arena_run_split_large(arena, run, size, zero)) |
| 1130 | run = NULL; |
| 1131 | } |
Jason Evans | 97c04a9 | 2015-03-06 19:57:36 -0800 | [diff] [blame] | 1132 | return (run); |
Jason Evans | 5b0c996 | 2012-05-10 15:47:24 -0700 | [diff] [blame] | 1133 | } |
| 1134 | |
| 1135 | static arena_run_t * |
Jason Evans | b2c0d63 | 2016-04-13 23:36:15 -0700 | [diff] [blame] | 1136 | arena_run_alloc_large(tsd_t *tsd, arena_t *arena, size_t size, bool zero) |
Jason Evans | 5b0c996 | 2012-05-10 15:47:24 -0700 | [diff] [blame] | 1137 | { |
| 1138 | arena_chunk_t *chunk; |
| 1139 | arena_run_t *run; |
| 1140 | |
Jason Evans | fc0b3b7 | 2014-10-09 17:54:06 -0700 | [diff] [blame] | 1141 | assert(size <= arena_maxrun); |
Jason Evans | 8a03cf0 | 2015-05-04 09:58:36 -0700 | [diff] [blame] | 1142 | assert(size == PAGE_CEILING(size)); |
Jason Evans | 5b0c996 | 2012-05-10 15:47:24 -0700 | [diff] [blame] | 1143 | |
| 1144 | /* Search the arena's chunks for the lowest best fit. */ |
Jason Evans | aa5113b | 2014-01-14 16:23:03 -0800 | [diff] [blame] | 1145 | run = arena_run_alloc_large_helper(arena, size, zero); |
Jason Evans | 5b0c996 | 2012-05-10 15:47:24 -0700 | [diff] [blame] | 1146 | if (run != NULL) |
| 1147 | return (run); |
| 1148 | |
Jason Evans | e476f8a | 2010-01-16 09:53:50 -0800 | [diff] [blame] | 1149 | /* |
| 1150 | * No usable runs. Create a new chunk from which to allocate the run. |
| 1151 | */ |
Jason Evans | b2c0d63 | 2016-04-13 23:36:15 -0700 | [diff] [blame] | 1152 | chunk = arena_chunk_alloc(tsd, arena); |
Jason Evans | e00572b | 2010-03-14 19:43:56 -0700 | [diff] [blame] | 1153 | if (chunk != NULL) { |
Jason Evans | 61a6dfc | 2016-03-23 16:04:38 -0700 | [diff] [blame] | 1154 | run = &arena_miscelm_get_mutable(chunk, map_bias)->run; |
Jason Evans | 8fadb1a | 2015-08-04 10:49:46 -0700 | [diff] [blame] | 1155 | if (arena_run_split_large(arena, run, size, zero)) |
| 1156 | run = NULL; |
Jason Evans | e00572b | 2010-03-14 19:43:56 -0700 | [diff] [blame] | 1157 | return (run); |
| 1158 | } |
| 1159 | |
| 1160 | /* |
| 1161 | * arena_chunk_alloc() failed, but another thread may have made |
| 1162 | * sufficient memory available while this one dropped arena->lock in |
| 1163 | * arena_chunk_alloc(), so search one more time. |
| 1164 | */ |
Jason Evans | aa5113b | 2014-01-14 16:23:03 -0800 | [diff] [blame] | 1165 | return (arena_run_alloc_large_helper(arena, size, zero)); |
| 1166 | } |
| 1167 | |
| 1168 | static arena_run_t * |
Jason Evans | d01fd19 | 2015-08-19 15:21:32 -0700 | [diff] [blame] | 1169 | arena_run_alloc_small_helper(arena_t *arena, size_t size, szind_t binind) |
Jason Evans | aa5113b | 2014-01-14 16:23:03 -0800 | [diff] [blame] | 1170 | { |
Jason Evans | aa28266 | 2015-07-15 16:02:21 -0700 | [diff] [blame] | 1171 | arena_run_t *run = arena_run_first_best_fit(arena, size); |
Jason Evans | 8fadb1a | 2015-08-04 10:49:46 -0700 | [diff] [blame] | 1172 | if (run != NULL) { |
| 1173 | if (arena_run_split_small(arena, run, size, binind)) |
| 1174 | run = NULL; |
| 1175 | } |
Jason Evans | 97c04a9 | 2015-03-06 19:57:36 -0800 | [diff] [blame] | 1176 | return (run); |
Jason Evans | aa5113b | 2014-01-14 16:23:03 -0800 | [diff] [blame] | 1177 | } |
| 1178 | |
| 1179 | static arena_run_t * |
Jason Evans | b2c0d63 | 2016-04-13 23:36:15 -0700 | [diff] [blame] | 1180 | arena_run_alloc_small(tsd_t *tsd, arena_t *arena, size_t size, szind_t binind) |
Jason Evans | aa5113b | 2014-01-14 16:23:03 -0800 | [diff] [blame] | 1181 | { |
| 1182 | arena_chunk_t *chunk; |
| 1183 | arena_run_t *run; |
| 1184 | |
Jason Evans | fc0b3b7 | 2014-10-09 17:54:06 -0700 | [diff] [blame] | 1185 | assert(size <= arena_maxrun); |
Jason Evans | 8a03cf0 | 2015-05-04 09:58:36 -0700 | [diff] [blame] | 1186 | assert(size == PAGE_CEILING(size)); |
Jason Evans | aa5113b | 2014-01-14 16:23:03 -0800 | [diff] [blame] | 1187 | assert(binind != BININD_INVALID); |
| 1188 | |
| 1189 | /* Search the arena's chunks for the lowest best fit. */ |
| 1190 | run = arena_run_alloc_small_helper(arena, size, binind); |
| 1191 | if (run != NULL) |
| 1192 | return (run); |
| 1193 | |
| 1194 | /* |
| 1195 | * No usable runs. Create a new chunk from which to allocate the run. |
| 1196 | */ |
Jason Evans | b2c0d63 | 2016-04-13 23:36:15 -0700 | [diff] [blame] | 1197 | chunk = arena_chunk_alloc(tsd, arena); |
Jason Evans | aa5113b | 2014-01-14 16:23:03 -0800 | [diff] [blame] | 1198 | if (chunk != NULL) { |
Jason Evans | 61a6dfc | 2016-03-23 16:04:38 -0700 | [diff] [blame] | 1199 | run = &arena_miscelm_get_mutable(chunk, map_bias)->run; |
Jason Evans | 8fadb1a | 2015-08-04 10:49:46 -0700 | [diff] [blame] | 1200 | if (arena_run_split_small(arena, run, size, binind)) |
| 1201 | run = NULL; |
Jason Evans | aa5113b | 2014-01-14 16:23:03 -0800 | [diff] [blame] | 1202 | return (run); |
| 1203 | } |
| 1204 | |
| 1205 | /* |
| 1206 | * arena_chunk_alloc() failed, but another thread may have made |
| 1207 | * sufficient memory available while this one dropped arena->lock in |
| 1208 | * arena_chunk_alloc(), so search one more time. |
| 1209 | */ |
| 1210 | return (arena_run_alloc_small_helper(arena, size, binind)); |
Jason Evans | e476f8a | 2010-01-16 09:53:50 -0800 | [diff] [blame] | 1211 | } |
| 1212 | |
Jason Evans | 8d6a3e8 | 2015-03-18 18:55:33 -0700 | [diff] [blame] | 1213 | static bool |
| 1214 | arena_lg_dirty_mult_valid(ssize_t lg_dirty_mult) |
| 1215 | { |
| 1216 | |
Jason Evans | bd16ea4 | 2015-03-24 15:59:28 -0700 | [diff] [blame] | 1217 | return (lg_dirty_mult >= -1 && lg_dirty_mult < (ssize_t)(sizeof(size_t) |
| 1218 | << 3)); |
Jason Evans | 8d6a3e8 | 2015-03-18 18:55:33 -0700 | [diff] [blame] | 1219 | } |
| 1220 | |
| 1221 | ssize_t |
Jason Evans | b2c0d63 | 2016-04-13 23:36:15 -0700 | [diff] [blame] | 1222 | arena_lg_dirty_mult_get(tsd_t *tsd, arena_t *arena) |
Jason Evans | 8d6a3e8 | 2015-03-18 18:55:33 -0700 | [diff] [blame] | 1223 | { |
| 1224 | ssize_t lg_dirty_mult; |
| 1225 | |
Jason Evans | b2c0d63 | 2016-04-13 23:36:15 -0700 | [diff] [blame] | 1226 | malloc_mutex_lock(tsd, &arena->lock); |
Jason Evans | 8d6a3e8 | 2015-03-18 18:55:33 -0700 | [diff] [blame] | 1227 | lg_dirty_mult = arena->lg_dirty_mult; |
Jason Evans | b2c0d63 | 2016-04-13 23:36:15 -0700 | [diff] [blame] | 1228 | malloc_mutex_unlock(tsd, &arena->lock); |
Jason Evans | 8d6a3e8 | 2015-03-18 18:55:33 -0700 | [diff] [blame] | 1229 | |
| 1230 | return (lg_dirty_mult); |
| 1231 | } |
| 1232 | |
| 1233 | bool |
Jason Evans | b2c0d63 | 2016-04-13 23:36:15 -0700 | [diff] [blame] | 1234 | arena_lg_dirty_mult_set(tsd_t *tsd, arena_t *arena, ssize_t lg_dirty_mult) |
Jason Evans | 8d6a3e8 | 2015-03-18 18:55:33 -0700 | [diff] [blame] | 1235 | { |
| 1236 | |
| 1237 | if (!arena_lg_dirty_mult_valid(lg_dirty_mult)) |
| 1238 | return (true); |
| 1239 | |
Jason Evans | b2c0d63 | 2016-04-13 23:36:15 -0700 | [diff] [blame] | 1240 | malloc_mutex_lock(tsd, &arena->lock); |
Jason Evans | 8d6a3e8 | 2015-03-18 18:55:33 -0700 | [diff] [blame] | 1241 | arena->lg_dirty_mult = lg_dirty_mult; |
Jason Evans | b2c0d63 | 2016-04-13 23:36:15 -0700 | [diff] [blame] | 1242 | arena_maybe_purge(tsd, arena); |
| 1243 | malloc_mutex_unlock(tsd, &arena->lock); |
Jason Evans | 8d6a3e8 | 2015-03-18 18:55:33 -0700 | [diff] [blame] | 1244 | |
| 1245 | return (false); |
| 1246 | } |
| 1247 | |
Jason Evans | 1a4ad3c | 2016-02-19 19:51:23 -0800 | [diff] [blame] | 1248 | static void |
Jason Evans | 243f7a0 | 2016-02-19 20:09:31 -0800 | [diff] [blame] | 1249 | arena_decay_deadline_init(arena_t *arena) |
| 1250 | { |
| 1251 | |
| 1252 | assert(opt_purge == purge_mode_decay); |
| 1253 | |
| 1254 | /* |
| 1255 | * Generate a new deadline that is uniformly random within the next |
| 1256 | * epoch after the current one. |
| 1257 | */ |
Jason Evans | 9bad079 | 2016-02-21 11:25:02 -0800 | [diff] [blame] | 1258 | nstime_copy(&arena->decay_deadline, &arena->decay_epoch); |
| 1259 | nstime_add(&arena->decay_deadline, &arena->decay_interval); |
Jason Evans | 243f7a0 | 2016-02-19 20:09:31 -0800 | [diff] [blame] | 1260 | if (arena->decay_time > 0) { |
Jason Evans | 9bad079 | 2016-02-21 11:25:02 -0800 | [diff] [blame] | 1261 | nstime_t jitter; |
Jason Evans | 243f7a0 | 2016-02-19 20:09:31 -0800 | [diff] [blame] | 1262 | |
Jason Evans | 9bad079 | 2016-02-21 11:25:02 -0800 | [diff] [blame] | 1263 | nstime_init(&jitter, prng_range(&arena->decay_jitter_state, |
| 1264 | nstime_ns(&arena->decay_interval))); |
| 1265 | nstime_add(&arena->decay_deadline, &jitter); |
Jason Evans | 243f7a0 | 2016-02-19 20:09:31 -0800 | [diff] [blame] | 1266 | } |
| 1267 | } |
| 1268 | |
| 1269 | static bool |
Jason Evans | 9bad079 | 2016-02-21 11:25:02 -0800 | [diff] [blame] | 1270 | arena_decay_deadline_reached(const arena_t *arena, const nstime_t *time) |
Jason Evans | 243f7a0 | 2016-02-19 20:09:31 -0800 | [diff] [blame] | 1271 | { |
| 1272 | |
| 1273 | assert(opt_purge == purge_mode_decay); |
| 1274 | |
Jason Evans | 9bad079 | 2016-02-21 11:25:02 -0800 | [diff] [blame] | 1275 | return (nstime_compare(&arena->decay_deadline, time) <= 0); |
Jason Evans | 243f7a0 | 2016-02-19 20:09:31 -0800 | [diff] [blame] | 1276 | } |
| 1277 | |
| 1278 | static size_t |
| 1279 | arena_decay_backlog_npages_limit(const arena_t *arena) |
| 1280 | { |
| 1281 | static const uint64_t h_steps[] = { |
| 1282 | #define STEP(step, h, x, y) \ |
| 1283 | h, |
| 1284 | SMOOTHSTEP |
| 1285 | #undef STEP |
| 1286 | }; |
| 1287 | uint64_t sum; |
| 1288 | size_t npages_limit_backlog; |
| 1289 | unsigned i; |
| 1290 | |
| 1291 | assert(opt_purge == purge_mode_decay); |
| 1292 | |
| 1293 | /* |
| 1294 | * For each element of decay_backlog, multiply by the corresponding |
| 1295 | * fixed-point smoothstep decay factor. Sum the products, then divide |
| 1296 | * to round down to the nearest whole number of pages. |
| 1297 | */ |
| 1298 | sum = 0; |
| 1299 | for (i = 0; i < SMOOTHSTEP_NSTEPS; i++) |
| 1300 | sum += arena->decay_backlog[i] * h_steps[i]; |
rustyx | 0043233 | 2016-04-12 09:50:54 +0200 | [diff] [blame] | 1301 | npages_limit_backlog = (size_t)(sum >> SMOOTHSTEP_BFP); |
Jason Evans | 243f7a0 | 2016-02-19 20:09:31 -0800 | [diff] [blame] | 1302 | |
| 1303 | return (npages_limit_backlog); |
| 1304 | } |
| 1305 | |
| 1306 | static void |
Jason Evans | 9bad079 | 2016-02-21 11:25:02 -0800 | [diff] [blame] | 1307 | arena_decay_epoch_advance(arena_t *arena, const nstime_t *time) |
Jason Evans | 243f7a0 | 2016-02-19 20:09:31 -0800 | [diff] [blame] | 1308 | { |
rustyx | 0043233 | 2016-04-12 09:50:54 +0200 | [diff] [blame] | 1309 | uint64_t nadvance_u64; |
Jason Evans | 9bad079 | 2016-02-21 11:25:02 -0800 | [diff] [blame] | 1310 | nstime_t delta; |
Jason Evans | 243f7a0 | 2016-02-19 20:09:31 -0800 | [diff] [blame] | 1311 | size_t ndirty_delta; |
| 1312 | |
| 1313 | assert(opt_purge == purge_mode_decay); |
| 1314 | assert(arena_decay_deadline_reached(arena, time)); |
| 1315 | |
Jason Evans | 9bad079 | 2016-02-21 11:25:02 -0800 | [diff] [blame] | 1316 | nstime_copy(&delta, time); |
| 1317 | nstime_subtract(&delta, &arena->decay_epoch); |
rustyx | 0043233 | 2016-04-12 09:50:54 +0200 | [diff] [blame] | 1318 | nadvance_u64 = nstime_divide(&delta, &arena->decay_interval); |
| 1319 | assert(nadvance_u64 > 0); |
Jason Evans | 243f7a0 | 2016-02-19 20:09:31 -0800 | [diff] [blame] | 1320 | |
rustyx | 0043233 | 2016-04-12 09:50:54 +0200 | [diff] [blame] | 1321 | /* Add nadvance_u64 decay intervals to epoch. */ |
Jason Evans | 9bad079 | 2016-02-21 11:25:02 -0800 | [diff] [blame] | 1322 | nstime_copy(&delta, &arena->decay_interval); |
rustyx | 0043233 | 2016-04-12 09:50:54 +0200 | [diff] [blame] | 1323 | nstime_imultiply(&delta, nadvance_u64); |
Jason Evans | 9bad079 | 2016-02-21 11:25:02 -0800 | [diff] [blame] | 1324 | nstime_add(&arena->decay_epoch, &delta); |
Jason Evans | 243f7a0 | 2016-02-19 20:09:31 -0800 | [diff] [blame] | 1325 | |
| 1326 | /* Set a new deadline. */ |
| 1327 | arena_decay_deadline_init(arena); |
| 1328 | |
| 1329 | /* Update the backlog. */ |
rustyx | 0043233 | 2016-04-12 09:50:54 +0200 | [diff] [blame] | 1330 | if (nadvance_u64 >= SMOOTHSTEP_NSTEPS) { |
Jason Evans | 243f7a0 | 2016-02-19 20:09:31 -0800 | [diff] [blame] | 1331 | memset(arena->decay_backlog, 0, (SMOOTHSTEP_NSTEPS-1) * |
| 1332 | sizeof(size_t)); |
| 1333 | } else { |
rustyx | 0043233 | 2016-04-12 09:50:54 +0200 | [diff] [blame] | 1334 | size_t nadvance_z = (size_t)nadvance_u64; |
| 1335 | |
| 1336 | assert((uint64_t)nadvance_z == nadvance_u64); |
| 1337 | |
| 1338 | memmove(arena->decay_backlog, &arena->decay_backlog[nadvance_z], |
| 1339 | (SMOOTHSTEP_NSTEPS - nadvance_z) * sizeof(size_t)); |
| 1340 | if (nadvance_z > 1) { |
Jason Evans | 243f7a0 | 2016-02-19 20:09:31 -0800 | [diff] [blame] | 1341 | memset(&arena->decay_backlog[SMOOTHSTEP_NSTEPS - |
rustyx | 0043233 | 2016-04-12 09:50:54 +0200 | [diff] [blame] | 1342 | nadvance_z], 0, (nadvance_z-1) * sizeof(size_t)); |
Jason Evans | 243f7a0 | 2016-02-19 20:09:31 -0800 | [diff] [blame] | 1343 | } |
| 1344 | } |
| 1345 | ndirty_delta = (arena->ndirty > arena->decay_ndirty) ? arena->ndirty - |
| 1346 | arena->decay_ndirty : 0; |
| 1347 | arena->decay_ndirty = arena->ndirty; |
| 1348 | arena->decay_backlog[SMOOTHSTEP_NSTEPS-1] = ndirty_delta; |
| 1349 | arena->decay_backlog_npages_limit = |
| 1350 | arena_decay_backlog_npages_limit(arena); |
| 1351 | } |
| 1352 | |
| 1353 | static size_t |
| 1354 | arena_decay_npages_limit(arena_t *arena) |
| 1355 | { |
| 1356 | size_t npages_limit; |
| 1357 | |
| 1358 | assert(opt_purge == purge_mode_decay); |
| 1359 | |
| 1360 | npages_limit = arena->decay_backlog_npages_limit; |
| 1361 | |
| 1362 | /* Add in any dirty pages created during the current epoch. */ |
| 1363 | if (arena->ndirty > arena->decay_ndirty) |
| 1364 | npages_limit += arena->ndirty - arena->decay_ndirty; |
| 1365 | |
| 1366 | return (npages_limit); |
| 1367 | } |
| 1368 | |
| 1369 | static void |
| 1370 | arena_decay_init(arena_t *arena, ssize_t decay_time) |
| 1371 | { |
| 1372 | |
| 1373 | arena->decay_time = decay_time; |
| 1374 | if (decay_time > 0) { |
Jason Evans | 9bad079 | 2016-02-21 11:25:02 -0800 | [diff] [blame] | 1375 | nstime_init2(&arena->decay_interval, decay_time, 0); |
| 1376 | nstime_idivide(&arena->decay_interval, SMOOTHSTEP_NSTEPS); |
Jason Evans | 243f7a0 | 2016-02-19 20:09:31 -0800 | [diff] [blame] | 1377 | } |
| 1378 | |
Jason Evans | 9bad079 | 2016-02-21 11:25:02 -0800 | [diff] [blame] | 1379 | nstime_init(&arena->decay_epoch, 0); |
| 1380 | nstime_update(&arena->decay_epoch); |
Jason Evans | 243f7a0 | 2016-02-19 20:09:31 -0800 | [diff] [blame] | 1381 | arena->decay_jitter_state = (uint64_t)(uintptr_t)arena; |
| 1382 | arena_decay_deadline_init(arena); |
| 1383 | arena->decay_ndirty = arena->ndirty; |
| 1384 | arena->decay_backlog_npages_limit = 0; |
| 1385 | memset(arena->decay_backlog, 0, SMOOTHSTEP_NSTEPS * sizeof(size_t)); |
| 1386 | } |
| 1387 | |
| 1388 | static bool |
| 1389 | arena_decay_time_valid(ssize_t decay_time) |
| 1390 | { |
| 1391 | |
Jason Evans | 022f689 | 2016-03-02 22:41:32 -0800 | [diff] [blame] | 1392 | if (decay_time < -1) |
| 1393 | return (false); |
| 1394 | if (decay_time == -1 || (uint64_t)decay_time <= NSTIME_SEC_MAX) |
| 1395 | return (true); |
| 1396 | return (false); |
Jason Evans | 243f7a0 | 2016-02-19 20:09:31 -0800 | [diff] [blame] | 1397 | } |
| 1398 | |
| 1399 | ssize_t |
Jason Evans | b2c0d63 | 2016-04-13 23:36:15 -0700 | [diff] [blame] | 1400 | arena_decay_time_get(tsd_t *tsd, arena_t *arena) |
Jason Evans | 243f7a0 | 2016-02-19 20:09:31 -0800 | [diff] [blame] | 1401 | { |
| 1402 | ssize_t decay_time; |
| 1403 | |
Jason Evans | b2c0d63 | 2016-04-13 23:36:15 -0700 | [diff] [blame] | 1404 | malloc_mutex_lock(tsd, &arena->lock); |
Jason Evans | 243f7a0 | 2016-02-19 20:09:31 -0800 | [diff] [blame] | 1405 | decay_time = arena->decay_time; |
Jason Evans | b2c0d63 | 2016-04-13 23:36:15 -0700 | [diff] [blame] | 1406 | malloc_mutex_unlock(tsd, &arena->lock); |
Jason Evans | 243f7a0 | 2016-02-19 20:09:31 -0800 | [diff] [blame] | 1407 | |
| 1408 | return (decay_time); |
| 1409 | } |
| 1410 | |
| 1411 | bool |
Jason Evans | b2c0d63 | 2016-04-13 23:36:15 -0700 | [diff] [blame] | 1412 | arena_decay_time_set(tsd_t *tsd, arena_t *arena, ssize_t decay_time) |
Jason Evans | 243f7a0 | 2016-02-19 20:09:31 -0800 | [diff] [blame] | 1413 | { |
| 1414 | |
| 1415 | if (!arena_decay_time_valid(decay_time)) |
| 1416 | return (true); |
| 1417 | |
Jason Evans | b2c0d63 | 2016-04-13 23:36:15 -0700 | [diff] [blame] | 1418 | malloc_mutex_lock(tsd, &arena->lock); |
Jason Evans | 243f7a0 | 2016-02-19 20:09:31 -0800 | [diff] [blame] | 1419 | /* |
| 1420 | * Restart decay backlog from scratch, which may cause many dirty pages |
| 1421 | * to be immediately purged. It would conceptually be possible to map |
| 1422 | * the old backlog onto the new backlog, but there is no justification |
| 1423 | * for such complexity since decay_time changes are intended to be |
| 1424 | * infrequent, either between the {-1, 0, >0} states, or a one-time |
| 1425 | * arbitrary change during initial arena configuration. |
| 1426 | */ |
| 1427 | arena_decay_init(arena, decay_time); |
Jason Evans | b2c0d63 | 2016-04-13 23:36:15 -0700 | [diff] [blame] | 1428 | arena_maybe_purge(tsd, arena); |
| 1429 | malloc_mutex_unlock(tsd, &arena->lock); |
Jason Evans | 243f7a0 | 2016-02-19 20:09:31 -0800 | [diff] [blame] | 1430 | |
| 1431 | return (false); |
| 1432 | } |
| 1433 | |
| 1434 | static void |
Jason Evans | b2c0d63 | 2016-04-13 23:36:15 -0700 | [diff] [blame] | 1435 | arena_maybe_purge_ratio(tsd_t *tsd, arena_t *arena) |
Jason Evans | 05b21be | 2010-03-14 17:36:10 -0700 | [diff] [blame] | 1436 | { |
| 1437 | |
Jason Evans | 243f7a0 | 2016-02-19 20:09:31 -0800 | [diff] [blame] | 1438 | assert(opt_purge == purge_mode_ratio); |
| 1439 | |
Jason Evans | e3d1306 | 2012-10-30 15:42:37 -0700 | [diff] [blame] | 1440 | /* Don't purge if the option is disabled. */ |
Jason Evans | 8d6a3e8 | 2015-03-18 18:55:33 -0700 | [diff] [blame] | 1441 | if (arena->lg_dirty_mult < 0) |
Jason Evans | e3d1306 | 2012-10-30 15:42:37 -0700 | [diff] [blame] | 1442 | return; |
Jason Evans | 1a4ad3c | 2016-02-19 19:51:23 -0800 | [diff] [blame] | 1443 | |
Jason Evans | 0a9f9a4 | 2015-06-22 18:50:32 -0700 | [diff] [blame] | 1444 | /* |
| 1445 | * Iterate, since preventing recursive purging could otherwise leave too |
| 1446 | * many dirty pages. |
| 1447 | */ |
| 1448 | while (true) { |
| 1449 | size_t threshold = (arena->nactive >> arena->lg_dirty_mult); |
| 1450 | if (threshold < chunk_npages) |
| 1451 | threshold = chunk_npages; |
| 1452 | /* |
| 1453 | * Don't purge unless the number of purgeable pages exceeds the |
| 1454 | * threshold. |
| 1455 | */ |
| 1456 | if (arena->ndirty <= threshold) |
| 1457 | return; |
Jason Evans | b2c0d63 | 2016-04-13 23:36:15 -0700 | [diff] [blame] | 1458 | arena_purge_to_limit(tsd, arena, threshold); |
Jason Evans | 0a9f9a4 | 2015-06-22 18:50:32 -0700 | [diff] [blame] | 1459 | } |
Jason Evans | 05b21be | 2010-03-14 17:36:10 -0700 | [diff] [blame] | 1460 | } |
| 1461 | |
Jason Evans | 243f7a0 | 2016-02-19 20:09:31 -0800 | [diff] [blame] | 1462 | static void |
Jason Evans | b2c0d63 | 2016-04-13 23:36:15 -0700 | [diff] [blame] | 1463 | arena_maybe_purge_decay(tsd_t *tsd, arena_t *arena) |
Jason Evans | 243f7a0 | 2016-02-19 20:09:31 -0800 | [diff] [blame] | 1464 | { |
Jason Evans | 9bad079 | 2016-02-21 11:25:02 -0800 | [diff] [blame] | 1465 | nstime_t time; |
Jason Evans | 243f7a0 | 2016-02-19 20:09:31 -0800 | [diff] [blame] | 1466 | size_t ndirty_limit; |
| 1467 | |
| 1468 | assert(opt_purge == purge_mode_decay); |
| 1469 | |
| 1470 | /* Purge all or nothing if the option is disabled. */ |
| 1471 | if (arena->decay_time <= 0) { |
| 1472 | if (arena->decay_time == 0) |
Jason Evans | b2c0d63 | 2016-04-13 23:36:15 -0700 | [diff] [blame] | 1473 | arena_purge_to_limit(tsd, arena, 0); |
Jason Evans | 243f7a0 | 2016-02-19 20:09:31 -0800 | [diff] [blame] | 1474 | return; |
| 1475 | } |
| 1476 | |
Jason Evans | 9bad079 | 2016-02-21 11:25:02 -0800 | [diff] [blame] | 1477 | nstime_copy(&time, &arena->decay_epoch); |
| 1478 | if (unlikely(nstime_update(&time))) { |
Jason Evans | 243f7a0 | 2016-02-19 20:09:31 -0800 | [diff] [blame] | 1479 | /* Time went backwards. Force an epoch advance. */ |
Jason Evans | 9bad079 | 2016-02-21 11:25:02 -0800 | [diff] [blame] | 1480 | nstime_copy(&time, &arena->decay_deadline); |
Jason Evans | 243f7a0 | 2016-02-19 20:09:31 -0800 | [diff] [blame] | 1481 | } |
| 1482 | |
| 1483 | if (arena_decay_deadline_reached(arena, &time)) |
| 1484 | arena_decay_epoch_advance(arena, &time); |
| 1485 | |
| 1486 | ndirty_limit = arena_decay_npages_limit(arena); |
| 1487 | |
| 1488 | /* |
| 1489 | * Don't try to purge unless the number of purgeable pages exceeds the |
| 1490 | * current limit. |
| 1491 | */ |
| 1492 | if (arena->ndirty <= ndirty_limit) |
| 1493 | return; |
Jason Evans | b2c0d63 | 2016-04-13 23:36:15 -0700 | [diff] [blame] | 1494 | arena_purge_to_limit(tsd, arena, ndirty_limit); |
Jason Evans | 243f7a0 | 2016-02-19 20:09:31 -0800 | [diff] [blame] | 1495 | } |
| 1496 | |
Jason Evans | 1a4ad3c | 2016-02-19 19:51:23 -0800 | [diff] [blame] | 1497 | void |
Jason Evans | b2c0d63 | 2016-04-13 23:36:15 -0700 | [diff] [blame] | 1498 | arena_maybe_purge(tsd_t *tsd, arena_t *arena) |
Jason Evans | 1a4ad3c | 2016-02-19 19:51:23 -0800 | [diff] [blame] | 1499 | { |
| 1500 | |
| 1501 | /* Don't recursively purge. */ |
| 1502 | if (arena->purging) |
| 1503 | return; |
| 1504 | |
Jason Evans | 243f7a0 | 2016-02-19 20:09:31 -0800 | [diff] [blame] | 1505 | if (opt_purge == purge_mode_ratio) |
Jason Evans | b2c0d63 | 2016-04-13 23:36:15 -0700 | [diff] [blame] | 1506 | arena_maybe_purge_ratio(tsd, arena); |
Jason Evans | 243f7a0 | 2016-02-19 20:09:31 -0800 | [diff] [blame] | 1507 | else |
Jason Evans | b2c0d63 | 2016-04-13 23:36:15 -0700 | [diff] [blame] | 1508 | arena_maybe_purge_decay(tsd, arena); |
Jason Evans | 1a4ad3c | 2016-02-19 19:51:23 -0800 | [diff] [blame] | 1509 | } |
| 1510 | |
Qinfan Wu | a244e50 | 2014-07-21 10:23:36 -0700 | [diff] [blame] | 1511 | static size_t |
| 1512 | arena_dirty_count(arena_t *arena) |
| 1513 | { |
| 1514 | size_t ndirty = 0; |
Jason Evans | 38e42d3 | 2015-03-10 18:15:40 -0700 | [diff] [blame] | 1515 | arena_runs_dirty_link_t *rdelm; |
Jason Evans | ee41ad4 | 2015-02-15 18:04:46 -0800 | [diff] [blame] | 1516 | extent_node_t *chunkselm; |
Qinfan Wu | a244e50 | 2014-07-21 10:23:36 -0700 | [diff] [blame] | 1517 | |
Jason Evans | 38e42d3 | 2015-03-10 18:15:40 -0700 | [diff] [blame] | 1518 | for (rdelm = qr_next(&arena->runs_dirty, rd_link), |
Jason Evans | 738e089 | 2015-02-18 01:15:50 -0800 | [diff] [blame] | 1519 | chunkselm = qr_next(&arena->chunks_cache, cc_link); |
Jason Evans | 38e42d3 | 2015-03-10 18:15:40 -0700 | [diff] [blame] | 1520 | rdelm != &arena->runs_dirty; rdelm = qr_next(rdelm, rd_link)) { |
Jason Evans | ee41ad4 | 2015-02-15 18:04:46 -0800 | [diff] [blame] | 1521 | size_t npages; |
| 1522 | |
Jason Evans | f5c8f37 | 2015-03-10 18:29:49 -0700 | [diff] [blame] | 1523 | if (rdelm == &chunkselm->rd) { |
Jason Evans | ee41ad4 | 2015-02-15 18:04:46 -0800 | [diff] [blame] | 1524 | npages = extent_node_size_get(chunkselm) >> LG_PAGE; |
Jason Evans | 738e089 | 2015-02-18 01:15:50 -0800 | [diff] [blame] | 1525 | chunkselm = qr_next(chunkselm, cc_link); |
Jason Evans | ee41ad4 | 2015-02-15 18:04:46 -0800 | [diff] [blame] | 1526 | } else { |
Jason Evans | 38e42d3 | 2015-03-10 18:15:40 -0700 | [diff] [blame] | 1527 | arena_chunk_t *chunk = (arena_chunk_t *)CHUNK_ADDR2BASE( |
| 1528 | rdelm); |
| 1529 | arena_chunk_map_misc_t *miscelm = |
| 1530 | arena_rd_to_miscelm(rdelm); |
| 1531 | size_t pageind = arena_miscelm_to_pageind(miscelm); |
Jason Evans | ee41ad4 | 2015-02-15 18:04:46 -0800 | [diff] [blame] | 1532 | assert(arena_mapbits_allocated_get(chunk, pageind) == |
| 1533 | 0); |
| 1534 | assert(arena_mapbits_large_get(chunk, pageind) == 0); |
| 1535 | assert(arena_mapbits_dirty_get(chunk, pageind) != 0); |
| 1536 | npages = arena_mapbits_unallocated_size_get(chunk, |
| 1537 | pageind) >> LG_PAGE; |
| 1538 | } |
Qinfan Wu | a244e50 | 2014-07-21 10:23:36 -0700 | [diff] [blame] | 1539 | ndirty += npages; |
| 1540 | } |
| 1541 | |
Jason Evans | 2b2f6dc | 2014-11-01 02:29:10 -0700 | [diff] [blame] | 1542 | return (ndirty); |
Jason Evans | aa5113b | 2014-01-14 16:23:03 -0800 | [diff] [blame] | 1543 | } |
| 1544 | |
| 1545 | static size_t |
Jason Evans | b2c0d63 | 2016-04-13 23:36:15 -0700 | [diff] [blame] | 1546 | arena_stash_dirty(tsd_t *tsd, arena_t *arena, chunk_hooks_t *chunk_hooks, |
Jason Evans | 1a4ad3c | 2016-02-19 19:51:23 -0800 | [diff] [blame] | 1547 | size_t ndirty_limit, arena_runs_dirty_link_t *purge_runs_sentinel, |
Jason Evans | ee41ad4 | 2015-02-15 18:04:46 -0800 | [diff] [blame] | 1548 | extent_node_t *purge_chunks_sentinel) |
Jason Evans | aa5113b | 2014-01-14 16:23:03 -0800 | [diff] [blame] | 1549 | { |
Jason Evans | 38e42d3 | 2015-03-10 18:15:40 -0700 | [diff] [blame] | 1550 | arena_runs_dirty_link_t *rdelm, *rdelm_next; |
Jason Evans | ee41ad4 | 2015-02-15 18:04:46 -0800 | [diff] [blame] | 1551 | extent_node_t *chunkselm; |
Qinfan Wu | e970800 | 2014-07-21 18:09:04 -0700 | [diff] [blame] | 1552 | size_t nstashed = 0; |
Jason Evans | aa5113b | 2014-01-14 16:23:03 -0800 | [diff] [blame] | 1553 | |
Jason Evans | 1a4ad3c | 2016-02-19 19:51:23 -0800 | [diff] [blame] | 1554 | /* Stash runs/chunks according to ndirty_limit. */ |
Jason Evans | 38e42d3 | 2015-03-10 18:15:40 -0700 | [diff] [blame] | 1555 | for (rdelm = qr_next(&arena->runs_dirty, rd_link), |
Jason Evans | 738e089 | 2015-02-18 01:15:50 -0800 | [diff] [blame] | 1556 | chunkselm = qr_next(&arena->chunks_cache, cc_link); |
Jason Evans | 38e42d3 | 2015-03-10 18:15:40 -0700 | [diff] [blame] | 1557 | rdelm != &arena->runs_dirty; rdelm = rdelm_next) { |
Jason Evans | ee41ad4 | 2015-02-15 18:04:46 -0800 | [diff] [blame] | 1558 | size_t npages; |
Jason Evans | 38e42d3 | 2015-03-10 18:15:40 -0700 | [diff] [blame] | 1559 | rdelm_next = qr_next(rdelm, rd_link); |
Jason Evans | aa5113b | 2014-01-14 16:23:03 -0800 | [diff] [blame] | 1560 | |
Jason Evans | f5c8f37 | 2015-03-10 18:29:49 -0700 | [diff] [blame] | 1561 | if (rdelm == &chunkselm->rd) { |
Jason Evans | 99bd94f | 2015-02-18 16:40:53 -0800 | [diff] [blame] | 1562 | extent_node_t *chunkselm_next; |
| 1563 | bool zero; |
Jason Evans | ee41ad4 | 2015-02-15 18:04:46 -0800 | [diff] [blame] | 1564 | UNUSED void *chunk; |
Jason Evans | aa5113b | 2014-01-14 16:23:03 -0800 | [diff] [blame] | 1565 | |
Jason Evans | 1a4ad3c | 2016-02-19 19:51:23 -0800 | [diff] [blame] | 1566 | npages = extent_node_size_get(chunkselm) >> LG_PAGE; |
Jason Evans | 243f7a0 | 2016-02-19 20:09:31 -0800 | [diff] [blame] | 1567 | if (opt_purge == purge_mode_decay && arena->ndirty - |
| 1568 | (nstashed + npages) < ndirty_limit) |
| 1569 | break; |
Jason Evans | 1a4ad3c | 2016-02-19 19:51:23 -0800 | [diff] [blame] | 1570 | |
Jason Evans | 738e089 | 2015-02-18 01:15:50 -0800 | [diff] [blame] | 1571 | chunkselm_next = qr_next(chunkselm, cc_link); |
Jason Evans | ee41ad4 | 2015-02-15 18:04:46 -0800 | [diff] [blame] | 1572 | /* |
Jason Evans | 99bd94f | 2015-02-18 16:40:53 -0800 | [diff] [blame] | 1573 | * Allocate. chunkselm remains valid due to the |
| 1574 | * dalloc_node=false argument to chunk_alloc_cache(). |
Jason Evans | ee41ad4 | 2015-02-15 18:04:46 -0800 | [diff] [blame] | 1575 | */ |
Jason Evans | ee41ad4 | 2015-02-15 18:04:46 -0800 | [diff] [blame] | 1576 | zero = false; |
Jason Evans | b2c0d63 | 2016-04-13 23:36:15 -0700 | [diff] [blame] | 1577 | chunk = chunk_alloc_cache(tsd, arena, chunk_hooks, |
Jason Evans | 99bd94f | 2015-02-18 16:40:53 -0800 | [diff] [blame] | 1578 | extent_node_addr_get(chunkselm), |
| 1579 | extent_node_size_get(chunkselm), chunksize, &zero, |
| 1580 | false); |
| 1581 | assert(chunk == extent_node_addr_get(chunkselm)); |
| 1582 | assert(zero == extent_node_zeroed_get(chunkselm)); |
| 1583 | extent_node_dirty_insert(chunkselm, purge_runs_sentinel, |
Jason Evans | 738e089 | 2015-02-18 01:15:50 -0800 | [diff] [blame] | 1584 | purge_chunks_sentinel); |
Jason Evans | 1a4ad3c | 2016-02-19 19:51:23 -0800 | [diff] [blame] | 1585 | assert(npages == (extent_node_size_get(chunkselm) >> |
| 1586 | LG_PAGE)); |
Jason Evans | ee41ad4 | 2015-02-15 18:04:46 -0800 | [diff] [blame] | 1587 | chunkselm = chunkselm_next; |
| 1588 | } else { |
| 1589 | arena_chunk_t *chunk = |
Jason Evans | 38e42d3 | 2015-03-10 18:15:40 -0700 | [diff] [blame] | 1590 | (arena_chunk_t *)CHUNK_ADDR2BASE(rdelm); |
| 1591 | arena_chunk_map_misc_t *miscelm = |
| 1592 | arena_rd_to_miscelm(rdelm); |
| 1593 | size_t pageind = arena_miscelm_to_pageind(miscelm); |
| 1594 | arena_run_t *run = &miscelm->run; |
Jason Evans | ee41ad4 | 2015-02-15 18:04:46 -0800 | [diff] [blame] | 1595 | size_t run_size = |
| 1596 | arena_mapbits_unallocated_size_get(chunk, pageind); |
Jason Evans | 070b3c3 | 2014-08-14 14:45:58 -0700 | [diff] [blame] | 1597 | |
Jason Evans | ee41ad4 | 2015-02-15 18:04:46 -0800 | [diff] [blame] | 1598 | npages = run_size >> LG_PAGE; |
Jason Evans | 243f7a0 | 2016-02-19 20:09:31 -0800 | [diff] [blame] | 1599 | if (opt_purge == purge_mode_decay && arena->ndirty - |
| 1600 | (nstashed + npages) < ndirty_limit) |
| 1601 | break; |
Jason Evans | ee41ad4 | 2015-02-15 18:04:46 -0800 | [diff] [blame] | 1602 | |
| 1603 | assert(pageind + npages <= chunk_npages); |
| 1604 | assert(arena_mapbits_dirty_get(chunk, pageind) == |
| 1605 | arena_mapbits_dirty_get(chunk, pageind+npages-1)); |
| 1606 | |
| 1607 | /* |
| 1608 | * If purging the spare chunk's run, make it available |
| 1609 | * prior to allocation. |
| 1610 | */ |
| 1611 | if (chunk == arena->spare) |
Jason Evans | b2c0d63 | 2016-04-13 23:36:15 -0700 | [diff] [blame] | 1612 | arena_chunk_alloc(tsd, arena); |
Jason Evans | ee41ad4 | 2015-02-15 18:04:46 -0800 | [diff] [blame] | 1613 | |
| 1614 | /* Temporarily allocate the free dirty run. */ |
| 1615 | arena_run_split_large(arena, run, run_size, false); |
Jason Evans | 339c2b2 | 2015-02-17 22:25:56 -0800 | [diff] [blame] | 1616 | /* Stash. */ |
Jason Evans | ee41ad4 | 2015-02-15 18:04:46 -0800 | [diff] [blame] | 1617 | if (false) |
Jason Evans | 38e42d3 | 2015-03-10 18:15:40 -0700 | [diff] [blame] | 1618 | qr_new(rdelm, rd_link); /* Redundant. */ |
Jason Evans | ee41ad4 | 2015-02-15 18:04:46 -0800 | [diff] [blame] | 1619 | else { |
Jason Evans | 38e42d3 | 2015-03-10 18:15:40 -0700 | [diff] [blame] | 1620 | assert(qr_next(rdelm, rd_link) == rdelm); |
| 1621 | assert(qr_prev(rdelm, rd_link) == rdelm); |
Jason Evans | ee41ad4 | 2015-02-15 18:04:46 -0800 | [diff] [blame] | 1622 | } |
Jason Evans | 38e42d3 | 2015-03-10 18:15:40 -0700 | [diff] [blame] | 1623 | qr_meld(purge_runs_sentinel, rdelm, rd_link); |
Jason Evans | ee41ad4 | 2015-02-15 18:04:46 -0800 | [diff] [blame] | 1624 | } |
Jason Evans | aa5113b | 2014-01-14 16:23:03 -0800 | [diff] [blame] | 1625 | |
Qinfan Wu | e970800 | 2014-07-21 18:09:04 -0700 | [diff] [blame] | 1626 | nstashed += npages; |
Jason Evans | 243f7a0 | 2016-02-19 20:09:31 -0800 | [diff] [blame] | 1627 | if (opt_purge == purge_mode_ratio && arena->ndirty - nstashed <= |
| 1628 | ndirty_limit) |
Qinfan Wu | e970800 | 2014-07-21 18:09:04 -0700 | [diff] [blame] | 1629 | break; |
Jason Evans | aa5113b | 2014-01-14 16:23:03 -0800 | [diff] [blame] | 1630 | } |
Qinfan Wu | e970800 | 2014-07-21 18:09:04 -0700 | [diff] [blame] | 1631 | |
| 1632 | return (nstashed); |
Jason Evans | aa5113b | 2014-01-14 16:23:03 -0800 | [diff] [blame] | 1633 | } |
| 1634 | |
| 1635 | static size_t |
Jason Evans | b2c0d63 | 2016-04-13 23:36:15 -0700 | [diff] [blame] | 1636 | arena_purge_stashed(tsd_t *tsd, arena_t *arena, chunk_hooks_t *chunk_hooks, |
Jason Evans | 38e42d3 | 2015-03-10 18:15:40 -0700 | [diff] [blame] | 1637 | arena_runs_dirty_link_t *purge_runs_sentinel, |
Jason Evans | ee41ad4 | 2015-02-15 18:04:46 -0800 | [diff] [blame] | 1638 | extent_node_t *purge_chunks_sentinel) |
Jason Evans | aa5113b | 2014-01-14 16:23:03 -0800 | [diff] [blame] | 1639 | { |
Qinfan Wu | e970800 | 2014-07-21 18:09:04 -0700 | [diff] [blame] | 1640 | size_t npurged, nmadvise; |
Jason Evans | 38e42d3 | 2015-03-10 18:15:40 -0700 | [diff] [blame] | 1641 | arena_runs_dirty_link_t *rdelm; |
Jason Evans | ee41ad4 | 2015-02-15 18:04:46 -0800 | [diff] [blame] | 1642 | extent_node_t *chunkselm; |
Jason Evans | aa5113b | 2014-01-14 16:23:03 -0800 | [diff] [blame] | 1643 | |
Jason Evans | aa5113b | 2014-01-14 16:23:03 -0800 | [diff] [blame] | 1644 | if (config_stats) |
| 1645 | nmadvise = 0; |
| 1646 | npurged = 0; |
Qinfan Wu | e970800 | 2014-07-21 18:09:04 -0700 | [diff] [blame] | 1647 | |
Jason Evans | b2c0d63 | 2016-04-13 23:36:15 -0700 | [diff] [blame] | 1648 | malloc_mutex_unlock(tsd, &arena->lock); |
Jason Evans | 38e42d3 | 2015-03-10 18:15:40 -0700 | [diff] [blame] | 1649 | for (rdelm = qr_next(purge_runs_sentinel, rd_link), |
Jason Evans | 738e089 | 2015-02-18 01:15:50 -0800 | [diff] [blame] | 1650 | chunkselm = qr_next(purge_chunks_sentinel, cc_link); |
Jason Evans | 38e42d3 | 2015-03-10 18:15:40 -0700 | [diff] [blame] | 1651 | rdelm != purge_runs_sentinel; rdelm = qr_next(rdelm, rd_link)) { |
Jason Evans | ee41ad4 | 2015-02-15 18:04:46 -0800 | [diff] [blame] | 1652 | size_t npages; |
Qinfan Wu | e970800 | 2014-07-21 18:09:04 -0700 | [diff] [blame] | 1653 | |
Jason Evans | f5c8f37 | 2015-03-10 18:29:49 -0700 | [diff] [blame] | 1654 | if (rdelm == &chunkselm->rd) { |
Jason Evans | b49a334 | 2015-07-28 11:28:19 -0400 | [diff] [blame] | 1655 | /* |
| 1656 | * Don't actually purge the chunk here because 1) |
| 1657 | * chunkselm is embedded in the chunk and must remain |
| 1658 | * valid, and 2) we deallocate the chunk in |
| 1659 | * arena_unstash_purged(), where it is destroyed, |
| 1660 | * decommitted, or purged, depending on chunk |
| 1661 | * deallocation policy. |
| 1662 | */ |
Jason Evans | ee41ad4 | 2015-02-15 18:04:46 -0800 | [diff] [blame] | 1663 | size_t size = extent_node_size_get(chunkselm); |
Jason Evans | ee41ad4 | 2015-02-15 18:04:46 -0800 | [diff] [blame] | 1664 | npages = size >> LG_PAGE; |
Jason Evans | 738e089 | 2015-02-18 01:15:50 -0800 | [diff] [blame] | 1665 | chunkselm = qr_next(chunkselm, cc_link); |
Jason Evans | ee41ad4 | 2015-02-15 18:04:46 -0800 | [diff] [blame] | 1666 | } else { |
Jason Evans | 45186f0 | 2015-08-10 23:03:34 -0700 | [diff] [blame] | 1667 | size_t pageind, run_size, flag_unzeroed, flags, i; |
| 1668 | bool decommitted; |
Jason Evans | 8fadb1a | 2015-08-04 10:49:46 -0700 | [diff] [blame] | 1669 | arena_chunk_t *chunk = |
| 1670 | (arena_chunk_t *)CHUNK_ADDR2BASE(rdelm); |
Jason Evans | 38e42d3 | 2015-03-10 18:15:40 -0700 | [diff] [blame] | 1671 | arena_chunk_map_misc_t *miscelm = |
| 1672 | arena_rd_to_miscelm(rdelm); |
| 1673 | pageind = arena_miscelm_to_pageind(miscelm); |
Jason Evans | ee41ad4 | 2015-02-15 18:04:46 -0800 | [diff] [blame] | 1674 | run_size = arena_mapbits_large_size_get(chunk, pageind); |
| 1675 | npages = run_size >> LG_PAGE; |
Qinfan Wu | e970800 | 2014-07-21 18:09:04 -0700 | [diff] [blame] | 1676 | |
Jason Evans | ee41ad4 | 2015-02-15 18:04:46 -0800 | [diff] [blame] | 1677 | assert(pageind + npages <= chunk_npages); |
Jason Evans | de249c8 | 2015-08-09 16:47:27 -0700 | [diff] [blame] | 1678 | assert(!arena_mapbits_decommitted_get(chunk, pageind)); |
| 1679 | assert(!arena_mapbits_decommitted_get(chunk, |
| 1680 | pageind+npages-1)); |
Jason Evans | 8fadb1a | 2015-08-04 10:49:46 -0700 | [diff] [blame] | 1681 | decommitted = !chunk_hooks->decommit(chunk, chunksize, |
| 1682 | pageind << LG_PAGE, npages << LG_PAGE, arena->ind); |
| 1683 | if (decommitted) { |
Jason Evans | 45186f0 | 2015-08-10 23:03:34 -0700 | [diff] [blame] | 1684 | flag_unzeroed = 0; |
| 1685 | flags = CHUNK_MAP_DECOMMITTED; |
Jason Evans | 8fadb1a | 2015-08-04 10:49:46 -0700 | [diff] [blame] | 1686 | } else { |
Jason Evans | b2c0d63 | 2016-04-13 23:36:15 -0700 | [diff] [blame] | 1687 | flag_unzeroed = chunk_purge_wrapper(tsd, arena, |
Jason Evans | 8fadb1a | 2015-08-04 10:49:46 -0700 | [diff] [blame] | 1688 | chunk_hooks, chunk, chunksize, pageind << |
Jason Evans | 45186f0 | 2015-08-10 23:03:34 -0700 | [diff] [blame] | 1689 | LG_PAGE, run_size) ? CHUNK_MAP_UNZEROED : 0; |
| 1690 | flags = flag_unzeroed; |
Jason Evans | 8fadb1a | 2015-08-04 10:49:46 -0700 | [diff] [blame] | 1691 | } |
Jason Evans | 45186f0 | 2015-08-10 23:03:34 -0700 | [diff] [blame] | 1692 | arena_mapbits_large_set(chunk, pageind+npages-1, 0, |
| 1693 | flags); |
| 1694 | arena_mapbits_large_set(chunk, pageind, run_size, |
| 1695 | flags); |
Jason Evans | ee41ad4 | 2015-02-15 18:04:46 -0800 | [diff] [blame] | 1696 | |
| 1697 | /* |
Jason Evans | 45186f0 | 2015-08-10 23:03:34 -0700 | [diff] [blame] | 1698 | * Set the unzeroed flag for internal pages, now that |
Jason Evans | 8d6a3e8 | 2015-03-18 18:55:33 -0700 | [diff] [blame] | 1699 | * chunk_purge_wrapper() has returned whether the pages |
| 1700 | * were zeroed as a side effect of purging. This chunk |
| 1701 | * map modification is safe even though the arena mutex |
Jason Evans | ee41ad4 | 2015-02-15 18:04:46 -0800 | [diff] [blame] | 1702 | * isn't currently owned by this thread, because the run |
| 1703 | * is marked as allocated, thus protecting it from being |
| 1704 | * modified by any other thread. As long as these |
| 1705 | * writes don't perturb the first and last elements' |
| 1706 | * CHUNK_MAP_ALLOCATED bits, behavior is well defined. |
| 1707 | */ |
Jason Evans | 45186f0 | 2015-08-10 23:03:34 -0700 | [diff] [blame] | 1708 | for (i = 1; i < npages-1; i++) { |
| 1709 | arena_mapbits_internal_set(chunk, pageind+i, |
Jason Evans | ee41ad4 | 2015-02-15 18:04:46 -0800 | [diff] [blame] | 1710 | flag_unzeroed); |
| 1711 | } |
Jason Evans | aa5113b | 2014-01-14 16:23:03 -0800 | [diff] [blame] | 1712 | } |
Qinfan Wu | e970800 | 2014-07-21 18:09:04 -0700 | [diff] [blame] | 1713 | |
Jason Evans | aa5113b | 2014-01-14 16:23:03 -0800 | [diff] [blame] | 1714 | npurged += npages; |
| 1715 | if (config_stats) |
| 1716 | nmadvise++; |
| 1717 | } |
Jason Evans | b2c0d63 | 2016-04-13 23:36:15 -0700 | [diff] [blame] | 1718 | malloc_mutex_lock(tsd, &arena->lock); |
Qinfan Wu | e970800 | 2014-07-21 18:09:04 -0700 | [diff] [blame] | 1719 | |
| 1720 | if (config_stats) { |
Jason Evans | aa5113b | 2014-01-14 16:23:03 -0800 | [diff] [blame] | 1721 | arena->stats.nmadvise += nmadvise; |
Qinfan Wu | e970800 | 2014-07-21 18:09:04 -0700 | [diff] [blame] | 1722 | arena->stats.purged += npurged; |
| 1723 | } |
Jason Evans | aa5113b | 2014-01-14 16:23:03 -0800 | [diff] [blame] | 1724 | |
| 1725 | return (npurged); |
| 1726 | } |
| 1727 | |
| 1728 | static void |
Jason Evans | b2c0d63 | 2016-04-13 23:36:15 -0700 | [diff] [blame] | 1729 | arena_unstash_purged(tsd_t *tsd, arena_t *arena, chunk_hooks_t *chunk_hooks, |
Jason Evans | 38e42d3 | 2015-03-10 18:15:40 -0700 | [diff] [blame] | 1730 | arena_runs_dirty_link_t *purge_runs_sentinel, |
Jason Evans | ee41ad4 | 2015-02-15 18:04:46 -0800 | [diff] [blame] | 1731 | extent_node_t *purge_chunks_sentinel) |
Jason Evans | aa5113b | 2014-01-14 16:23:03 -0800 | [diff] [blame] | 1732 | { |
Jason Evans | 38e42d3 | 2015-03-10 18:15:40 -0700 | [diff] [blame] | 1733 | arena_runs_dirty_link_t *rdelm, *rdelm_next; |
Jason Evans | ee41ad4 | 2015-02-15 18:04:46 -0800 | [diff] [blame] | 1734 | extent_node_t *chunkselm; |
Jason Evans | aa5113b | 2014-01-14 16:23:03 -0800 | [diff] [blame] | 1735 | |
Jason Evans | b49a334 | 2015-07-28 11:28:19 -0400 | [diff] [blame] | 1736 | /* Deallocate chunks/runs. */ |
Jason Evans | 38e42d3 | 2015-03-10 18:15:40 -0700 | [diff] [blame] | 1737 | for (rdelm = qr_next(purge_runs_sentinel, rd_link), |
Jason Evans | 738e089 | 2015-02-18 01:15:50 -0800 | [diff] [blame] | 1738 | chunkselm = qr_next(purge_chunks_sentinel, cc_link); |
Jason Evans | 38e42d3 | 2015-03-10 18:15:40 -0700 | [diff] [blame] | 1739 | rdelm != purge_runs_sentinel; rdelm = rdelm_next) { |
| 1740 | rdelm_next = qr_next(rdelm, rd_link); |
Jason Evans | f5c8f37 | 2015-03-10 18:29:49 -0700 | [diff] [blame] | 1741 | if (rdelm == &chunkselm->rd) { |
Jason Evans | ee41ad4 | 2015-02-15 18:04:46 -0800 | [diff] [blame] | 1742 | extent_node_t *chunkselm_next = qr_next(chunkselm, |
Jason Evans | 738e089 | 2015-02-18 01:15:50 -0800 | [diff] [blame] | 1743 | cc_link); |
Jason Evans | 339c2b2 | 2015-02-17 22:25:56 -0800 | [diff] [blame] | 1744 | void *addr = extent_node_addr_get(chunkselm); |
| 1745 | size_t size = extent_node_size_get(chunkselm); |
Jason Evans | 738e089 | 2015-02-18 01:15:50 -0800 | [diff] [blame] | 1746 | bool zeroed = extent_node_zeroed_get(chunkselm); |
Jason Evans | de249c8 | 2015-08-09 16:47:27 -0700 | [diff] [blame] | 1747 | bool committed = extent_node_committed_get(chunkselm); |
Jason Evans | 738e089 | 2015-02-18 01:15:50 -0800 | [diff] [blame] | 1748 | extent_node_dirty_remove(chunkselm); |
Jason Evans | b2c0d63 | 2016-04-13 23:36:15 -0700 | [diff] [blame] | 1749 | arena_node_dalloc(tsd, arena, chunkselm); |
Jason Evans | ee41ad4 | 2015-02-15 18:04:46 -0800 | [diff] [blame] | 1750 | chunkselm = chunkselm_next; |
Jason Evans | b2c0d63 | 2016-04-13 23:36:15 -0700 | [diff] [blame] | 1751 | chunk_dalloc_wrapper(tsd, arena, chunk_hooks, addr, |
| 1752 | size, zeroed, committed); |
Jason Evans | ee41ad4 | 2015-02-15 18:04:46 -0800 | [diff] [blame] | 1753 | } else { |
Jason Evans | 8fadb1a | 2015-08-04 10:49:46 -0700 | [diff] [blame] | 1754 | arena_chunk_t *chunk = |
| 1755 | (arena_chunk_t *)CHUNK_ADDR2BASE(rdelm); |
Jason Evans | 38e42d3 | 2015-03-10 18:15:40 -0700 | [diff] [blame] | 1756 | arena_chunk_map_misc_t *miscelm = |
| 1757 | arena_rd_to_miscelm(rdelm); |
Jason Evans | 8fadb1a | 2015-08-04 10:49:46 -0700 | [diff] [blame] | 1758 | size_t pageind = arena_miscelm_to_pageind(miscelm); |
| 1759 | bool decommitted = (arena_mapbits_decommitted_get(chunk, |
| 1760 | pageind) != 0); |
Jason Evans | 38e42d3 | 2015-03-10 18:15:40 -0700 | [diff] [blame] | 1761 | arena_run_t *run = &miscelm->run; |
| 1762 | qr_remove(rdelm, rd_link); |
Jason Evans | b2c0d63 | 2016-04-13 23:36:15 -0700 | [diff] [blame] | 1763 | arena_run_dalloc(tsd, arena, run, false, true, |
| 1764 | decommitted); |
Jason Evans | ee41ad4 | 2015-02-15 18:04:46 -0800 | [diff] [blame] | 1765 | } |
Jason Evans | aa5113b | 2014-01-14 16:23:03 -0800 | [diff] [blame] | 1766 | } |
| 1767 | } |
| 1768 | |
Jason Evans | 243f7a0 | 2016-02-19 20:09:31 -0800 | [diff] [blame] | 1769 | /* |
| 1770 | * NB: ndirty_limit is interpreted differently depending on opt_purge: |
| 1771 | * - purge_mode_ratio: Purge as few dirty run/chunks as possible to reach the |
| 1772 | * desired state: |
| 1773 | * (arena->ndirty <= ndirty_limit) |
| 1774 | * - purge_mode_decay: Purge as many dirty runs/chunks as possible without |
| 1775 | * violating the invariant: |
| 1776 | * (arena->ndirty >= ndirty_limit) |
| 1777 | */ |
Jason Evans | 8d6a3e8 | 2015-03-18 18:55:33 -0700 | [diff] [blame] | 1778 | static void |
Jason Evans | b2c0d63 | 2016-04-13 23:36:15 -0700 | [diff] [blame] | 1779 | arena_purge_to_limit(tsd_t *tsd, arena_t *arena, size_t ndirty_limit) |
Jason Evans | e476f8a | 2010-01-16 09:53:50 -0800 | [diff] [blame] | 1780 | { |
Jason Evans | b2c0d63 | 2016-04-13 23:36:15 -0700 | [diff] [blame] | 1781 | chunk_hooks_t chunk_hooks = chunk_hooks_get(tsd, arena); |
Jason Evans | 1a4ad3c | 2016-02-19 19:51:23 -0800 | [diff] [blame] | 1782 | size_t npurge, npurged; |
Jason Evans | 38e42d3 | 2015-03-10 18:15:40 -0700 | [diff] [blame] | 1783 | arena_runs_dirty_link_t purge_runs_sentinel; |
Jason Evans | ee41ad4 | 2015-02-15 18:04:46 -0800 | [diff] [blame] | 1784 | extent_node_t purge_chunks_sentinel; |
Qinfan Wu | e970800 | 2014-07-21 18:09:04 -0700 | [diff] [blame] | 1785 | |
Jason Evans | 0a9f9a4 | 2015-06-22 18:50:32 -0700 | [diff] [blame] | 1786 | arena->purging = true; |
| 1787 | |
Jason Evans | 2b2f6dc | 2014-11-01 02:29:10 -0700 | [diff] [blame] | 1788 | /* |
| 1789 | * Calls to arena_dirty_count() are disabled even for debug builds |
| 1790 | * because overhead grows nonlinearly as memory usage increases. |
| 1791 | */ |
| 1792 | if (false && config_debug) { |
Qinfan Wu | 90737fc | 2014-07-21 19:39:20 -0700 | [diff] [blame] | 1793 | size_t ndirty = arena_dirty_count(arena); |
Qinfan Wu | a244e50 | 2014-07-21 10:23:36 -0700 | [diff] [blame] | 1794 | assert(ndirty == arena->ndirty); |
Jason Evans | 2caa471 | 2010-03-04 21:35:07 -0800 | [diff] [blame] | 1795 | } |
Jason Evans | 243f7a0 | 2016-02-19 20:09:31 -0800 | [diff] [blame] | 1796 | assert(opt_purge != purge_mode_ratio || (arena->nactive >> |
| 1797 | arena->lg_dirty_mult) < arena->ndirty || ndirty_limit == 0); |
Jason Evans | 1a4ad3c | 2016-02-19 19:51:23 -0800 | [diff] [blame] | 1798 | |
| 1799 | qr_new(&purge_runs_sentinel, rd_link); |
| 1800 | extent_node_dirty_linkage_init(&purge_chunks_sentinel); |
| 1801 | |
Jason Evans | b2c0d63 | 2016-04-13 23:36:15 -0700 | [diff] [blame] | 1802 | npurge = arena_stash_dirty(tsd, arena, &chunk_hooks, ndirty_limit, |
Jason Evans | 1a4ad3c | 2016-02-19 19:51:23 -0800 | [diff] [blame] | 1803 | &purge_runs_sentinel, &purge_chunks_sentinel); |
| 1804 | if (npurge == 0) |
| 1805 | goto label_return; |
Jason Evans | b2c0d63 | 2016-04-13 23:36:15 -0700 | [diff] [blame] | 1806 | npurged = arena_purge_stashed(tsd, arena, &chunk_hooks, |
| 1807 | &purge_runs_sentinel, &purge_chunks_sentinel); |
Jason Evans | 1a4ad3c | 2016-02-19 19:51:23 -0800 | [diff] [blame] | 1808 | assert(npurged == npurge); |
Jason Evans | b2c0d63 | 2016-04-13 23:36:15 -0700 | [diff] [blame] | 1809 | arena_unstash_purged(tsd, arena, &chunk_hooks, &purge_runs_sentinel, |
Jason Evans | 1a4ad3c | 2016-02-19 19:51:23 -0800 | [diff] [blame] | 1810 | &purge_chunks_sentinel); |
Jason Evans | e476f8a | 2010-01-16 09:53:50 -0800 | [diff] [blame] | 1811 | |
Jason Evans | 7372b15 | 2012-02-10 20:22:09 -0800 | [diff] [blame] | 1812 | if (config_stats) |
| 1813 | arena->stats.npurge++; |
Jason Evans | e476f8a | 2010-01-16 09:53:50 -0800 | [diff] [blame] | 1814 | |
Jason Evans | 1a4ad3c | 2016-02-19 19:51:23 -0800 | [diff] [blame] | 1815 | label_return: |
Jason Evans | 0a9f9a4 | 2015-06-22 18:50:32 -0700 | [diff] [blame] | 1816 | arena->purging = false; |
Jason Evans | e476f8a | 2010-01-16 09:53:50 -0800 | [diff] [blame] | 1817 | } |
| 1818 | |
Jason Evans | 6005f07 | 2010-09-30 16:55:08 -0700 | [diff] [blame] | 1819 | void |
Jason Evans | b2c0d63 | 2016-04-13 23:36:15 -0700 | [diff] [blame] | 1820 | arena_purge(tsd_t *tsd, arena_t *arena, bool all) |
Jason Evans | 6005f07 | 2010-09-30 16:55:08 -0700 | [diff] [blame] | 1821 | { |
| 1822 | |
Jason Evans | b2c0d63 | 2016-04-13 23:36:15 -0700 | [diff] [blame] | 1823 | malloc_mutex_lock(tsd, &arena->lock); |
Jason Evans | 243f7a0 | 2016-02-19 20:09:31 -0800 | [diff] [blame] | 1824 | if (all) |
Jason Evans | b2c0d63 | 2016-04-13 23:36:15 -0700 | [diff] [blame] | 1825 | arena_purge_to_limit(tsd, arena, 0); |
Jason Evans | 243f7a0 | 2016-02-19 20:09:31 -0800 | [diff] [blame] | 1826 | else |
Jason Evans | b2c0d63 | 2016-04-13 23:36:15 -0700 | [diff] [blame] | 1827 | arena_maybe_purge(tsd, arena); |
| 1828 | malloc_mutex_unlock(tsd, &arena->lock); |
Jason Evans | 6005f07 | 2010-09-30 16:55:08 -0700 | [diff] [blame] | 1829 | } |
| 1830 | |
Jason Evans | e476f8a | 2010-01-16 09:53:50 -0800 | [diff] [blame] | 1831 | static void |
Jason Evans | 19ff2ce | 2016-04-22 14:37:17 -0700 | [diff] [blame] | 1832 | arena_achunk_prof_reset(tsd_t *tsd, arena_t *arena, arena_chunk_t *chunk) |
| 1833 | { |
| 1834 | size_t pageind, npages; |
| 1835 | |
| 1836 | cassert(config_prof); |
| 1837 | assert(opt_prof); |
| 1838 | |
| 1839 | /* |
| 1840 | * Iterate over the allocated runs and remove profiled allocations from |
| 1841 | * the sample set. |
| 1842 | */ |
| 1843 | for (pageind = map_bias; pageind < chunk_npages; pageind += npages) { |
| 1844 | if (arena_mapbits_allocated_get(chunk, pageind) != 0) { |
| 1845 | if (arena_mapbits_large_get(chunk, pageind) != 0) { |
| 1846 | void *ptr = (void *)((uintptr_t)chunk + (pageind |
| 1847 | << LG_PAGE)); |
| 1848 | size_t usize = isalloc(tsd, ptr, config_prof); |
| 1849 | |
| 1850 | prof_free(tsd, ptr, usize); |
| 1851 | npages = arena_mapbits_large_size_get(chunk, |
| 1852 | pageind) >> LG_PAGE; |
| 1853 | } else { |
| 1854 | /* Skip small run. */ |
| 1855 | size_t binind = arena_mapbits_binind_get(chunk, |
| 1856 | pageind); |
| 1857 | arena_bin_info_t *bin_info = |
| 1858 | &arena_bin_info[binind]; |
| 1859 | npages = bin_info->run_size >> LG_PAGE; |
| 1860 | } |
| 1861 | } else { |
| 1862 | /* Skip unallocated run. */ |
| 1863 | npages = arena_mapbits_unallocated_size_get(chunk, |
| 1864 | pageind) >> LG_PAGE; |
| 1865 | } |
| 1866 | assert(pageind + npages <= chunk_npages); |
| 1867 | } |
| 1868 | } |
| 1869 | |
| 1870 | void |
| 1871 | arena_reset(tsd_t *tsd, arena_t *arena) |
| 1872 | { |
| 1873 | unsigned i; |
| 1874 | extent_node_t *node; |
| 1875 | |
| 1876 | /* |
| 1877 | * Locking in this function is unintuitive. The caller guarantees that |
| 1878 | * no concurrent operations are happening in this arena, but there are |
| 1879 | * still reasons that some locking is necessary: |
| 1880 | * |
| 1881 | * - Some of the functions in the transitive closure of calls assume |
| 1882 | * appropriate locks are held, and in some cases these locks are |
| 1883 | * temporarily dropped to avoid lock order reversal or deadlock due to |
| 1884 | * reentry. |
| 1885 | * - mallctl("epoch", ...) may concurrently refresh stats. While |
| 1886 | * strictly speaking this is a "concurrent operation", disallowing |
| 1887 | * stats refreshes would impose an inconvenient burden. |
| 1888 | */ |
| 1889 | |
| 1890 | /* Remove large allocations from prof sample set. */ |
| 1891 | if (config_prof && opt_prof) { |
| 1892 | ql_foreach(node, &arena->achunks, ql_link) { |
| 1893 | arena_achunk_prof_reset(tsd, arena, |
| 1894 | extent_node_addr_get(node)); |
| 1895 | } |
| 1896 | } |
| 1897 | |
Jason Evans | 7e67495 | 2016-04-25 13:26:54 -0700 | [diff] [blame] | 1898 | /* Reset curruns for large size classes. */ |
| 1899 | if (config_stats) { |
| 1900 | for (i = 0; i < nlclasses; i++) |
| 1901 | arena->stats.lstats[i].curruns = 0; |
| 1902 | } |
| 1903 | |
Jason Evans | 19ff2ce | 2016-04-22 14:37:17 -0700 | [diff] [blame] | 1904 | /* Huge allocations. */ |
| 1905 | malloc_mutex_lock(tsd, &arena->huge_mtx); |
| 1906 | for (node = ql_last(&arena->huge, ql_link); node != NULL; node = |
| 1907 | ql_last(&arena->huge, ql_link)) { |
| 1908 | void *ptr = extent_node_addr_get(node); |
Jason Evans | 7e67495 | 2016-04-25 13:26:54 -0700 | [diff] [blame] | 1909 | size_t usize; |
Jason Evans | 19ff2ce | 2016-04-22 14:37:17 -0700 | [diff] [blame] | 1910 | |
| 1911 | malloc_mutex_unlock(tsd, &arena->huge_mtx); |
Jason Evans | 7e67495 | 2016-04-25 13:26:54 -0700 | [diff] [blame] | 1912 | if (config_stats || (config_prof && opt_prof)) |
Jason Evans | 19ff2ce | 2016-04-22 14:37:17 -0700 | [diff] [blame] | 1913 | usize = isalloc(tsd, ptr, config_prof); |
Jason Evans | 7e67495 | 2016-04-25 13:26:54 -0700 | [diff] [blame] | 1914 | /* Remove huge allocation from prof sample set. */ |
| 1915 | if (config_prof && opt_prof) |
Jason Evans | 19ff2ce | 2016-04-22 14:37:17 -0700 | [diff] [blame] | 1916 | prof_free(tsd, ptr, usize); |
Jason Evans | 19ff2ce | 2016-04-22 14:37:17 -0700 | [diff] [blame] | 1917 | huge_dalloc(tsd, ptr); |
| 1918 | malloc_mutex_lock(tsd, &arena->huge_mtx); |
Jason Evans | 7e67495 | 2016-04-25 13:26:54 -0700 | [diff] [blame] | 1919 | /* Cancel out unwanted effects on stats. */ |
| 1920 | if (config_stats) |
| 1921 | arena_huge_reset_stats_cancel(arena, usize); |
Jason Evans | 19ff2ce | 2016-04-22 14:37:17 -0700 | [diff] [blame] | 1922 | } |
| 1923 | malloc_mutex_unlock(tsd, &arena->huge_mtx); |
| 1924 | |
| 1925 | malloc_mutex_lock(tsd, &arena->lock); |
| 1926 | |
| 1927 | /* Bins. */ |
| 1928 | for (i = 0; i < NBINS; i++) { |
| 1929 | arena_bin_t *bin = &arena->bins[i]; |
| 1930 | malloc_mutex_lock(tsd, &bin->lock); |
| 1931 | bin->runcur = NULL; |
| 1932 | arena_run_heap_new(&bin->runs); |
| 1933 | if (config_stats) { |
| 1934 | bin->stats.curregs = 0; |
| 1935 | bin->stats.curruns = 0; |
| 1936 | } |
| 1937 | malloc_mutex_unlock(tsd, &bin->lock); |
| 1938 | } |
| 1939 | |
| 1940 | /* |
| 1941 | * Re-initialize runs_dirty such that the chunks_cache and runs_dirty |
| 1942 | * chains directly correspond. |
| 1943 | */ |
| 1944 | qr_new(&arena->runs_dirty, rd_link); |
| 1945 | for (node = qr_next(&arena->chunks_cache, cc_link); |
| 1946 | node != &arena->chunks_cache; node = qr_next(node, cc_link)) { |
| 1947 | qr_new(&node->rd, rd_link); |
| 1948 | qr_meld(&arena->runs_dirty, &node->rd, rd_link); |
| 1949 | } |
| 1950 | |
| 1951 | /* Arena chunks. */ |
| 1952 | for (node = ql_last(&arena->achunks, ql_link); node != NULL; node = |
| 1953 | ql_last(&arena->achunks, ql_link)) { |
| 1954 | ql_remove(&arena->achunks, node, ql_link); |
| 1955 | arena_chunk_discard(tsd, arena, extent_node_addr_get(node)); |
| 1956 | } |
| 1957 | |
| 1958 | /* Spare. */ |
| 1959 | if (arena->spare != NULL) { |
| 1960 | arena_chunk_discard(tsd, arena, arena->spare); |
| 1961 | arena->spare = NULL; |
| 1962 | } |
| 1963 | |
| 1964 | assert(!arena->purging); |
| 1965 | arena->nactive = 0; |
| 1966 | |
| 1967 | for(i = 0; i < runs_avail_nclasses; i++) |
| 1968 | arena_run_heap_new(&arena->runs_avail[i]); |
| 1969 | |
| 1970 | malloc_mutex_unlock(tsd, &arena->lock); |
| 1971 | } |
| 1972 | |
| 1973 | static void |
Jason Evans | aa5113b | 2014-01-14 16:23:03 -0800 | [diff] [blame] | 1974 | arena_run_coalesce(arena_t *arena, arena_chunk_t *chunk, size_t *p_size, |
Jason Evans | 8fadb1a | 2015-08-04 10:49:46 -0700 | [diff] [blame] | 1975 | size_t *p_run_ind, size_t *p_run_pages, size_t flag_dirty, |
| 1976 | size_t flag_decommitted) |
Jason Evans | e476f8a | 2010-01-16 09:53:50 -0800 | [diff] [blame] | 1977 | { |
Jason Evans | aa5113b | 2014-01-14 16:23:03 -0800 | [diff] [blame] | 1978 | size_t size = *p_size; |
| 1979 | size_t run_ind = *p_run_ind; |
| 1980 | size_t run_pages = *p_run_pages; |
Jason Evans | e476f8a | 2010-01-16 09:53:50 -0800 | [diff] [blame] | 1981 | |
| 1982 | /* Try to coalesce forward. */ |
| 1983 | if (run_ind + run_pages < chunk_npages && |
Jason Evans | 203484e | 2012-05-02 00:30:36 -0700 | [diff] [blame] | 1984 | arena_mapbits_allocated_get(chunk, run_ind+run_pages) == 0 && |
Jason Evans | 8fadb1a | 2015-08-04 10:49:46 -0700 | [diff] [blame] | 1985 | arena_mapbits_dirty_get(chunk, run_ind+run_pages) == flag_dirty && |
| 1986 | arena_mapbits_decommitted_get(chunk, run_ind+run_pages) == |
| 1987 | flag_decommitted) { |
Jason Evans | 203484e | 2012-05-02 00:30:36 -0700 | [diff] [blame] | 1988 | size_t nrun_size = arena_mapbits_unallocated_size_get(chunk, |
| 1989 | run_ind+run_pages); |
Jason Evans | ae4c7b4 | 2012-04-02 07:04:34 -0700 | [diff] [blame] | 1990 | size_t nrun_pages = nrun_size >> LG_PAGE; |
Jason Evans | e476f8a | 2010-01-16 09:53:50 -0800 | [diff] [blame] | 1991 | |
| 1992 | /* |
| 1993 | * Remove successor from runs_avail; the coalesced run is |
| 1994 | * inserted later. |
| 1995 | */ |
Jason Evans | 203484e | 2012-05-02 00:30:36 -0700 | [diff] [blame] | 1996 | assert(arena_mapbits_unallocated_size_get(chunk, |
| 1997 | run_ind+run_pages+nrun_pages-1) == nrun_size); |
| 1998 | assert(arena_mapbits_dirty_get(chunk, |
| 1999 | run_ind+run_pages+nrun_pages-1) == flag_dirty); |
Jason Evans | 8fadb1a | 2015-08-04 10:49:46 -0700 | [diff] [blame] | 2000 | assert(arena_mapbits_decommitted_get(chunk, |
| 2001 | run_ind+run_pages+nrun_pages-1) == flag_decommitted); |
Qinfan Wu | 90737fc | 2014-07-21 19:39:20 -0700 | [diff] [blame] | 2002 | arena_avail_remove(arena, chunk, run_ind+run_pages, nrun_pages); |
Jason Evans | e476f8a | 2010-01-16 09:53:50 -0800 | [diff] [blame] | 2003 | |
Jason Evans | ee41ad4 | 2015-02-15 18:04:46 -0800 | [diff] [blame] | 2004 | /* |
| 2005 | * If the successor is dirty, remove it from the set of dirty |
| 2006 | * pages. |
| 2007 | */ |
Qinfan Wu | 04d60a1 | 2014-07-18 14:21:17 -0700 | [diff] [blame] | 2008 | if (flag_dirty != 0) { |
Jason Evans | ee41ad4 | 2015-02-15 18:04:46 -0800 | [diff] [blame] | 2009 | arena_run_dirty_remove(arena, chunk, run_ind+run_pages, |
Jason Evans | 070b3c3 | 2014-08-14 14:45:58 -0700 | [diff] [blame] | 2010 | nrun_pages); |
Qinfan Wu | 04d60a1 | 2014-07-18 14:21:17 -0700 | [diff] [blame] | 2011 | } |
| 2012 | |
Jason Evans | e476f8a | 2010-01-16 09:53:50 -0800 | [diff] [blame] | 2013 | size += nrun_size; |
Jason Evans | 12ca914 | 2010-10-17 19:56:09 -0700 | [diff] [blame] | 2014 | run_pages += nrun_pages; |
Jason Evans | e476f8a | 2010-01-16 09:53:50 -0800 | [diff] [blame] | 2015 | |
Jason Evans | 203484e | 2012-05-02 00:30:36 -0700 | [diff] [blame] | 2016 | arena_mapbits_unallocated_size_set(chunk, run_ind, size); |
| 2017 | arena_mapbits_unallocated_size_set(chunk, run_ind+run_pages-1, |
| 2018 | size); |
Jason Evans | e476f8a | 2010-01-16 09:53:50 -0800 | [diff] [blame] | 2019 | } |
| 2020 | |
| 2021 | /* Try to coalesce backward. */ |
Jason Evans | aa5113b | 2014-01-14 16:23:03 -0800 | [diff] [blame] | 2022 | if (run_ind > map_bias && arena_mapbits_allocated_get(chunk, |
| 2023 | run_ind-1) == 0 && arena_mapbits_dirty_get(chunk, run_ind-1) == |
Jason Evans | 8fadb1a | 2015-08-04 10:49:46 -0700 | [diff] [blame] | 2024 | flag_dirty && arena_mapbits_decommitted_get(chunk, run_ind-1) == |
| 2025 | flag_decommitted) { |
Jason Evans | 203484e | 2012-05-02 00:30:36 -0700 | [diff] [blame] | 2026 | size_t prun_size = arena_mapbits_unallocated_size_get(chunk, |
| 2027 | run_ind-1); |
Jason Evans | ae4c7b4 | 2012-04-02 07:04:34 -0700 | [diff] [blame] | 2028 | size_t prun_pages = prun_size >> LG_PAGE; |
Jason Evans | e476f8a | 2010-01-16 09:53:50 -0800 | [diff] [blame] | 2029 | |
Jason Evans | 12ca914 | 2010-10-17 19:56:09 -0700 | [diff] [blame] | 2030 | run_ind -= prun_pages; |
Jason Evans | e476f8a | 2010-01-16 09:53:50 -0800 | [diff] [blame] | 2031 | |
| 2032 | /* |
| 2033 | * Remove predecessor from runs_avail; the coalesced run is |
| 2034 | * inserted later. |
| 2035 | */ |
Jason Evans | 203484e | 2012-05-02 00:30:36 -0700 | [diff] [blame] | 2036 | assert(arena_mapbits_unallocated_size_get(chunk, run_ind) == |
| 2037 | prun_size); |
| 2038 | assert(arena_mapbits_dirty_get(chunk, run_ind) == flag_dirty); |
Jason Evans | 8fadb1a | 2015-08-04 10:49:46 -0700 | [diff] [blame] | 2039 | assert(arena_mapbits_decommitted_get(chunk, run_ind) == |
| 2040 | flag_decommitted); |
Qinfan Wu | 90737fc | 2014-07-21 19:39:20 -0700 | [diff] [blame] | 2041 | arena_avail_remove(arena, chunk, run_ind, prun_pages); |
Jason Evans | e476f8a | 2010-01-16 09:53:50 -0800 | [diff] [blame] | 2042 | |
Jason Evans | ee41ad4 | 2015-02-15 18:04:46 -0800 | [diff] [blame] | 2043 | /* |
| 2044 | * If the predecessor is dirty, remove it from the set of dirty |
| 2045 | * pages. |
| 2046 | */ |
| 2047 | if (flag_dirty != 0) { |
| 2048 | arena_run_dirty_remove(arena, chunk, run_ind, |
| 2049 | prun_pages); |
| 2050 | } |
Qinfan Wu | 04d60a1 | 2014-07-18 14:21:17 -0700 | [diff] [blame] | 2051 | |
Jason Evans | e476f8a | 2010-01-16 09:53:50 -0800 | [diff] [blame] | 2052 | size += prun_size; |
Jason Evans | 12ca914 | 2010-10-17 19:56:09 -0700 | [diff] [blame] | 2053 | run_pages += prun_pages; |
Jason Evans | e476f8a | 2010-01-16 09:53:50 -0800 | [diff] [blame] | 2054 | |
Jason Evans | 203484e | 2012-05-02 00:30:36 -0700 | [diff] [blame] | 2055 | arena_mapbits_unallocated_size_set(chunk, run_ind, size); |
| 2056 | arena_mapbits_unallocated_size_set(chunk, run_ind+run_pages-1, |
| 2057 | size); |
Jason Evans | e476f8a | 2010-01-16 09:53:50 -0800 | [diff] [blame] | 2058 | } |
| 2059 | |
Jason Evans | aa5113b | 2014-01-14 16:23:03 -0800 | [diff] [blame] | 2060 | *p_size = size; |
| 2061 | *p_run_ind = run_ind; |
| 2062 | *p_run_pages = run_pages; |
| 2063 | } |
| 2064 | |
Jason Evans | 8fadb1a | 2015-08-04 10:49:46 -0700 | [diff] [blame] | 2065 | static size_t |
| 2066 | arena_run_size_get(arena_t *arena, arena_chunk_t *chunk, arena_run_t *run, |
| 2067 | size_t run_ind) |
| 2068 | { |
| 2069 | size_t size; |
| 2070 | |
| 2071 | assert(run_ind >= map_bias); |
| 2072 | assert(run_ind < chunk_npages); |
| 2073 | |
| 2074 | if (arena_mapbits_large_get(chunk, run_ind) != 0) { |
| 2075 | size = arena_mapbits_large_size_get(chunk, run_ind); |
| 2076 | assert(size == PAGE || arena_mapbits_large_size_get(chunk, |
| 2077 | run_ind+(size>>LG_PAGE)-1) == 0); |
| 2078 | } else { |
| 2079 | arena_bin_info_t *bin_info = &arena_bin_info[run->binind]; |
| 2080 | size = bin_info->run_size; |
| 2081 | } |
| 2082 | |
| 2083 | return (size); |
| 2084 | } |
| 2085 | |
Jason Evans | aa5113b | 2014-01-14 16:23:03 -0800 | [diff] [blame] | 2086 | static void |
Jason Evans | b2c0d63 | 2016-04-13 23:36:15 -0700 | [diff] [blame] | 2087 | arena_run_dalloc(tsd_t *tsd, arena_t *arena, arena_run_t *run, bool dirty, |
| 2088 | bool cleaned, bool decommitted) |
Jason Evans | aa5113b | 2014-01-14 16:23:03 -0800 | [diff] [blame] | 2089 | { |
| 2090 | arena_chunk_t *chunk; |
Jason Evans | 0c5dd03 | 2014-09-29 01:31:39 -0700 | [diff] [blame] | 2091 | arena_chunk_map_misc_t *miscelm; |
Jason Evans | 8fadb1a | 2015-08-04 10:49:46 -0700 | [diff] [blame] | 2092 | size_t size, run_ind, run_pages, flag_dirty, flag_decommitted; |
Jason Evans | aa5113b | 2014-01-14 16:23:03 -0800 | [diff] [blame] | 2093 | |
| 2094 | chunk = (arena_chunk_t *)CHUNK_ADDR2BASE(run); |
Jason Evans | 0c5dd03 | 2014-09-29 01:31:39 -0700 | [diff] [blame] | 2095 | miscelm = arena_run_to_miscelm(run); |
| 2096 | run_ind = arena_miscelm_to_pageind(miscelm); |
Jason Evans | aa5113b | 2014-01-14 16:23:03 -0800 | [diff] [blame] | 2097 | assert(run_ind >= map_bias); |
| 2098 | assert(run_ind < chunk_npages); |
Jason Evans | 8fadb1a | 2015-08-04 10:49:46 -0700 | [diff] [blame] | 2099 | size = arena_run_size_get(arena, chunk, run, run_ind); |
Jason Evans | aa5113b | 2014-01-14 16:23:03 -0800 | [diff] [blame] | 2100 | run_pages = (size >> LG_PAGE); |
Jason Evans | 40ee9aa | 2016-02-27 12:34:50 -0800 | [diff] [blame] | 2101 | arena_nactive_sub(arena, run_pages); |
Jason Evans | aa5113b | 2014-01-14 16:23:03 -0800 | [diff] [blame] | 2102 | |
| 2103 | /* |
| 2104 | * The run is dirty if the caller claims to have dirtied it, as well as |
| 2105 | * if it was already dirty before being allocated and the caller |
| 2106 | * doesn't claim to have cleaned it. |
| 2107 | */ |
| 2108 | assert(arena_mapbits_dirty_get(chunk, run_ind) == |
| 2109 | arena_mapbits_dirty_get(chunk, run_ind+run_pages-1)); |
Jason Evans | 8fadb1a | 2015-08-04 10:49:46 -0700 | [diff] [blame] | 2110 | if (!cleaned && !decommitted && arena_mapbits_dirty_get(chunk, run_ind) |
| 2111 | != 0) |
Jason Evans | aa5113b | 2014-01-14 16:23:03 -0800 | [diff] [blame] | 2112 | dirty = true; |
| 2113 | flag_dirty = dirty ? CHUNK_MAP_DIRTY : 0; |
Jason Evans | 8fadb1a | 2015-08-04 10:49:46 -0700 | [diff] [blame] | 2114 | flag_decommitted = decommitted ? CHUNK_MAP_DECOMMITTED : 0; |
Jason Evans | aa5113b | 2014-01-14 16:23:03 -0800 | [diff] [blame] | 2115 | |
| 2116 | /* Mark pages as unallocated in the chunk map. */ |
Jason Evans | 8fadb1a | 2015-08-04 10:49:46 -0700 | [diff] [blame] | 2117 | if (dirty || decommitted) { |
| 2118 | size_t flags = flag_dirty | flag_decommitted; |
| 2119 | arena_mapbits_unallocated_set(chunk, run_ind, size, flags); |
Jason Evans | aa5113b | 2014-01-14 16:23:03 -0800 | [diff] [blame] | 2120 | arena_mapbits_unallocated_set(chunk, run_ind+run_pages-1, size, |
Jason Evans | 8fadb1a | 2015-08-04 10:49:46 -0700 | [diff] [blame] | 2121 | flags); |
Jason Evans | aa5113b | 2014-01-14 16:23:03 -0800 | [diff] [blame] | 2122 | } else { |
| 2123 | arena_mapbits_unallocated_set(chunk, run_ind, size, |
| 2124 | arena_mapbits_unzeroed_get(chunk, run_ind)); |
| 2125 | arena_mapbits_unallocated_set(chunk, run_ind+run_pages-1, size, |
| 2126 | arena_mapbits_unzeroed_get(chunk, run_ind+run_pages-1)); |
| 2127 | } |
| 2128 | |
Jason Evans | 8fadb1a | 2015-08-04 10:49:46 -0700 | [diff] [blame] | 2129 | arena_run_coalesce(arena, chunk, &size, &run_ind, &run_pages, |
| 2130 | flag_dirty, flag_decommitted); |
Jason Evans | aa5113b | 2014-01-14 16:23:03 -0800 | [diff] [blame] | 2131 | |
Jason Evans | e476f8a | 2010-01-16 09:53:50 -0800 | [diff] [blame] | 2132 | /* Insert into runs_avail, now that coalescing is complete. */ |
Jason Evans | 203484e | 2012-05-02 00:30:36 -0700 | [diff] [blame] | 2133 | assert(arena_mapbits_unallocated_size_get(chunk, run_ind) == |
| 2134 | arena_mapbits_unallocated_size_get(chunk, run_ind+run_pages-1)); |
| 2135 | assert(arena_mapbits_dirty_get(chunk, run_ind) == |
| 2136 | arena_mapbits_dirty_get(chunk, run_ind+run_pages-1)); |
Jason Evans | 8fadb1a | 2015-08-04 10:49:46 -0700 | [diff] [blame] | 2137 | assert(arena_mapbits_decommitted_get(chunk, run_ind) == |
| 2138 | arena_mapbits_decommitted_get(chunk, run_ind+run_pages-1)); |
Qinfan Wu | 90737fc | 2014-07-21 19:39:20 -0700 | [diff] [blame] | 2139 | arena_avail_insert(arena, chunk, run_ind, run_pages); |
Jason Evans | 8d4203c | 2010-04-13 20:53:21 -0700 | [diff] [blame] | 2140 | |
Jason Evans | 070b3c3 | 2014-08-14 14:45:58 -0700 | [diff] [blame] | 2141 | if (dirty) |
Jason Evans | ee41ad4 | 2015-02-15 18:04:46 -0800 | [diff] [blame] | 2142 | arena_run_dirty_insert(arena, chunk, run_ind, run_pages); |
Qinfan Wu | 04d60a1 | 2014-07-18 14:21:17 -0700 | [diff] [blame] | 2143 | |
Jason Evans | 203484e | 2012-05-02 00:30:36 -0700 | [diff] [blame] | 2144 | /* Deallocate chunk if it is now completely unused. */ |
Jason Evans | 155bfa7 | 2014-10-05 17:54:10 -0700 | [diff] [blame] | 2145 | if (size == arena_maxrun) { |
Jason Evans | 203484e | 2012-05-02 00:30:36 -0700 | [diff] [blame] | 2146 | assert(run_ind == map_bias); |
Jason Evans | 155bfa7 | 2014-10-05 17:54:10 -0700 | [diff] [blame] | 2147 | assert(run_pages == (arena_maxrun >> LG_PAGE)); |
Jason Evans | b2c0d63 | 2016-04-13 23:36:15 -0700 | [diff] [blame] | 2148 | arena_chunk_dalloc(tsd, arena, chunk); |
Jason Evans | 203484e | 2012-05-02 00:30:36 -0700 | [diff] [blame] | 2149 | } |
Jason Evans | e476f8a | 2010-01-16 09:53:50 -0800 | [diff] [blame] | 2150 | |
Jason Evans | 4fb7f51 | 2010-01-27 18:27:09 -0800 | [diff] [blame] | 2151 | /* |
Jason Evans | 8d4203c | 2010-04-13 20:53:21 -0700 | [diff] [blame] | 2152 | * It is okay to do dirty page processing here even if the chunk was |
Jason Evans | 4fb7f51 | 2010-01-27 18:27:09 -0800 | [diff] [blame] | 2153 | * deallocated above, since in that case it is the spare. Waiting |
| 2154 | * until after possible chunk deallocation to do dirty processing |
| 2155 | * allows for an old spare to be fully deallocated, thus decreasing the |
| 2156 | * chances of spuriously crossing the dirty page purging threshold. |
| 2157 | */ |
Jason Evans | 8d4203c | 2010-04-13 20:53:21 -0700 | [diff] [blame] | 2158 | if (dirty) |
Jason Evans | b2c0d63 | 2016-04-13 23:36:15 -0700 | [diff] [blame] | 2159 | arena_maybe_purge(tsd, arena); |
Jason Evans | e476f8a | 2010-01-16 09:53:50 -0800 | [diff] [blame] | 2160 | } |
| 2161 | |
| 2162 | static void |
Jason Evans | b2c0d63 | 2016-04-13 23:36:15 -0700 | [diff] [blame] | 2163 | arena_run_trim_head(tsd_t *tsd, arena_t *arena, arena_chunk_t *chunk, |
| 2164 | arena_run_t *run, size_t oldsize, size_t newsize) |
Jason Evans | e476f8a | 2010-01-16 09:53:50 -0800 | [diff] [blame] | 2165 | { |
Jason Evans | 0c5dd03 | 2014-09-29 01:31:39 -0700 | [diff] [blame] | 2166 | arena_chunk_map_misc_t *miscelm = arena_run_to_miscelm(run); |
| 2167 | size_t pageind = arena_miscelm_to_pageind(miscelm); |
Jason Evans | ae4c7b4 | 2012-04-02 07:04:34 -0700 | [diff] [blame] | 2168 | size_t head_npages = (oldsize - newsize) >> LG_PAGE; |
Jason Evans | 203484e | 2012-05-02 00:30:36 -0700 | [diff] [blame] | 2169 | size_t flag_dirty = arena_mapbits_dirty_get(chunk, pageind); |
Jason Evans | 1f27abc | 2015-08-11 12:42:33 -0700 | [diff] [blame] | 2170 | size_t flag_decommitted = arena_mapbits_decommitted_get(chunk, pageind); |
| 2171 | size_t flag_unzeroed_mask = (flag_dirty | flag_decommitted) == 0 ? |
| 2172 | CHUNK_MAP_UNZEROED : 0; |
Jason Evans | e476f8a | 2010-01-16 09:53:50 -0800 | [diff] [blame] | 2173 | |
| 2174 | assert(oldsize > newsize); |
| 2175 | |
| 2176 | /* |
| 2177 | * Update the chunk map so that arena_run_dalloc() can treat the |
Jason Evans | 940a2e0 | 2010-10-17 17:51:37 -0700 | [diff] [blame] | 2178 | * leading run as separately allocated. Set the last element of each |
| 2179 | * run first, in case of single-page runs. |
Jason Evans | e476f8a | 2010-01-16 09:53:50 -0800 | [diff] [blame] | 2180 | */ |
Jason Evans | 203484e | 2012-05-02 00:30:36 -0700 | [diff] [blame] | 2181 | assert(arena_mapbits_large_size_get(chunk, pageind) == oldsize); |
Jason Evans | 1f27abc | 2015-08-11 12:42:33 -0700 | [diff] [blame] | 2182 | arena_mapbits_large_set(chunk, pageind+head_npages-1, 0, flag_dirty | |
| 2183 | (flag_unzeroed_mask & arena_mapbits_unzeroed_get(chunk, |
| 2184 | pageind+head_npages-1))); |
| 2185 | arena_mapbits_large_set(chunk, pageind, oldsize-newsize, flag_dirty | |
| 2186 | (flag_unzeroed_mask & arena_mapbits_unzeroed_get(chunk, pageind))); |
Jason Evans | 940a2e0 | 2010-10-17 17:51:37 -0700 | [diff] [blame] | 2187 | |
Jason Evans | 7372b15 | 2012-02-10 20:22:09 -0800 | [diff] [blame] | 2188 | if (config_debug) { |
Jason Evans | ae4c7b4 | 2012-04-02 07:04:34 -0700 | [diff] [blame] | 2189 | UNUSED size_t tail_npages = newsize >> LG_PAGE; |
Jason Evans | 203484e | 2012-05-02 00:30:36 -0700 | [diff] [blame] | 2190 | assert(arena_mapbits_large_size_get(chunk, |
| 2191 | pageind+head_npages+tail_npages-1) == 0); |
| 2192 | assert(arena_mapbits_dirty_get(chunk, |
| 2193 | pageind+head_npages+tail_npages-1) == flag_dirty); |
Jason Evans | 940a2e0 | 2010-10-17 17:51:37 -0700 | [diff] [blame] | 2194 | } |
Jason Evans | d8ceef6 | 2012-05-10 20:59:39 -0700 | [diff] [blame] | 2195 | arena_mapbits_large_set(chunk, pageind+head_npages, newsize, |
Jason Evans | 1f27abc | 2015-08-11 12:42:33 -0700 | [diff] [blame] | 2196 | flag_dirty | (flag_unzeroed_mask & arena_mapbits_unzeroed_get(chunk, |
| 2197 | pageind+head_npages))); |
Jason Evans | e476f8a | 2010-01-16 09:53:50 -0800 | [diff] [blame] | 2198 | |
Jason Evans | b2c0d63 | 2016-04-13 23:36:15 -0700 | [diff] [blame] | 2199 | arena_run_dalloc(tsd, arena, run, false, false, (flag_decommitted != |
| 2200 | 0)); |
Jason Evans | e476f8a | 2010-01-16 09:53:50 -0800 | [diff] [blame] | 2201 | } |
| 2202 | |
| 2203 | static void |
Jason Evans | b2c0d63 | 2016-04-13 23:36:15 -0700 | [diff] [blame] | 2204 | arena_run_trim_tail(tsd_t *tsd, arena_t *arena, arena_chunk_t *chunk, |
| 2205 | arena_run_t *run, size_t oldsize, size_t newsize, bool dirty) |
Jason Evans | e476f8a | 2010-01-16 09:53:50 -0800 | [diff] [blame] | 2206 | { |
Jason Evans | 0c5dd03 | 2014-09-29 01:31:39 -0700 | [diff] [blame] | 2207 | arena_chunk_map_misc_t *miscelm = arena_run_to_miscelm(run); |
| 2208 | size_t pageind = arena_miscelm_to_pageind(miscelm); |
Jason Evans | ae4c7b4 | 2012-04-02 07:04:34 -0700 | [diff] [blame] | 2209 | size_t head_npages = newsize >> LG_PAGE; |
Jason Evans | 203484e | 2012-05-02 00:30:36 -0700 | [diff] [blame] | 2210 | size_t flag_dirty = arena_mapbits_dirty_get(chunk, pageind); |
Jason Evans | 1f27abc | 2015-08-11 12:42:33 -0700 | [diff] [blame] | 2211 | size_t flag_decommitted = arena_mapbits_decommitted_get(chunk, pageind); |
| 2212 | size_t flag_unzeroed_mask = (flag_dirty | flag_decommitted) == 0 ? |
| 2213 | CHUNK_MAP_UNZEROED : 0; |
Jason Evans | 0c5dd03 | 2014-09-29 01:31:39 -0700 | [diff] [blame] | 2214 | arena_chunk_map_misc_t *tail_miscelm; |
| 2215 | arena_run_t *tail_run; |
Jason Evans | e476f8a | 2010-01-16 09:53:50 -0800 | [diff] [blame] | 2216 | |
| 2217 | assert(oldsize > newsize); |
| 2218 | |
| 2219 | /* |
| 2220 | * Update the chunk map so that arena_run_dalloc() can treat the |
Jason Evans | 940a2e0 | 2010-10-17 17:51:37 -0700 | [diff] [blame] | 2221 | * trailing run as separately allocated. Set the last element of each |
| 2222 | * run first, in case of single-page runs. |
Jason Evans | e476f8a | 2010-01-16 09:53:50 -0800 | [diff] [blame] | 2223 | */ |
Jason Evans | 203484e | 2012-05-02 00:30:36 -0700 | [diff] [blame] | 2224 | assert(arena_mapbits_large_size_get(chunk, pageind) == oldsize); |
Jason Evans | 1f27abc | 2015-08-11 12:42:33 -0700 | [diff] [blame] | 2225 | arena_mapbits_large_set(chunk, pageind+head_npages-1, 0, flag_dirty | |
| 2226 | (flag_unzeroed_mask & arena_mapbits_unzeroed_get(chunk, |
| 2227 | pageind+head_npages-1))); |
| 2228 | arena_mapbits_large_set(chunk, pageind, newsize, flag_dirty | |
| 2229 | (flag_unzeroed_mask & arena_mapbits_unzeroed_get(chunk, pageind))); |
Jason Evans | 940a2e0 | 2010-10-17 17:51:37 -0700 | [diff] [blame] | 2230 | |
Jason Evans | 203484e | 2012-05-02 00:30:36 -0700 | [diff] [blame] | 2231 | if (config_debug) { |
| 2232 | UNUSED size_t tail_npages = (oldsize - newsize) >> LG_PAGE; |
| 2233 | assert(arena_mapbits_large_size_get(chunk, |
| 2234 | pageind+head_npages+tail_npages-1) == 0); |
| 2235 | assert(arena_mapbits_dirty_get(chunk, |
| 2236 | pageind+head_npages+tail_npages-1) == flag_dirty); |
| 2237 | } |
| 2238 | arena_mapbits_large_set(chunk, pageind+head_npages, oldsize-newsize, |
Jason Evans | 1f27abc | 2015-08-11 12:42:33 -0700 | [diff] [blame] | 2239 | flag_dirty | (flag_unzeroed_mask & arena_mapbits_unzeroed_get(chunk, |
| 2240 | pageind+head_npages))); |
Jason Evans | e476f8a | 2010-01-16 09:53:50 -0800 | [diff] [blame] | 2241 | |
Jason Evans | 61a6dfc | 2016-03-23 16:04:38 -0700 | [diff] [blame] | 2242 | tail_miscelm = arena_miscelm_get_mutable(chunk, pageind + head_npages); |
Jason Evans | 0c5dd03 | 2014-09-29 01:31:39 -0700 | [diff] [blame] | 2243 | tail_run = &tail_miscelm->run; |
Jason Evans | b2c0d63 | 2016-04-13 23:36:15 -0700 | [diff] [blame] | 2244 | arena_run_dalloc(tsd, arena, tail_run, dirty, false, (flag_decommitted |
| 2245 | != 0)); |
Jason Evans | e476f8a | 2010-01-16 09:53:50 -0800 | [diff] [blame] | 2246 | } |
| 2247 | |
Jason Evans | e7a1058 | 2012-02-13 17:36:52 -0800 | [diff] [blame] | 2248 | static void |
| 2249 | arena_bin_runs_insert(arena_bin_t *bin, arena_run_t *run) |
| 2250 | { |
Jason Evans | 0c5dd03 | 2014-09-29 01:31:39 -0700 | [diff] [blame] | 2251 | arena_chunk_map_misc_t *miscelm = arena_run_to_miscelm(run); |
Jason Evans | e7a1058 | 2012-02-13 17:36:52 -0800 | [diff] [blame] | 2252 | |
Jason Evans | c6a2c39 | 2016-03-26 17:30:37 -0700 | [diff] [blame] | 2253 | arena_run_heap_insert(&bin->runs, miscelm); |
Jason Evans | e7a1058 | 2012-02-13 17:36:52 -0800 | [diff] [blame] | 2254 | } |
| 2255 | |
| 2256 | static arena_run_t * |
| 2257 | arena_bin_nonfull_run_tryget(arena_bin_t *bin) |
| 2258 | { |
Jason Evans | c6a2c39 | 2016-03-26 17:30:37 -0700 | [diff] [blame] | 2259 | arena_chunk_map_misc_t *miscelm; |
| 2260 | |
| 2261 | miscelm = arena_run_heap_remove_first(&bin->runs); |
| 2262 | if (miscelm == NULL) |
| 2263 | return (NULL); |
| 2264 | if (config_stats) |
| 2265 | bin->stats.reruns++; |
| 2266 | |
| 2267 | return (&miscelm->run); |
Jason Evans | e7a1058 | 2012-02-13 17:36:52 -0800 | [diff] [blame] | 2268 | } |
| 2269 | |
| 2270 | static arena_run_t * |
Jason Evans | b2c0d63 | 2016-04-13 23:36:15 -0700 | [diff] [blame] | 2271 | arena_bin_nonfull_run_get(tsd_t *tsd, arena_t *arena, arena_bin_t *bin) |
Jason Evans | e476f8a | 2010-01-16 09:53:50 -0800 | [diff] [blame] | 2272 | { |
Jason Evans | e476f8a | 2010-01-16 09:53:50 -0800 | [diff] [blame] | 2273 | arena_run_t *run; |
Jason Evans | d01fd19 | 2015-08-19 15:21:32 -0700 | [diff] [blame] | 2274 | szind_t binind; |
Jason Evans | 49f7e8f | 2011-03-15 13:59:15 -0700 | [diff] [blame] | 2275 | arena_bin_info_t *bin_info; |
Jason Evans | e476f8a | 2010-01-16 09:53:50 -0800 | [diff] [blame] | 2276 | |
| 2277 | /* Look for a usable run. */ |
Jason Evans | e7a1058 | 2012-02-13 17:36:52 -0800 | [diff] [blame] | 2278 | run = arena_bin_nonfull_run_tryget(bin); |
| 2279 | if (run != NULL) |
Jason Evans | e476f8a | 2010-01-16 09:53:50 -0800 | [diff] [blame] | 2280 | return (run); |
Jason Evans | e476f8a | 2010-01-16 09:53:50 -0800 | [diff] [blame] | 2281 | /* No existing runs have any space available. */ |
| 2282 | |
Jason Evans | 49f7e8f | 2011-03-15 13:59:15 -0700 | [diff] [blame] | 2283 | binind = arena_bin_index(arena, bin); |
| 2284 | bin_info = &arena_bin_info[binind]; |
| 2285 | |
Jason Evans | e476f8a | 2010-01-16 09:53:50 -0800 | [diff] [blame] | 2286 | /* Allocate a new run. */ |
Jason Evans | b2c0d63 | 2016-04-13 23:36:15 -0700 | [diff] [blame] | 2287 | malloc_mutex_unlock(tsd, &bin->lock); |
Jason Evans | e69bee0 | 2010-03-15 22:25:23 -0700 | [diff] [blame] | 2288 | /******************************/ |
Jason Evans | b2c0d63 | 2016-04-13 23:36:15 -0700 | [diff] [blame] | 2289 | malloc_mutex_lock(tsd, &arena->lock); |
| 2290 | run = arena_run_alloc_small(tsd, arena, bin_info->run_size, binind); |
Jason Evans | e00572b | 2010-03-14 19:43:56 -0700 | [diff] [blame] | 2291 | if (run != NULL) { |
| 2292 | /* Initialize run internals. */ |
Jason Evans | 381c23d | 2014-10-10 23:01:03 -0700 | [diff] [blame] | 2293 | run->binind = binind; |
Jason Evans | 49f7e8f | 2011-03-15 13:59:15 -0700 | [diff] [blame] | 2294 | run->nfree = bin_info->nregs; |
Jason Evans | 0c5dd03 | 2014-09-29 01:31:39 -0700 | [diff] [blame] | 2295 | bitmap_init(run->bitmap, &bin_info->bitmap_info); |
Jason Evans | e69bee0 | 2010-03-15 22:25:23 -0700 | [diff] [blame] | 2296 | } |
Jason Evans | b2c0d63 | 2016-04-13 23:36:15 -0700 | [diff] [blame] | 2297 | malloc_mutex_unlock(tsd, &arena->lock); |
Jason Evans | e69bee0 | 2010-03-15 22:25:23 -0700 | [diff] [blame] | 2298 | /********************************/ |
Jason Evans | b2c0d63 | 2016-04-13 23:36:15 -0700 | [diff] [blame] | 2299 | malloc_mutex_lock(tsd, &bin->lock); |
Jason Evans | e69bee0 | 2010-03-15 22:25:23 -0700 | [diff] [blame] | 2300 | if (run != NULL) { |
Jason Evans | 7372b15 | 2012-02-10 20:22:09 -0800 | [diff] [blame] | 2301 | if (config_stats) { |
| 2302 | bin->stats.nruns++; |
| 2303 | bin->stats.curruns++; |
Jason Evans | 7372b15 | 2012-02-10 20:22:09 -0800 | [diff] [blame] | 2304 | } |
Jason Evans | e00572b | 2010-03-14 19:43:56 -0700 | [diff] [blame] | 2305 | return (run); |
| 2306 | } |
| 2307 | |
| 2308 | /* |
Jason Evans | aa5113b | 2014-01-14 16:23:03 -0800 | [diff] [blame] | 2309 | * arena_run_alloc_small() failed, but another thread may have made |
Jason Evans | 940a2e0 | 2010-10-17 17:51:37 -0700 | [diff] [blame] | 2310 | * sufficient memory available while this one dropped bin->lock above, |
Jason Evans | e00572b | 2010-03-14 19:43:56 -0700 | [diff] [blame] | 2311 | * so search one more time. |
| 2312 | */ |
Jason Evans | e7a1058 | 2012-02-13 17:36:52 -0800 | [diff] [blame] | 2313 | run = arena_bin_nonfull_run_tryget(bin); |
| 2314 | if (run != NULL) |
Jason Evans | e00572b | 2010-03-14 19:43:56 -0700 | [diff] [blame] | 2315 | return (run); |
Jason Evans | e00572b | 2010-03-14 19:43:56 -0700 | [diff] [blame] | 2316 | |
| 2317 | return (NULL); |
Jason Evans | e476f8a | 2010-01-16 09:53:50 -0800 | [diff] [blame] | 2318 | } |
| 2319 | |
Jason Evans | 1e0a636 | 2010-03-13 13:41:58 -0800 | [diff] [blame] | 2320 | /* Re-fill bin->runcur, then call arena_run_reg_alloc(). */ |
Jason Evans | e476f8a | 2010-01-16 09:53:50 -0800 | [diff] [blame] | 2321 | static void * |
Jason Evans | b2c0d63 | 2016-04-13 23:36:15 -0700 | [diff] [blame] | 2322 | arena_bin_malloc_hard(tsd_t *tsd, arena_t *arena, arena_bin_t *bin) |
Jason Evans | e476f8a | 2010-01-16 09:53:50 -0800 | [diff] [blame] | 2323 | { |
Jason Evans | d01fd19 | 2015-08-19 15:21:32 -0700 | [diff] [blame] | 2324 | szind_t binind; |
Jason Evans | 49f7e8f | 2011-03-15 13:59:15 -0700 | [diff] [blame] | 2325 | arena_bin_info_t *bin_info; |
Jason Evans | e00572b | 2010-03-14 19:43:56 -0700 | [diff] [blame] | 2326 | arena_run_t *run; |
Jason Evans | e476f8a | 2010-01-16 09:53:50 -0800 | [diff] [blame] | 2327 | |
Jason Evans | 49f7e8f | 2011-03-15 13:59:15 -0700 | [diff] [blame] | 2328 | binind = arena_bin_index(arena, bin); |
| 2329 | bin_info = &arena_bin_info[binind]; |
Jason Evans | e00572b | 2010-03-14 19:43:56 -0700 | [diff] [blame] | 2330 | bin->runcur = NULL; |
Jason Evans | b2c0d63 | 2016-04-13 23:36:15 -0700 | [diff] [blame] | 2331 | run = arena_bin_nonfull_run_get(tsd, arena, bin); |
Jason Evans | e00572b | 2010-03-14 19:43:56 -0700 | [diff] [blame] | 2332 | if (bin->runcur != NULL && bin->runcur->nfree > 0) { |
| 2333 | /* |
| 2334 | * Another thread updated runcur while this one ran without the |
| 2335 | * bin lock in arena_bin_nonfull_run_get(). |
| 2336 | */ |
Dmitry-Me | a306a60 | 2015-09-04 13:15:28 +0300 | [diff] [blame] | 2337 | void *ret; |
Jason Evans | e00572b | 2010-03-14 19:43:56 -0700 | [diff] [blame] | 2338 | assert(bin->runcur->nfree > 0); |
Jason Evans | 49f7e8f | 2011-03-15 13:59:15 -0700 | [diff] [blame] | 2339 | ret = arena_run_reg_alloc(bin->runcur, bin_info); |
Jason Evans | e00572b | 2010-03-14 19:43:56 -0700 | [diff] [blame] | 2340 | if (run != NULL) { |
Jason Evans | 940a2e0 | 2010-10-17 17:51:37 -0700 | [diff] [blame] | 2341 | arena_chunk_t *chunk; |
| 2342 | |
| 2343 | /* |
Jason Evans | aa5113b | 2014-01-14 16:23:03 -0800 | [diff] [blame] | 2344 | * arena_run_alloc_small() may have allocated run, or |
| 2345 | * it may have pulled run from the bin's run tree. |
| 2346 | * Therefore it is unsafe to make any assumptions about |
| 2347 | * how run has previously been used, and |
| 2348 | * arena_bin_lower_run() must be called, as if a region |
| 2349 | * were just deallocated from the run. |
Jason Evans | 940a2e0 | 2010-10-17 17:51:37 -0700 | [diff] [blame] | 2350 | */ |
| 2351 | chunk = (arena_chunk_t *)CHUNK_ADDR2BASE(run); |
Jason Evans | b2c0d63 | 2016-04-13 23:36:15 -0700 | [diff] [blame] | 2352 | if (run->nfree == bin_info->nregs) { |
| 2353 | arena_dalloc_bin_run(tsd, arena, chunk, run, |
| 2354 | bin); |
| 2355 | } else |
Jason Evans | 8de6a02 | 2010-10-17 20:57:30 -0700 | [diff] [blame] | 2356 | arena_bin_lower_run(arena, chunk, run, bin); |
Jason Evans | e00572b | 2010-03-14 19:43:56 -0700 | [diff] [blame] | 2357 | } |
| 2358 | return (ret); |
| 2359 | } |
| 2360 | |
| 2361 | if (run == NULL) |
Jason Evans | e476f8a | 2010-01-16 09:53:50 -0800 | [diff] [blame] | 2362 | return (NULL); |
Jason Evans | e00572b | 2010-03-14 19:43:56 -0700 | [diff] [blame] | 2363 | |
| 2364 | bin->runcur = run; |
| 2365 | |
Jason Evans | e476f8a | 2010-01-16 09:53:50 -0800 | [diff] [blame] | 2366 | assert(bin->runcur->nfree > 0); |
| 2367 | |
Jason Evans | 49f7e8f | 2011-03-15 13:59:15 -0700 | [diff] [blame] | 2368 | return (arena_run_reg_alloc(bin->runcur, bin_info)); |
Jason Evans | e476f8a | 2010-01-16 09:53:50 -0800 | [diff] [blame] | 2369 | } |
| 2370 | |
Jason Evans | 86815df | 2010-03-13 20:32:56 -0800 | [diff] [blame] | 2371 | void |
Jason Evans | 243f7a0 | 2016-02-19 20:09:31 -0800 | [diff] [blame] | 2372 | arena_tcache_fill_small(tsd_t *tsd, arena_t *arena, tcache_bin_t *tbin, |
| 2373 | szind_t binind, uint64_t prof_accumbytes) |
Jason Evans | e476f8a | 2010-01-16 09:53:50 -0800 | [diff] [blame] | 2374 | { |
| 2375 | unsigned i, nfill; |
| 2376 | arena_bin_t *bin; |
Jason Evans | e476f8a | 2010-01-16 09:53:50 -0800 | [diff] [blame] | 2377 | |
| 2378 | assert(tbin->ncached == 0); |
| 2379 | |
Jason Evans | b2c0d63 | 2016-04-13 23:36:15 -0700 | [diff] [blame] | 2380 | if (config_prof && arena_prof_accum(tsd, arena, prof_accumbytes)) |
| 2381 | prof_idump(tsd); |
Jason Evans | e69bee0 | 2010-03-15 22:25:23 -0700 | [diff] [blame] | 2382 | bin = &arena->bins[binind]; |
Jason Evans | b2c0d63 | 2016-04-13 23:36:15 -0700 | [diff] [blame] | 2383 | malloc_mutex_lock(tsd, &bin->lock); |
Jason Evans | 1dcb4f8 | 2011-03-21 00:18:17 -0700 | [diff] [blame] | 2384 | for (i = 0, nfill = (tcache_bin_info[binind].ncached_max >> |
| 2385 | tbin->lg_fill_div); i < nfill; i++) { |
Dmitry-Me | a306a60 | 2015-09-04 13:15:28 +0300 | [diff] [blame] | 2386 | arena_run_t *run; |
| 2387 | void *ptr; |
Jason Evans | e476f8a | 2010-01-16 09:53:50 -0800 | [diff] [blame] | 2388 | if ((run = bin->runcur) != NULL && run->nfree > 0) |
Jason Evans | 49f7e8f | 2011-03-15 13:59:15 -0700 | [diff] [blame] | 2389 | ptr = arena_run_reg_alloc(run, &arena_bin_info[binind]); |
Jason Evans | e476f8a | 2010-01-16 09:53:50 -0800 | [diff] [blame] | 2390 | else |
Jason Evans | b2c0d63 | 2016-04-13 23:36:15 -0700 | [diff] [blame] | 2391 | ptr = arena_bin_malloc_hard(tsd, arena, bin); |
Jason Evans | f11a677 | 2014-10-05 13:05:10 -0700 | [diff] [blame] | 2392 | if (ptr == NULL) { |
| 2393 | /* |
| 2394 | * OOM. tbin->avail isn't yet filled down to its first |
| 2395 | * element, so the successful allocations (if any) must |
Qi Wang | f4a0f32 | 2015-10-27 15:12:10 -0700 | [diff] [blame] | 2396 | * be moved just before tbin->avail before bailing out. |
Jason Evans | f11a677 | 2014-10-05 13:05:10 -0700 | [diff] [blame] | 2397 | */ |
| 2398 | if (i > 0) { |
Qi Wang | f4a0f32 | 2015-10-27 15:12:10 -0700 | [diff] [blame] | 2399 | memmove(tbin->avail - i, tbin->avail - nfill, |
Jason Evans | f11a677 | 2014-10-05 13:05:10 -0700 | [diff] [blame] | 2400 | i * sizeof(void *)); |
| 2401 | } |
Jason Evans | e476f8a | 2010-01-16 09:53:50 -0800 | [diff] [blame] | 2402 | break; |
Jason Evans | f11a677 | 2014-10-05 13:05:10 -0700 | [diff] [blame] | 2403 | } |
Guilherme Goncalves | 2c5cb61 | 2014-12-08 19:12:41 -0200 | [diff] [blame] | 2404 | if (config_fill && unlikely(opt_junk_alloc)) { |
Jason Evans | 122449b | 2012-04-06 00:35:09 -0700 | [diff] [blame] | 2405 | arena_alloc_junk_small(ptr, &arena_bin_info[binind], |
| 2406 | true); |
| 2407 | } |
Jason Evans | 9c43c13 | 2011-03-18 10:53:15 -0700 | [diff] [blame] | 2408 | /* Insert such that low regions get used first. */ |
Qi Wang | f4a0f32 | 2015-10-27 15:12:10 -0700 | [diff] [blame] | 2409 | *(tbin->avail - nfill + i) = ptr; |
Jason Evans | e476f8a | 2010-01-16 09:53:50 -0800 | [diff] [blame] | 2410 | } |
Jason Evans | 7372b15 | 2012-02-10 20:22:09 -0800 | [diff] [blame] | 2411 | if (config_stats) { |
Jason Evans | 7372b15 | 2012-02-10 20:22:09 -0800 | [diff] [blame] | 2412 | bin->stats.nmalloc += i; |
| 2413 | bin->stats.nrequests += tbin->tstats.nrequests; |
Jason Evans | 3c4d92e | 2014-10-12 22:53:59 -0700 | [diff] [blame] | 2414 | bin->stats.curregs += i; |
Jason Evans | 7372b15 | 2012-02-10 20:22:09 -0800 | [diff] [blame] | 2415 | bin->stats.nfills++; |
| 2416 | tbin->tstats.nrequests = 0; |
| 2417 | } |
Jason Evans | b2c0d63 | 2016-04-13 23:36:15 -0700 | [diff] [blame] | 2418 | malloc_mutex_unlock(tsd, &bin->lock); |
Jason Evans | e476f8a | 2010-01-16 09:53:50 -0800 | [diff] [blame] | 2419 | tbin->ncached = i; |
Jason Evans | 243f7a0 | 2016-02-19 20:09:31 -0800 | [diff] [blame] | 2420 | arena_decay_tick(tsd, arena); |
Jason Evans | e476f8a | 2010-01-16 09:53:50 -0800 | [diff] [blame] | 2421 | } |
Jason Evans | e476f8a | 2010-01-16 09:53:50 -0800 | [diff] [blame] | 2422 | |
Jason Evans | 122449b | 2012-04-06 00:35:09 -0700 | [diff] [blame] | 2423 | void |
| 2424 | arena_alloc_junk_small(void *ptr, arena_bin_info_t *bin_info, bool zero) |
| 2425 | { |
| 2426 | |
Chris Peterson | a82070e | 2016-03-27 23:28:39 -0700 | [diff] [blame] | 2427 | size_t redzone_size = bin_info->redzone_size; |
| 2428 | |
Jason Evans | 122449b | 2012-04-06 00:35:09 -0700 | [diff] [blame] | 2429 | if (zero) { |
Chris Peterson | a82070e | 2016-03-27 23:28:39 -0700 | [diff] [blame] | 2430 | memset((void *)((uintptr_t)ptr - redzone_size), |
| 2431 | JEMALLOC_ALLOC_JUNK, redzone_size); |
| 2432 | memset((void *)((uintptr_t)ptr + bin_info->reg_size), |
| 2433 | JEMALLOC_ALLOC_JUNK, redzone_size); |
Jason Evans | 122449b | 2012-04-06 00:35:09 -0700 | [diff] [blame] | 2434 | } else { |
Chris Peterson | a82070e | 2016-03-27 23:28:39 -0700 | [diff] [blame] | 2435 | memset((void *)((uintptr_t)ptr - redzone_size), |
| 2436 | JEMALLOC_ALLOC_JUNK, bin_info->reg_interval); |
Jason Evans | 122449b | 2012-04-06 00:35:09 -0700 | [diff] [blame] | 2437 | } |
| 2438 | } |
| 2439 | |
Jason Evans | 0d6c5d8 | 2013-12-17 15:14:36 -0800 | [diff] [blame] | 2440 | #ifdef JEMALLOC_JET |
| 2441 | #undef arena_redzone_corruption |
Jason Evans | ab0cfe0 | 2016-04-18 15:11:20 -0700 | [diff] [blame] | 2442 | #define arena_redzone_corruption JEMALLOC_N(n_arena_redzone_corruption) |
Jason Evans | 0d6c5d8 | 2013-12-17 15:14:36 -0800 | [diff] [blame] | 2443 | #endif |
| 2444 | static void |
| 2445 | arena_redzone_corruption(void *ptr, size_t usize, bool after, |
| 2446 | size_t offset, uint8_t byte) |
| 2447 | { |
| 2448 | |
Jason Evans | 5fae7dc | 2015-07-23 13:56:25 -0700 | [diff] [blame] | 2449 | malloc_printf("<jemalloc>: Corrupt redzone %zu byte%s %s %p " |
| 2450 | "(size %zu), byte=%#x\n", offset, (offset == 1) ? "" : "s", |
Jason Evans | 0d6c5d8 | 2013-12-17 15:14:36 -0800 | [diff] [blame] | 2451 | after ? "after" : "before", ptr, usize, byte); |
| 2452 | } |
| 2453 | #ifdef JEMALLOC_JET |
Jason Evans | 0d6c5d8 | 2013-12-17 15:14:36 -0800 | [diff] [blame] | 2454 | #undef arena_redzone_corruption |
Jason Evans | 6b694c4 | 2014-01-07 16:47:56 -0800 | [diff] [blame] | 2455 | #define arena_redzone_corruption JEMALLOC_N(arena_redzone_corruption) |
| 2456 | arena_redzone_corruption_t *arena_redzone_corruption = |
Jason Evans | ab0cfe0 | 2016-04-18 15:11:20 -0700 | [diff] [blame] | 2457 | JEMALLOC_N(n_arena_redzone_corruption); |
Jason Evans | 0d6c5d8 | 2013-12-17 15:14:36 -0800 | [diff] [blame] | 2458 | #endif |
| 2459 | |
| 2460 | static void |
| 2461 | arena_redzones_validate(void *ptr, arena_bin_info_t *bin_info, bool reset) |
Jason Evans | 122449b | 2012-04-06 00:35:09 -0700 | [diff] [blame] | 2462 | { |
Jason Evans | 122449b | 2012-04-06 00:35:09 -0700 | [diff] [blame] | 2463 | bool error = false; |
| 2464 | |
Guilherme Goncalves | 2c5cb61 | 2014-12-08 19:12:41 -0200 | [diff] [blame] | 2465 | if (opt_junk_alloc) { |
Dmitry-Me | a306a60 | 2015-09-04 13:15:28 +0300 | [diff] [blame] | 2466 | size_t size = bin_info->reg_size; |
| 2467 | size_t redzone_size = bin_info->redzone_size; |
| 2468 | size_t i; |
| 2469 | |
Guilherme Goncalves | 2c5cb61 | 2014-12-08 19:12:41 -0200 | [diff] [blame] | 2470 | for (i = 1; i <= redzone_size; i++) { |
| 2471 | uint8_t *byte = (uint8_t *)((uintptr_t)ptr - i); |
Chris Peterson | a82070e | 2016-03-27 23:28:39 -0700 | [diff] [blame] | 2472 | if (*byte != JEMALLOC_ALLOC_JUNK) { |
Guilherme Goncalves | 2c5cb61 | 2014-12-08 19:12:41 -0200 | [diff] [blame] | 2473 | error = true; |
Jason Evans | 8fadb1a | 2015-08-04 10:49:46 -0700 | [diff] [blame] | 2474 | arena_redzone_corruption(ptr, size, false, i, |
| 2475 | *byte); |
Guilherme Goncalves | 2c5cb61 | 2014-12-08 19:12:41 -0200 | [diff] [blame] | 2476 | if (reset) |
Chris Peterson | a82070e | 2016-03-27 23:28:39 -0700 | [diff] [blame] | 2477 | *byte = JEMALLOC_ALLOC_JUNK; |
Guilherme Goncalves | 2c5cb61 | 2014-12-08 19:12:41 -0200 | [diff] [blame] | 2478 | } |
| 2479 | } |
| 2480 | for (i = 0; i < redzone_size; i++) { |
| 2481 | uint8_t *byte = (uint8_t *)((uintptr_t)ptr + size + i); |
Chris Peterson | a82070e | 2016-03-27 23:28:39 -0700 | [diff] [blame] | 2482 | if (*byte != JEMALLOC_ALLOC_JUNK) { |
Guilherme Goncalves | 2c5cb61 | 2014-12-08 19:12:41 -0200 | [diff] [blame] | 2483 | error = true; |
Jason Evans | 8fadb1a | 2015-08-04 10:49:46 -0700 | [diff] [blame] | 2484 | arena_redzone_corruption(ptr, size, true, i, |
| 2485 | *byte); |
Guilherme Goncalves | 2c5cb61 | 2014-12-08 19:12:41 -0200 | [diff] [blame] | 2486 | if (reset) |
Chris Peterson | a82070e | 2016-03-27 23:28:39 -0700 | [diff] [blame] | 2487 | *byte = JEMALLOC_ALLOC_JUNK; |
Guilherme Goncalves | 2c5cb61 | 2014-12-08 19:12:41 -0200 | [diff] [blame] | 2488 | } |
Jason Evans | 122449b | 2012-04-06 00:35:09 -0700 | [diff] [blame] | 2489 | } |
| 2490 | } |
Guilherme Goncalves | 2c5cb61 | 2014-12-08 19:12:41 -0200 | [diff] [blame] | 2491 | |
Jason Evans | 122449b | 2012-04-06 00:35:09 -0700 | [diff] [blame] | 2492 | if (opt_abort && error) |
| 2493 | abort(); |
Jason Evans | 0d6c5d8 | 2013-12-17 15:14:36 -0800 | [diff] [blame] | 2494 | } |
Jason Evans | 122449b | 2012-04-06 00:35:09 -0700 | [diff] [blame] | 2495 | |
Jason Evans | 6b694c4 | 2014-01-07 16:47:56 -0800 | [diff] [blame] | 2496 | #ifdef JEMALLOC_JET |
| 2497 | #undef arena_dalloc_junk_small |
Jason Evans | ab0cfe0 | 2016-04-18 15:11:20 -0700 | [diff] [blame] | 2498 | #define arena_dalloc_junk_small JEMALLOC_N(n_arena_dalloc_junk_small) |
Jason Evans | 6b694c4 | 2014-01-07 16:47:56 -0800 | [diff] [blame] | 2499 | #endif |
Jason Evans | 0d6c5d8 | 2013-12-17 15:14:36 -0800 | [diff] [blame] | 2500 | void |
| 2501 | arena_dalloc_junk_small(void *ptr, arena_bin_info_t *bin_info) |
| 2502 | { |
| 2503 | size_t redzone_size = bin_info->redzone_size; |
| 2504 | |
| 2505 | arena_redzones_validate(ptr, bin_info, false); |
Chris Peterson | a82070e | 2016-03-27 23:28:39 -0700 | [diff] [blame] | 2506 | memset((void *)((uintptr_t)ptr - redzone_size), JEMALLOC_FREE_JUNK, |
Jason Evans | 122449b | 2012-04-06 00:35:09 -0700 | [diff] [blame] | 2507 | bin_info->reg_interval); |
| 2508 | } |
Jason Evans | 6b694c4 | 2014-01-07 16:47:56 -0800 | [diff] [blame] | 2509 | #ifdef JEMALLOC_JET |
| 2510 | #undef arena_dalloc_junk_small |
| 2511 | #define arena_dalloc_junk_small JEMALLOC_N(arena_dalloc_junk_small) |
| 2512 | arena_dalloc_junk_small_t *arena_dalloc_junk_small = |
Jason Evans | ab0cfe0 | 2016-04-18 15:11:20 -0700 | [diff] [blame] | 2513 | JEMALLOC_N(n_arena_dalloc_junk_small); |
Jason Evans | 6b694c4 | 2014-01-07 16:47:56 -0800 | [diff] [blame] | 2514 | #endif |
Jason Evans | 122449b | 2012-04-06 00:35:09 -0700 | [diff] [blame] | 2515 | |
Jason Evans | 0d6c5d8 | 2013-12-17 15:14:36 -0800 | [diff] [blame] | 2516 | void |
| 2517 | arena_quarantine_junk_small(void *ptr, size_t usize) |
| 2518 | { |
Jason Evans | d01fd19 | 2015-08-19 15:21:32 -0700 | [diff] [blame] | 2519 | szind_t binind; |
Jason Evans | 0d6c5d8 | 2013-12-17 15:14:36 -0800 | [diff] [blame] | 2520 | arena_bin_info_t *bin_info; |
| 2521 | cassert(config_fill); |
Guilherme Goncalves | 2c5cb61 | 2014-12-08 19:12:41 -0200 | [diff] [blame] | 2522 | assert(opt_junk_free); |
Jason Evans | 0d6c5d8 | 2013-12-17 15:14:36 -0800 | [diff] [blame] | 2523 | assert(opt_quarantine); |
| 2524 | assert(usize <= SMALL_MAXCLASS); |
| 2525 | |
Jason Evans | 155bfa7 | 2014-10-05 17:54:10 -0700 | [diff] [blame] | 2526 | binind = size2index(usize); |
Jason Evans | 0d6c5d8 | 2013-12-17 15:14:36 -0800 | [diff] [blame] | 2527 | bin_info = &arena_bin_info[binind]; |
| 2528 | arena_redzones_validate(ptr, bin_info, true); |
| 2529 | } |
| 2530 | |
Jason Evans | 578cd16 | 2016-02-19 18:40:03 -0800 | [diff] [blame] | 2531 | static void * |
Jason Evans | 0c516a0 | 2016-02-25 15:29:49 -0800 | [diff] [blame] | 2532 | arena_malloc_small(tsd_t *tsd, arena_t *arena, szind_t binind, bool zero) |
Jason Evans | e476f8a | 2010-01-16 09:53:50 -0800 | [diff] [blame] | 2533 | { |
| 2534 | void *ret; |
| 2535 | arena_bin_t *bin; |
Jason Evans | 0c516a0 | 2016-02-25 15:29:49 -0800 | [diff] [blame] | 2536 | size_t usize; |
Jason Evans | e476f8a | 2010-01-16 09:53:50 -0800 | [diff] [blame] | 2537 | arena_run_t *run; |
Jason Evans | e476f8a | 2010-01-16 09:53:50 -0800 | [diff] [blame] | 2538 | |
Jason Evans | b172610 | 2012-02-28 16:50:47 -0800 | [diff] [blame] | 2539 | assert(binind < NBINS); |
Jason Evans | e476f8a | 2010-01-16 09:53:50 -0800 | [diff] [blame] | 2540 | bin = &arena->bins[binind]; |
Jason Evans | 0c516a0 | 2016-02-25 15:29:49 -0800 | [diff] [blame] | 2541 | usize = index2size(binind); |
Jason Evans | e476f8a | 2010-01-16 09:53:50 -0800 | [diff] [blame] | 2542 | |
Jason Evans | b2c0d63 | 2016-04-13 23:36:15 -0700 | [diff] [blame] | 2543 | malloc_mutex_lock(tsd, &bin->lock); |
Jason Evans | e476f8a | 2010-01-16 09:53:50 -0800 | [diff] [blame] | 2544 | if ((run = bin->runcur) != NULL && run->nfree > 0) |
Jason Evans | 49f7e8f | 2011-03-15 13:59:15 -0700 | [diff] [blame] | 2545 | ret = arena_run_reg_alloc(run, &arena_bin_info[binind]); |
Jason Evans | e476f8a | 2010-01-16 09:53:50 -0800 | [diff] [blame] | 2546 | else |
Jason Evans | b2c0d63 | 2016-04-13 23:36:15 -0700 | [diff] [blame] | 2547 | ret = arena_bin_malloc_hard(tsd, arena, bin); |
Jason Evans | e476f8a | 2010-01-16 09:53:50 -0800 | [diff] [blame] | 2548 | |
| 2549 | if (ret == NULL) { |
Jason Evans | b2c0d63 | 2016-04-13 23:36:15 -0700 | [diff] [blame] | 2550 | malloc_mutex_unlock(tsd, &bin->lock); |
Jason Evans | e476f8a | 2010-01-16 09:53:50 -0800 | [diff] [blame] | 2551 | return (NULL); |
| 2552 | } |
| 2553 | |
Jason Evans | 7372b15 | 2012-02-10 20:22:09 -0800 | [diff] [blame] | 2554 | if (config_stats) { |
Jason Evans | 7372b15 | 2012-02-10 20:22:09 -0800 | [diff] [blame] | 2555 | bin->stats.nmalloc++; |
| 2556 | bin->stats.nrequests++; |
Jason Evans | 3c4d92e | 2014-10-12 22:53:59 -0700 | [diff] [blame] | 2557 | bin->stats.curregs++; |
Jason Evans | 7372b15 | 2012-02-10 20:22:09 -0800 | [diff] [blame] | 2558 | } |
Jason Evans | b2c0d63 | 2016-04-13 23:36:15 -0700 | [diff] [blame] | 2559 | malloc_mutex_unlock(tsd, &bin->lock); |
| 2560 | if (config_prof && !isthreaded && arena_prof_accum(tsd, arena, usize)) |
| 2561 | prof_idump(tsd); |
Jason Evans | e476f8a | 2010-01-16 09:53:50 -0800 | [diff] [blame] | 2562 | |
Jason Evans | 551ebc4 | 2014-10-03 10:16:09 -0700 | [diff] [blame] | 2563 | if (!zero) { |
Jason Evans | 7372b15 | 2012-02-10 20:22:09 -0800 | [diff] [blame] | 2564 | if (config_fill) { |
Guilherme Goncalves | 2c5cb61 | 2014-12-08 19:12:41 -0200 | [diff] [blame] | 2565 | if (unlikely(opt_junk_alloc)) { |
Jason Evans | 122449b | 2012-04-06 00:35:09 -0700 | [diff] [blame] | 2566 | arena_alloc_junk_small(ret, |
| 2567 | &arena_bin_info[binind], false); |
Jason Evans | 9c640bf | 2014-09-11 16:20:44 -0700 | [diff] [blame] | 2568 | } else if (unlikely(opt_zero)) |
Jason Evans | 0c516a0 | 2016-02-25 15:29:49 -0800 | [diff] [blame] | 2569 | memset(ret, 0, usize); |
Jason Evans | 7372b15 | 2012-02-10 20:22:09 -0800 | [diff] [blame] | 2570 | } |
Jason Evans | 0c516a0 | 2016-02-25 15:29:49 -0800 | [diff] [blame] | 2571 | JEMALLOC_VALGRIND_MAKE_MEM_UNDEFINED(ret, usize); |
Jason Evans | 122449b | 2012-04-06 00:35:09 -0700 | [diff] [blame] | 2572 | } else { |
Guilherme Goncalves | 2c5cb61 | 2014-12-08 19:12:41 -0200 | [diff] [blame] | 2573 | if (config_fill && unlikely(opt_junk_alloc)) { |
Jason Evans | 122449b | 2012-04-06 00:35:09 -0700 | [diff] [blame] | 2574 | arena_alloc_junk_small(ret, &arena_bin_info[binind], |
| 2575 | true); |
| 2576 | } |
Jason Evans | 0c516a0 | 2016-02-25 15:29:49 -0800 | [diff] [blame] | 2577 | JEMALLOC_VALGRIND_MAKE_MEM_UNDEFINED(ret, usize); |
| 2578 | memset(ret, 0, usize); |
Jason Evans | 122449b | 2012-04-06 00:35:09 -0700 | [diff] [blame] | 2579 | } |
Jason Evans | e476f8a | 2010-01-16 09:53:50 -0800 | [diff] [blame] | 2580 | |
Jason Evans | 243f7a0 | 2016-02-19 20:09:31 -0800 | [diff] [blame] | 2581 | arena_decay_tick(tsd, arena); |
Jason Evans | e476f8a | 2010-01-16 09:53:50 -0800 | [diff] [blame] | 2582 | return (ret); |
| 2583 | } |
| 2584 | |
| 2585 | void * |
Jason Evans | 0c516a0 | 2016-02-25 15:29:49 -0800 | [diff] [blame] | 2586 | arena_malloc_large(tsd_t *tsd, arena_t *arena, szind_t binind, bool zero) |
Jason Evans | e476f8a | 2010-01-16 09:53:50 -0800 | [diff] [blame] | 2587 | { |
| 2588 | void *ret; |
Jason Evans | 155bfa7 | 2014-10-05 17:54:10 -0700 | [diff] [blame] | 2589 | size_t usize; |
Jason Evans | 8a03cf0 | 2015-05-04 09:58:36 -0700 | [diff] [blame] | 2590 | uintptr_t random_offset; |
Jason Evans | 0c5dd03 | 2014-09-29 01:31:39 -0700 | [diff] [blame] | 2591 | arena_run_t *run; |
| 2592 | arena_chunk_map_misc_t *miscelm; |
Dmitri Smirnov | 33184bf | 2016-02-29 14:30:19 -0800 | [diff] [blame] | 2593 | UNUSED bool idump JEMALLOC_CC_SILENCE_INIT(false); |
Jason Evans | e476f8a | 2010-01-16 09:53:50 -0800 | [diff] [blame] | 2594 | |
| 2595 | /* Large allocation. */ |
Qi Wang | f4a0f32 | 2015-10-27 15:12:10 -0700 | [diff] [blame] | 2596 | usize = index2size(binind); |
Jason Evans | b2c0d63 | 2016-04-13 23:36:15 -0700 | [diff] [blame] | 2597 | malloc_mutex_lock(tsd, &arena->lock); |
Jason Evans | 8a03cf0 | 2015-05-04 09:58:36 -0700 | [diff] [blame] | 2598 | if (config_cache_oblivious) { |
Jason Evans | bce61d6 | 2015-07-07 09:32:05 -0700 | [diff] [blame] | 2599 | uint64_t r; |
| 2600 | |
Jason Evans | 8a03cf0 | 2015-05-04 09:58:36 -0700 | [diff] [blame] | 2601 | /* |
| 2602 | * Compute a uniformly distributed offset within the first page |
| 2603 | * that is a multiple of the cacheline size, e.g. [0 .. 63) * 64 |
| 2604 | * for 4 KiB pages and 64-byte cachelines. |
| 2605 | */ |
Jason Evans | 34676d3 | 2016-02-09 16:28:40 -0800 | [diff] [blame] | 2606 | r = prng_lg_range(&arena->offset_state, LG_PAGE - LG_CACHELINE); |
Jason Evans | 8a03cf0 | 2015-05-04 09:58:36 -0700 | [diff] [blame] | 2607 | random_offset = ((uintptr_t)r) << LG_CACHELINE; |
| 2608 | } else |
| 2609 | random_offset = 0; |
Jason Evans | b2c0d63 | 2016-04-13 23:36:15 -0700 | [diff] [blame] | 2610 | run = arena_run_alloc_large(tsd, arena, usize + large_pad, zero); |
Jason Evans | 0c5dd03 | 2014-09-29 01:31:39 -0700 | [diff] [blame] | 2611 | if (run == NULL) { |
Jason Evans | b2c0d63 | 2016-04-13 23:36:15 -0700 | [diff] [blame] | 2612 | malloc_mutex_unlock(tsd, &arena->lock); |
Jason Evans | e476f8a | 2010-01-16 09:53:50 -0800 | [diff] [blame] | 2613 | return (NULL); |
| 2614 | } |
Jason Evans | 0c5dd03 | 2014-09-29 01:31:39 -0700 | [diff] [blame] | 2615 | miscelm = arena_run_to_miscelm(run); |
Jason Evans | 8a03cf0 | 2015-05-04 09:58:36 -0700 | [diff] [blame] | 2616 | ret = (void *)((uintptr_t)arena_miscelm_to_rpages(miscelm) + |
| 2617 | random_offset); |
Jason Evans | 7372b15 | 2012-02-10 20:22:09 -0800 | [diff] [blame] | 2618 | if (config_stats) { |
Qi Wang | f4a0f32 | 2015-10-27 15:12:10 -0700 | [diff] [blame] | 2619 | szind_t index = binind - NBINS; |
Jason Evans | 155bfa7 | 2014-10-05 17:54:10 -0700 | [diff] [blame] | 2620 | |
Jason Evans | 7372b15 | 2012-02-10 20:22:09 -0800 | [diff] [blame] | 2621 | arena->stats.nmalloc_large++; |
| 2622 | arena->stats.nrequests_large++; |
Jason Evans | 155bfa7 | 2014-10-05 17:54:10 -0700 | [diff] [blame] | 2623 | arena->stats.allocated_large += usize; |
| 2624 | arena->stats.lstats[index].nmalloc++; |
| 2625 | arena->stats.lstats[index].nrequests++; |
| 2626 | arena->stats.lstats[index].curruns++; |
Jason Evans | e476f8a | 2010-01-16 09:53:50 -0800 | [diff] [blame] | 2627 | } |
Jason Evans | 7372b15 | 2012-02-10 20:22:09 -0800 | [diff] [blame] | 2628 | if (config_prof) |
Jason Evans | 155bfa7 | 2014-10-05 17:54:10 -0700 | [diff] [blame] | 2629 | idump = arena_prof_accum_locked(arena, usize); |
Jason Evans | b2c0d63 | 2016-04-13 23:36:15 -0700 | [diff] [blame] | 2630 | malloc_mutex_unlock(tsd, &arena->lock); |
Jason Evans | 88c222c | 2013-02-06 11:59:30 -0800 | [diff] [blame] | 2631 | if (config_prof && idump) |
Jason Evans | b2c0d63 | 2016-04-13 23:36:15 -0700 | [diff] [blame] | 2632 | prof_idump(tsd); |
Jason Evans | e476f8a | 2010-01-16 09:53:50 -0800 | [diff] [blame] | 2633 | |
Jason Evans | 551ebc4 | 2014-10-03 10:16:09 -0700 | [diff] [blame] | 2634 | if (!zero) { |
Jason Evans | 7372b15 | 2012-02-10 20:22:09 -0800 | [diff] [blame] | 2635 | if (config_fill) { |
Guilherme Goncalves | 2c5cb61 | 2014-12-08 19:12:41 -0200 | [diff] [blame] | 2636 | if (unlikely(opt_junk_alloc)) |
Chris Peterson | a82070e | 2016-03-27 23:28:39 -0700 | [diff] [blame] | 2637 | memset(ret, JEMALLOC_ALLOC_JUNK, usize); |
Jason Evans | 9c640bf | 2014-09-11 16:20:44 -0700 | [diff] [blame] | 2638 | else if (unlikely(opt_zero)) |
Jason Evans | 155bfa7 | 2014-10-05 17:54:10 -0700 | [diff] [blame] | 2639 | memset(ret, 0, usize); |
Jason Evans | 7372b15 | 2012-02-10 20:22:09 -0800 | [diff] [blame] | 2640 | } |
Jason Evans | e476f8a | 2010-01-16 09:53:50 -0800 | [diff] [blame] | 2641 | } |
| 2642 | |
Jason Evans | 243f7a0 | 2016-02-19 20:09:31 -0800 | [diff] [blame] | 2643 | arena_decay_tick(tsd, arena); |
Jason Evans | e476f8a | 2010-01-16 09:53:50 -0800 | [diff] [blame] | 2644 | return (ret); |
| 2645 | } |
| 2646 | |
Jason Evans | 578cd16 | 2016-02-19 18:40:03 -0800 | [diff] [blame] | 2647 | void * |
| 2648 | arena_malloc_hard(tsd_t *tsd, arena_t *arena, size_t size, szind_t ind, |
Jason Evans | 66cd953 | 2016-04-22 14:34:14 -0700 | [diff] [blame] | 2649 | bool zero) |
Jason Evans | 578cd16 | 2016-02-19 18:40:03 -0800 | [diff] [blame] | 2650 | { |
| 2651 | |
Jason Evans | 90827a3 | 2016-05-03 15:00:42 -0700 | [diff] [blame] | 2652 | arena = arena_choose(tsd, arena); |
Jason Evans | 578cd16 | 2016-02-19 18:40:03 -0800 | [diff] [blame] | 2653 | if (unlikely(arena == NULL)) |
| 2654 | return (NULL); |
| 2655 | |
| 2656 | if (likely(size <= SMALL_MAXCLASS)) |
Jason Evans | 0c516a0 | 2016-02-25 15:29:49 -0800 | [diff] [blame] | 2657 | return (arena_malloc_small(tsd, arena, ind, zero)); |
Jason Evans | 578cd16 | 2016-02-19 18:40:03 -0800 | [diff] [blame] | 2658 | if (likely(size <= large_maxclass)) |
Jason Evans | 0c516a0 | 2016-02-25 15:29:49 -0800 | [diff] [blame] | 2659 | return (arena_malloc_large(tsd, arena, ind, zero)); |
Jason Evans | 66cd953 | 2016-04-22 14:34:14 -0700 | [diff] [blame] | 2660 | return (huge_malloc(tsd, arena, index2size(ind), zero)); |
Jason Evans | 578cd16 | 2016-02-19 18:40:03 -0800 | [diff] [blame] | 2661 | } |
| 2662 | |
Jason Evans | e476f8a | 2010-01-16 09:53:50 -0800 | [diff] [blame] | 2663 | /* Only handles large allocations that require more than page alignment. */ |
Jason Evans | 88fef7c | 2015-02-12 14:06:37 -0800 | [diff] [blame] | 2664 | static void * |
Jason Evans | 50883de | 2015-07-23 17:13:18 -0700 | [diff] [blame] | 2665 | arena_palloc_large(tsd_t *tsd, arena_t *arena, size_t usize, size_t alignment, |
Jason Evans | 88fef7c | 2015-02-12 14:06:37 -0800 | [diff] [blame] | 2666 | bool zero) |
Jason Evans | e476f8a | 2010-01-16 09:53:50 -0800 | [diff] [blame] | 2667 | { |
| 2668 | void *ret; |
Jason Evans | 5ff709c | 2012-04-11 18:13:45 -0700 | [diff] [blame] | 2669 | size_t alloc_size, leadsize, trailsize; |
| 2670 | arena_run_t *run; |
Jason Evans | e476f8a | 2010-01-16 09:53:50 -0800 | [diff] [blame] | 2671 | arena_chunk_t *chunk; |
Jason Evans | 0c5dd03 | 2014-09-29 01:31:39 -0700 | [diff] [blame] | 2672 | arena_chunk_map_misc_t *miscelm; |
| 2673 | void *rpages; |
Jason Evans | e476f8a | 2010-01-16 09:53:50 -0800 | [diff] [blame] | 2674 | |
Jason Evans | 50883de | 2015-07-23 17:13:18 -0700 | [diff] [blame] | 2675 | assert(usize == PAGE_CEILING(usize)); |
Jason Evans | 9344368 | 2010-10-20 17:39:18 -0700 | [diff] [blame] | 2676 | |
Jason Evans | 90827a3 | 2016-05-03 15:00:42 -0700 | [diff] [blame] | 2677 | arena = arena_choose(tsd, arena); |
Jason Evans | 88fef7c | 2015-02-12 14:06:37 -0800 | [diff] [blame] | 2678 | if (unlikely(arena == NULL)) |
| 2679 | return (NULL); |
| 2680 | |
Jason Evans | 9344368 | 2010-10-20 17:39:18 -0700 | [diff] [blame] | 2681 | alignment = PAGE_CEILING(alignment); |
Jason Evans | 245ae60 | 2016-04-06 11:54:44 -0700 | [diff] [blame] | 2682 | alloc_size = usize + large_pad + alignment; |
Jason Evans | e476f8a | 2010-01-16 09:53:50 -0800 | [diff] [blame] | 2683 | |
Jason Evans | b2c0d63 | 2016-04-13 23:36:15 -0700 | [diff] [blame] | 2684 | malloc_mutex_lock(tsd, &arena->lock); |
| 2685 | run = arena_run_alloc_large(tsd, arena, alloc_size, false); |
Jason Evans | 5ff709c | 2012-04-11 18:13:45 -0700 | [diff] [blame] | 2686 | if (run == NULL) { |
Jason Evans | b2c0d63 | 2016-04-13 23:36:15 -0700 | [diff] [blame] | 2687 | malloc_mutex_unlock(tsd, &arena->lock); |
Jason Evans | e476f8a | 2010-01-16 09:53:50 -0800 | [diff] [blame] | 2688 | return (NULL); |
| 2689 | } |
Jason Evans | 5ff709c | 2012-04-11 18:13:45 -0700 | [diff] [blame] | 2690 | chunk = (arena_chunk_t *)CHUNK_ADDR2BASE(run); |
Jason Evans | 0c5dd03 | 2014-09-29 01:31:39 -0700 | [diff] [blame] | 2691 | miscelm = arena_run_to_miscelm(run); |
| 2692 | rpages = arena_miscelm_to_rpages(miscelm); |
Jason Evans | e476f8a | 2010-01-16 09:53:50 -0800 | [diff] [blame] | 2693 | |
Jason Evans | 0c5dd03 | 2014-09-29 01:31:39 -0700 | [diff] [blame] | 2694 | leadsize = ALIGNMENT_CEILING((uintptr_t)rpages, alignment) - |
| 2695 | (uintptr_t)rpages; |
Jason Evans | 50883de | 2015-07-23 17:13:18 -0700 | [diff] [blame] | 2696 | assert(alloc_size >= leadsize + usize); |
| 2697 | trailsize = alloc_size - leadsize - usize - large_pad; |
Jason Evans | 5ff709c | 2012-04-11 18:13:45 -0700 | [diff] [blame] | 2698 | if (leadsize != 0) { |
Jason Evans | 0c5dd03 | 2014-09-29 01:31:39 -0700 | [diff] [blame] | 2699 | arena_chunk_map_misc_t *head_miscelm = miscelm; |
| 2700 | arena_run_t *head_run = run; |
| 2701 | |
Jason Evans | 61a6dfc | 2016-03-23 16:04:38 -0700 | [diff] [blame] | 2702 | miscelm = arena_miscelm_get_mutable(chunk, |
Jason Evans | 0c5dd03 | 2014-09-29 01:31:39 -0700 | [diff] [blame] | 2703 | arena_miscelm_to_pageind(head_miscelm) + (leadsize >> |
| 2704 | LG_PAGE)); |
| 2705 | run = &miscelm->run; |
| 2706 | |
Jason Evans | b2c0d63 | 2016-04-13 23:36:15 -0700 | [diff] [blame] | 2707 | arena_run_trim_head(tsd, arena, chunk, head_run, alloc_size, |
Jason Evans | 0c5dd03 | 2014-09-29 01:31:39 -0700 | [diff] [blame] | 2708 | alloc_size - leadsize); |
Jason Evans | 5ff709c | 2012-04-11 18:13:45 -0700 | [diff] [blame] | 2709 | } |
| 2710 | if (trailsize != 0) { |
Jason Evans | b2c0d63 | 2016-04-13 23:36:15 -0700 | [diff] [blame] | 2711 | arena_run_trim_tail(tsd, arena, chunk, run, usize + large_pad + |
Jason Evans | 50883de | 2015-07-23 17:13:18 -0700 | [diff] [blame] | 2712 | trailsize, usize + large_pad, false); |
Jason Evans | e476f8a | 2010-01-16 09:53:50 -0800 | [diff] [blame] | 2713 | } |
Jason Evans | 8fadb1a | 2015-08-04 10:49:46 -0700 | [diff] [blame] | 2714 | if (arena_run_init_large(arena, run, usize + large_pad, zero)) { |
| 2715 | size_t run_ind = |
| 2716 | arena_miscelm_to_pageind(arena_run_to_miscelm(run)); |
Jason Evans | de249c8 | 2015-08-09 16:47:27 -0700 | [diff] [blame] | 2717 | bool dirty = (arena_mapbits_dirty_get(chunk, run_ind) != 0); |
| 2718 | bool decommitted = (arena_mapbits_decommitted_get(chunk, |
| 2719 | run_ind) != 0); |
Jason Evans | 8fadb1a | 2015-08-04 10:49:46 -0700 | [diff] [blame] | 2720 | |
Jason Evans | de249c8 | 2015-08-09 16:47:27 -0700 | [diff] [blame] | 2721 | assert(decommitted); /* Cause of OOM. */ |
Jason Evans | b2c0d63 | 2016-04-13 23:36:15 -0700 | [diff] [blame] | 2722 | arena_run_dalloc(tsd, arena, run, dirty, false, decommitted); |
| 2723 | malloc_mutex_unlock(tsd, &arena->lock); |
Jason Evans | 8fadb1a | 2015-08-04 10:49:46 -0700 | [diff] [blame] | 2724 | return (NULL); |
| 2725 | } |
Jason Evans | 0c5dd03 | 2014-09-29 01:31:39 -0700 | [diff] [blame] | 2726 | ret = arena_miscelm_to_rpages(miscelm); |
Jason Evans | e476f8a | 2010-01-16 09:53:50 -0800 | [diff] [blame] | 2727 | |
Jason Evans | 7372b15 | 2012-02-10 20:22:09 -0800 | [diff] [blame] | 2728 | if (config_stats) { |
Jason Evans | d01fd19 | 2015-08-19 15:21:32 -0700 | [diff] [blame] | 2729 | szind_t index = size2index(usize) - NBINS; |
Jason Evans | 155bfa7 | 2014-10-05 17:54:10 -0700 | [diff] [blame] | 2730 | |
Jason Evans | 7372b15 | 2012-02-10 20:22:09 -0800 | [diff] [blame] | 2731 | arena->stats.nmalloc_large++; |
| 2732 | arena->stats.nrequests_large++; |
Jason Evans | 50883de | 2015-07-23 17:13:18 -0700 | [diff] [blame] | 2733 | arena->stats.allocated_large += usize; |
Jason Evans | 155bfa7 | 2014-10-05 17:54:10 -0700 | [diff] [blame] | 2734 | arena->stats.lstats[index].nmalloc++; |
| 2735 | arena->stats.lstats[index].nrequests++; |
| 2736 | arena->stats.lstats[index].curruns++; |
Jason Evans | e476f8a | 2010-01-16 09:53:50 -0800 | [diff] [blame] | 2737 | } |
Jason Evans | b2c0d63 | 2016-04-13 23:36:15 -0700 | [diff] [blame] | 2738 | malloc_mutex_unlock(tsd, &arena->lock); |
Jason Evans | e476f8a | 2010-01-16 09:53:50 -0800 | [diff] [blame] | 2739 | |
Jason Evans | 551ebc4 | 2014-10-03 10:16:09 -0700 | [diff] [blame] | 2740 | if (config_fill && !zero) { |
Guilherme Goncalves | 2c5cb61 | 2014-12-08 19:12:41 -0200 | [diff] [blame] | 2741 | if (unlikely(opt_junk_alloc)) |
Chris Peterson | a82070e | 2016-03-27 23:28:39 -0700 | [diff] [blame] | 2742 | memset(ret, JEMALLOC_ALLOC_JUNK, usize); |
Jason Evans | 9c640bf | 2014-09-11 16:20:44 -0700 | [diff] [blame] | 2743 | else if (unlikely(opt_zero)) |
Jason Evans | 50883de | 2015-07-23 17:13:18 -0700 | [diff] [blame] | 2744 | memset(ret, 0, usize); |
Jason Evans | 8e3c3c6 | 2010-09-17 15:46:18 -0700 | [diff] [blame] | 2745 | } |
Jason Evans | 243f7a0 | 2016-02-19 20:09:31 -0800 | [diff] [blame] | 2746 | arena_decay_tick(tsd, arena); |
Jason Evans | e476f8a | 2010-01-16 09:53:50 -0800 | [diff] [blame] | 2747 | return (ret); |
| 2748 | } |
| 2749 | |
Jason Evans | 88fef7c | 2015-02-12 14:06:37 -0800 | [diff] [blame] | 2750 | void * |
| 2751 | arena_palloc(tsd_t *tsd, arena_t *arena, size_t usize, size_t alignment, |
| 2752 | bool zero, tcache_t *tcache) |
| 2753 | { |
| 2754 | void *ret; |
| 2755 | |
Jason Evans | 8a03cf0 | 2015-05-04 09:58:36 -0700 | [diff] [blame] | 2756 | if (usize <= SMALL_MAXCLASS && (alignment < PAGE || (alignment == PAGE |
Jason Evans | 5154175 | 2015-05-19 17:42:31 -0700 | [diff] [blame] | 2757 | && (usize & PAGE_MASK) == 0))) { |
| 2758 | /* Small; alignment doesn't require special run placement. */ |
Qi Wang | f4a0f32 | 2015-10-27 15:12:10 -0700 | [diff] [blame] | 2759 | ret = arena_malloc(tsd, arena, usize, size2index(usize), zero, |
| 2760 | tcache, true); |
Jason Evans | 676df88 | 2015-09-11 20:50:20 -0700 | [diff] [blame] | 2761 | } else if (usize <= large_maxclass && alignment <= PAGE) { |
Jason Evans | 5154175 | 2015-05-19 17:42:31 -0700 | [diff] [blame] | 2762 | /* |
| 2763 | * Large; alignment doesn't require special run placement. |
| 2764 | * However, the cached pointer may be at a random offset from |
| 2765 | * the base of the run, so do some bit manipulation to retrieve |
| 2766 | * the base. |
| 2767 | */ |
Qi Wang | f4a0f32 | 2015-10-27 15:12:10 -0700 | [diff] [blame] | 2768 | ret = arena_malloc(tsd, arena, usize, size2index(usize), zero, |
| 2769 | tcache, true); |
Jason Evans | 5154175 | 2015-05-19 17:42:31 -0700 | [diff] [blame] | 2770 | if (config_cache_oblivious) |
| 2771 | ret = (void *)((uintptr_t)ret & ~PAGE_MASK); |
| 2772 | } else { |
Jason Evans | 676df88 | 2015-09-11 20:50:20 -0700 | [diff] [blame] | 2773 | if (likely(usize <= large_maxclass)) { |
Jason Evans | 88fef7c | 2015-02-12 14:06:37 -0800 | [diff] [blame] | 2774 | ret = arena_palloc_large(tsd, arena, usize, alignment, |
| 2775 | zero); |
| 2776 | } else if (likely(alignment <= chunksize)) |
Jason Evans | 66cd953 | 2016-04-22 14:34:14 -0700 | [diff] [blame] | 2777 | ret = huge_malloc(tsd, arena, usize, zero); |
Jason Evans | 88fef7c | 2015-02-12 14:06:37 -0800 | [diff] [blame] | 2778 | else { |
Jason Evans | 66cd953 | 2016-04-22 14:34:14 -0700 | [diff] [blame] | 2779 | ret = huge_palloc(tsd, arena, usize, alignment, zero); |
Jason Evans | 88fef7c | 2015-02-12 14:06:37 -0800 | [diff] [blame] | 2780 | } |
| 2781 | } |
| 2782 | return (ret); |
| 2783 | } |
| 2784 | |
Jason Evans | 0b270a9 | 2010-03-31 16:45:04 -0700 | [diff] [blame] | 2785 | void |
Jason Evans | b2c0d63 | 2016-04-13 23:36:15 -0700 | [diff] [blame] | 2786 | arena_prof_promoted(tsd_t *tsd, const void *ptr, size_t size) |
Jason Evans | 0b270a9 | 2010-03-31 16:45:04 -0700 | [diff] [blame] | 2787 | { |
| 2788 | arena_chunk_t *chunk; |
Jason Evans | 155bfa7 | 2014-10-05 17:54:10 -0700 | [diff] [blame] | 2789 | size_t pageind; |
Jason Evans | d01fd19 | 2015-08-19 15:21:32 -0700 | [diff] [blame] | 2790 | szind_t binind; |
Jason Evans | 0b270a9 | 2010-03-31 16:45:04 -0700 | [diff] [blame] | 2791 | |
Jason Evans | 78f7352 | 2012-04-18 13:38:40 -0700 | [diff] [blame] | 2792 | cassert(config_prof); |
Jason Evans | 0b270a9 | 2010-03-31 16:45:04 -0700 | [diff] [blame] | 2793 | assert(ptr != NULL); |
| 2794 | assert(CHUNK_ADDR2BASE(ptr) != ptr); |
Jason Evans | b2c0d63 | 2016-04-13 23:36:15 -0700 | [diff] [blame] | 2795 | assert(isalloc(tsd, ptr, false) == LARGE_MINCLASS); |
| 2796 | assert(isalloc(tsd, ptr, true) == LARGE_MINCLASS); |
Jason Evans | b172610 | 2012-02-28 16:50:47 -0800 | [diff] [blame] | 2797 | assert(size <= SMALL_MAXCLASS); |
Jason Evans | 0b270a9 | 2010-03-31 16:45:04 -0700 | [diff] [blame] | 2798 | |
| 2799 | chunk = (arena_chunk_t *)CHUNK_ADDR2BASE(ptr); |
Jason Evans | ae4c7b4 | 2012-04-02 07:04:34 -0700 | [diff] [blame] | 2800 | pageind = ((uintptr_t)ptr - (uintptr_t)chunk) >> LG_PAGE; |
Jason Evans | 155bfa7 | 2014-10-05 17:54:10 -0700 | [diff] [blame] | 2801 | binind = size2index(size); |
Jason Evans | b172610 | 2012-02-28 16:50:47 -0800 | [diff] [blame] | 2802 | assert(binind < NBINS); |
Jason Evans | 203484e | 2012-05-02 00:30:36 -0700 | [diff] [blame] | 2803 | arena_mapbits_large_binind_set(chunk, pageind, binind); |
Jason Evans | 0b270a9 | 2010-03-31 16:45:04 -0700 | [diff] [blame] | 2804 | |
Jason Evans | b2c0d63 | 2016-04-13 23:36:15 -0700 | [diff] [blame] | 2805 | assert(isalloc(tsd, ptr, false) == LARGE_MINCLASS); |
| 2806 | assert(isalloc(tsd, ptr, true) == size); |
Jason Evans | 0b270a9 | 2010-03-31 16:45:04 -0700 | [diff] [blame] | 2807 | } |
Jason Evans | 6109fe0 | 2010-02-10 10:37:56 -0800 | [diff] [blame] | 2808 | |
Jason Evans | e476f8a | 2010-01-16 09:53:50 -0800 | [diff] [blame] | 2809 | static void |
Jason Evans | 088e6a0 | 2010-10-18 00:04:44 -0700 | [diff] [blame] | 2810 | arena_dissociate_bin_run(arena_chunk_t *chunk, arena_run_t *run, |
Jason Evans | e476f8a | 2010-01-16 09:53:50 -0800 | [diff] [blame] | 2811 | arena_bin_t *bin) |
| 2812 | { |
Jason Evans | e476f8a | 2010-01-16 09:53:50 -0800 | [diff] [blame] | 2813 | |
Jason Evans | 19b3d61 | 2010-03-18 20:36:40 -0700 | [diff] [blame] | 2814 | /* Dissociate run from bin. */ |
Jason Evans | e476f8a | 2010-01-16 09:53:50 -0800 | [diff] [blame] | 2815 | if (run == bin->runcur) |
| 2816 | bin->runcur = NULL; |
Jason Evans | 49f7e8f | 2011-03-15 13:59:15 -0700 | [diff] [blame] | 2817 | else { |
Jason Evans | d01fd19 | 2015-08-19 15:21:32 -0700 | [diff] [blame] | 2818 | szind_t binind = arena_bin_index(extent_node_arena_get( |
Jason Evans | ee41ad4 | 2015-02-15 18:04:46 -0800 | [diff] [blame] | 2819 | &chunk->node), bin); |
Jason Evans | 49f7e8f | 2011-03-15 13:59:15 -0700 | [diff] [blame] | 2820 | arena_bin_info_t *bin_info = &arena_bin_info[binind]; |
| 2821 | |
Jason Evans | c6a2c39 | 2016-03-26 17:30:37 -0700 | [diff] [blame] | 2822 | /* |
| 2823 | * The following block's conditional is necessary because if the |
| 2824 | * run only contains one region, then it never gets inserted |
| 2825 | * into the non-full runs tree. |
| 2826 | */ |
Jason Evans | 49f7e8f | 2011-03-15 13:59:15 -0700 | [diff] [blame] | 2827 | if (bin_info->nregs != 1) { |
Jason Evans | c6a2c39 | 2016-03-26 17:30:37 -0700 | [diff] [blame] | 2828 | arena_chunk_map_misc_t *miscelm = |
| 2829 | arena_run_to_miscelm(run); |
| 2830 | |
| 2831 | arena_run_heap_remove(&bin->runs, miscelm); |
Jason Evans | 49f7e8f | 2011-03-15 13:59:15 -0700 | [diff] [blame] | 2832 | } |
Jason Evans | e476f8a | 2010-01-16 09:53:50 -0800 | [diff] [blame] | 2833 | } |
Jason Evans | 088e6a0 | 2010-10-18 00:04:44 -0700 | [diff] [blame] | 2834 | } |
| 2835 | |
| 2836 | static void |
Jason Evans | b2c0d63 | 2016-04-13 23:36:15 -0700 | [diff] [blame] | 2837 | arena_dalloc_bin_run(tsd_t *tsd, arena_t *arena, arena_chunk_t *chunk, |
| 2838 | arena_run_t *run, arena_bin_t *bin) |
Jason Evans | 088e6a0 | 2010-10-18 00:04:44 -0700 | [diff] [blame] | 2839 | { |
Jason Evans | 088e6a0 | 2010-10-18 00:04:44 -0700 | [diff] [blame] | 2840 | |
| 2841 | assert(run != bin->runcur); |
Jason Evans | 86815df | 2010-03-13 20:32:56 -0800 | [diff] [blame] | 2842 | |
Jason Evans | b2c0d63 | 2016-04-13 23:36:15 -0700 | [diff] [blame] | 2843 | malloc_mutex_unlock(tsd, &bin->lock); |
Jason Evans | e00572b | 2010-03-14 19:43:56 -0700 | [diff] [blame] | 2844 | /******************************/ |
Jason Evans | b2c0d63 | 2016-04-13 23:36:15 -0700 | [diff] [blame] | 2845 | malloc_mutex_lock(tsd, &arena->lock); |
| 2846 | arena_run_dalloc(tsd, arena, run, true, false, false); |
| 2847 | malloc_mutex_unlock(tsd, &arena->lock); |
Jason Evans | e00572b | 2010-03-14 19:43:56 -0700 | [diff] [blame] | 2848 | /****************************/ |
Jason Evans | b2c0d63 | 2016-04-13 23:36:15 -0700 | [diff] [blame] | 2849 | malloc_mutex_lock(tsd, &bin->lock); |
Jason Evans | 7372b15 | 2012-02-10 20:22:09 -0800 | [diff] [blame] | 2850 | if (config_stats) |
| 2851 | bin->stats.curruns--; |
Jason Evans | e476f8a | 2010-01-16 09:53:50 -0800 | [diff] [blame] | 2852 | } |
| 2853 | |
Jason Evans | 940a2e0 | 2010-10-17 17:51:37 -0700 | [diff] [blame] | 2854 | static void |
| 2855 | arena_bin_lower_run(arena_t *arena, arena_chunk_t *chunk, arena_run_t *run, |
| 2856 | arena_bin_t *bin) |
Jason Evans | e476f8a | 2010-01-16 09:53:50 -0800 | [diff] [blame] | 2857 | { |
Jason Evans | e476f8a | 2010-01-16 09:53:50 -0800 | [diff] [blame] | 2858 | |
Jason Evans | 8de6a02 | 2010-10-17 20:57:30 -0700 | [diff] [blame] | 2859 | /* |
Jason Evans | e7a1058 | 2012-02-13 17:36:52 -0800 | [diff] [blame] | 2860 | * Make sure that if bin->runcur is non-NULL, it refers to the lowest |
| 2861 | * non-full run. It is okay to NULL runcur out rather than proactively |
| 2862 | * keeping it pointing at the lowest non-full run. |
Jason Evans | 8de6a02 | 2010-10-17 20:57:30 -0700 | [diff] [blame] | 2863 | */ |
Jason Evans | e7a1058 | 2012-02-13 17:36:52 -0800 | [diff] [blame] | 2864 | if ((uintptr_t)run < (uintptr_t)bin->runcur) { |
Jason Evans | 8de6a02 | 2010-10-17 20:57:30 -0700 | [diff] [blame] | 2865 | /* Switch runcur. */ |
Jason Evans | e7a1058 | 2012-02-13 17:36:52 -0800 | [diff] [blame] | 2866 | if (bin->runcur->nfree > 0) |
| 2867 | arena_bin_runs_insert(bin, bin->runcur); |
Jason Evans | 8de6a02 | 2010-10-17 20:57:30 -0700 | [diff] [blame] | 2868 | bin->runcur = run; |
Jason Evans | e7a1058 | 2012-02-13 17:36:52 -0800 | [diff] [blame] | 2869 | if (config_stats) |
| 2870 | bin->stats.reruns++; |
| 2871 | } else |
| 2872 | arena_bin_runs_insert(bin, run); |
Jason Evans | 940a2e0 | 2010-10-17 17:51:37 -0700 | [diff] [blame] | 2873 | } |
| 2874 | |
Jason Evans | fc0b3b7 | 2014-10-09 17:54:06 -0700 | [diff] [blame] | 2875 | static void |
Jason Evans | b2c0d63 | 2016-04-13 23:36:15 -0700 | [diff] [blame] | 2876 | arena_dalloc_bin_locked_impl(tsd_t *tsd, arena_t *arena, arena_chunk_t *chunk, |
| 2877 | void *ptr, arena_chunk_map_bits_t *bitselm, bool junked) |
Jason Evans | 940a2e0 | 2010-10-17 17:51:37 -0700 | [diff] [blame] | 2878 | { |
Jason Evans | 0c5dd03 | 2014-09-29 01:31:39 -0700 | [diff] [blame] | 2879 | size_t pageind, rpages_ind; |
Jason Evans | 940a2e0 | 2010-10-17 17:51:37 -0700 | [diff] [blame] | 2880 | arena_run_t *run; |
| 2881 | arena_bin_t *bin; |
Mike Hommey | 8b49971 | 2012-04-24 23:22:02 +0200 | [diff] [blame] | 2882 | arena_bin_info_t *bin_info; |
Jason Evans | d01fd19 | 2015-08-19 15:21:32 -0700 | [diff] [blame] | 2883 | szind_t binind; |
Jason Evans | 940a2e0 | 2010-10-17 17:51:37 -0700 | [diff] [blame] | 2884 | |
Jason Evans | ae4c7b4 | 2012-04-02 07:04:34 -0700 | [diff] [blame] | 2885 | pageind = ((uintptr_t)ptr - (uintptr_t)chunk) >> LG_PAGE; |
Jason Evans | 0c5dd03 | 2014-09-29 01:31:39 -0700 | [diff] [blame] | 2886 | rpages_ind = pageind - arena_mapbits_small_runind_get(chunk, pageind); |
Jason Evans | 61a6dfc | 2016-03-23 16:04:38 -0700 | [diff] [blame] | 2887 | run = &arena_miscelm_get_mutable(chunk, rpages_ind)->run; |
Jason Evans | 381c23d | 2014-10-10 23:01:03 -0700 | [diff] [blame] | 2888 | binind = run->binind; |
| 2889 | bin = &arena->bins[binind]; |
Mike Hommey | 8b49971 | 2012-04-24 23:22:02 +0200 | [diff] [blame] | 2890 | bin_info = &arena_bin_info[binind]; |
Jason Evans | 940a2e0 | 2010-10-17 17:51:37 -0700 | [diff] [blame] | 2891 | |
Guilherme Goncalves | 2c5cb61 | 2014-12-08 19:12:41 -0200 | [diff] [blame] | 2892 | if (!junked && config_fill && unlikely(opt_junk_free)) |
Jason Evans | 122449b | 2012-04-06 00:35:09 -0700 | [diff] [blame] | 2893 | arena_dalloc_junk_small(ptr, bin_info); |
Jason Evans | 940a2e0 | 2010-10-17 17:51:37 -0700 | [diff] [blame] | 2894 | |
| 2895 | arena_run_reg_dalloc(run, ptr); |
Jason Evans | 49f7e8f | 2011-03-15 13:59:15 -0700 | [diff] [blame] | 2896 | if (run->nfree == bin_info->nregs) { |
Jason Evans | 088e6a0 | 2010-10-18 00:04:44 -0700 | [diff] [blame] | 2897 | arena_dissociate_bin_run(chunk, run, bin); |
Jason Evans | b2c0d63 | 2016-04-13 23:36:15 -0700 | [diff] [blame] | 2898 | arena_dalloc_bin_run(tsd, arena, chunk, run, bin); |
Jason Evans | 088e6a0 | 2010-10-18 00:04:44 -0700 | [diff] [blame] | 2899 | } else if (run->nfree == 1 && run != bin->runcur) |
Jason Evans | 8de6a02 | 2010-10-17 20:57:30 -0700 | [diff] [blame] | 2900 | arena_bin_lower_run(arena, chunk, run, bin); |
Jason Evans | e476f8a | 2010-01-16 09:53:50 -0800 | [diff] [blame] | 2901 | |
Jason Evans | 7372b15 | 2012-02-10 20:22:09 -0800 | [diff] [blame] | 2902 | if (config_stats) { |
Jason Evans | 7372b15 | 2012-02-10 20:22:09 -0800 | [diff] [blame] | 2903 | bin->stats.ndalloc++; |
Jason Evans | 3c4d92e | 2014-10-12 22:53:59 -0700 | [diff] [blame] | 2904 | bin->stats.curregs--; |
Jason Evans | 7372b15 | 2012-02-10 20:22:09 -0800 | [diff] [blame] | 2905 | } |
Jason Evans | e476f8a | 2010-01-16 09:53:50 -0800 | [diff] [blame] | 2906 | } |
| 2907 | |
Jason Evans | e476f8a | 2010-01-16 09:53:50 -0800 | [diff] [blame] | 2908 | void |
Jason Evans | b2c0d63 | 2016-04-13 23:36:15 -0700 | [diff] [blame] | 2909 | arena_dalloc_bin_junked_locked(tsd_t *tsd, arena_t *arena, arena_chunk_t *chunk, |
| 2910 | void *ptr, arena_chunk_map_bits_t *bitselm) |
Jason Evans | fc0b3b7 | 2014-10-09 17:54:06 -0700 | [diff] [blame] | 2911 | { |
| 2912 | |
Jason Evans | b2c0d63 | 2016-04-13 23:36:15 -0700 | [diff] [blame] | 2913 | arena_dalloc_bin_locked_impl(tsd, arena, chunk, ptr, bitselm, true); |
Jason Evans | fc0b3b7 | 2014-10-09 17:54:06 -0700 | [diff] [blame] | 2914 | } |
| 2915 | |
| 2916 | void |
Jason Evans | b2c0d63 | 2016-04-13 23:36:15 -0700 | [diff] [blame] | 2917 | arena_dalloc_bin(tsd_t *tsd, arena_t *arena, arena_chunk_t *chunk, void *ptr, |
Qinfan Wu | ff6a31d | 2014-08-29 13:34:40 -0700 | [diff] [blame] | 2918 | size_t pageind, arena_chunk_map_bits_t *bitselm) |
Jason Evans | 203484e | 2012-05-02 00:30:36 -0700 | [diff] [blame] | 2919 | { |
| 2920 | arena_run_t *run; |
| 2921 | arena_bin_t *bin; |
Jason Evans | 0c5dd03 | 2014-09-29 01:31:39 -0700 | [diff] [blame] | 2922 | size_t rpages_ind; |
Jason Evans | 203484e | 2012-05-02 00:30:36 -0700 | [diff] [blame] | 2923 | |
Jason Evans | 0c5dd03 | 2014-09-29 01:31:39 -0700 | [diff] [blame] | 2924 | rpages_ind = pageind - arena_mapbits_small_runind_get(chunk, pageind); |
Jason Evans | 61a6dfc | 2016-03-23 16:04:38 -0700 | [diff] [blame] | 2925 | run = &arena_miscelm_get_mutable(chunk, rpages_ind)->run; |
Jason Evans | 381c23d | 2014-10-10 23:01:03 -0700 | [diff] [blame] | 2926 | bin = &arena->bins[run->binind]; |
Jason Evans | b2c0d63 | 2016-04-13 23:36:15 -0700 | [diff] [blame] | 2927 | malloc_mutex_lock(tsd, &bin->lock); |
| 2928 | arena_dalloc_bin_locked_impl(tsd, arena, chunk, ptr, bitselm, false); |
| 2929 | malloc_mutex_unlock(tsd, &bin->lock); |
Jason Evans | 203484e | 2012-05-02 00:30:36 -0700 | [diff] [blame] | 2930 | } |
| 2931 | |
| 2932 | void |
Jason Evans | 243f7a0 | 2016-02-19 20:09:31 -0800 | [diff] [blame] | 2933 | arena_dalloc_small(tsd_t *tsd, arena_t *arena, arena_chunk_t *chunk, void *ptr, |
Jason Evans | 203484e | 2012-05-02 00:30:36 -0700 | [diff] [blame] | 2934 | size_t pageind) |
| 2935 | { |
Qinfan Wu | ff6a31d | 2014-08-29 13:34:40 -0700 | [diff] [blame] | 2936 | arena_chunk_map_bits_t *bitselm; |
Jason Evans | 203484e | 2012-05-02 00:30:36 -0700 | [diff] [blame] | 2937 | |
| 2938 | if (config_debug) { |
Jason Evans | 80737c3 | 2012-05-02 16:11:03 -0700 | [diff] [blame] | 2939 | /* arena_ptr_small_binind_get() does extra sanity checking. */ |
| 2940 | assert(arena_ptr_small_binind_get(ptr, arena_mapbits_get(chunk, |
| 2941 | pageind)) != BININD_INVALID); |
Jason Evans | 203484e | 2012-05-02 00:30:36 -0700 | [diff] [blame] | 2942 | } |
Jason Evans | 61a6dfc | 2016-03-23 16:04:38 -0700 | [diff] [blame] | 2943 | bitselm = arena_bitselm_get_mutable(chunk, pageind); |
Jason Evans | b2c0d63 | 2016-04-13 23:36:15 -0700 | [diff] [blame] | 2944 | arena_dalloc_bin(tsd, arena, chunk, ptr, pageind, bitselm); |
Jason Evans | 243f7a0 | 2016-02-19 20:09:31 -0800 | [diff] [blame] | 2945 | arena_decay_tick(tsd, arena); |
Jason Evans | 203484e | 2012-05-02 00:30:36 -0700 | [diff] [blame] | 2946 | } |
Jason Evans | e476f8a | 2010-01-16 09:53:50 -0800 | [diff] [blame] | 2947 | |
Jason Evans | 6b694c4 | 2014-01-07 16:47:56 -0800 | [diff] [blame] | 2948 | #ifdef JEMALLOC_JET |
| 2949 | #undef arena_dalloc_junk_large |
Jason Evans | ab0cfe0 | 2016-04-18 15:11:20 -0700 | [diff] [blame] | 2950 | #define arena_dalloc_junk_large JEMALLOC_N(n_arena_dalloc_junk_large) |
Jason Evans | 6b694c4 | 2014-01-07 16:47:56 -0800 | [diff] [blame] | 2951 | #endif |
Jason Evans | fc0b3b7 | 2014-10-09 17:54:06 -0700 | [diff] [blame] | 2952 | void |
Jason Evans | 6b694c4 | 2014-01-07 16:47:56 -0800 | [diff] [blame] | 2953 | arena_dalloc_junk_large(void *ptr, size_t usize) |
| 2954 | { |
| 2955 | |
Guilherme Goncalves | 2c5cb61 | 2014-12-08 19:12:41 -0200 | [diff] [blame] | 2956 | if (config_fill && unlikely(opt_junk_free)) |
Chris Peterson | a82070e | 2016-03-27 23:28:39 -0700 | [diff] [blame] | 2957 | memset(ptr, JEMALLOC_FREE_JUNK, usize); |
Jason Evans | 6b694c4 | 2014-01-07 16:47:56 -0800 | [diff] [blame] | 2958 | } |
| 2959 | #ifdef JEMALLOC_JET |
| 2960 | #undef arena_dalloc_junk_large |
| 2961 | #define arena_dalloc_junk_large JEMALLOC_N(arena_dalloc_junk_large) |
| 2962 | arena_dalloc_junk_large_t *arena_dalloc_junk_large = |
Jason Evans | ab0cfe0 | 2016-04-18 15:11:20 -0700 | [diff] [blame] | 2963 | JEMALLOC_N(n_arena_dalloc_junk_large); |
Jason Evans | 6b694c4 | 2014-01-07 16:47:56 -0800 | [diff] [blame] | 2964 | #endif |
| 2965 | |
Jason Evans | e56b24e | 2015-09-20 09:58:10 -0700 | [diff] [blame] | 2966 | static void |
Jason Evans | b2c0d63 | 2016-04-13 23:36:15 -0700 | [diff] [blame] | 2967 | arena_dalloc_large_locked_impl(tsd_t *tsd, arena_t *arena, arena_chunk_t *chunk, |
Jason Evans | fc0b3b7 | 2014-10-09 17:54:06 -0700 | [diff] [blame] | 2968 | void *ptr, bool junked) |
Jason Evans | e476f8a | 2010-01-16 09:53:50 -0800 | [diff] [blame] | 2969 | { |
Jason Evans | 0c5dd03 | 2014-09-29 01:31:39 -0700 | [diff] [blame] | 2970 | size_t pageind = ((uintptr_t)ptr - (uintptr_t)chunk) >> LG_PAGE; |
Jason Evans | 61a6dfc | 2016-03-23 16:04:38 -0700 | [diff] [blame] | 2971 | arena_chunk_map_misc_t *miscelm = arena_miscelm_get_mutable(chunk, |
| 2972 | pageind); |
Jason Evans | 0c5dd03 | 2014-09-29 01:31:39 -0700 | [diff] [blame] | 2973 | arena_run_t *run = &miscelm->run; |
Jason Evans | 1366826 | 2010-01-31 03:57:29 -0800 | [diff] [blame] | 2974 | |
Jason Evans | 7372b15 | 2012-02-10 20:22:09 -0800 | [diff] [blame] | 2975 | if (config_fill || config_stats) { |
Jason Evans | 8a03cf0 | 2015-05-04 09:58:36 -0700 | [diff] [blame] | 2976 | size_t usize = arena_mapbits_large_size_get(chunk, pageind) - |
| 2977 | large_pad; |
Jason Evans | e476f8a | 2010-01-16 09:53:50 -0800 | [diff] [blame] | 2978 | |
Jason Evans | fc0b3b7 | 2014-10-09 17:54:06 -0700 | [diff] [blame] | 2979 | if (!junked) |
| 2980 | arena_dalloc_junk_large(ptr, usize); |
Jason Evans | 7372b15 | 2012-02-10 20:22:09 -0800 | [diff] [blame] | 2981 | if (config_stats) { |
Jason Evans | d01fd19 | 2015-08-19 15:21:32 -0700 | [diff] [blame] | 2982 | szind_t index = size2index(usize) - NBINS; |
Jason Evans | 155bfa7 | 2014-10-05 17:54:10 -0700 | [diff] [blame] | 2983 | |
Jason Evans | 7372b15 | 2012-02-10 20:22:09 -0800 | [diff] [blame] | 2984 | arena->stats.ndalloc_large++; |
Jason Evans | 6b694c4 | 2014-01-07 16:47:56 -0800 | [diff] [blame] | 2985 | arena->stats.allocated_large -= usize; |
Jason Evans | 155bfa7 | 2014-10-05 17:54:10 -0700 | [diff] [blame] | 2986 | arena->stats.lstats[index].ndalloc++; |
| 2987 | arena->stats.lstats[index].curruns--; |
Jason Evans | 7372b15 | 2012-02-10 20:22:09 -0800 | [diff] [blame] | 2988 | } |
Jason Evans | e476f8a | 2010-01-16 09:53:50 -0800 | [diff] [blame] | 2989 | } |
Jason Evans | e476f8a | 2010-01-16 09:53:50 -0800 | [diff] [blame] | 2990 | |
Jason Evans | b2c0d63 | 2016-04-13 23:36:15 -0700 | [diff] [blame] | 2991 | arena_run_dalloc(tsd, arena, run, true, false, false); |
Jason Evans | e476f8a | 2010-01-16 09:53:50 -0800 | [diff] [blame] | 2992 | } |
| 2993 | |
Jason Evans | 203484e | 2012-05-02 00:30:36 -0700 | [diff] [blame] | 2994 | void |
Jason Evans | b2c0d63 | 2016-04-13 23:36:15 -0700 | [diff] [blame] | 2995 | arena_dalloc_large_junked_locked(tsd_t *tsd, arena_t *arena, |
| 2996 | arena_chunk_t *chunk, void *ptr) |
Jason Evans | fc0b3b7 | 2014-10-09 17:54:06 -0700 | [diff] [blame] | 2997 | { |
| 2998 | |
Jason Evans | b2c0d63 | 2016-04-13 23:36:15 -0700 | [diff] [blame] | 2999 | arena_dalloc_large_locked_impl(tsd, arena, chunk, ptr, true); |
Jason Evans | fc0b3b7 | 2014-10-09 17:54:06 -0700 | [diff] [blame] | 3000 | } |
| 3001 | |
| 3002 | void |
Jason Evans | 243f7a0 | 2016-02-19 20:09:31 -0800 | [diff] [blame] | 3003 | arena_dalloc_large(tsd_t *tsd, arena_t *arena, arena_chunk_t *chunk, void *ptr) |
Jason Evans | 203484e | 2012-05-02 00:30:36 -0700 | [diff] [blame] | 3004 | { |
| 3005 | |
Jason Evans | b2c0d63 | 2016-04-13 23:36:15 -0700 | [diff] [blame] | 3006 | malloc_mutex_lock(tsd, &arena->lock); |
| 3007 | arena_dalloc_large_locked_impl(tsd, arena, chunk, ptr, false); |
| 3008 | malloc_mutex_unlock(tsd, &arena->lock); |
Jason Evans | 243f7a0 | 2016-02-19 20:09:31 -0800 | [diff] [blame] | 3009 | arena_decay_tick(tsd, arena); |
Jason Evans | 203484e | 2012-05-02 00:30:36 -0700 | [diff] [blame] | 3010 | } |
| 3011 | |
Jason Evans | e476f8a | 2010-01-16 09:53:50 -0800 | [diff] [blame] | 3012 | static void |
Jason Evans | b2c0d63 | 2016-04-13 23:36:15 -0700 | [diff] [blame] | 3013 | arena_ralloc_large_shrink(tsd_t *tsd, arena_t *arena, arena_chunk_t *chunk, |
| 3014 | void *ptr, size_t oldsize, size_t size) |
Jason Evans | e476f8a | 2010-01-16 09:53:50 -0800 | [diff] [blame] | 3015 | { |
Jason Evans | 0c5dd03 | 2014-09-29 01:31:39 -0700 | [diff] [blame] | 3016 | size_t pageind = ((uintptr_t)ptr - (uintptr_t)chunk) >> LG_PAGE; |
Jason Evans | 61a6dfc | 2016-03-23 16:04:38 -0700 | [diff] [blame] | 3017 | arena_chunk_map_misc_t *miscelm = arena_miscelm_get_mutable(chunk, |
| 3018 | pageind); |
Jason Evans | 0c5dd03 | 2014-09-29 01:31:39 -0700 | [diff] [blame] | 3019 | arena_run_t *run = &miscelm->run; |
Jason Evans | e476f8a | 2010-01-16 09:53:50 -0800 | [diff] [blame] | 3020 | |
| 3021 | assert(size < oldsize); |
| 3022 | |
| 3023 | /* |
| 3024 | * Shrink the run, and make trailing pages available for other |
| 3025 | * allocations. |
| 3026 | */ |
Jason Evans | b2c0d63 | 2016-04-13 23:36:15 -0700 | [diff] [blame] | 3027 | malloc_mutex_lock(tsd, &arena->lock); |
| 3028 | arena_run_trim_tail(tsd, arena, chunk, run, oldsize + large_pad, size + |
Jason Evans | 8a03cf0 | 2015-05-04 09:58:36 -0700 | [diff] [blame] | 3029 | large_pad, true); |
Jason Evans | 7372b15 | 2012-02-10 20:22:09 -0800 | [diff] [blame] | 3030 | if (config_stats) { |
Jason Evans | d01fd19 | 2015-08-19 15:21:32 -0700 | [diff] [blame] | 3031 | szind_t oldindex = size2index(oldsize) - NBINS; |
| 3032 | szind_t index = size2index(size) - NBINS; |
Jason Evans | 155bfa7 | 2014-10-05 17:54:10 -0700 | [diff] [blame] | 3033 | |
Jason Evans | 7372b15 | 2012-02-10 20:22:09 -0800 | [diff] [blame] | 3034 | arena->stats.ndalloc_large++; |
| 3035 | arena->stats.allocated_large -= oldsize; |
Jason Evans | 155bfa7 | 2014-10-05 17:54:10 -0700 | [diff] [blame] | 3036 | arena->stats.lstats[oldindex].ndalloc++; |
| 3037 | arena->stats.lstats[oldindex].curruns--; |
Jason Evans | 990d10c | 2010-01-31 03:49:35 -0800 | [diff] [blame] | 3038 | |
Jason Evans | 7372b15 | 2012-02-10 20:22:09 -0800 | [diff] [blame] | 3039 | arena->stats.nmalloc_large++; |
| 3040 | arena->stats.nrequests_large++; |
| 3041 | arena->stats.allocated_large += size; |
Jason Evans | 155bfa7 | 2014-10-05 17:54:10 -0700 | [diff] [blame] | 3042 | arena->stats.lstats[index].nmalloc++; |
| 3043 | arena->stats.lstats[index].nrequests++; |
| 3044 | arena->stats.lstats[index].curruns++; |
Jason Evans | e476f8a | 2010-01-16 09:53:50 -0800 | [diff] [blame] | 3045 | } |
Jason Evans | b2c0d63 | 2016-04-13 23:36:15 -0700 | [diff] [blame] | 3046 | malloc_mutex_unlock(tsd, &arena->lock); |
Jason Evans | e476f8a | 2010-01-16 09:53:50 -0800 | [diff] [blame] | 3047 | } |
| 3048 | |
| 3049 | static bool |
Jason Evans | b2c0d63 | 2016-04-13 23:36:15 -0700 | [diff] [blame] | 3050 | arena_ralloc_large_grow(tsd_t *tsd, arena_t *arena, arena_chunk_t *chunk, |
| 3051 | void *ptr, size_t oldsize, size_t usize_min, size_t usize_max, bool zero) |
Jason Evans | e476f8a | 2010-01-16 09:53:50 -0800 | [diff] [blame] | 3052 | { |
Jason Evans | ae4c7b4 | 2012-04-02 07:04:34 -0700 | [diff] [blame] | 3053 | size_t pageind = ((uintptr_t)ptr - (uintptr_t)chunk) >> LG_PAGE; |
Jason Evans | 5716d97 | 2015-08-06 23:34:12 -0700 | [diff] [blame] | 3054 | size_t npages = (oldsize + large_pad) >> LG_PAGE; |
Jason Evans | 8e3c3c6 | 2010-09-17 15:46:18 -0700 | [diff] [blame] | 3055 | size_t followsize; |
Jason Evans | e476f8a | 2010-01-16 09:53:50 -0800 | [diff] [blame] | 3056 | |
Jason Evans | 8a03cf0 | 2015-05-04 09:58:36 -0700 | [diff] [blame] | 3057 | assert(oldsize == arena_mapbits_large_size_get(chunk, pageind) - |
| 3058 | large_pad); |
Jason Evans | e476f8a | 2010-01-16 09:53:50 -0800 | [diff] [blame] | 3059 | |
| 3060 | /* Try to extend the run. */ |
Jason Evans | b2c0d63 | 2016-04-13 23:36:15 -0700 | [diff] [blame] | 3061 | malloc_mutex_lock(tsd, &arena->lock); |
Jason Evans | 560a4e1 | 2015-09-11 16:18:53 -0700 | [diff] [blame] | 3062 | if (pageind+npages >= chunk_npages || arena_mapbits_allocated_get(chunk, |
| 3063 | pageind+npages) != 0) |
| 3064 | goto label_fail; |
| 3065 | followsize = arena_mapbits_unallocated_size_get(chunk, pageind+npages); |
| 3066 | if (oldsize + followsize >= usize_min) { |
Jason Evans | e476f8a | 2010-01-16 09:53:50 -0800 | [diff] [blame] | 3067 | /* |
| 3068 | * The next run is available and sufficiently large. Split the |
| 3069 | * following run, then merge the first part with the existing |
| 3070 | * allocation. |
| 3071 | */ |
Guilherme Goncalves | 9c6a8d3 | 2014-12-17 14:46:35 -0200 | [diff] [blame] | 3072 | arena_run_t *run; |
Jason Evans | 560a4e1 | 2015-09-11 16:18:53 -0700 | [diff] [blame] | 3073 | size_t usize, splitsize, size, flag_dirty, flag_unzeroed_mask; |
Jason Evans | 155bfa7 | 2014-10-05 17:54:10 -0700 | [diff] [blame] | 3074 | |
Jason Evans | 560a4e1 | 2015-09-11 16:18:53 -0700 | [diff] [blame] | 3075 | usize = usize_max; |
Jason Evans | 155bfa7 | 2014-10-05 17:54:10 -0700 | [diff] [blame] | 3076 | while (oldsize + followsize < usize) |
| 3077 | usize = index2size(size2index(usize)-1); |
| 3078 | assert(usize >= usize_min); |
Jason Evans | 560a4e1 | 2015-09-11 16:18:53 -0700 | [diff] [blame] | 3079 | assert(usize >= oldsize); |
Jason Evans | 5716d97 | 2015-08-06 23:34:12 -0700 | [diff] [blame] | 3080 | splitsize = usize - oldsize; |
Jason Evans | 560a4e1 | 2015-09-11 16:18:53 -0700 | [diff] [blame] | 3081 | if (splitsize == 0) |
| 3082 | goto label_fail; |
Jason Evans | 155bfa7 | 2014-10-05 17:54:10 -0700 | [diff] [blame] | 3083 | |
Jason Evans | 61a6dfc | 2016-03-23 16:04:38 -0700 | [diff] [blame] | 3084 | run = &arena_miscelm_get_mutable(chunk, pageind+npages)->run; |
Jason Evans | 560a4e1 | 2015-09-11 16:18:53 -0700 | [diff] [blame] | 3085 | if (arena_run_split_large(arena, run, splitsize, zero)) |
| 3086 | goto label_fail; |
Jason Evans | e476f8a | 2010-01-16 09:53:50 -0800 | [diff] [blame] | 3087 | |
Jason Evans | d260f44 | 2015-09-24 16:38:45 -0700 | [diff] [blame] | 3088 | if (config_cache_oblivious && zero) { |
| 3089 | /* |
| 3090 | * Zero the trailing bytes of the original allocation's |
| 3091 | * last page, since they are in an indeterminate state. |
Jason Evans | a784e41 | 2015-09-24 22:21:55 -0700 | [diff] [blame] | 3092 | * There will always be trailing bytes, because ptr's |
| 3093 | * offset from the beginning of the run is a multiple of |
| 3094 | * CACHELINE in [0 .. PAGE). |
Jason Evans | d260f44 | 2015-09-24 16:38:45 -0700 | [diff] [blame] | 3095 | */ |
Jason Evans | a784e41 | 2015-09-24 22:21:55 -0700 | [diff] [blame] | 3096 | void *zbase = (void *)((uintptr_t)ptr + oldsize); |
| 3097 | void *zpast = PAGE_ADDR2BASE((void *)((uintptr_t)zbase + |
| 3098 | PAGE)); |
| 3099 | size_t nzero = (uintptr_t)zpast - (uintptr_t)zbase; |
| 3100 | assert(nzero > 0); |
| 3101 | memset(zbase, 0, nzero); |
Jason Evans | d260f44 | 2015-09-24 16:38:45 -0700 | [diff] [blame] | 3102 | } |
| 3103 | |
Jason Evans | 088e6a0 | 2010-10-18 00:04:44 -0700 | [diff] [blame] | 3104 | size = oldsize + splitsize; |
Jason Evans | 5716d97 | 2015-08-06 23:34:12 -0700 | [diff] [blame] | 3105 | npages = (size + large_pad) >> LG_PAGE; |
Jason Evans | 940a2e0 | 2010-10-17 17:51:37 -0700 | [diff] [blame] | 3106 | |
| 3107 | /* |
| 3108 | * Mark the extended run as dirty if either portion of the run |
| 3109 | * was dirty before allocation. This is rather pedantic, |
| 3110 | * because there's not actually any sequence of events that |
| 3111 | * could cause the resulting run to be passed to |
| 3112 | * arena_run_dalloc() with the dirty argument set to false |
| 3113 | * (which is when dirty flag consistency would really matter). |
| 3114 | */ |
Jason Evans | 203484e | 2012-05-02 00:30:36 -0700 | [diff] [blame] | 3115 | flag_dirty = arena_mapbits_dirty_get(chunk, pageind) | |
| 3116 | arena_mapbits_dirty_get(chunk, pageind+npages-1); |
Jason Evans | 1f27abc | 2015-08-11 12:42:33 -0700 | [diff] [blame] | 3117 | flag_unzeroed_mask = flag_dirty == 0 ? CHUNK_MAP_UNZEROED : 0; |
Jason Evans | 5716d97 | 2015-08-06 23:34:12 -0700 | [diff] [blame] | 3118 | arena_mapbits_large_set(chunk, pageind, size + large_pad, |
Jason Evans | 1f27abc | 2015-08-11 12:42:33 -0700 | [diff] [blame] | 3119 | flag_dirty | (flag_unzeroed_mask & |
| 3120 | arena_mapbits_unzeroed_get(chunk, pageind))); |
| 3121 | arena_mapbits_large_set(chunk, pageind+npages-1, 0, flag_dirty | |
| 3122 | (flag_unzeroed_mask & arena_mapbits_unzeroed_get(chunk, |
| 3123 | pageind+npages-1))); |
Jason Evans | e476f8a | 2010-01-16 09:53:50 -0800 | [diff] [blame] | 3124 | |
Jason Evans | 7372b15 | 2012-02-10 20:22:09 -0800 | [diff] [blame] | 3125 | if (config_stats) { |
Jason Evans | d01fd19 | 2015-08-19 15:21:32 -0700 | [diff] [blame] | 3126 | szind_t oldindex = size2index(oldsize) - NBINS; |
| 3127 | szind_t index = size2index(size) - NBINS; |
Jason Evans | 155bfa7 | 2014-10-05 17:54:10 -0700 | [diff] [blame] | 3128 | |
Jason Evans | 7372b15 | 2012-02-10 20:22:09 -0800 | [diff] [blame] | 3129 | arena->stats.ndalloc_large++; |
| 3130 | arena->stats.allocated_large -= oldsize; |
Jason Evans | 155bfa7 | 2014-10-05 17:54:10 -0700 | [diff] [blame] | 3131 | arena->stats.lstats[oldindex].ndalloc++; |
| 3132 | arena->stats.lstats[oldindex].curruns--; |
Jason Evans | 990d10c | 2010-01-31 03:49:35 -0800 | [diff] [blame] | 3133 | |
Jason Evans | 7372b15 | 2012-02-10 20:22:09 -0800 | [diff] [blame] | 3134 | arena->stats.nmalloc_large++; |
| 3135 | arena->stats.nrequests_large++; |
| 3136 | arena->stats.allocated_large += size; |
Jason Evans | 155bfa7 | 2014-10-05 17:54:10 -0700 | [diff] [blame] | 3137 | arena->stats.lstats[index].nmalloc++; |
| 3138 | arena->stats.lstats[index].nrequests++; |
| 3139 | arena->stats.lstats[index].curruns++; |
Jason Evans | 940a2e0 | 2010-10-17 17:51:37 -0700 | [diff] [blame] | 3140 | } |
Jason Evans | b2c0d63 | 2016-04-13 23:36:15 -0700 | [diff] [blame] | 3141 | malloc_mutex_unlock(tsd, &arena->lock); |
Jason Evans | e476f8a | 2010-01-16 09:53:50 -0800 | [diff] [blame] | 3142 | return (false); |
| 3143 | } |
Jason Evans | 560a4e1 | 2015-09-11 16:18:53 -0700 | [diff] [blame] | 3144 | label_fail: |
Jason Evans | b2c0d63 | 2016-04-13 23:36:15 -0700 | [diff] [blame] | 3145 | malloc_mutex_unlock(tsd, &arena->lock); |
Jason Evans | e476f8a | 2010-01-16 09:53:50 -0800 | [diff] [blame] | 3146 | return (true); |
| 3147 | } |
| 3148 | |
Jason Evans | 6b694c4 | 2014-01-07 16:47:56 -0800 | [diff] [blame] | 3149 | #ifdef JEMALLOC_JET |
| 3150 | #undef arena_ralloc_junk_large |
Jason Evans | ab0cfe0 | 2016-04-18 15:11:20 -0700 | [diff] [blame] | 3151 | #define arena_ralloc_junk_large JEMALLOC_N(n_arena_ralloc_junk_large) |
Jason Evans | 6b694c4 | 2014-01-07 16:47:56 -0800 | [diff] [blame] | 3152 | #endif |
| 3153 | static void |
| 3154 | arena_ralloc_junk_large(void *ptr, size_t old_usize, size_t usize) |
| 3155 | { |
| 3156 | |
Guilherme Goncalves | 2c5cb61 | 2014-12-08 19:12:41 -0200 | [diff] [blame] | 3157 | if (config_fill && unlikely(opt_junk_free)) { |
Chris Peterson | a82070e | 2016-03-27 23:28:39 -0700 | [diff] [blame] | 3158 | memset((void *)((uintptr_t)ptr + usize), JEMALLOC_FREE_JUNK, |
Jason Evans | 6b694c4 | 2014-01-07 16:47:56 -0800 | [diff] [blame] | 3159 | old_usize - usize); |
| 3160 | } |
| 3161 | } |
| 3162 | #ifdef JEMALLOC_JET |
| 3163 | #undef arena_ralloc_junk_large |
| 3164 | #define arena_ralloc_junk_large JEMALLOC_N(arena_ralloc_junk_large) |
| 3165 | arena_ralloc_junk_large_t *arena_ralloc_junk_large = |
Jason Evans | ab0cfe0 | 2016-04-18 15:11:20 -0700 | [diff] [blame] | 3166 | JEMALLOC_N(n_arena_ralloc_junk_large); |
Jason Evans | 6b694c4 | 2014-01-07 16:47:56 -0800 | [diff] [blame] | 3167 | #endif |
| 3168 | |
Jason Evans | e476f8a | 2010-01-16 09:53:50 -0800 | [diff] [blame] | 3169 | /* |
| 3170 | * Try to resize a large allocation, in order to avoid copying. This will |
| 3171 | * always fail if growing an object, and the following run is already in use. |
| 3172 | */ |
| 3173 | static bool |
Jason Evans | b2c0d63 | 2016-04-13 23:36:15 -0700 | [diff] [blame] | 3174 | arena_ralloc_large(tsd_t *tsd, void *ptr, size_t oldsize, size_t usize_min, |
Jason Evans | 560a4e1 | 2015-09-11 16:18:53 -0700 | [diff] [blame] | 3175 | size_t usize_max, bool zero) |
Jason Evans | e476f8a | 2010-01-16 09:53:50 -0800 | [diff] [blame] | 3176 | { |
Jason Evans | 560a4e1 | 2015-09-11 16:18:53 -0700 | [diff] [blame] | 3177 | arena_chunk_t *chunk; |
| 3178 | arena_t *arena; |
Jason Evans | e476f8a | 2010-01-16 09:53:50 -0800 | [diff] [blame] | 3179 | |
Jason Evans | 560a4e1 | 2015-09-11 16:18:53 -0700 | [diff] [blame] | 3180 | if (oldsize == usize_max) { |
| 3181 | /* Current size class is compatible and maximal. */ |
Jason Evans | e476f8a | 2010-01-16 09:53:50 -0800 | [diff] [blame] | 3182 | return (false); |
Jason Evans | e476f8a | 2010-01-16 09:53:50 -0800 | [diff] [blame] | 3183 | } |
Jason Evans | 560a4e1 | 2015-09-11 16:18:53 -0700 | [diff] [blame] | 3184 | |
| 3185 | chunk = (arena_chunk_t *)CHUNK_ADDR2BASE(ptr); |
| 3186 | arena = extent_node_arena_get(&chunk->node); |
| 3187 | |
| 3188 | if (oldsize < usize_max) { |
Jason Evans | b2c0d63 | 2016-04-13 23:36:15 -0700 | [diff] [blame] | 3189 | bool ret = arena_ralloc_large_grow(tsd, arena, chunk, ptr, |
| 3190 | oldsize, usize_min, usize_max, zero); |
Jason Evans | 560a4e1 | 2015-09-11 16:18:53 -0700 | [diff] [blame] | 3191 | if (config_fill && !ret && !zero) { |
| 3192 | if (unlikely(opt_junk_alloc)) { |
Chris Peterson | a82070e | 2016-03-27 23:28:39 -0700 | [diff] [blame] | 3193 | memset((void *)((uintptr_t)ptr + oldsize), |
| 3194 | JEMALLOC_ALLOC_JUNK, |
Jason Evans | b2c0d63 | 2016-04-13 23:36:15 -0700 | [diff] [blame] | 3195 | isalloc(tsd, ptr, config_prof) - oldsize); |
Jason Evans | 560a4e1 | 2015-09-11 16:18:53 -0700 | [diff] [blame] | 3196 | } else if (unlikely(opt_zero)) { |
| 3197 | memset((void *)((uintptr_t)ptr + oldsize), 0, |
Jason Evans | b2c0d63 | 2016-04-13 23:36:15 -0700 | [diff] [blame] | 3198 | isalloc(tsd, ptr, config_prof) - oldsize); |
Jason Evans | 560a4e1 | 2015-09-11 16:18:53 -0700 | [diff] [blame] | 3199 | } |
| 3200 | } |
| 3201 | return (ret); |
| 3202 | } |
| 3203 | |
| 3204 | assert(oldsize > usize_max); |
| 3205 | /* Fill before shrinking in order avoid a race. */ |
| 3206 | arena_ralloc_junk_large(ptr, oldsize, usize_max); |
Jason Evans | b2c0d63 | 2016-04-13 23:36:15 -0700 | [diff] [blame] | 3207 | arena_ralloc_large_shrink(tsd, arena, chunk, ptr, oldsize, usize_max); |
Jason Evans | 560a4e1 | 2015-09-11 16:18:53 -0700 | [diff] [blame] | 3208 | return (false); |
Jason Evans | e476f8a | 2010-01-16 09:53:50 -0800 | [diff] [blame] | 3209 | } |
| 3210 | |
Jason Evans | b2c3166 | 2014-01-12 15:05:44 -0800 | [diff] [blame] | 3211 | bool |
Jason Evans | 243f7a0 | 2016-02-19 20:09:31 -0800 | [diff] [blame] | 3212 | arena_ralloc_no_move(tsd_t *tsd, void *ptr, size_t oldsize, size_t size, |
| 3213 | size_t extra, bool zero) |
Jason Evans | e476f8a | 2010-01-16 09:53:50 -0800 | [diff] [blame] | 3214 | { |
Jason Evans | 560a4e1 | 2015-09-11 16:18:53 -0700 | [diff] [blame] | 3215 | size_t usize_min, usize_max; |
Jason Evans | e476f8a | 2010-01-16 09:53:50 -0800 | [diff] [blame] | 3216 | |
Jason Evans | 0c516a0 | 2016-02-25 15:29:49 -0800 | [diff] [blame] | 3217 | /* Calls with non-zero extra had to clamp extra. */ |
| 3218 | assert(extra == 0 || size + extra <= HUGE_MAXCLASS); |
| 3219 | |
Jason Evans | 0c516a0 | 2016-02-25 15:29:49 -0800 | [diff] [blame] | 3220 | if (unlikely(size > HUGE_MAXCLASS)) |
| 3221 | return (true); |
| 3222 | |
Jason Evans | 560a4e1 | 2015-09-11 16:18:53 -0700 | [diff] [blame] | 3223 | usize_min = s2u(size); |
Jason Evans | 560a4e1 | 2015-09-11 16:18:53 -0700 | [diff] [blame] | 3224 | usize_max = s2u(size + extra); |
Jason Evans | 676df88 | 2015-09-11 20:50:20 -0700 | [diff] [blame] | 3225 | if (likely(oldsize <= large_maxclass && usize_min <= large_maxclass)) { |
Jason Evans | 243f7a0 | 2016-02-19 20:09:31 -0800 | [diff] [blame] | 3226 | arena_chunk_t *chunk; |
| 3227 | |
Jason Evans | 88fef7c | 2015-02-12 14:06:37 -0800 | [diff] [blame] | 3228 | /* |
| 3229 | * Avoid moving the allocation if the size class can be left the |
| 3230 | * same. |
| 3231 | */ |
Jason Evans | 560a4e1 | 2015-09-11 16:18:53 -0700 | [diff] [blame] | 3232 | if (oldsize <= SMALL_MAXCLASS) { |
| 3233 | assert(arena_bin_info[size2index(oldsize)].reg_size == |
| 3234 | oldsize); |
Jason Evans | 4985dc6 | 2016-02-19 19:24:58 -0800 | [diff] [blame] | 3235 | if ((usize_max > SMALL_MAXCLASS || |
| 3236 | size2index(usize_max) != size2index(oldsize)) && |
| 3237 | (size > oldsize || usize_max < oldsize)) |
| 3238 | return (true); |
Jason Evans | 560a4e1 | 2015-09-11 16:18:53 -0700 | [diff] [blame] | 3239 | } else { |
Jason Evans | 4985dc6 | 2016-02-19 19:24:58 -0800 | [diff] [blame] | 3240 | if (usize_max <= SMALL_MAXCLASS) |
| 3241 | return (true); |
Jason Evans | b2c0d63 | 2016-04-13 23:36:15 -0700 | [diff] [blame] | 3242 | if (arena_ralloc_large(tsd, ptr, oldsize, usize_min, |
Jason Evans | 4985dc6 | 2016-02-19 19:24:58 -0800 | [diff] [blame] | 3243 | usize_max, zero)) |
| 3244 | return (true); |
Jason Evans | e476f8a | 2010-01-16 09:53:50 -0800 | [diff] [blame] | 3245 | } |
Jason Evans | e476f8a | 2010-01-16 09:53:50 -0800 | [diff] [blame] | 3246 | |
Jason Evans | 243f7a0 | 2016-02-19 20:09:31 -0800 | [diff] [blame] | 3247 | chunk = (arena_chunk_t *)CHUNK_ADDR2BASE(ptr); |
| 3248 | arena_decay_tick(tsd, extent_node_arena_get(&chunk->node)); |
Jason Evans | 4985dc6 | 2016-02-19 19:24:58 -0800 | [diff] [blame] | 3249 | return (false); |
Jason Evans | 560a4e1 | 2015-09-11 16:18:53 -0700 | [diff] [blame] | 3250 | } else { |
Jason Evans | 243f7a0 | 2016-02-19 20:09:31 -0800 | [diff] [blame] | 3251 | return (huge_ralloc_no_move(tsd, ptr, oldsize, usize_min, |
| 3252 | usize_max, zero)); |
Jason Evans | 560a4e1 | 2015-09-11 16:18:53 -0700 | [diff] [blame] | 3253 | } |
| 3254 | } |
| 3255 | |
| 3256 | static void * |
| 3257 | arena_ralloc_move_helper(tsd_t *tsd, arena_t *arena, size_t usize, |
| 3258 | size_t alignment, bool zero, tcache_t *tcache) |
| 3259 | { |
| 3260 | |
| 3261 | if (alignment == 0) |
Qi Wang | f4a0f32 | 2015-10-27 15:12:10 -0700 | [diff] [blame] | 3262 | return (arena_malloc(tsd, arena, usize, size2index(usize), zero, |
| 3263 | tcache, true)); |
Jason Evans | 560a4e1 | 2015-09-11 16:18:53 -0700 | [diff] [blame] | 3264 | usize = sa2u(usize, alignment); |
Jason Evans | 0c516a0 | 2016-02-25 15:29:49 -0800 | [diff] [blame] | 3265 | if (unlikely(usize == 0 || usize > HUGE_MAXCLASS)) |
Jason Evans | 560a4e1 | 2015-09-11 16:18:53 -0700 | [diff] [blame] | 3266 | return (NULL); |
| 3267 | return (ipalloct(tsd, usize, alignment, zero, tcache, arena)); |
Jason Evans | 8e3c3c6 | 2010-09-17 15:46:18 -0700 | [diff] [blame] | 3268 | } |
Jason Evans | e476f8a | 2010-01-16 09:53:50 -0800 | [diff] [blame] | 3269 | |
Jason Evans | 8e3c3c6 | 2010-09-17 15:46:18 -0700 | [diff] [blame] | 3270 | void * |
Jason Evans | 5460aa6 | 2014-09-22 21:09:23 -0700 | [diff] [blame] | 3271 | arena_ralloc(tsd_t *tsd, arena_t *arena, void *ptr, size_t oldsize, size_t size, |
Jason Evans | 560a4e1 | 2015-09-11 16:18:53 -0700 | [diff] [blame] | 3272 | size_t alignment, bool zero, tcache_t *tcache) |
Jason Evans | 8e3c3c6 | 2010-09-17 15:46:18 -0700 | [diff] [blame] | 3273 | { |
| 3274 | void *ret; |
Jason Evans | 560a4e1 | 2015-09-11 16:18:53 -0700 | [diff] [blame] | 3275 | size_t usize; |
Jason Evans | 8e3c3c6 | 2010-09-17 15:46:18 -0700 | [diff] [blame] | 3276 | |
Jason Evans | 560a4e1 | 2015-09-11 16:18:53 -0700 | [diff] [blame] | 3277 | usize = s2u(size); |
Jason Evans | 0c516a0 | 2016-02-25 15:29:49 -0800 | [diff] [blame] | 3278 | if (unlikely(usize == 0 || size > HUGE_MAXCLASS)) |
Jason Evans | 560a4e1 | 2015-09-11 16:18:53 -0700 | [diff] [blame] | 3279 | return (NULL); |
| 3280 | |
Jason Evans | 676df88 | 2015-09-11 20:50:20 -0700 | [diff] [blame] | 3281 | if (likely(usize <= large_maxclass)) { |
Jason Evans | 88fef7c | 2015-02-12 14:06:37 -0800 | [diff] [blame] | 3282 | size_t copysize; |
Jason Evans | 8e3c3c6 | 2010-09-17 15:46:18 -0700 | [diff] [blame] | 3283 | |
Jason Evans | 88fef7c | 2015-02-12 14:06:37 -0800 | [diff] [blame] | 3284 | /* Try to avoid moving the allocation. */ |
Jason Evans | 243f7a0 | 2016-02-19 20:09:31 -0800 | [diff] [blame] | 3285 | if (!arena_ralloc_no_move(tsd, ptr, oldsize, usize, 0, zero)) |
Jason Evans | 88fef7c | 2015-02-12 14:06:37 -0800 | [diff] [blame] | 3286 | return (ptr); |
Jason Evans | 8e3c3c6 | 2010-09-17 15:46:18 -0700 | [diff] [blame] | 3287 | |
Jason Evans | 88fef7c | 2015-02-12 14:06:37 -0800 | [diff] [blame] | 3288 | /* |
| 3289 | * size and oldsize are different enough that we need to move |
| 3290 | * the object. In that case, fall back to allocating new space |
| 3291 | * and copying. |
| 3292 | */ |
Jason Evans | 560a4e1 | 2015-09-11 16:18:53 -0700 | [diff] [blame] | 3293 | ret = arena_ralloc_move_helper(tsd, arena, usize, alignment, |
| 3294 | zero, tcache); |
| 3295 | if (ret == NULL) |
| 3296 | return (NULL); |
Jason Evans | 88fef7c | 2015-02-12 14:06:37 -0800 | [diff] [blame] | 3297 | |
| 3298 | /* |
| 3299 | * Junk/zero-filling were already done by |
| 3300 | * ipalloc()/arena_malloc(). |
| 3301 | */ |
| 3302 | |
Jason Evans | 560a4e1 | 2015-09-11 16:18:53 -0700 | [diff] [blame] | 3303 | copysize = (usize < oldsize) ? usize : oldsize; |
Jason Evans | 88fef7c | 2015-02-12 14:06:37 -0800 | [diff] [blame] | 3304 | JEMALLOC_VALGRIND_MAKE_MEM_UNDEFINED(ret, copysize); |
| 3305 | memcpy(ret, ptr, copysize); |
| 3306 | isqalloc(tsd, ptr, oldsize, tcache); |
| 3307 | } else { |
Jason Evans | 560a4e1 | 2015-09-11 16:18:53 -0700 | [diff] [blame] | 3308 | ret = huge_ralloc(tsd, arena, ptr, oldsize, usize, alignment, |
| 3309 | zero, tcache); |
Jason Evans | 8e3c3c6 | 2010-09-17 15:46:18 -0700 | [diff] [blame] | 3310 | } |
Jason Evans | e476f8a | 2010-01-16 09:53:50 -0800 | [diff] [blame] | 3311 | return (ret); |
Jason Evans | e476f8a | 2010-01-16 09:53:50 -0800 | [diff] [blame] | 3312 | } |
| 3313 | |
Jason Evans | 609ae59 | 2012-10-11 13:53:15 -0700 | [diff] [blame] | 3314 | dss_prec_t |
Jason Evans | b2c0d63 | 2016-04-13 23:36:15 -0700 | [diff] [blame] | 3315 | arena_dss_prec_get(tsd_t *tsd, arena_t *arena) |
Jason Evans | 609ae59 | 2012-10-11 13:53:15 -0700 | [diff] [blame] | 3316 | { |
| 3317 | dss_prec_t ret; |
| 3318 | |
Jason Evans | b2c0d63 | 2016-04-13 23:36:15 -0700 | [diff] [blame] | 3319 | malloc_mutex_lock(tsd, &arena->lock); |
Jason Evans | 609ae59 | 2012-10-11 13:53:15 -0700 | [diff] [blame] | 3320 | ret = arena->dss_prec; |
Jason Evans | b2c0d63 | 2016-04-13 23:36:15 -0700 | [diff] [blame] | 3321 | malloc_mutex_unlock(tsd, &arena->lock); |
Jason Evans | 609ae59 | 2012-10-11 13:53:15 -0700 | [diff] [blame] | 3322 | return (ret); |
| 3323 | } |
| 3324 | |
Jason Evans | 4d434ad | 2014-04-15 12:09:48 -0700 | [diff] [blame] | 3325 | bool |
Jason Evans | b2c0d63 | 2016-04-13 23:36:15 -0700 | [diff] [blame] | 3326 | arena_dss_prec_set(tsd_t *tsd, arena_t *arena, dss_prec_t dss_prec) |
Jason Evans | 609ae59 | 2012-10-11 13:53:15 -0700 | [diff] [blame] | 3327 | { |
| 3328 | |
Jason Evans | 551ebc4 | 2014-10-03 10:16:09 -0700 | [diff] [blame] | 3329 | if (!have_dss) |
Jason Evans | 4d434ad | 2014-04-15 12:09:48 -0700 | [diff] [blame] | 3330 | return (dss_prec != dss_prec_disabled); |
Jason Evans | b2c0d63 | 2016-04-13 23:36:15 -0700 | [diff] [blame] | 3331 | malloc_mutex_lock(tsd, &arena->lock); |
Jason Evans | 609ae59 | 2012-10-11 13:53:15 -0700 | [diff] [blame] | 3332 | arena->dss_prec = dss_prec; |
Jason Evans | b2c0d63 | 2016-04-13 23:36:15 -0700 | [diff] [blame] | 3333 | malloc_mutex_unlock(tsd, &arena->lock); |
Jason Evans | 4d434ad | 2014-04-15 12:09:48 -0700 | [diff] [blame] | 3334 | return (false); |
Jason Evans | 609ae59 | 2012-10-11 13:53:15 -0700 | [diff] [blame] | 3335 | } |
| 3336 | |
Jason Evans | 8d6a3e8 | 2015-03-18 18:55:33 -0700 | [diff] [blame] | 3337 | ssize_t |
| 3338 | arena_lg_dirty_mult_default_get(void) |
| 3339 | { |
| 3340 | |
| 3341 | return ((ssize_t)atomic_read_z((size_t *)&lg_dirty_mult_default)); |
| 3342 | } |
| 3343 | |
| 3344 | bool |
| 3345 | arena_lg_dirty_mult_default_set(ssize_t lg_dirty_mult) |
| 3346 | { |
| 3347 | |
Jason Evans | 243f7a0 | 2016-02-19 20:09:31 -0800 | [diff] [blame] | 3348 | if (opt_purge != purge_mode_ratio) |
| 3349 | return (true); |
Jason Evans | 8d6a3e8 | 2015-03-18 18:55:33 -0700 | [diff] [blame] | 3350 | if (!arena_lg_dirty_mult_valid(lg_dirty_mult)) |
| 3351 | return (true); |
| 3352 | atomic_write_z((size_t *)&lg_dirty_mult_default, (size_t)lg_dirty_mult); |
| 3353 | return (false); |
| 3354 | } |
| 3355 | |
Jason Evans | 243f7a0 | 2016-02-19 20:09:31 -0800 | [diff] [blame] | 3356 | ssize_t |
| 3357 | arena_decay_time_default_get(void) |
| 3358 | { |
| 3359 | |
| 3360 | return ((ssize_t)atomic_read_z((size_t *)&decay_time_default)); |
| 3361 | } |
| 3362 | |
| 3363 | bool |
| 3364 | arena_decay_time_default_set(ssize_t decay_time) |
| 3365 | { |
| 3366 | |
| 3367 | if (opt_purge != purge_mode_decay) |
| 3368 | return (true); |
| 3369 | if (!arena_decay_time_valid(decay_time)) |
| 3370 | return (true); |
| 3371 | atomic_write_z((size_t *)&decay_time_default, (size_t)decay_time); |
| 3372 | return (false); |
| 3373 | } |
| 3374 | |
Jason Evans | 3c07f80 | 2016-02-27 20:40:13 -0800 | [diff] [blame] | 3375 | static void |
| 3376 | arena_basic_stats_merge_locked(arena_t *arena, unsigned *nthreads, |
| 3377 | const char **dss, ssize_t *lg_dirty_mult, ssize_t *decay_time, |
| 3378 | size_t *nactive, size_t *ndirty) |
Jason Evans | 609ae59 | 2012-10-11 13:53:15 -0700 | [diff] [blame] | 3379 | { |
Jason Evans | 609ae59 | 2012-10-11 13:53:15 -0700 | [diff] [blame] | 3380 | |
Jason Evans | 66cd953 | 2016-04-22 14:34:14 -0700 | [diff] [blame] | 3381 | *nthreads += arena_nthreads_get(arena, false); |
Jason Evans | 609ae59 | 2012-10-11 13:53:15 -0700 | [diff] [blame] | 3382 | *dss = dss_prec_names[arena->dss_prec]; |
Jason Evans | 562d266 | 2015-03-24 16:36:12 -0700 | [diff] [blame] | 3383 | *lg_dirty_mult = arena->lg_dirty_mult; |
Jason Evans | 243f7a0 | 2016-02-19 20:09:31 -0800 | [diff] [blame] | 3384 | *decay_time = arena->decay_time; |
Jason Evans | 609ae59 | 2012-10-11 13:53:15 -0700 | [diff] [blame] | 3385 | *nactive += arena->nactive; |
| 3386 | *ndirty += arena->ndirty; |
Jason Evans | 3c07f80 | 2016-02-27 20:40:13 -0800 | [diff] [blame] | 3387 | } |
| 3388 | |
| 3389 | void |
Jason Evans | b2c0d63 | 2016-04-13 23:36:15 -0700 | [diff] [blame] | 3390 | arena_basic_stats_merge(tsd_t *tsd, arena_t *arena, unsigned *nthreads, |
| 3391 | const char **dss, ssize_t *lg_dirty_mult, ssize_t *decay_time, |
| 3392 | size_t *nactive, size_t *ndirty) |
Jason Evans | 3c07f80 | 2016-02-27 20:40:13 -0800 | [diff] [blame] | 3393 | { |
| 3394 | |
Jason Evans | b2c0d63 | 2016-04-13 23:36:15 -0700 | [diff] [blame] | 3395 | malloc_mutex_lock(tsd, &arena->lock); |
Jason Evans | 3c07f80 | 2016-02-27 20:40:13 -0800 | [diff] [blame] | 3396 | arena_basic_stats_merge_locked(arena, nthreads, dss, lg_dirty_mult, |
| 3397 | decay_time, nactive, ndirty); |
Jason Evans | b2c0d63 | 2016-04-13 23:36:15 -0700 | [diff] [blame] | 3398 | malloc_mutex_unlock(tsd, &arena->lock); |
Jason Evans | 3c07f80 | 2016-02-27 20:40:13 -0800 | [diff] [blame] | 3399 | } |
| 3400 | |
| 3401 | void |
Jason Evans | b2c0d63 | 2016-04-13 23:36:15 -0700 | [diff] [blame] | 3402 | arena_stats_merge(tsd_t *tsd, arena_t *arena, unsigned *nthreads, |
| 3403 | const char **dss, ssize_t *lg_dirty_mult, ssize_t *decay_time, |
| 3404 | size_t *nactive, size_t *ndirty, arena_stats_t *astats, |
| 3405 | malloc_bin_stats_t *bstats, malloc_large_stats_t *lstats, |
| 3406 | malloc_huge_stats_t *hstats) |
Jason Evans | 3c07f80 | 2016-02-27 20:40:13 -0800 | [diff] [blame] | 3407 | { |
| 3408 | unsigned i; |
| 3409 | |
| 3410 | cassert(config_stats); |
| 3411 | |
Jason Evans | b2c0d63 | 2016-04-13 23:36:15 -0700 | [diff] [blame] | 3412 | malloc_mutex_lock(tsd, &arena->lock); |
Jason Evans | 3c07f80 | 2016-02-27 20:40:13 -0800 | [diff] [blame] | 3413 | arena_basic_stats_merge_locked(arena, nthreads, dss, lg_dirty_mult, |
| 3414 | decay_time, nactive, ndirty); |
Jason Evans | 609ae59 | 2012-10-11 13:53:15 -0700 | [diff] [blame] | 3415 | |
| 3416 | astats->mapped += arena->stats.mapped; |
| 3417 | astats->npurge += arena->stats.npurge; |
| 3418 | astats->nmadvise += arena->stats.nmadvise; |
| 3419 | astats->purged += arena->stats.purged; |
Jason Evans | 4581b97 | 2014-11-27 17:22:36 -0200 | [diff] [blame] | 3420 | astats->metadata_mapped += arena->stats.metadata_mapped; |
| 3421 | astats->metadata_allocated += arena_metadata_allocated_get(arena); |
Jason Evans | 609ae59 | 2012-10-11 13:53:15 -0700 | [diff] [blame] | 3422 | astats->allocated_large += arena->stats.allocated_large; |
| 3423 | astats->nmalloc_large += arena->stats.nmalloc_large; |
| 3424 | astats->ndalloc_large += arena->stats.ndalloc_large; |
| 3425 | astats->nrequests_large += arena->stats.nrequests_large; |
Jason Evans | e2deab7 | 2014-05-15 22:22:27 -0700 | [diff] [blame] | 3426 | astats->allocated_huge += arena->stats.allocated_huge; |
| 3427 | astats->nmalloc_huge += arena->stats.nmalloc_huge; |
| 3428 | astats->ndalloc_huge += arena->stats.ndalloc_huge; |
Jason Evans | 609ae59 | 2012-10-11 13:53:15 -0700 | [diff] [blame] | 3429 | |
| 3430 | for (i = 0; i < nlclasses; i++) { |
| 3431 | lstats[i].nmalloc += arena->stats.lstats[i].nmalloc; |
| 3432 | lstats[i].ndalloc += arena->stats.lstats[i].ndalloc; |
| 3433 | lstats[i].nrequests += arena->stats.lstats[i].nrequests; |
| 3434 | lstats[i].curruns += arena->stats.lstats[i].curruns; |
| 3435 | } |
Jason Evans | 3c4d92e | 2014-10-12 22:53:59 -0700 | [diff] [blame] | 3436 | |
| 3437 | for (i = 0; i < nhclasses; i++) { |
| 3438 | hstats[i].nmalloc += arena->stats.hstats[i].nmalloc; |
| 3439 | hstats[i].ndalloc += arena->stats.hstats[i].ndalloc; |
| 3440 | hstats[i].curhchunks += arena->stats.hstats[i].curhchunks; |
| 3441 | } |
Jason Evans | b2c0d63 | 2016-04-13 23:36:15 -0700 | [diff] [blame] | 3442 | malloc_mutex_unlock(tsd, &arena->lock); |
Jason Evans | 609ae59 | 2012-10-11 13:53:15 -0700 | [diff] [blame] | 3443 | |
| 3444 | for (i = 0; i < NBINS; i++) { |
| 3445 | arena_bin_t *bin = &arena->bins[i]; |
| 3446 | |
Jason Evans | b2c0d63 | 2016-04-13 23:36:15 -0700 | [diff] [blame] | 3447 | malloc_mutex_lock(tsd, &bin->lock); |
Jason Evans | 609ae59 | 2012-10-11 13:53:15 -0700 | [diff] [blame] | 3448 | bstats[i].nmalloc += bin->stats.nmalloc; |
| 3449 | bstats[i].ndalloc += bin->stats.ndalloc; |
| 3450 | bstats[i].nrequests += bin->stats.nrequests; |
Jason Evans | 3c4d92e | 2014-10-12 22:53:59 -0700 | [diff] [blame] | 3451 | bstats[i].curregs += bin->stats.curregs; |
Jason Evans | 609ae59 | 2012-10-11 13:53:15 -0700 | [diff] [blame] | 3452 | if (config_tcache) { |
| 3453 | bstats[i].nfills += bin->stats.nfills; |
| 3454 | bstats[i].nflushes += bin->stats.nflushes; |
| 3455 | } |
| 3456 | bstats[i].nruns += bin->stats.nruns; |
| 3457 | bstats[i].reruns += bin->stats.reruns; |
| 3458 | bstats[i].curruns += bin->stats.curruns; |
Jason Evans | b2c0d63 | 2016-04-13 23:36:15 -0700 | [diff] [blame] | 3459 | malloc_mutex_unlock(tsd, &bin->lock); |
Jason Evans | 609ae59 | 2012-10-11 13:53:15 -0700 | [diff] [blame] | 3460 | } |
| 3461 | } |
| 3462 | |
Jason Evans | 767d850 | 2016-02-24 23:58:10 -0800 | [diff] [blame] | 3463 | unsigned |
Jason Evans | 66cd953 | 2016-04-22 14:34:14 -0700 | [diff] [blame] | 3464 | arena_nthreads_get(arena_t *arena, bool internal) |
Jason Evans | 767d850 | 2016-02-24 23:58:10 -0800 | [diff] [blame] | 3465 | { |
| 3466 | |
Jason Evans | 66cd953 | 2016-04-22 14:34:14 -0700 | [diff] [blame] | 3467 | return (atomic_read_u(&arena->nthreads[internal])); |
Jason Evans | 767d850 | 2016-02-24 23:58:10 -0800 | [diff] [blame] | 3468 | } |
| 3469 | |
| 3470 | void |
Jason Evans | 66cd953 | 2016-04-22 14:34:14 -0700 | [diff] [blame] | 3471 | arena_nthreads_inc(arena_t *arena, bool internal) |
Jason Evans | 767d850 | 2016-02-24 23:58:10 -0800 | [diff] [blame] | 3472 | { |
| 3473 | |
Jason Evans | 66cd953 | 2016-04-22 14:34:14 -0700 | [diff] [blame] | 3474 | atomic_add_u(&arena->nthreads[internal], 1); |
Jason Evans | 767d850 | 2016-02-24 23:58:10 -0800 | [diff] [blame] | 3475 | } |
| 3476 | |
| 3477 | void |
Jason Evans | 66cd953 | 2016-04-22 14:34:14 -0700 | [diff] [blame] | 3478 | arena_nthreads_dec(arena_t *arena, bool internal) |
Jason Evans | 767d850 | 2016-02-24 23:58:10 -0800 | [diff] [blame] | 3479 | { |
| 3480 | |
Jason Evans | 66cd953 | 2016-04-22 14:34:14 -0700 | [diff] [blame] | 3481 | atomic_sub_u(&arena->nthreads[internal], 1); |
Jason Evans | 767d850 | 2016-02-24 23:58:10 -0800 | [diff] [blame] | 3482 | } |
| 3483 | |
Jason Evans | 8bb3198 | 2014-10-07 23:14:57 -0700 | [diff] [blame] | 3484 | arena_t * |
Jason Evans | b2c0d63 | 2016-04-13 23:36:15 -0700 | [diff] [blame] | 3485 | arena_new(tsd_t *tsd, unsigned ind) |
Jason Evans | e476f8a | 2010-01-16 09:53:50 -0800 | [diff] [blame] | 3486 | { |
Jason Evans | 8bb3198 | 2014-10-07 23:14:57 -0700 | [diff] [blame] | 3487 | arena_t *arena; |
Dave Watson | 3417a30 | 2016-02-23 12:06:21 -0800 | [diff] [blame] | 3488 | size_t arena_size; |
Jason Evans | e476f8a | 2010-01-16 09:53:50 -0800 | [diff] [blame] | 3489 | unsigned i; |
Jason Evans | e476f8a | 2010-01-16 09:53:50 -0800 | [diff] [blame] | 3490 | |
Dave Watson | 3417a30 | 2016-02-23 12:06:21 -0800 | [diff] [blame] | 3491 | /* Compute arena size to incorporate sufficient runs_avail elements. */ |
Jason Evans | c6a2c39 | 2016-03-26 17:30:37 -0700 | [diff] [blame] | 3492 | arena_size = offsetof(arena_t, runs_avail) + (sizeof(arena_run_heap_t) * |
Dave Watson | 3812729 | 2016-02-24 20:10:02 -0800 | [diff] [blame] | 3493 | runs_avail_nclasses); |
Jason Evans | 8bb3198 | 2014-10-07 23:14:57 -0700 | [diff] [blame] | 3494 | /* |
Jason Evans | 3c4d92e | 2014-10-12 22:53:59 -0700 | [diff] [blame] | 3495 | * Allocate arena, arena->lstats, and arena->hstats contiguously, mainly |
| 3496 | * because there is no way to clean up if base_alloc() OOMs. |
Jason Evans | 8bb3198 | 2014-10-07 23:14:57 -0700 | [diff] [blame] | 3497 | */ |
| 3498 | if (config_stats) { |
Jason Evans | b2c0d63 | 2016-04-13 23:36:15 -0700 | [diff] [blame] | 3499 | arena = (arena_t *)base_alloc(tsd, CACHELINE_CEILING(arena_size) |
| 3500 | + QUANTUM_CEILING(nlclasses * sizeof(malloc_large_stats_t) + |
Jason Evans | 3c4d92e | 2014-10-12 22:53:59 -0700 | [diff] [blame] | 3501 | nhclasses) * sizeof(malloc_huge_stats_t)); |
Jason Evans | 8bb3198 | 2014-10-07 23:14:57 -0700 | [diff] [blame] | 3502 | } else |
Jason Evans | b2c0d63 | 2016-04-13 23:36:15 -0700 | [diff] [blame] | 3503 | arena = (arena_t *)base_alloc(tsd, arena_size); |
Jason Evans | 8bb3198 | 2014-10-07 23:14:57 -0700 | [diff] [blame] | 3504 | if (arena == NULL) |
| 3505 | return (NULL); |
| 3506 | |
Jason Evans | 6109fe0 | 2010-02-10 10:37:56 -0800 | [diff] [blame] | 3507 | arena->ind = ind; |
Jason Evans | 66cd953 | 2016-04-22 14:34:14 -0700 | [diff] [blame] | 3508 | arena->nthreads[0] = arena->nthreads[1] = 0; |
Jason Evans | b2c0d63 | 2016-04-13 23:36:15 -0700 | [diff] [blame] | 3509 | if (malloc_mutex_init(&arena->lock, "arena", WITNESS_RANK_ARENA)) |
Jason Evans | cbf3a6d | 2015-02-11 12:24:27 -0800 | [diff] [blame] | 3510 | return (NULL); |
Jason Evans | e476f8a | 2010-01-16 09:53:50 -0800 | [diff] [blame] | 3511 | |
Jason Evans | 7372b15 | 2012-02-10 20:22:09 -0800 | [diff] [blame] | 3512 | if (config_stats) { |
| 3513 | memset(&arena->stats, 0, sizeof(arena_stats_t)); |
Jason Evans | d49cb68 | 2014-11-17 10:31:59 -0800 | [diff] [blame] | 3514 | arena->stats.lstats = (malloc_large_stats_t *)((uintptr_t)arena |
Dave Watson | 3417a30 | 2016-02-23 12:06:21 -0800 | [diff] [blame] | 3515 | + CACHELINE_CEILING(arena_size)); |
Jason Evans | 7372b15 | 2012-02-10 20:22:09 -0800 | [diff] [blame] | 3516 | memset(arena->stats.lstats, 0, nlclasses * |
| 3517 | sizeof(malloc_large_stats_t)); |
Jason Evans | d49cb68 | 2014-11-17 10:31:59 -0800 | [diff] [blame] | 3518 | arena->stats.hstats = (malloc_huge_stats_t *)((uintptr_t)arena |
Dave Watson | 3417a30 | 2016-02-23 12:06:21 -0800 | [diff] [blame] | 3519 | + CACHELINE_CEILING(arena_size) + |
Jason Evans | 3c4d92e | 2014-10-12 22:53:59 -0700 | [diff] [blame] | 3520 | QUANTUM_CEILING(nlclasses * sizeof(malloc_large_stats_t))); |
| 3521 | memset(arena->stats.hstats, 0, nhclasses * |
| 3522 | sizeof(malloc_huge_stats_t)); |
Jason Evans | 7372b15 | 2012-02-10 20:22:09 -0800 | [diff] [blame] | 3523 | if (config_tcache) |
| 3524 | ql_new(&arena->tcache_ql); |
| 3525 | } |
Jason Evans | e476f8a | 2010-01-16 09:53:50 -0800 | [diff] [blame] | 3526 | |
Jason Evans | 7372b15 | 2012-02-10 20:22:09 -0800 | [diff] [blame] | 3527 | if (config_prof) |
| 3528 | arena->prof_accumbytes = 0; |
Jason Evans | d34f9e7 | 2010-02-11 13:19:21 -0800 | [diff] [blame] | 3529 | |
Jason Evans | 8a03cf0 | 2015-05-04 09:58:36 -0700 | [diff] [blame] | 3530 | if (config_cache_oblivious) { |
| 3531 | /* |
| 3532 | * A nondeterministic seed based on the address of arena reduces |
| 3533 | * the likelihood of lockstep non-uniform cache index |
| 3534 | * utilization among identical concurrent processes, but at the |
| 3535 | * cost of test repeatability. For debug builds, instead use a |
| 3536 | * deterministic seed. |
| 3537 | */ |
| 3538 | arena->offset_state = config_debug ? ind : |
| 3539 | (uint64_t)(uintptr_t)arena; |
| 3540 | } |
| 3541 | |
Jason Evans | b2c0d63 | 2016-04-13 23:36:15 -0700 | [diff] [blame] | 3542 | arena->dss_prec = chunk_dss_prec_get(tsd); |
Jason Evans | 609ae59 | 2012-10-11 13:53:15 -0700 | [diff] [blame] | 3543 | |
Jason Evans | 19ff2ce | 2016-04-22 14:37:17 -0700 | [diff] [blame] | 3544 | ql_new(&arena->achunks); |
| 3545 | |
Jason Evans | e476f8a | 2010-01-16 09:53:50 -0800 | [diff] [blame] | 3546 | arena->spare = NULL; |
| 3547 | |
Jason Evans | 8d6a3e8 | 2015-03-18 18:55:33 -0700 | [diff] [blame] | 3548 | arena->lg_dirty_mult = arena_lg_dirty_mult_default_get(); |
Jason Evans | 0a9f9a4 | 2015-06-22 18:50:32 -0700 | [diff] [blame] | 3549 | arena->purging = false; |
Jason Evans | e476f8a | 2010-01-16 09:53:50 -0800 | [diff] [blame] | 3550 | arena->nactive = 0; |
| 3551 | arena->ndirty = 0; |
| 3552 | |
Dave Watson | 3417a30 | 2016-02-23 12:06:21 -0800 | [diff] [blame] | 3553 | for(i = 0; i < runs_avail_nclasses; i++) |
Jason Evans | c6a2c39 | 2016-03-26 17:30:37 -0700 | [diff] [blame] | 3554 | arena_run_heap_new(&arena->runs_avail[i]); |
Jason Evans | ee41ad4 | 2015-02-15 18:04:46 -0800 | [diff] [blame] | 3555 | qr_new(&arena->runs_dirty, rd_link); |
Jason Evans | 738e089 | 2015-02-18 01:15:50 -0800 | [diff] [blame] | 3556 | qr_new(&arena->chunks_cache, cc_link); |
Jason Evans | ee41ad4 | 2015-02-15 18:04:46 -0800 | [diff] [blame] | 3557 | |
Jason Evans | 243f7a0 | 2016-02-19 20:09:31 -0800 | [diff] [blame] | 3558 | if (opt_purge == purge_mode_decay) |
| 3559 | arena_decay_init(arena, arena_decay_time_default_get()); |
| 3560 | |
Jason Evans | ee41ad4 | 2015-02-15 18:04:46 -0800 | [diff] [blame] | 3561 | ql_new(&arena->huge); |
Jason Evans | b2c0d63 | 2016-04-13 23:36:15 -0700 | [diff] [blame] | 3562 | if (malloc_mutex_init(&arena->huge_mtx, "arena_huge", |
| 3563 | WITNESS_RANK_ARENA_HUGE)) |
Jason Evans | ee41ad4 | 2015-02-15 18:04:46 -0800 | [diff] [blame] | 3564 | return (NULL); |
| 3565 | |
Jason Evans | b49a334 | 2015-07-28 11:28:19 -0400 | [diff] [blame] | 3566 | extent_tree_szad_new(&arena->chunks_szad_cached); |
| 3567 | extent_tree_ad_new(&arena->chunks_ad_cached); |
| 3568 | extent_tree_szad_new(&arena->chunks_szad_retained); |
| 3569 | extent_tree_ad_new(&arena->chunks_ad_retained); |
Jason Evans | b2c0d63 | 2016-04-13 23:36:15 -0700 | [diff] [blame] | 3570 | if (malloc_mutex_init(&arena->chunks_mtx, "arena_chunks", |
| 3571 | WITNESS_RANK_ARENA_CHUNKS)) |
Jason Evans | ee41ad4 | 2015-02-15 18:04:46 -0800 | [diff] [blame] | 3572 | return (NULL); |
| 3573 | ql_new(&arena->node_cache); |
Jason Evans | b2c0d63 | 2016-04-13 23:36:15 -0700 | [diff] [blame] | 3574 | if (malloc_mutex_init(&arena->node_cache_mtx, "arena_node_cache", |
| 3575 | WITNESS_RANK_ARENA_NODE_CACHE)) |
Jason Evans | ee41ad4 | 2015-02-15 18:04:46 -0800 | [diff] [blame] | 3576 | return (NULL); |
| 3577 | |
Jason Evans | b49a334 | 2015-07-28 11:28:19 -0400 | [diff] [blame] | 3578 | arena->chunk_hooks = chunk_hooks_default; |
Jason Evans | e476f8a | 2010-01-16 09:53:50 -0800 | [diff] [blame] | 3579 | |
| 3580 | /* Initialize bins. */ |
Jason Evans | b172610 | 2012-02-28 16:50:47 -0800 | [diff] [blame] | 3581 | for (i = 0; i < NBINS; i++) { |
Jason Evans | c9a4bf9 | 2016-04-22 14:36:48 -0700 | [diff] [blame] | 3582 | arena_bin_t *bin = &arena->bins[i]; |
Jason Evans | b2c0d63 | 2016-04-13 23:36:15 -0700 | [diff] [blame] | 3583 | if (malloc_mutex_init(&bin->lock, "arena_bin", |
| 3584 | WITNESS_RANK_ARENA_BIN)) |
Jason Evans | 8bb3198 | 2014-10-07 23:14:57 -0700 | [diff] [blame] | 3585 | return (NULL); |
Jason Evans | e476f8a | 2010-01-16 09:53:50 -0800 | [diff] [blame] | 3586 | bin->runcur = NULL; |
Jason Evans | c6a2c39 | 2016-03-26 17:30:37 -0700 | [diff] [blame] | 3587 | arena_run_heap_new(&bin->runs); |
Jason Evans | 7372b15 | 2012-02-10 20:22:09 -0800 | [diff] [blame] | 3588 | if (config_stats) |
| 3589 | memset(&bin->stats, 0, sizeof(malloc_bin_stats_t)); |
Jason Evans | e476f8a | 2010-01-16 09:53:50 -0800 | [diff] [blame] | 3590 | } |
| 3591 | |
Jason Evans | 8bb3198 | 2014-10-07 23:14:57 -0700 | [diff] [blame] | 3592 | return (arena); |
Jason Evans | e476f8a | 2010-01-16 09:53:50 -0800 | [diff] [blame] | 3593 | } |
| 3594 | |
Jason Evans | 49f7e8f | 2011-03-15 13:59:15 -0700 | [diff] [blame] | 3595 | /* |
| 3596 | * Calculate bin_info->run_size such that it meets the following constraints: |
| 3597 | * |
Jason Evans | 155bfa7 | 2014-10-05 17:54:10 -0700 | [diff] [blame] | 3598 | * *) bin_info->run_size <= arena_maxrun |
Jason Evans | 47e57f9 | 2011-03-22 09:00:56 -0700 | [diff] [blame] | 3599 | * *) bin_info->nregs <= RUN_MAXREGS |
Jason Evans | 49f7e8f | 2011-03-15 13:59:15 -0700 | [diff] [blame] | 3600 | * |
Jason Evans | 0c5dd03 | 2014-09-29 01:31:39 -0700 | [diff] [blame] | 3601 | * bin_info->nregs and bin_info->reg0_offset are also calculated here, since |
| 3602 | * these settings are all interdependent. |
Jason Evans | 49f7e8f | 2011-03-15 13:59:15 -0700 | [diff] [blame] | 3603 | */ |
Jason Evans | 0c5dd03 | 2014-09-29 01:31:39 -0700 | [diff] [blame] | 3604 | static void |
| 3605 | bin_info_run_size_calc(arena_bin_info_t *bin_info) |
Jason Evans | 49f7e8f | 2011-03-15 13:59:15 -0700 | [diff] [blame] | 3606 | { |
Jason Evans | 122449b | 2012-04-06 00:35:09 -0700 | [diff] [blame] | 3607 | size_t pad_size; |
Jason Evans | 0c5dd03 | 2014-09-29 01:31:39 -0700 | [diff] [blame] | 3608 | size_t try_run_size, perfect_run_size, actual_run_size; |
| 3609 | uint32_t try_nregs, perfect_nregs, actual_nregs; |
Jason Evans | 49f7e8f | 2011-03-15 13:59:15 -0700 | [diff] [blame] | 3610 | |
| 3611 | /* |
Jason Evans | 122449b | 2012-04-06 00:35:09 -0700 | [diff] [blame] | 3612 | * Determine redzone size based on minimum alignment and minimum |
| 3613 | * redzone size. Add padding to the end of the run if it is needed to |
| 3614 | * align the regions. The padding allows each redzone to be half the |
| 3615 | * minimum alignment; without the padding, each redzone would have to |
| 3616 | * be twice as large in order to maintain alignment. |
| 3617 | */ |
Jason Evans | 9c640bf | 2014-09-11 16:20:44 -0700 | [diff] [blame] | 3618 | if (config_fill && unlikely(opt_redzone)) { |
Jason Evans | 9f4ee60 | 2016-02-24 10:32:45 -0800 | [diff] [blame] | 3619 | size_t align_min = ZU(1) << (ffs_zu(bin_info->reg_size) - 1); |
Jason Evans | 122449b | 2012-04-06 00:35:09 -0700 | [diff] [blame] | 3620 | if (align_min <= REDZONE_MINSIZE) { |
| 3621 | bin_info->redzone_size = REDZONE_MINSIZE; |
| 3622 | pad_size = 0; |
| 3623 | } else { |
| 3624 | bin_info->redzone_size = align_min >> 1; |
| 3625 | pad_size = bin_info->redzone_size; |
| 3626 | } |
| 3627 | } else { |
| 3628 | bin_info->redzone_size = 0; |
| 3629 | pad_size = 0; |
| 3630 | } |
| 3631 | bin_info->reg_interval = bin_info->reg_size + |
| 3632 | (bin_info->redzone_size << 1); |
| 3633 | |
| 3634 | /* |
Jason Evans | 0c5dd03 | 2014-09-29 01:31:39 -0700 | [diff] [blame] | 3635 | * Compute run size under ideal conditions (no redzones, no limit on run |
| 3636 | * size). |
Jason Evans | 49f7e8f | 2011-03-15 13:59:15 -0700 | [diff] [blame] | 3637 | */ |
Jason Evans | 0c5dd03 | 2014-09-29 01:31:39 -0700 | [diff] [blame] | 3638 | try_run_size = PAGE; |
Jason Evans | 9e1810c | 2016-02-24 12:42:23 -0800 | [diff] [blame] | 3639 | try_nregs = (uint32_t)(try_run_size / bin_info->reg_size); |
Jason Evans | 49f7e8f | 2011-03-15 13:59:15 -0700 | [diff] [blame] | 3640 | do { |
Jason Evans | 0c5dd03 | 2014-09-29 01:31:39 -0700 | [diff] [blame] | 3641 | perfect_run_size = try_run_size; |
| 3642 | perfect_nregs = try_nregs; |
Jason Evans | 49f7e8f | 2011-03-15 13:59:15 -0700 | [diff] [blame] | 3643 | |
Jason Evans | ae4c7b4 | 2012-04-02 07:04:34 -0700 | [diff] [blame] | 3644 | try_run_size += PAGE; |
Jason Evans | 9e1810c | 2016-02-24 12:42:23 -0800 | [diff] [blame] | 3645 | try_nregs = (uint32_t)(try_run_size / bin_info->reg_size); |
Jason Evans | 0c5dd03 | 2014-09-29 01:31:39 -0700 | [diff] [blame] | 3646 | } while (perfect_run_size != perfect_nregs * bin_info->reg_size); |
| 3647 | assert(perfect_nregs <= RUN_MAXREGS); |
Jason Evans | 49f7e8f | 2011-03-15 13:59:15 -0700 | [diff] [blame] | 3648 | |
Jason Evans | 0c5dd03 | 2014-09-29 01:31:39 -0700 | [diff] [blame] | 3649 | actual_run_size = perfect_run_size; |
Jason Evans | 9e1810c | 2016-02-24 12:42:23 -0800 | [diff] [blame] | 3650 | actual_nregs = (uint32_t)((actual_run_size - pad_size) / |
| 3651 | bin_info->reg_interval); |
Jason Evans | 0c5dd03 | 2014-09-29 01:31:39 -0700 | [diff] [blame] | 3652 | |
| 3653 | /* |
| 3654 | * Redzones can require enough padding that not even a single region can |
| 3655 | * fit within the number of pages that would normally be dedicated to a |
| 3656 | * run for this size class. Increase the run size until at least one |
| 3657 | * region fits. |
| 3658 | */ |
| 3659 | while (actual_nregs == 0) { |
| 3660 | assert(config_fill && unlikely(opt_redzone)); |
| 3661 | |
| 3662 | actual_run_size += PAGE; |
Jason Evans | 9e1810c | 2016-02-24 12:42:23 -0800 | [diff] [blame] | 3663 | actual_nregs = (uint32_t)((actual_run_size - pad_size) / |
| 3664 | bin_info->reg_interval); |
Jason Evans | 0c5dd03 | 2014-09-29 01:31:39 -0700 | [diff] [blame] | 3665 | } |
| 3666 | |
| 3667 | /* |
| 3668 | * Make sure that the run will fit within an arena chunk. |
| 3669 | */ |
Jason Evans | 155bfa7 | 2014-10-05 17:54:10 -0700 | [diff] [blame] | 3670 | while (actual_run_size > arena_maxrun) { |
Jason Evans | 0c5dd03 | 2014-09-29 01:31:39 -0700 | [diff] [blame] | 3671 | actual_run_size -= PAGE; |
Jason Evans | 9e1810c | 2016-02-24 12:42:23 -0800 | [diff] [blame] | 3672 | actual_nregs = (uint32_t)((actual_run_size - pad_size) / |
| 3673 | bin_info->reg_interval); |
Jason Evans | 0c5dd03 | 2014-09-29 01:31:39 -0700 | [diff] [blame] | 3674 | } |
| 3675 | assert(actual_nregs > 0); |
Jason Evans | 5707d6f | 2015-03-06 17:14:05 -0800 | [diff] [blame] | 3676 | assert(actual_run_size == s2u(actual_run_size)); |
Jason Evans | 49f7e8f | 2011-03-15 13:59:15 -0700 | [diff] [blame] | 3677 | |
| 3678 | /* Copy final settings. */ |
Jason Evans | 0c5dd03 | 2014-09-29 01:31:39 -0700 | [diff] [blame] | 3679 | bin_info->run_size = actual_run_size; |
| 3680 | bin_info->nregs = actual_nregs; |
Jason Evans | 9e1810c | 2016-02-24 12:42:23 -0800 | [diff] [blame] | 3681 | bin_info->reg0_offset = (uint32_t)(actual_run_size - (actual_nregs * |
| 3682 | bin_info->reg_interval) - pad_size + bin_info->redzone_size); |
Jason Evans | 122449b | 2012-04-06 00:35:09 -0700 | [diff] [blame] | 3683 | |
Jason Evans | 8a03cf0 | 2015-05-04 09:58:36 -0700 | [diff] [blame] | 3684 | if (actual_run_size > small_maxrun) |
| 3685 | small_maxrun = actual_run_size; |
| 3686 | |
Jason Evans | 122449b | 2012-04-06 00:35:09 -0700 | [diff] [blame] | 3687 | assert(bin_info->reg0_offset - bin_info->redzone_size + (bin_info->nregs |
| 3688 | * bin_info->reg_interval) + pad_size == bin_info->run_size); |
Jason Evans | 49f7e8f | 2011-03-15 13:59:15 -0700 | [diff] [blame] | 3689 | } |
| 3690 | |
Jason Evans | b172610 | 2012-02-28 16:50:47 -0800 | [diff] [blame] | 3691 | static void |
Jason Evans | 49f7e8f | 2011-03-15 13:59:15 -0700 | [diff] [blame] | 3692 | bin_info_init(void) |
| 3693 | { |
| 3694 | arena_bin_info_t *bin_info; |
Jason Evans | 49f7e8f | 2011-03-15 13:59:15 -0700 | [diff] [blame] | 3695 | |
Jason Evans | 8a03cf0 | 2015-05-04 09:58:36 -0700 | [diff] [blame] | 3696 | #define BIN_INFO_INIT_bin_yes(index, size) \ |
Jason Evans | d04047c | 2014-05-28 16:11:55 -0700 | [diff] [blame] | 3697 | bin_info = &arena_bin_info[index]; \ |
Jason Evans | b172610 | 2012-02-28 16:50:47 -0800 | [diff] [blame] | 3698 | bin_info->reg_size = size; \ |
Jason Evans | 0c5dd03 | 2014-09-29 01:31:39 -0700 | [diff] [blame] | 3699 | bin_info_run_size_calc(bin_info); \ |
Jason Evans | b172610 | 2012-02-28 16:50:47 -0800 | [diff] [blame] | 3700 | bitmap_info_init(&bin_info->bitmap_info, bin_info->nregs); |
Jason Evans | d04047c | 2014-05-28 16:11:55 -0700 | [diff] [blame] | 3701 | #define BIN_INFO_INIT_bin_no(index, size) |
| 3702 | #define SC(index, lg_grp, lg_delta, ndelta, bin, lg_delta_lookup) \ |
| 3703 | BIN_INFO_INIT_bin_##bin(index, (ZU(1)<<lg_grp) + (ZU(ndelta)<<lg_delta)) |
Jason Evans | b172610 | 2012-02-28 16:50:47 -0800 | [diff] [blame] | 3704 | SIZE_CLASSES |
Jason Evans | d04047c | 2014-05-28 16:11:55 -0700 | [diff] [blame] | 3705 | #undef BIN_INFO_INIT_bin_yes |
| 3706 | #undef BIN_INFO_INIT_bin_no |
| 3707 | #undef SC |
Jason Evans | 49f7e8f | 2011-03-15 13:59:15 -0700 | [diff] [blame] | 3708 | } |
| 3709 | |
Jason Evans | 8a03cf0 | 2015-05-04 09:58:36 -0700 | [diff] [blame] | 3710 | static bool |
| 3711 | small_run_size_init(void) |
| 3712 | { |
| 3713 | |
| 3714 | assert(small_maxrun != 0); |
| 3715 | |
Jason Evans | b2c0d63 | 2016-04-13 23:36:15 -0700 | [diff] [blame] | 3716 | small_run_tab = (bool *)base_alloc(NULL, sizeof(bool) * (small_maxrun >> |
Jason Evans | 8a03cf0 | 2015-05-04 09:58:36 -0700 | [diff] [blame] | 3717 | LG_PAGE)); |
| 3718 | if (small_run_tab == NULL) |
| 3719 | return (true); |
| 3720 | |
| 3721 | #define TAB_INIT_bin_yes(index, size) { \ |
| 3722 | arena_bin_info_t *bin_info = &arena_bin_info[index]; \ |
| 3723 | small_run_tab[bin_info->run_size >> LG_PAGE] = true; \ |
| 3724 | } |
| 3725 | #define TAB_INIT_bin_no(index, size) |
| 3726 | #define SC(index, lg_grp, lg_delta, ndelta, bin, lg_delta_lookup) \ |
| 3727 | TAB_INIT_bin_##bin(index, (ZU(1)<<lg_grp) + (ZU(ndelta)<<lg_delta)) |
| 3728 | SIZE_CLASSES |
| 3729 | #undef TAB_INIT_bin_yes |
| 3730 | #undef TAB_INIT_bin_no |
| 3731 | #undef SC |
| 3732 | |
| 3733 | return (false); |
| 3734 | } |
| 3735 | |
Jason Evans | 0da8ce1 | 2016-02-22 16:20:56 -0800 | [diff] [blame] | 3736 | static bool |
| 3737 | run_quantize_init(void) |
| 3738 | { |
| 3739 | unsigned i; |
| 3740 | |
| 3741 | run_quantize_max = chunksize + large_pad; |
| 3742 | |
Jason Evans | b2c0d63 | 2016-04-13 23:36:15 -0700 | [diff] [blame] | 3743 | run_quantize_floor_tab = (size_t *)base_alloc(NULL, sizeof(size_t) * |
Jason Evans | 0da8ce1 | 2016-02-22 16:20:56 -0800 | [diff] [blame] | 3744 | (run_quantize_max >> LG_PAGE)); |
| 3745 | if (run_quantize_floor_tab == NULL) |
| 3746 | return (true); |
| 3747 | |
Jason Evans | b2c0d63 | 2016-04-13 23:36:15 -0700 | [diff] [blame] | 3748 | run_quantize_ceil_tab = (size_t *)base_alloc(NULL, sizeof(size_t) * |
Jason Evans | 0da8ce1 | 2016-02-22 16:20:56 -0800 | [diff] [blame] | 3749 | (run_quantize_max >> LG_PAGE)); |
| 3750 | if (run_quantize_ceil_tab == NULL) |
| 3751 | return (true); |
| 3752 | |
| 3753 | for (i = 1; i <= run_quantize_max >> LG_PAGE; i++) { |
| 3754 | size_t run_size = i << LG_PAGE; |
| 3755 | |
| 3756 | run_quantize_floor_tab[i-1] = |
| 3757 | run_quantize_floor_compute(run_size); |
| 3758 | run_quantize_ceil_tab[i-1] = |
| 3759 | run_quantize_ceil_compute(run_size); |
| 3760 | } |
| 3761 | |
| 3762 | return (false); |
| 3763 | } |
| 3764 | |
Jason Evans | 8a03cf0 | 2015-05-04 09:58:36 -0700 | [diff] [blame] | 3765 | bool |
Jason Evans | a0bf242 | 2010-01-29 14:30:41 -0800 | [diff] [blame] | 3766 | arena_boot(void) |
Jason Evans | e476f8a | 2010-01-16 09:53:50 -0800 | [diff] [blame] | 3767 | { |
Jason Evans | 7393f44 | 2010-10-01 17:35:43 -0700 | [diff] [blame] | 3768 | unsigned i; |
Jason Evans | e476f8a | 2010-01-16 09:53:50 -0800 | [diff] [blame] | 3769 | |
Jason Evans | 8d6a3e8 | 2015-03-18 18:55:33 -0700 | [diff] [blame] | 3770 | arena_lg_dirty_mult_default_set(opt_lg_dirty_mult); |
Jason Evans | 243f7a0 | 2016-02-19 20:09:31 -0800 | [diff] [blame] | 3771 | arena_decay_time_default_set(opt_decay_time); |
Jason Evans | 8d6a3e8 | 2015-03-18 18:55:33 -0700 | [diff] [blame] | 3772 | |
Jason Evans | e476f8a | 2010-01-16 09:53:50 -0800 | [diff] [blame] | 3773 | /* |
| 3774 | * Compute the header size such that it is large enough to contain the |
Jason Evans | 7393f44 | 2010-10-01 17:35:43 -0700 | [diff] [blame] | 3775 | * page map. The page map is biased to omit entries for the header |
| 3776 | * itself, so some iteration is necessary to compute the map bias. |
| 3777 | * |
| 3778 | * 1) Compute safe header_size and map_bias values that include enough |
| 3779 | * space for an unbiased page map. |
| 3780 | * 2) Refine map_bias based on (1) to omit the header pages in the page |
| 3781 | * map. The resulting map_bias may be one too small. |
| 3782 | * 3) Refine map_bias based on (2). The result will be >= the result |
| 3783 | * from (2), and will always be correct. |
Jason Evans | e476f8a | 2010-01-16 09:53:50 -0800 | [diff] [blame] | 3784 | */ |
Jason Evans | 7393f44 | 2010-10-01 17:35:43 -0700 | [diff] [blame] | 3785 | map_bias = 0; |
| 3786 | for (i = 0; i < 3; i++) { |
Dmitry-Me | a306a60 | 2015-09-04 13:15:28 +0300 | [diff] [blame] | 3787 | size_t header_size = offsetof(arena_chunk_t, map_bits) + |
Qinfan Wu | ff6a31d | 2014-08-29 13:34:40 -0700 | [diff] [blame] | 3788 | ((sizeof(arena_chunk_map_bits_t) + |
| 3789 | sizeof(arena_chunk_map_misc_t)) * (chunk_npages-map_bias)); |
Jason Evans | 0c5dd03 | 2014-09-29 01:31:39 -0700 | [diff] [blame] | 3790 | map_bias = (header_size + PAGE_MASK) >> LG_PAGE; |
Jason Evans | 7393f44 | 2010-10-01 17:35:43 -0700 | [diff] [blame] | 3791 | } |
| 3792 | assert(map_bias > 0); |
| 3793 | |
Qinfan Wu | ff6a31d | 2014-08-29 13:34:40 -0700 | [diff] [blame] | 3794 | map_misc_offset = offsetof(arena_chunk_t, map_bits) + |
| 3795 | sizeof(arena_chunk_map_bits_t) * (chunk_npages-map_bias); |
| 3796 | |
Jason Evans | 155bfa7 | 2014-10-05 17:54:10 -0700 | [diff] [blame] | 3797 | arena_maxrun = chunksize - (map_bias << LG_PAGE); |
Jason Evans | fc0b3b7 | 2014-10-09 17:54:06 -0700 | [diff] [blame] | 3798 | assert(arena_maxrun > 0); |
Jason Evans | 676df88 | 2015-09-11 20:50:20 -0700 | [diff] [blame] | 3799 | large_maxclass = index2size(size2index(chunksize)-1); |
| 3800 | if (large_maxclass > arena_maxrun) { |
Jason Evans | 155bfa7 | 2014-10-05 17:54:10 -0700 | [diff] [blame] | 3801 | /* |
| 3802 | * For small chunk sizes it's possible for there to be fewer |
| 3803 | * non-header pages available than are necessary to serve the |
| 3804 | * size classes just below chunksize. |
| 3805 | */ |
Jason Evans | 676df88 | 2015-09-11 20:50:20 -0700 | [diff] [blame] | 3806 | large_maxclass = arena_maxrun; |
Jason Evans | 155bfa7 | 2014-10-05 17:54:10 -0700 | [diff] [blame] | 3807 | } |
Jason Evans | 676df88 | 2015-09-11 20:50:20 -0700 | [diff] [blame] | 3808 | assert(large_maxclass > 0); |
| 3809 | nlclasses = size2index(large_maxclass) - size2index(SMALL_MAXCLASS); |
Jason Evans | 3c4d92e | 2014-10-12 22:53:59 -0700 | [diff] [blame] | 3810 | nhclasses = NSIZES - nlclasses - NBINS; |
Jason Evans | a0bf242 | 2010-01-29 14:30:41 -0800 | [diff] [blame] | 3811 | |
Jason Evans | b172610 | 2012-02-28 16:50:47 -0800 | [diff] [blame] | 3812 | bin_info_init(); |
Jason Evans | 0da8ce1 | 2016-02-22 16:20:56 -0800 | [diff] [blame] | 3813 | if (small_run_size_init()) |
| 3814 | return (true); |
| 3815 | if (run_quantize_init()) |
| 3816 | return (true); |
| 3817 | |
Dave Watson | 3417a30 | 2016-02-23 12:06:21 -0800 | [diff] [blame] | 3818 | runs_avail_bias = size2index(PAGE); |
| 3819 | runs_avail_nclasses = size2index(run_quantize_max)+1 - runs_avail_bias; |
| 3820 | |
Jason Evans | 0da8ce1 | 2016-02-22 16:20:56 -0800 | [diff] [blame] | 3821 | return (false); |
Jason Evans | e476f8a | 2010-01-16 09:53:50 -0800 | [diff] [blame] | 3822 | } |
Jason Evans | 4e2e3dd | 2012-03-13 16:31:41 -0700 | [diff] [blame] | 3823 | |
| 3824 | void |
Jason Evans | 174c0c3 | 2016-04-25 23:14:40 -0700 | [diff] [blame] | 3825 | arena_prefork0(tsd_t *tsd, arena_t *arena) |
| 3826 | { |
| 3827 | |
| 3828 | malloc_mutex_prefork(tsd, &arena->lock); |
| 3829 | } |
| 3830 | |
| 3831 | void |
| 3832 | arena_prefork1(tsd_t *tsd, arena_t *arena) |
| 3833 | { |
| 3834 | |
| 3835 | malloc_mutex_prefork(tsd, &arena->chunks_mtx); |
| 3836 | } |
| 3837 | |
| 3838 | void |
| 3839 | arena_prefork2(tsd_t *tsd, arena_t *arena) |
| 3840 | { |
| 3841 | |
| 3842 | malloc_mutex_prefork(tsd, &arena->node_cache_mtx); |
| 3843 | } |
| 3844 | |
| 3845 | void |
| 3846 | arena_prefork3(tsd_t *tsd, arena_t *arena) |
Jason Evans | 4e2e3dd | 2012-03-13 16:31:41 -0700 | [diff] [blame] | 3847 | { |
| 3848 | unsigned i; |
| 3849 | |
Jason Evans | 4e2e3dd | 2012-03-13 16:31:41 -0700 | [diff] [blame] | 3850 | for (i = 0; i < NBINS; i++) |
Jason Evans | b2c0d63 | 2016-04-13 23:36:15 -0700 | [diff] [blame] | 3851 | malloc_mutex_prefork(tsd, &arena->bins[i].lock); |
Jason Evans | 174c0c3 | 2016-04-25 23:14:40 -0700 | [diff] [blame] | 3852 | malloc_mutex_prefork(tsd, &arena->huge_mtx); |
Jason Evans | 4e2e3dd | 2012-03-13 16:31:41 -0700 | [diff] [blame] | 3853 | } |
| 3854 | |
| 3855 | void |
Jason Evans | b2c0d63 | 2016-04-13 23:36:15 -0700 | [diff] [blame] | 3856 | arena_postfork_parent(tsd_t *tsd, arena_t *arena) |
Jason Evans | 4e2e3dd | 2012-03-13 16:31:41 -0700 | [diff] [blame] | 3857 | { |
| 3858 | unsigned i; |
| 3859 | |
Jason Evans | 174c0c3 | 2016-04-25 23:14:40 -0700 | [diff] [blame] | 3860 | malloc_mutex_postfork_parent(tsd, &arena->huge_mtx); |
Jason Evans | 4e2e3dd | 2012-03-13 16:31:41 -0700 | [diff] [blame] | 3861 | for (i = 0; i < NBINS; i++) |
Jason Evans | b2c0d63 | 2016-04-13 23:36:15 -0700 | [diff] [blame] | 3862 | malloc_mutex_postfork_parent(tsd, &arena->bins[i].lock); |
| 3863 | malloc_mutex_postfork_parent(tsd, &arena->node_cache_mtx); |
| 3864 | malloc_mutex_postfork_parent(tsd, &arena->chunks_mtx); |
Jason Evans | b2c0d63 | 2016-04-13 23:36:15 -0700 | [diff] [blame] | 3865 | malloc_mutex_postfork_parent(tsd, &arena->lock); |
Jason Evans | 4e2e3dd | 2012-03-13 16:31:41 -0700 | [diff] [blame] | 3866 | } |
| 3867 | |
| 3868 | void |
Jason Evans | b2c0d63 | 2016-04-13 23:36:15 -0700 | [diff] [blame] | 3869 | arena_postfork_child(tsd_t *tsd, arena_t *arena) |
Jason Evans | 4e2e3dd | 2012-03-13 16:31:41 -0700 | [diff] [blame] | 3870 | { |
| 3871 | unsigned i; |
| 3872 | |
Jason Evans | 174c0c3 | 2016-04-25 23:14:40 -0700 | [diff] [blame] | 3873 | malloc_mutex_postfork_child(tsd, &arena->huge_mtx); |
Jason Evans | 4e2e3dd | 2012-03-13 16:31:41 -0700 | [diff] [blame] | 3874 | for (i = 0; i < NBINS; i++) |
Jason Evans | b2c0d63 | 2016-04-13 23:36:15 -0700 | [diff] [blame] | 3875 | malloc_mutex_postfork_child(tsd, &arena->bins[i].lock); |
| 3876 | malloc_mutex_postfork_child(tsd, &arena->node_cache_mtx); |
| 3877 | malloc_mutex_postfork_child(tsd, &arena->chunks_mtx); |
Jason Evans | b2c0d63 | 2016-04-13 23:36:15 -0700 | [diff] [blame] | 3878 | malloc_mutex_postfork_child(tsd, &arena->lock); |
Jason Evans | 4e2e3dd | 2012-03-13 16:31:41 -0700 | [diff] [blame] | 3879 | } |