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