arena.c 55 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891
  1. #include "jemalloc/internal/jemalloc_preamble.h"
  2. #include "jemalloc/internal/jemalloc_internal_includes.h"
  3. #include "jemalloc/internal/assert.h"
  4. #include "jemalloc/internal/decay.h"
  5. #include "jemalloc/internal/ehooks.h"
  6. #include "jemalloc/internal/extent_dss.h"
  7. #include "jemalloc/internal/extent_mmap.h"
  8. #include "jemalloc/internal/san.h"
  9. #include "jemalloc/internal/mutex.h"
  10. #include "jemalloc/internal/rtree.h"
  11. #include "jemalloc/internal/safety_check.h"
  12. #include "jemalloc/internal/util.h"
  13. JEMALLOC_DIAGNOSTIC_DISABLE_SPURIOUS
  14. /******************************************************************************/
  15. /* Data. */
  16. /*
  17. * Define names for both unininitialized and initialized phases, so that
  18. * options and mallctl processing are straightforward.
  19. */
  20. const char *percpu_arena_mode_names[] = {
  21. "percpu",
  22. "phycpu",
  23. "disabled",
  24. "percpu",
  25. "phycpu"
  26. };
  27. percpu_arena_mode_t opt_percpu_arena = PERCPU_ARENA_DEFAULT;
  28. ssize_t opt_dirty_decay_ms = DIRTY_DECAY_MS_DEFAULT;
  29. ssize_t opt_muzzy_decay_ms = MUZZY_DECAY_MS_DEFAULT;
  30. static atomic_zd_t dirty_decay_ms_default;
  31. static atomic_zd_t muzzy_decay_ms_default;
  32. emap_t arena_emap_global;
  33. pa_central_t arena_pa_central_global;
  34. div_info_t arena_binind_div_info[SC_NBINS];
  35. size_t opt_oversize_threshold = OVERSIZE_THRESHOLD_DEFAULT;
  36. size_t oversize_threshold = OVERSIZE_THRESHOLD_DEFAULT;
  37. uint32_t arena_bin_offsets[SC_NBINS];
  38. static unsigned nbins_total;
  39. static unsigned huge_arena_ind;
  40. const arena_config_t arena_config_default = {
  41. /* .extent_hooks = */ (extent_hooks_t *)&ehooks_default_extent_hooks,
  42. /* .metadata_use_hooks = */ true,
  43. };
  44. /******************************************************************************/
  45. /*
  46. * Function prototypes for static functions that are referenced prior to
  47. * definition.
  48. */
  49. static bool arena_decay_dirty(tsdn_t *tsdn, arena_t *arena,
  50. bool is_background_thread, bool all);
  51. static void arena_bin_lower_slab(tsdn_t *tsdn, arena_t *arena, edata_t *slab,
  52. bin_t *bin);
  53. static void
  54. arena_maybe_do_deferred_work(tsdn_t *tsdn, arena_t *arena, decay_t *decay,
  55. size_t npages_new);
  56. /******************************************************************************/
  57. void
  58. arena_basic_stats_merge(tsdn_t *tsdn, arena_t *arena, unsigned *nthreads,
  59. const char **dss, ssize_t *dirty_decay_ms, ssize_t *muzzy_decay_ms,
  60. size_t *nactive, size_t *ndirty, size_t *nmuzzy) {
  61. *nthreads += arena_nthreads_get(arena, false);
  62. *dss = dss_prec_names[arena_dss_prec_get(arena)];
  63. *dirty_decay_ms = arena_decay_ms_get(arena, extent_state_dirty);
  64. *muzzy_decay_ms = arena_decay_ms_get(arena, extent_state_muzzy);
  65. pa_shard_basic_stats_merge(&arena->pa_shard, nactive, ndirty, nmuzzy);
  66. }
  67. void
  68. arena_stats_merge(tsdn_t *tsdn, arena_t *arena, unsigned *nthreads,
  69. const char **dss, ssize_t *dirty_decay_ms, ssize_t *muzzy_decay_ms,
  70. size_t *nactive, size_t *ndirty, size_t *nmuzzy, arena_stats_t *astats,
  71. bin_stats_data_t *bstats, arena_stats_large_t *lstats,
  72. pac_estats_t *estats, hpa_shard_stats_t *hpastats, sec_stats_t *secstats) {
  73. cassert(config_stats);
  74. arena_basic_stats_merge(tsdn, arena, nthreads, dss, dirty_decay_ms,
  75. muzzy_decay_ms, nactive, ndirty, nmuzzy);
  76. size_t base_allocated, base_resident, base_mapped, metadata_thp;
  77. base_stats_get(tsdn, arena->base, &base_allocated, &base_resident,
  78. &base_mapped, &metadata_thp);
  79. size_t pac_mapped_sz = pac_mapped(&arena->pa_shard.pac);
  80. astats->mapped += base_mapped + pac_mapped_sz;
  81. astats->resident += base_resident;
  82. LOCKEDINT_MTX_LOCK(tsdn, arena->stats.mtx);
  83. astats->base += base_allocated;
  84. atomic_load_add_store_zu(&astats->internal, arena_internal_get(arena));
  85. astats->metadata_thp += metadata_thp;
  86. for (szind_t i = 0; i < SC_NSIZES - SC_NBINS; i++) {
  87. uint64_t nmalloc = locked_read_u64(tsdn,
  88. LOCKEDINT_MTX(arena->stats.mtx),
  89. &arena->stats.lstats[i].nmalloc);
  90. locked_inc_u64_unsynchronized(&lstats[i].nmalloc, nmalloc);
  91. astats->nmalloc_large += nmalloc;
  92. uint64_t ndalloc = locked_read_u64(tsdn,
  93. LOCKEDINT_MTX(arena->stats.mtx),
  94. &arena->stats.lstats[i].ndalloc);
  95. locked_inc_u64_unsynchronized(&lstats[i].ndalloc, ndalloc);
  96. astats->ndalloc_large += ndalloc;
  97. uint64_t nrequests = locked_read_u64(tsdn,
  98. LOCKEDINT_MTX(arena->stats.mtx),
  99. &arena->stats.lstats[i].nrequests);
  100. locked_inc_u64_unsynchronized(&lstats[i].nrequests,
  101. nmalloc + nrequests);
  102. astats->nrequests_large += nmalloc + nrequests;
  103. /* nfill == nmalloc for large currently. */
  104. locked_inc_u64_unsynchronized(&lstats[i].nfills, nmalloc);
  105. astats->nfills_large += nmalloc;
  106. uint64_t nflush = locked_read_u64(tsdn,
  107. LOCKEDINT_MTX(arena->stats.mtx),
  108. &arena->stats.lstats[i].nflushes);
  109. locked_inc_u64_unsynchronized(&lstats[i].nflushes, nflush);
  110. astats->nflushes_large += nflush;
  111. assert(nmalloc >= ndalloc);
  112. assert(nmalloc - ndalloc <= SIZE_T_MAX);
  113. size_t curlextents = (size_t)(nmalloc - ndalloc);
  114. lstats[i].curlextents += curlextents;
  115. astats->allocated_large +=
  116. curlextents * sz_index2size(SC_NBINS + i);
  117. }
  118. pa_shard_stats_merge(tsdn, &arena->pa_shard, &astats->pa_shard_stats,
  119. estats, hpastats, secstats, &astats->resident);
  120. LOCKEDINT_MTX_UNLOCK(tsdn, arena->stats.mtx);
  121. /* Currently cached bytes and sanitizer-stashed bytes in tcache. */
  122. astats->tcache_bytes = 0;
  123. astats->tcache_stashed_bytes = 0;
  124. malloc_mutex_lock(tsdn, &arena->tcache_ql_mtx);
  125. cache_bin_array_descriptor_t *descriptor;
  126. ql_foreach(descriptor, &arena->cache_bin_array_descriptor_ql, link) {
  127. for (szind_t i = 0; i < nhbins; i++) {
  128. cache_bin_t *cache_bin = &descriptor->bins[i];
  129. cache_bin_sz_t ncached, nstashed;
  130. cache_bin_nitems_get_remote(cache_bin,
  131. &tcache_bin_info[i], &ncached, &nstashed);
  132. astats->tcache_bytes += ncached * sz_index2size(i);
  133. astats->tcache_stashed_bytes += nstashed *
  134. sz_index2size(i);
  135. }
  136. }
  137. malloc_mutex_prof_read(tsdn,
  138. &astats->mutex_prof_data[arena_prof_mutex_tcache_list],
  139. &arena->tcache_ql_mtx);
  140. malloc_mutex_unlock(tsdn, &arena->tcache_ql_mtx);
  141. #define READ_ARENA_MUTEX_PROF_DATA(mtx, ind) \
  142. malloc_mutex_lock(tsdn, &arena->mtx); \
  143. malloc_mutex_prof_read(tsdn, &astats->mutex_prof_data[ind], \
  144. &arena->mtx); \
  145. malloc_mutex_unlock(tsdn, &arena->mtx);
  146. /* Gather per arena mutex profiling data. */
  147. READ_ARENA_MUTEX_PROF_DATA(large_mtx, arena_prof_mutex_large);
  148. READ_ARENA_MUTEX_PROF_DATA(base->mtx,
  149. arena_prof_mutex_base);
  150. #undef READ_ARENA_MUTEX_PROF_DATA
  151. pa_shard_mtx_stats_read(tsdn, &arena->pa_shard,
  152. astats->mutex_prof_data);
  153. nstime_copy(&astats->uptime, &arena->create_time);
  154. nstime_update(&astats->uptime);
  155. nstime_subtract(&astats->uptime, &arena->create_time);
  156. for (szind_t i = 0; i < SC_NBINS; i++) {
  157. for (unsigned j = 0; j < bin_infos[i].n_shards; j++) {
  158. bin_stats_merge(tsdn, &bstats[i],
  159. arena_get_bin(arena, i, j));
  160. }
  161. }
  162. }
  163. static void
  164. arena_background_thread_inactivity_check(tsdn_t *tsdn, arena_t *arena,
  165. bool is_background_thread) {
  166. if (!background_thread_enabled() || is_background_thread) {
  167. return;
  168. }
  169. background_thread_info_t *info =
  170. arena_background_thread_info_get(arena);
  171. if (background_thread_indefinite_sleep(info)) {
  172. arena_maybe_do_deferred_work(tsdn, arena,
  173. &arena->pa_shard.pac.decay_dirty, 0);
  174. }
  175. }
  176. /*
  177. * React to deferred work generated by a PAI function.
  178. */
  179. void arena_handle_deferred_work(tsdn_t *tsdn, arena_t *arena) {
  180. witness_assert_depth_to_rank(tsdn_witness_tsdp_get(tsdn),
  181. WITNESS_RANK_CORE, 0);
  182. if (decay_immediately(&arena->pa_shard.pac.decay_dirty)) {
  183. arena_decay_dirty(tsdn, arena, false, true);
  184. }
  185. arena_background_thread_inactivity_check(tsdn, arena, false);
  186. }
  187. static void *
  188. arena_slab_reg_alloc(edata_t *slab, const bin_info_t *bin_info) {
  189. void *ret;
  190. slab_data_t *slab_data = edata_slab_data_get(slab);
  191. size_t regind;
  192. assert(edata_nfree_get(slab) > 0);
  193. assert(!bitmap_full(slab_data->bitmap, &bin_info->bitmap_info));
  194. regind = bitmap_sfu(slab_data->bitmap, &bin_info->bitmap_info);
  195. ret = (void *)((uintptr_t)edata_addr_get(slab) +
  196. (uintptr_t)(bin_info->reg_size * regind));
  197. edata_nfree_dec(slab);
  198. return ret;
  199. }
  200. static void
  201. arena_slab_reg_alloc_batch(edata_t *slab, const bin_info_t *bin_info,
  202. unsigned cnt, void** ptrs) {
  203. slab_data_t *slab_data = edata_slab_data_get(slab);
  204. assert(edata_nfree_get(slab) >= cnt);
  205. assert(!bitmap_full(slab_data->bitmap, &bin_info->bitmap_info));
  206. #if (! defined JEMALLOC_INTERNAL_POPCOUNTL) || (defined BITMAP_USE_TREE)
  207. for (unsigned i = 0; i < cnt; i++) {
  208. size_t regind = bitmap_sfu(slab_data->bitmap,
  209. &bin_info->bitmap_info);
  210. *(ptrs + i) = (void *)((uintptr_t)edata_addr_get(slab) +
  211. (uintptr_t)(bin_info->reg_size * regind));
  212. }
  213. #else
  214. unsigned group = 0;
  215. bitmap_t g = slab_data->bitmap[group];
  216. unsigned i = 0;
  217. while (i < cnt) {
  218. while (g == 0) {
  219. g = slab_data->bitmap[++group];
  220. }
  221. size_t shift = group << LG_BITMAP_GROUP_NBITS;
  222. size_t pop = popcount_lu(g);
  223. if (pop > (cnt - i)) {
  224. pop = cnt - i;
  225. }
  226. /*
  227. * Load from memory locations only once, outside the
  228. * hot loop below.
  229. */
  230. uintptr_t base = (uintptr_t)edata_addr_get(slab);
  231. uintptr_t regsize = (uintptr_t)bin_info->reg_size;
  232. while (pop--) {
  233. size_t bit = cfs_lu(&g);
  234. size_t regind = shift + bit;
  235. *(ptrs + i) = (void *)(base + regsize * regind);
  236. i++;
  237. }
  238. slab_data->bitmap[group] = g;
  239. }
  240. #endif
  241. edata_nfree_sub(slab, cnt);
  242. }
  243. static void
  244. arena_large_malloc_stats_update(tsdn_t *tsdn, arena_t *arena, size_t usize) {
  245. szind_t index, hindex;
  246. cassert(config_stats);
  247. if (usize < SC_LARGE_MINCLASS) {
  248. usize = SC_LARGE_MINCLASS;
  249. }
  250. index = sz_size2index(usize);
  251. hindex = (index >= SC_NBINS) ? index - SC_NBINS : 0;
  252. locked_inc_u64(tsdn, LOCKEDINT_MTX(arena->stats.mtx),
  253. &arena->stats.lstats[hindex].nmalloc, 1);
  254. }
  255. static void
  256. arena_large_dalloc_stats_update(tsdn_t *tsdn, arena_t *arena, size_t usize) {
  257. szind_t index, hindex;
  258. cassert(config_stats);
  259. if (usize < SC_LARGE_MINCLASS) {
  260. usize = SC_LARGE_MINCLASS;
  261. }
  262. index = sz_size2index(usize);
  263. hindex = (index >= SC_NBINS) ? index - SC_NBINS : 0;
  264. locked_inc_u64(tsdn, LOCKEDINT_MTX(arena->stats.mtx),
  265. &arena->stats.lstats[hindex].ndalloc, 1);
  266. }
  267. static void
  268. arena_large_ralloc_stats_update(tsdn_t *tsdn, arena_t *arena, size_t oldusize,
  269. size_t usize) {
  270. arena_large_malloc_stats_update(tsdn, arena, usize);
  271. arena_large_dalloc_stats_update(tsdn, arena, oldusize);
  272. }
  273. edata_t *
  274. arena_extent_alloc_large(tsdn_t *tsdn, arena_t *arena, size_t usize,
  275. size_t alignment, bool zero) {
  276. bool deferred_work_generated = false;
  277. szind_t szind = sz_size2index(usize);
  278. size_t esize = usize + sz_large_pad;
  279. bool guarded = san_large_extent_decide_guard(tsdn,
  280. arena_get_ehooks(arena), esize, alignment);
  281. edata_t *edata = pa_alloc(tsdn, &arena->pa_shard, esize, alignment,
  282. /* slab */ false, szind, zero, guarded, &deferred_work_generated);
  283. assert(deferred_work_generated == false);
  284. if (edata != NULL) {
  285. if (config_stats) {
  286. LOCKEDINT_MTX_LOCK(tsdn, arena->stats.mtx);
  287. arena_large_malloc_stats_update(tsdn, arena, usize);
  288. LOCKEDINT_MTX_UNLOCK(tsdn, arena->stats.mtx);
  289. }
  290. }
  291. if (edata != NULL && sz_large_pad != 0) {
  292. arena_cache_oblivious_randomize(tsdn, arena, edata, alignment);
  293. }
  294. return edata;
  295. }
  296. void
  297. arena_extent_dalloc_large_prep(tsdn_t *tsdn, arena_t *arena, edata_t *edata) {
  298. if (config_stats) {
  299. LOCKEDINT_MTX_LOCK(tsdn, arena->stats.mtx);
  300. arena_large_dalloc_stats_update(tsdn, arena,
  301. edata_usize_get(edata));
  302. LOCKEDINT_MTX_UNLOCK(tsdn, arena->stats.mtx);
  303. }
  304. }
  305. void
  306. arena_extent_ralloc_large_shrink(tsdn_t *tsdn, arena_t *arena, edata_t *edata,
  307. size_t oldusize) {
  308. size_t usize = edata_usize_get(edata);
  309. if (config_stats) {
  310. LOCKEDINT_MTX_LOCK(tsdn, arena->stats.mtx);
  311. arena_large_ralloc_stats_update(tsdn, arena, oldusize, usize);
  312. LOCKEDINT_MTX_UNLOCK(tsdn, arena->stats.mtx);
  313. }
  314. }
  315. void
  316. arena_extent_ralloc_large_expand(tsdn_t *tsdn, arena_t *arena, edata_t *edata,
  317. size_t oldusize) {
  318. size_t usize = edata_usize_get(edata);
  319. if (config_stats) {
  320. LOCKEDINT_MTX_LOCK(tsdn, arena->stats.mtx);
  321. arena_large_ralloc_stats_update(tsdn, arena, oldusize, usize);
  322. LOCKEDINT_MTX_UNLOCK(tsdn, arena->stats.mtx);
  323. }
  324. }
  325. /*
  326. * In situations where we're not forcing a decay (i.e. because the user
  327. * specifically requested it), should we purge ourselves, or wait for the
  328. * background thread to get to it.
  329. */
  330. static pac_purge_eagerness_t
  331. arena_decide_unforced_purge_eagerness(bool is_background_thread) {
  332. if (is_background_thread) {
  333. return PAC_PURGE_ALWAYS;
  334. } else if (!is_background_thread && background_thread_enabled()) {
  335. return PAC_PURGE_NEVER;
  336. } else {
  337. return PAC_PURGE_ON_EPOCH_ADVANCE;
  338. }
  339. }
  340. bool
  341. arena_decay_ms_set(tsdn_t *tsdn, arena_t *arena, extent_state_t state,
  342. ssize_t decay_ms) {
  343. pac_purge_eagerness_t eagerness = arena_decide_unforced_purge_eagerness(
  344. /* is_background_thread */ false);
  345. return pa_decay_ms_set(tsdn, &arena->pa_shard, state, decay_ms,
  346. eagerness);
  347. }
  348. ssize_t
  349. arena_decay_ms_get(arena_t *arena, extent_state_t state) {
  350. return pa_decay_ms_get(&arena->pa_shard, state);
  351. }
  352. static bool
  353. arena_decay_impl(tsdn_t *tsdn, arena_t *arena, decay_t *decay,
  354. pac_decay_stats_t *decay_stats, ecache_t *ecache,
  355. bool is_background_thread, bool all) {
  356. if (all) {
  357. malloc_mutex_lock(tsdn, &decay->mtx);
  358. pac_decay_all(tsdn, &arena->pa_shard.pac, decay, decay_stats,
  359. ecache, /* fully_decay */ all);
  360. malloc_mutex_unlock(tsdn, &decay->mtx);
  361. return false;
  362. }
  363. if (malloc_mutex_trylock(tsdn, &decay->mtx)) {
  364. /* No need to wait if another thread is in progress. */
  365. return true;
  366. }
  367. pac_purge_eagerness_t eagerness =
  368. arena_decide_unforced_purge_eagerness(is_background_thread);
  369. bool epoch_advanced = pac_maybe_decay_purge(tsdn, &arena->pa_shard.pac,
  370. decay, decay_stats, ecache, eagerness);
  371. size_t npages_new;
  372. if (epoch_advanced) {
  373. /* Backlog is updated on epoch advance. */
  374. npages_new = decay_epoch_npages_delta(decay);
  375. }
  376. malloc_mutex_unlock(tsdn, &decay->mtx);
  377. if (have_background_thread && background_thread_enabled() &&
  378. epoch_advanced && !is_background_thread) {
  379. arena_maybe_do_deferred_work(tsdn, arena, decay, npages_new);
  380. }
  381. return false;
  382. }
  383. static bool
  384. arena_decay_dirty(tsdn_t *tsdn, arena_t *arena, bool is_background_thread,
  385. bool all) {
  386. return arena_decay_impl(tsdn, arena, &arena->pa_shard.pac.decay_dirty,
  387. &arena->pa_shard.pac.stats->decay_dirty,
  388. &arena->pa_shard.pac.ecache_dirty, is_background_thread, all);
  389. }
  390. static bool
  391. arena_decay_muzzy(tsdn_t *tsdn, arena_t *arena, bool is_background_thread,
  392. bool all) {
  393. if (pa_shard_dont_decay_muzzy(&arena->pa_shard)) {
  394. return false;
  395. }
  396. return arena_decay_impl(tsdn, arena, &arena->pa_shard.pac.decay_muzzy,
  397. &arena->pa_shard.pac.stats->decay_muzzy,
  398. &arena->pa_shard.pac.ecache_muzzy, is_background_thread, all);
  399. }
  400. void
  401. arena_decay(tsdn_t *tsdn, arena_t *arena, bool is_background_thread, bool all) {
  402. if (all) {
  403. /*
  404. * We should take a purge of "all" to mean "save as much memory
  405. * as possible", including flushing any caches (for situations
  406. * like thread death, or manual purge calls).
  407. */
  408. sec_flush(tsdn, &arena->pa_shard.hpa_sec);
  409. }
  410. if (arena_decay_dirty(tsdn, arena, is_background_thread, all)) {
  411. return;
  412. }
  413. arena_decay_muzzy(tsdn, arena, is_background_thread, all);
  414. }
  415. static bool
  416. arena_should_decay_early(tsdn_t *tsdn, arena_t *arena, decay_t *decay,
  417. background_thread_info_t *info, nstime_t *remaining_sleep,
  418. size_t npages_new) {
  419. malloc_mutex_assert_owner(tsdn, &info->mtx);
  420. if (malloc_mutex_trylock(tsdn, &decay->mtx)) {
  421. return false;
  422. }
  423. if (!decay_gradually(decay)) {
  424. malloc_mutex_unlock(tsdn, &decay->mtx);
  425. return false;
  426. }
  427. nstime_init(remaining_sleep, background_thread_wakeup_time_get(info));
  428. if (nstime_compare(remaining_sleep, &decay->epoch) <= 0) {
  429. malloc_mutex_unlock(tsdn, &decay->mtx);
  430. return false;
  431. }
  432. nstime_subtract(remaining_sleep, &decay->epoch);
  433. if (npages_new > 0) {
  434. uint64_t npurge_new = decay_npages_purge_in(decay,
  435. remaining_sleep, npages_new);
  436. info->npages_to_purge_new += npurge_new;
  437. }
  438. malloc_mutex_unlock(tsdn, &decay->mtx);
  439. return info->npages_to_purge_new >
  440. ARENA_DEFERRED_PURGE_NPAGES_THRESHOLD;
  441. }
  442. /*
  443. * Check if deferred work needs to be done sooner than planned.
  444. * For decay we might want to wake up earlier because of an influx of dirty
  445. * pages. Rather than waiting for previously estimated time, we proactively
  446. * purge those pages.
  447. * If background thread sleeps indefinitely, always wake up because some
  448. * deferred work has been generated.
  449. */
  450. static void
  451. arena_maybe_do_deferred_work(tsdn_t *tsdn, arena_t *arena, decay_t *decay,
  452. size_t npages_new) {
  453. background_thread_info_t *info = arena_background_thread_info_get(
  454. arena);
  455. if (malloc_mutex_trylock(tsdn, &info->mtx)) {
  456. /*
  457. * Background thread may hold the mutex for a long period of
  458. * time. We'd like to avoid the variance on application
  459. * threads. So keep this non-blocking, and leave the work to a
  460. * future epoch.
  461. */
  462. return;
  463. }
  464. if (!background_thread_is_started(info)) {
  465. goto label_done;
  466. }
  467. nstime_t remaining_sleep;
  468. if (background_thread_indefinite_sleep(info)) {
  469. background_thread_wakeup_early(info, NULL);
  470. } else if (arena_should_decay_early(tsdn, arena, decay, info,
  471. &remaining_sleep, npages_new)) {
  472. info->npages_to_purge_new = 0;
  473. background_thread_wakeup_early(info, &remaining_sleep);
  474. }
  475. label_done:
  476. malloc_mutex_unlock(tsdn, &info->mtx);
  477. }
  478. /* Called from background threads. */
  479. void
  480. arena_do_deferred_work(tsdn_t *tsdn, arena_t *arena) {
  481. arena_decay(tsdn, arena, true, false);
  482. pa_shard_do_deferred_work(tsdn, &arena->pa_shard);
  483. }
  484. void
  485. arena_slab_dalloc(tsdn_t *tsdn, arena_t *arena, edata_t *slab) {
  486. bool deferred_work_generated = false;
  487. pa_dalloc(tsdn, &arena->pa_shard, slab, &deferred_work_generated);
  488. if (deferred_work_generated) {
  489. arena_handle_deferred_work(tsdn, arena);
  490. }
  491. }
  492. static void
  493. arena_bin_slabs_nonfull_insert(bin_t *bin, edata_t *slab) {
  494. assert(edata_nfree_get(slab) > 0);
  495. edata_heap_insert(&bin->slabs_nonfull, slab);
  496. if (config_stats) {
  497. bin->stats.nonfull_slabs++;
  498. }
  499. }
  500. static void
  501. arena_bin_slabs_nonfull_remove(bin_t *bin, edata_t *slab) {
  502. edata_heap_remove(&bin->slabs_nonfull, slab);
  503. if (config_stats) {
  504. bin->stats.nonfull_slabs--;
  505. }
  506. }
  507. static edata_t *
  508. arena_bin_slabs_nonfull_tryget(bin_t *bin) {
  509. edata_t *slab = edata_heap_remove_first(&bin->slabs_nonfull);
  510. if (slab == NULL) {
  511. return NULL;
  512. }
  513. if (config_stats) {
  514. bin->stats.reslabs++;
  515. bin->stats.nonfull_slabs--;
  516. }
  517. return slab;
  518. }
  519. static void
  520. arena_bin_slabs_full_insert(arena_t *arena, bin_t *bin, edata_t *slab) {
  521. assert(edata_nfree_get(slab) == 0);
  522. /*
  523. * Tracking extents is required by arena_reset, which is not allowed
  524. * for auto arenas. Bypass this step to avoid touching the edata
  525. * linkage (often results in cache misses) for auto arenas.
  526. */
  527. if (arena_is_auto(arena)) {
  528. return;
  529. }
  530. edata_list_active_append(&bin->slabs_full, slab);
  531. }
  532. static void
  533. arena_bin_slabs_full_remove(arena_t *arena, bin_t *bin, edata_t *slab) {
  534. if (arena_is_auto(arena)) {
  535. return;
  536. }
  537. edata_list_active_remove(&bin->slabs_full, slab);
  538. }
  539. static void
  540. arena_bin_reset(tsd_t *tsd, arena_t *arena, bin_t *bin) {
  541. edata_t *slab;
  542. malloc_mutex_lock(tsd_tsdn(tsd), &bin->lock);
  543. if (bin->slabcur != NULL) {
  544. slab = bin->slabcur;
  545. bin->slabcur = NULL;
  546. malloc_mutex_unlock(tsd_tsdn(tsd), &bin->lock);
  547. arena_slab_dalloc(tsd_tsdn(tsd), arena, slab);
  548. malloc_mutex_lock(tsd_tsdn(tsd), &bin->lock);
  549. }
  550. while ((slab = edata_heap_remove_first(&bin->slabs_nonfull)) != NULL) {
  551. malloc_mutex_unlock(tsd_tsdn(tsd), &bin->lock);
  552. arena_slab_dalloc(tsd_tsdn(tsd), arena, slab);
  553. malloc_mutex_lock(tsd_tsdn(tsd), &bin->lock);
  554. }
  555. for (slab = edata_list_active_first(&bin->slabs_full); slab != NULL;
  556. slab = edata_list_active_first(&bin->slabs_full)) {
  557. arena_bin_slabs_full_remove(arena, bin, slab);
  558. malloc_mutex_unlock(tsd_tsdn(tsd), &bin->lock);
  559. arena_slab_dalloc(tsd_tsdn(tsd), arena, slab);
  560. malloc_mutex_lock(tsd_tsdn(tsd), &bin->lock);
  561. }
  562. if (config_stats) {
  563. bin->stats.curregs = 0;
  564. bin->stats.curslabs = 0;
  565. }
  566. malloc_mutex_unlock(tsd_tsdn(tsd), &bin->lock);
  567. }
  568. void
  569. arena_reset(tsd_t *tsd, arena_t *arena) {
  570. /*
  571. * Locking in this function is unintuitive. The caller guarantees that
  572. * no concurrent operations are happening in this arena, but there are
  573. * still reasons that some locking is necessary:
  574. *
  575. * - Some of the functions in the transitive closure of calls assume
  576. * appropriate locks are held, and in some cases these locks are
  577. * temporarily dropped to avoid lock order reversal or deadlock due to
  578. * reentry.
  579. * - mallctl("epoch", ...) may concurrently refresh stats. While
  580. * strictly speaking this is a "concurrent operation", disallowing
  581. * stats refreshes would impose an inconvenient burden.
  582. */
  583. /* Large allocations. */
  584. malloc_mutex_lock(tsd_tsdn(tsd), &arena->large_mtx);
  585. for (edata_t *edata = edata_list_active_first(&arena->large);
  586. edata != NULL; edata = edata_list_active_first(&arena->large)) {
  587. void *ptr = edata_base_get(edata);
  588. size_t usize;
  589. malloc_mutex_unlock(tsd_tsdn(tsd), &arena->large_mtx);
  590. emap_alloc_ctx_t alloc_ctx;
  591. emap_alloc_ctx_lookup(tsd_tsdn(tsd), &arena_emap_global, ptr,
  592. &alloc_ctx);
  593. assert(alloc_ctx.szind != SC_NSIZES);
  594. if (config_stats || (config_prof && opt_prof)) {
  595. usize = sz_index2size(alloc_ctx.szind);
  596. assert(usize == isalloc(tsd_tsdn(tsd), ptr));
  597. }
  598. /* Remove large allocation from prof sample set. */
  599. if (config_prof && opt_prof) {
  600. prof_free(tsd, ptr, usize, &alloc_ctx);
  601. }
  602. large_dalloc(tsd_tsdn(tsd), edata);
  603. malloc_mutex_lock(tsd_tsdn(tsd), &arena->large_mtx);
  604. }
  605. malloc_mutex_unlock(tsd_tsdn(tsd), &arena->large_mtx);
  606. /* Bins. */
  607. for (unsigned i = 0; i < SC_NBINS; i++) {
  608. for (unsigned j = 0; j < bin_infos[i].n_shards; j++) {
  609. arena_bin_reset(tsd, arena, arena_get_bin(arena, i, j));
  610. }
  611. }
  612. pa_shard_reset(tsd_tsdn(tsd), &arena->pa_shard);
  613. }
  614. static void
  615. arena_prepare_base_deletion_sync_finish(tsd_t *tsd, malloc_mutex_t **mutexes,
  616. unsigned n_mtx) {
  617. for (unsigned i = 0; i < n_mtx; i++) {
  618. malloc_mutex_lock(tsd_tsdn(tsd), mutexes[i]);
  619. malloc_mutex_unlock(tsd_tsdn(tsd), mutexes[i]);
  620. }
  621. }
  622. #define ARENA_DESTROY_MAX_DELAYED_MTX 32
  623. static void
  624. arena_prepare_base_deletion_sync(tsd_t *tsd, malloc_mutex_t *mtx,
  625. malloc_mutex_t **delayed_mtx, unsigned *n_delayed) {
  626. if (!malloc_mutex_trylock(tsd_tsdn(tsd), mtx)) {
  627. /* No contention. */
  628. malloc_mutex_unlock(tsd_tsdn(tsd), mtx);
  629. return;
  630. }
  631. unsigned n = *n_delayed;
  632. assert(n < ARENA_DESTROY_MAX_DELAYED_MTX);
  633. /* Add another to the batch. */
  634. delayed_mtx[n++] = mtx;
  635. if (n == ARENA_DESTROY_MAX_DELAYED_MTX) {
  636. arena_prepare_base_deletion_sync_finish(tsd, delayed_mtx, n);
  637. n = 0;
  638. }
  639. *n_delayed = n;
  640. }
  641. static void
  642. arena_prepare_base_deletion(tsd_t *tsd, base_t *base_to_destroy) {
  643. /*
  644. * In order to coalesce, emap_try_acquire_edata_neighbor will attempt to
  645. * check neighbor edata's state to determine eligibility. This means
  646. * under certain conditions, the metadata from an arena can be accessed
  647. * w/o holding any locks from that arena. In order to guarantee safe
  648. * memory access, the metadata and the underlying base allocator needs
  649. * to be kept alive, until all pending accesses are done.
  650. *
  651. * 1) with opt_retain, the arena boundary implies the is_head state
  652. * (tracked in the rtree leaf), and the coalesce flow will stop at the
  653. * head state branch. Therefore no cross arena metadata access
  654. * possible.
  655. *
  656. * 2) w/o opt_retain, the arena id needs to be read from the edata_t,
  657. * meaning read only cross-arena metadata access is possible. The
  658. * coalesce attempt will stop at the arena_id mismatch, and is always
  659. * under one of the ecache locks. To allow safe passthrough of such
  660. * metadata accesses, the loop below will iterate through all manual
  661. * arenas' ecache locks. As all the metadata from this base allocator
  662. * have been unlinked from the rtree, after going through all the
  663. * relevant ecache locks, it's safe to say that a) pending accesses are
  664. * all finished, and b) no new access will be generated.
  665. */
  666. if (opt_retain) {
  667. return;
  668. }
  669. unsigned destroy_ind = base_ind_get(base_to_destroy);
  670. assert(destroy_ind >= manual_arena_base);
  671. tsdn_t *tsdn = tsd_tsdn(tsd);
  672. malloc_mutex_t *delayed_mtx[ARENA_DESTROY_MAX_DELAYED_MTX];
  673. unsigned n_delayed = 0, total = narenas_total_get();
  674. for (unsigned i = 0; i < total; i++) {
  675. if (i == destroy_ind) {
  676. continue;
  677. }
  678. arena_t *arena = arena_get(tsdn, i, false);
  679. if (arena == NULL) {
  680. continue;
  681. }
  682. pac_t *pac = &arena->pa_shard.pac;
  683. arena_prepare_base_deletion_sync(tsd, &pac->ecache_dirty.mtx,
  684. delayed_mtx, &n_delayed);
  685. arena_prepare_base_deletion_sync(tsd, &pac->ecache_muzzy.mtx,
  686. delayed_mtx, &n_delayed);
  687. arena_prepare_base_deletion_sync(tsd, &pac->ecache_retained.mtx,
  688. delayed_mtx, &n_delayed);
  689. }
  690. arena_prepare_base_deletion_sync_finish(tsd, delayed_mtx, n_delayed);
  691. }
  692. #undef ARENA_DESTROY_MAX_DELAYED_MTX
  693. void
  694. arena_destroy(tsd_t *tsd, arena_t *arena) {
  695. assert(base_ind_get(arena->base) >= narenas_auto);
  696. assert(arena_nthreads_get(arena, false) == 0);
  697. assert(arena_nthreads_get(arena, true) == 0);
  698. /*
  699. * No allocations have occurred since arena_reset() was called.
  700. * Furthermore, the caller (arena_i_destroy_ctl()) purged all cached
  701. * extents, so only retained extents may remain and it's safe to call
  702. * pa_shard_destroy_retained.
  703. */
  704. pa_shard_destroy(tsd_tsdn(tsd), &arena->pa_shard);
  705. /*
  706. * Remove the arena pointer from the arenas array. We rely on the fact
  707. * that there is no way for the application to get a dirty read from the
  708. * arenas array unless there is an inherent race in the application
  709. * involving access of an arena being concurrently destroyed. The
  710. * application must synchronize knowledge of the arena's validity, so as
  711. * long as we use an atomic write to update the arenas array, the
  712. * application will get a clean read any time after it synchronizes
  713. * knowledge that the arena is no longer valid.
  714. */
  715. arena_set(base_ind_get(arena->base), NULL);
  716. /*
  717. * Destroy the base allocator, which manages all metadata ever mapped by
  718. * this arena. The prepare function will make sure no pending access to
  719. * the metadata in this base anymore.
  720. */
  721. arena_prepare_base_deletion(tsd, arena->base);
  722. base_delete(tsd_tsdn(tsd), arena->base);
  723. }
  724. static edata_t *
  725. arena_slab_alloc(tsdn_t *tsdn, arena_t *arena, szind_t binind, unsigned binshard,
  726. const bin_info_t *bin_info) {
  727. bool deferred_work_generated = false;
  728. witness_assert_depth_to_rank(tsdn_witness_tsdp_get(tsdn),
  729. WITNESS_RANK_CORE, 0);
  730. bool guarded = san_slab_extent_decide_guard(tsdn,
  731. arena_get_ehooks(arena));
  732. edata_t *slab = pa_alloc(tsdn, &arena->pa_shard, bin_info->slab_size,
  733. /* alignment */ PAGE, /* slab */ true, /* szind */ binind,
  734. /* zero */ false, guarded, &deferred_work_generated);
  735. if (deferred_work_generated) {
  736. arena_handle_deferred_work(tsdn, arena);
  737. }
  738. if (slab == NULL) {
  739. return NULL;
  740. }
  741. assert(edata_slab_get(slab));
  742. /* Initialize slab internals. */
  743. slab_data_t *slab_data = edata_slab_data_get(slab);
  744. edata_nfree_binshard_set(slab, bin_info->nregs, binshard);
  745. bitmap_init(slab_data->bitmap, &bin_info->bitmap_info, false);
  746. return slab;
  747. }
  748. /*
  749. * Before attempting the _with_fresh_slab approaches below, the _no_fresh_slab
  750. * variants (i.e. through slabcur and nonfull) must be tried first.
  751. */
  752. static void
  753. arena_bin_refill_slabcur_with_fresh_slab(tsdn_t *tsdn, arena_t *arena,
  754. bin_t *bin, szind_t binind, edata_t *fresh_slab) {
  755. malloc_mutex_assert_owner(tsdn, &bin->lock);
  756. /* Only called after slabcur and nonfull both failed. */
  757. assert(bin->slabcur == NULL);
  758. assert(edata_heap_first(&bin->slabs_nonfull) == NULL);
  759. assert(fresh_slab != NULL);
  760. /* A new slab from arena_slab_alloc() */
  761. assert(edata_nfree_get(fresh_slab) == bin_infos[binind].nregs);
  762. if (config_stats) {
  763. bin->stats.nslabs++;
  764. bin->stats.curslabs++;
  765. }
  766. bin->slabcur = fresh_slab;
  767. }
  768. /* Refill slabcur and then alloc using the fresh slab */
  769. static void *
  770. arena_bin_malloc_with_fresh_slab(tsdn_t *tsdn, arena_t *arena, bin_t *bin,
  771. szind_t binind, edata_t *fresh_slab) {
  772. malloc_mutex_assert_owner(tsdn, &bin->lock);
  773. arena_bin_refill_slabcur_with_fresh_slab(tsdn, arena, bin, binind,
  774. fresh_slab);
  775. return arena_slab_reg_alloc(bin->slabcur, &bin_infos[binind]);
  776. }
  777. static bool
  778. arena_bin_refill_slabcur_no_fresh_slab(tsdn_t *tsdn, arena_t *arena,
  779. bin_t *bin) {
  780. malloc_mutex_assert_owner(tsdn, &bin->lock);
  781. /* Only called after arena_slab_reg_alloc[_batch] failed. */
  782. assert(bin->slabcur == NULL || edata_nfree_get(bin->slabcur) == 0);
  783. if (bin->slabcur != NULL) {
  784. arena_bin_slabs_full_insert(arena, bin, bin->slabcur);
  785. }
  786. /* Look for a usable slab. */
  787. bin->slabcur = arena_bin_slabs_nonfull_tryget(bin);
  788. assert(bin->slabcur == NULL || edata_nfree_get(bin->slabcur) > 0);
  789. return (bin->slabcur == NULL);
  790. }
  791. bin_t *
  792. arena_bin_choose(tsdn_t *tsdn, arena_t *arena, szind_t binind,
  793. unsigned *binshard_p) {
  794. unsigned binshard;
  795. if (tsdn_null(tsdn) || tsd_arena_get(tsdn_tsd(tsdn)) == NULL) {
  796. binshard = 0;
  797. } else {
  798. binshard = tsd_binshardsp_get(tsdn_tsd(tsdn))->binshard[binind];
  799. }
  800. assert(binshard < bin_infos[binind].n_shards);
  801. if (binshard_p != NULL) {
  802. *binshard_p = binshard;
  803. }
  804. return arena_get_bin(arena, binind, binshard);
  805. }
  806. void
  807. arena_cache_bin_fill_small(tsdn_t *tsdn, arena_t *arena,
  808. cache_bin_t *cache_bin, cache_bin_info_t *cache_bin_info, szind_t binind,
  809. const unsigned nfill) {
  810. assert(cache_bin_ncached_get_local(cache_bin, cache_bin_info) == 0);
  811. const bin_info_t *bin_info = &bin_infos[binind];
  812. CACHE_BIN_PTR_ARRAY_DECLARE(ptrs, nfill);
  813. cache_bin_init_ptr_array_for_fill(cache_bin, cache_bin_info, &ptrs,
  814. nfill);
  815. /*
  816. * Bin-local resources are used first: 1) bin->slabcur, and 2) nonfull
  817. * slabs. After both are exhausted, new slabs will be allocated through
  818. * arena_slab_alloc().
  819. *
  820. * Bin lock is only taken / released right before / after the while(...)
  821. * refill loop, with new slab allocation (which has its own locking)
  822. * kept outside of the loop. This setup facilitates flat combining, at
  823. * the cost of the nested loop (through goto label_refill).
  824. *
  825. * To optimize for cases with contention and limited resources
  826. * (e.g. hugepage-backed or non-overcommit arenas), each fill-iteration
  827. * gets one chance of slab_alloc, and a retry of bin local resources
  828. * after the slab allocation (regardless if slab_alloc failed, because
  829. * the bin lock is dropped during the slab allocation).
  830. *
  831. * In other words, new slab allocation is allowed, as long as there was
  832. * progress since the previous slab_alloc. This is tracked with
  833. * made_progress below, initialized to true to jump start the first
  834. * iteration.
  835. *
  836. * In other words (again), the loop will only terminate early (i.e. stop
  837. * with filled < nfill) after going through the three steps: a) bin
  838. * local exhausted, b) unlock and slab_alloc returns null, c) re-lock
  839. * and bin local fails again.
  840. */
  841. bool made_progress = true;
  842. edata_t *fresh_slab = NULL;
  843. bool alloc_and_retry = false;
  844. unsigned filled = 0;
  845. unsigned binshard;
  846. bin_t *bin = arena_bin_choose(tsdn, arena, binind, &binshard);
  847. label_refill:
  848. malloc_mutex_lock(tsdn, &bin->lock);
  849. while (filled < nfill) {
  850. /* Try batch-fill from slabcur first. */
  851. edata_t *slabcur = bin->slabcur;
  852. if (slabcur != NULL && edata_nfree_get(slabcur) > 0) {
  853. unsigned tofill = nfill - filled;
  854. unsigned nfree = edata_nfree_get(slabcur);
  855. unsigned cnt = tofill < nfree ? tofill : nfree;
  856. arena_slab_reg_alloc_batch(slabcur, bin_info, cnt,
  857. &ptrs.ptr[filled]);
  858. made_progress = true;
  859. filled += cnt;
  860. continue;
  861. }
  862. /* Next try refilling slabcur from nonfull slabs. */
  863. if (!arena_bin_refill_slabcur_no_fresh_slab(tsdn, arena, bin)) {
  864. assert(bin->slabcur != NULL);
  865. continue;
  866. }
  867. /* Then see if a new slab was reserved already. */
  868. if (fresh_slab != NULL) {
  869. arena_bin_refill_slabcur_with_fresh_slab(tsdn, arena,
  870. bin, binind, fresh_slab);
  871. assert(bin->slabcur != NULL);
  872. fresh_slab = NULL;
  873. continue;
  874. }
  875. /* Try slab_alloc if made progress (or never did slab_alloc). */
  876. if (made_progress) {
  877. assert(bin->slabcur == NULL);
  878. assert(fresh_slab == NULL);
  879. alloc_and_retry = true;
  880. /* Alloc a new slab then come back. */
  881. break;
  882. }
  883. /* OOM. */
  884. assert(fresh_slab == NULL);
  885. assert(!alloc_and_retry);
  886. break;
  887. } /* while (filled < nfill) loop. */
  888. if (config_stats && !alloc_and_retry) {
  889. bin->stats.nmalloc += filled;
  890. bin->stats.nrequests += cache_bin->tstats.nrequests;
  891. bin->stats.curregs += filled;
  892. bin->stats.nfills++;
  893. cache_bin->tstats.nrequests = 0;
  894. }
  895. malloc_mutex_unlock(tsdn, &bin->lock);
  896. if (alloc_and_retry) {
  897. assert(fresh_slab == NULL);
  898. assert(filled < nfill);
  899. assert(made_progress);
  900. fresh_slab = arena_slab_alloc(tsdn, arena, binind, binshard,
  901. bin_info);
  902. /* fresh_slab NULL case handled in the for loop. */
  903. alloc_and_retry = false;
  904. made_progress = false;
  905. goto label_refill;
  906. }
  907. assert(filled == nfill || (fresh_slab == NULL && !made_progress));
  908. /* Release if allocated but not used. */
  909. if (fresh_slab != NULL) {
  910. assert(edata_nfree_get(fresh_slab) == bin_info->nregs);
  911. arena_slab_dalloc(tsdn, arena, fresh_slab);
  912. fresh_slab = NULL;
  913. }
  914. cache_bin_finish_fill(cache_bin, cache_bin_info, &ptrs, filled);
  915. arena_decay_tick(tsdn, arena);
  916. }
  917. size_t
  918. arena_fill_small_fresh(tsdn_t *tsdn, arena_t *arena, szind_t binind,
  919. void **ptrs, size_t nfill, bool zero) {
  920. assert(binind < SC_NBINS);
  921. const bin_info_t *bin_info = &bin_infos[binind];
  922. const size_t nregs = bin_info->nregs;
  923. assert(nregs > 0);
  924. const size_t usize = bin_info->reg_size;
  925. const bool manual_arena = !arena_is_auto(arena);
  926. unsigned binshard;
  927. bin_t *bin = arena_bin_choose(tsdn, arena, binind, &binshard);
  928. size_t nslab = 0;
  929. size_t filled = 0;
  930. edata_t *slab = NULL;
  931. edata_list_active_t fulls;
  932. edata_list_active_init(&fulls);
  933. while (filled < nfill && (slab = arena_slab_alloc(tsdn, arena, binind,
  934. binshard, bin_info)) != NULL) {
  935. assert((size_t)edata_nfree_get(slab) == nregs);
  936. ++nslab;
  937. size_t batch = nfill - filled;
  938. if (batch > nregs) {
  939. batch = nregs;
  940. }
  941. assert(batch > 0);
  942. arena_slab_reg_alloc_batch(slab, bin_info, (unsigned)batch,
  943. &ptrs[filled]);
  944. assert(edata_addr_get(slab) == ptrs[filled]);
  945. if (zero) {
  946. memset(ptrs[filled], 0, batch * usize);
  947. }
  948. filled += batch;
  949. if (batch == nregs) {
  950. if (manual_arena) {
  951. edata_list_active_append(&fulls, slab);
  952. }
  953. slab = NULL;
  954. }
  955. }
  956. malloc_mutex_lock(tsdn, &bin->lock);
  957. /*
  958. * Only the last slab can be non-empty, and the last slab is non-empty
  959. * iff slab != NULL.
  960. */
  961. if (slab != NULL) {
  962. arena_bin_lower_slab(tsdn, arena, slab, bin);
  963. }
  964. if (manual_arena) {
  965. edata_list_active_concat(&bin->slabs_full, &fulls);
  966. }
  967. assert(edata_list_active_empty(&fulls));
  968. if (config_stats) {
  969. bin->stats.nslabs += nslab;
  970. bin->stats.curslabs += nslab;
  971. bin->stats.nmalloc += filled;
  972. bin->stats.nrequests += filled;
  973. bin->stats.curregs += filled;
  974. }
  975. malloc_mutex_unlock(tsdn, &bin->lock);
  976. arena_decay_tick(tsdn, arena);
  977. return filled;
  978. }
  979. /*
  980. * Without allocating a new slab, try arena_slab_reg_alloc() and re-fill
  981. * bin->slabcur if necessary.
  982. */
  983. static void *
  984. arena_bin_malloc_no_fresh_slab(tsdn_t *tsdn, arena_t *arena, bin_t *bin,
  985. szind_t binind) {
  986. malloc_mutex_assert_owner(tsdn, &bin->lock);
  987. if (bin->slabcur == NULL || edata_nfree_get(bin->slabcur) == 0) {
  988. if (arena_bin_refill_slabcur_no_fresh_slab(tsdn, arena, bin)) {
  989. return NULL;
  990. }
  991. }
  992. assert(bin->slabcur != NULL && edata_nfree_get(bin->slabcur) > 0);
  993. return arena_slab_reg_alloc(bin->slabcur, &bin_infos[binind]);
  994. }
  995. static void *
  996. arena_malloc_small(tsdn_t *tsdn, arena_t *arena, szind_t binind, bool zero) {
  997. assert(binind < SC_NBINS);
  998. const bin_info_t *bin_info = &bin_infos[binind];
  999. size_t usize = sz_index2size(binind);
  1000. unsigned binshard;
  1001. bin_t *bin = arena_bin_choose(tsdn, arena, binind, &binshard);
  1002. malloc_mutex_lock(tsdn, &bin->lock);
  1003. edata_t *fresh_slab = NULL;
  1004. void *ret = arena_bin_malloc_no_fresh_slab(tsdn, arena, bin, binind);
  1005. if (ret == NULL) {
  1006. malloc_mutex_unlock(tsdn, &bin->lock);
  1007. /******************************/
  1008. fresh_slab = arena_slab_alloc(tsdn, arena, binind, binshard,
  1009. bin_info);
  1010. /********************************/
  1011. malloc_mutex_lock(tsdn, &bin->lock);
  1012. /* Retry since the lock was dropped. */
  1013. ret = arena_bin_malloc_no_fresh_slab(tsdn, arena, bin, binind);
  1014. if (ret == NULL) {
  1015. if (fresh_slab == NULL) {
  1016. /* OOM */
  1017. malloc_mutex_unlock(tsdn, &bin->lock);
  1018. return NULL;
  1019. }
  1020. ret = arena_bin_malloc_with_fresh_slab(tsdn, arena, bin,
  1021. binind, fresh_slab);
  1022. fresh_slab = NULL;
  1023. }
  1024. }
  1025. if (config_stats) {
  1026. bin->stats.nmalloc++;
  1027. bin->stats.nrequests++;
  1028. bin->stats.curregs++;
  1029. }
  1030. malloc_mutex_unlock(tsdn, &bin->lock);
  1031. if (fresh_slab != NULL) {
  1032. arena_slab_dalloc(tsdn, arena, fresh_slab);
  1033. }
  1034. if (zero) {
  1035. memset(ret, 0, usize);
  1036. }
  1037. arena_decay_tick(tsdn, arena);
  1038. return ret;
  1039. }
  1040. void *
  1041. arena_malloc_hard(tsdn_t *tsdn, arena_t *arena, size_t size, szind_t ind,
  1042. bool zero) {
  1043. assert(!tsdn_null(tsdn) || arena != NULL);
  1044. if (likely(!tsdn_null(tsdn))) {
  1045. arena = arena_choose_maybe_huge(tsdn_tsd(tsdn), arena, size);
  1046. }
  1047. if (unlikely(arena == NULL)) {
  1048. return NULL;
  1049. }
  1050. if (likely(size <= SC_SMALL_MAXCLASS)) {
  1051. return arena_malloc_small(tsdn, arena, ind, zero);
  1052. }
  1053. return large_malloc(tsdn, arena, sz_index2size(ind), zero);
  1054. }
  1055. void *
  1056. arena_palloc(tsdn_t *tsdn, arena_t *arena, size_t usize, size_t alignment,
  1057. bool zero, tcache_t *tcache) {
  1058. void *ret;
  1059. if (usize <= SC_SMALL_MAXCLASS) {
  1060. /* Small; alignment doesn't require special slab placement. */
  1061. /* usize should be a result of sz_sa2u() */
  1062. assert((usize & (alignment - 1)) == 0);
  1063. /*
  1064. * Small usize can't come from an alignment larger than a page.
  1065. */
  1066. assert(alignment <= PAGE);
  1067. ret = arena_malloc(tsdn, arena, usize, sz_size2index(usize),
  1068. zero, tcache, true);
  1069. } else {
  1070. if (likely(alignment <= CACHELINE)) {
  1071. ret = large_malloc(tsdn, arena, usize, zero);
  1072. } else {
  1073. ret = large_palloc(tsdn, arena, usize, alignment, zero);
  1074. }
  1075. }
  1076. return ret;
  1077. }
  1078. void
  1079. arena_prof_promote(tsdn_t *tsdn, void *ptr, size_t usize) {
  1080. cassert(config_prof);
  1081. assert(ptr != NULL);
  1082. assert(isalloc(tsdn, ptr) == SC_LARGE_MINCLASS);
  1083. assert(usize <= SC_SMALL_MAXCLASS);
  1084. if (config_opt_safety_checks) {
  1085. safety_check_set_redzone(ptr, usize, SC_LARGE_MINCLASS);
  1086. }
  1087. edata_t *edata = emap_edata_lookup(tsdn, &arena_emap_global, ptr);
  1088. szind_t szind = sz_size2index(usize);
  1089. edata_szind_set(edata, szind);
  1090. emap_remap(tsdn, &arena_emap_global, edata, szind, /* slab */ false);
  1091. assert(isalloc(tsdn, ptr) == usize);
  1092. }
  1093. static size_t
  1094. arena_prof_demote(tsdn_t *tsdn, edata_t *edata, const void *ptr) {
  1095. cassert(config_prof);
  1096. assert(ptr != NULL);
  1097. edata_szind_set(edata, SC_NBINS);
  1098. emap_remap(tsdn, &arena_emap_global, edata, SC_NBINS, /* slab */ false);
  1099. assert(isalloc(tsdn, ptr) == SC_LARGE_MINCLASS);
  1100. return SC_LARGE_MINCLASS;
  1101. }
  1102. void
  1103. arena_dalloc_promoted(tsdn_t *tsdn, void *ptr, tcache_t *tcache,
  1104. bool slow_path) {
  1105. cassert(config_prof);
  1106. assert(opt_prof);
  1107. edata_t *edata = emap_edata_lookup(tsdn, &arena_emap_global, ptr);
  1108. size_t usize = edata_usize_get(edata);
  1109. size_t bumped_usize = arena_prof_demote(tsdn, edata, ptr);
  1110. if (config_opt_safety_checks && usize < SC_LARGE_MINCLASS) {
  1111. /*
  1112. * Currently, we only do redzoning for small sampled
  1113. * allocations.
  1114. */
  1115. assert(bumped_usize == SC_LARGE_MINCLASS);
  1116. safety_check_verify_redzone(ptr, usize, bumped_usize);
  1117. }
  1118. if (bumped_usize <= tcache_maxclass && tcache != NULL) {
  1119. tcache_dalloc_large(tsdn_tsd(tsdn), tcache, ptr,
  1120. sz_size2index(bumped_usize), slow_path);
  1121. } else {
  1122. large_dalloc(tsdn, edata);
  1123. }
  1124. }
  1125. static void
  1126. arena_dissociate_bin_slab(arena_t *arena, edata_t *slab, bin_t *bin) {
  1127. /* Dissociate slab from bin. */
  1128. if (slab == bin->slabcur) {
  1129. bin->slabcur = NULL;
  1130. } else {
  1131. szind_t binind = edata_szind_get(slab);
  1132. const bin_info_t *bin_info = &bin_infos[binind];
  1133. /*
  1134. * The following block's conditional is necessary because if the
  1135. * slab only contains one region, then it never gets inserted
  1136. * into the non-full slabs heap.
  1137. */
  1138. if (bin_info->nregs == 1) {
  1139. arena_bin_slabs_full_remove(arena, bin, slab);
  1140. } else {
  1141. arena_bin_slabs_nonfull_remove(bin, slab);
  1142. }
  1143. }
  1144. }
  1145. static void
  1146. arena_bin_lower_slab(tsdn_t *tsdn, arena_t *arena, edata_t *slab,
  1147. bin_t *bin) {
  1148. assert(edata_nfree_get(slab) > 0);
  1149. /*
  1150. * Make sure that if bin->slabcur is non-NULL, it refers to the
  1151. * oldest/lowest non-full slab. It is okay to NULL slabcur out rather
  1152. * than proactively keeping it pointing at the oldest/lowest non-full
  1153. * slab.
  1154. */
  1155. if (bin->slabcur != NULL && edata_snad_comp(bin->slabcur, slab) > 0) {
  1156. /* Switch slabcur. */
  1157. if (edata_nfree_get(bin->slabcur) > 0) {
  1158. arena_bin_slabs_nonfull_insert(bin, bin->slabcur);
  1159. } else {
  1160. arena_bin_slabs_full_insert(arena, bin, bin->slabcur);
  1161. }
  1162. bin->slabcur = slab;
  1163. if (config_stats) {
  1164. bin->stats.reslabs++;
  1165. }
  1166. } else {
  1167. arena_bin_slabs_nonfull_insert(bin, slab);
  1168. }
  1169. }
  1170. static void
  1171. arena_dalloc_bin_slab_prepare(tsdn_t *tsdn, edata_t *slab, bin_t *bin) {
  1172. malloc_mutex_assert_owner(tsdn, &bin->lock);
  1173. assert(slab != bin->slabcur);
  1174. if (config_stats) {
  1175. bin->stats.curslabs--;
  1176. }
  1177. }
  1178. void
  1179. arena_dalloc_bin_locked_handle_newly_empty(tsdn_t *tsdn, arena_t *arena,
  1180. edata_t *slab, bin_t *bin) {
  1181. arena_dissociate_bin_slab(arena, slab, bin);
  1182. arena_dalloc_bin_slab_prepare(tsdn, slab, bin);
  1183. }
  1184. void
  1185. arena_dalloc_bin_locked_handle_newly_nonempty(tsdn_t *tsdn, arena_t *arena,
  1186. edata_t *slab, bin_t *bin) {
  1187. arena_bin_slabs_full_remove(arena, bin, slab);
  1188. arena_bin_lower_slab(tsdn, arena, slab, bin);
  1189. }
  1190. static void
  1191. arena_dalloc_bin(tsdn_t *tsdn, arena_t *arena, edata_t *edata, void *ptr) {
  1192. szind_t binind = edata_szind_get(edata);
  1193. unsigned binshard = edata_binshard_get(edata);
  1194. bin_t *bin = arena_get_bin(arena, binind, binshard);
  1195. malloc_mutex_lock(tsdn, &bin->lock);
  1196. arena_dalloc_bin_locked_info_t info;
  1197. arena_dalloc_bin_locked_begin(&info, binind);
  1198. bool ret = arena_dalloc_bin_locked_step(tsdn, arena, bin,
  1199. &info, binind, edata, ptr);
  1200. arena_dalloc_bin_locked_finish(tsdn, arena, bin, &info);
  1201. malloc_mutex_unlock(tsdn, &bin->lock);
  1202. if (ret) {
  1203. arena_slab_dalloc(tsdn, arena, edata);
  1204. }
  1205. }
  1206. void
  1207. arena_dalloc_small(tsdn_t *tsdn, void *ptr) {
  1208. edata_t *edata = emap_edata_lookup(tsdn, &arena_emap_global, ptr);
  1209. arena_t *arena = arena_get_from_edata(edata);
  1210. arena_dalloc_bin(tsdn, arena, edata, ptr);
  1211. arena_decay_tick(tsdn, arena);
  1212. }
  1213. bool
  1214. arena_ralloc_no_move(tsdn_t *tsdn, void *ptr, size_t oldsize, size_t size,
  1215. size_t extra, bool zero, size_t *newsize) {
  1216. bool ret;
  1217. /* Calls with non-zero extra had to clamp extra. */
  1218. assert(extra == 0 || size + extra <= SC_LARGE_MAXCLASS);
  1219. edata_t *edata = emap_edata_lookup(tsdn, &arena_emap_global, ptr);
  1220. if (unlikely(size > SC_LARGE_MAXCLASS)) {
  1221. ret = true;
  1222. goto done;
  1223. }
  1224. size_t usize_min = sz_s2u(size);
  1225. size_t usize_max = sz_s2u(size + extra);
  1226. if (likely(oldsize <= SC_SMALL_MAXCLASS && usize_min
  1227. <= SC_SMALL_MAXCLASS)) {
  1228. /*
  1229. * Avoid moving the allocation if the size class can be left the
  1230. * same.
  1231. */
  1232. assert(bin_infos[sz_size2index(oldsize)].reg_size ==
  1233. oldsize);
  1234. if ((usize_max > SC_SMALL_MAXCLASS
  1235. || sz_size2index(usize_max) != sz_size2index(oldsize))
  1236. && (size > oldsize || usize_max < oldsize)) {
  1237. ret = true;
  1238. goto done;
  1239. }
  1240. arena_t *arena = arena_get_from_edata(edata);
  1241. arena_decay_tick(tsdn, arena);
  1242. ret = false;
  1243. } else if (oldsize >= SC_LARGE_MINCLASS
  1244. && usize_max >= SC_LARGE_MINCLASS) {
  1245. ret = large_ralloc_no_move(tsdn, edata, usize_min, usize_max,
  1246. zero);
  1247. } else {
  1248. ret = true;
  1249. }
  1250. done:
  1251. assert(edata == emap_edata_lookup(tsdn, &arena_emap_global, ptr));
  1252. *newsize = edata_usize_get(edata);
  1253. return ret;
  1254. }
  1255. static void *
  1256. arena_ralloc_move_helper(tsdn_t *tsdn, arena_t *arena, size_t usize,
  1257. size_t alignment, bool zero, tcache_t *tcache) {
  1258. if (alignment == 0) {
  1259. return arena_malloc(tsdn, arena, usize, sz_size2index(usize),
  1260. zero, tcache, true);
  1261. }
  1262. usize = sz_sa2u(usize, alignment);
  1263. if (unlikely(usize == 0 || usize > SC_LARGE_MAXCLASS)) {
  1264. return NULL;
  1265. }
  1266. return ipalloct(tsdn, usize, alignment, zero, tcache, arena);
  1267. }
  1268. void *
  1269. arena_ralloc(tsdn_t *tsdn, arena_t *arena, void *ptr, size_t oldsize,
  1270. size_t size, size_t alignment, bool zero, tcache_t *tcache,
  1271. hook_ralloc_args_t *hook_args) {
  1272. size_t usize = alignment == 0 ? sz_s2u(size) : sz_sa2u(size, alignment);
  1273. if (unlikely(usize == 0 || size > SC_LARGE_MAXCLASS)) {
  1274. return NULL;
  1275. }
  1276. if (likely(usize <= SC_SMALL_MAXCLASS)) {
  1277. /* Try to avoid moving the allocation. */
  1278. UNUSED size_t newsize;
  1279. if (!arena_ralloc_no_move(tsdn, ptr, oldsize, usize, 0, zero,
  1280. &newsize)) {
  1281. hook_invoke_expand(hook_args->is_realloc
  1282. ? hook_expand_realloc : hook_expand_rallocx,
  1283. ptr, oldsize, usize, (uintptr_t)ptr,
  1284. hook_args->args);
  1285. return ptr;
  1286. }
  1287. }
  1288. if (oldsize >= SC_LARGE_MINCLASS
  1289. && usize >= SC_LARGE_MINCLASS) {
  1290. return large_ralloc(tsdn, arena, ptr, usize,
  1291. alignment, zero, tcache, hook_args);
  1292. }
  1293. /*
  1294. * size and oldsize are different enough that we need to move the
  1295. * object. In that case, fall back to allocating new space and copying.
  1296. */
  1297. void *ret = arena_ralloc_move_helper(tsdn, arena, usize, alignment,
  1298. zero, tcache);
  1299. if (ret == NULL) {
  1300. return NULL;
  1301. }
  1302. hook_invoke_alloc(hook_args->is_realloc
  1303. ? hook_alloc_realloc : hook_alloc_rallocx, ret, (uintptr_t)ret,
  1304. hook_args->args);
  1305. hook_invoke_dalloc(hook_args->is_realloc
  1306. ? hook_dalloc_realloc : hook_dalloc_rallocx, ptr, hook_args->args);
  1307. /*
  1308. * Junk/zero-filling were already done by
  1309. * ipalloc()/arena_malloc().
  1310. */
  1311. size_t copysize = (usize < oldsize) ? usize : oldsize;
  1312. memcpy(ret, ptr, copysize);
  1313. isdalloct(tsdn, ptr, oldsize, tcache, NULL, true);
  1314. return ret;
  1315. }
  1316. ehooks_t *
  1317. arena_get_ehooks(arena_t *arena) {
  1318. return base_ehooks_get(arena->base);
  1319. }
  1320. extent_hooks_t *
  1321. arena_set_extent_hooks(tsd_t *tsd, arena_t *arena,
  1322. extent_hooks_t *extent_hooks) {
  1323. background_thread_info_t *info;
  1324. if (have_background_thread) {
  1325. info = arena_background_thread_info_get(arena);
  1326. malloc_mutex_lock(tsd_tsdn(tsd), &info->mtx);
  1327. }
  1328. /* No using the HPA now that we have the custom hooks. */
  1329. pa_shard_disable_hpa(tsd_tsdn(tsd), &arena->pa_shard);
  1330. extent_hooks_t *ret = base_extent_hooks_set(arena->base, extent_hooks);
  1331. if (have_background_thread) {
  1332. malloc_mutex_unlock(tsd_tsdn(tsd), &info->mtx);
  1333. }
  1334. return ret;
  1335. }
  1336. dss_prec_t
  1337. arena_dss_prec_get(arena_t *arena) {
  1338. return (dss_prec_t)atomic_load_u(&arena->dss_prec, ATOMIC_ACQUIRE);
  1339. }
  1340. bool
  1341. arena_dss_prec_set(arena_t *arena, dss_prec_t dss_prec) {
  1342. if (!have_dss) {
  1343. return (dss_prec != dss_prec_disabled);
  1344. }
  1345. atomic_store_u(&arena->dss_prec, (unsigned)dss_prec, ATOMIC_RELEASE);
  1346. return false;
  1347. }
  1348. ssize_t
  1349. arena_dirty_decay_ms_default_get(void) {
  1350. return atomic_load_zd(&dirty_decay_ms_default, ATOMIC_RELAXED);
  1351. }
  1352. bool
  1353. arena_dirty_decay_ms_default_set(ssize_t decay_ms) {
  1354. if (!decay_ms_valid(decay_ms)) {
  1355. return true;
  1356. }
  1357. atomic_store_zd(&dirty_decay_ms_default, decay_ms, ATOMIC_RELAXED);
  1358. return false;
  1359. }
  1360. ssize_t
  1361. arena_muzzy_decay_ms_default_get(void) {
  1362. return atomic_load_zd(&muzzy_decay_ms_default, ATOMIC_RELAXED);
  1363. }
  1364. bool
  1365. arena_muzzy_decay_ms_default_set(ssize_t decay_ms) {
  1366. if (!decay_ms_valid(decay_ms)) {
  1367. return true;
  1368. }
  1369. atomic_store_zd(&muzzy_decay_ms_default, decay_ms, ATOMIC_RELAXED);
  1370. return false;
  1371. }
  1372. bool
  1373. arena_retain_grow_limit_get_set(tsd_t *tsd, arena_t *arena, size_t *old_limit,
  1374. size_t *new_limit) {
  1375. assert(opt_retain);
  1376. return pac_retain_grow_limit_get_set(tsd_tsdn(tsd),
  1377. &arena->pa_shard.pac, old_limit, new_limit);
  1378. }
  1379. unsigned
  1380. arena_nthreads_get(arena_t *arena, bool internal) {
  1381. return atomic_load_u(&arena->nthreads[internal], ATOMIC_RELAXED);
  1382. }
  1383. void
  1384. arena_nthreads_inc(arena_t *arena, bool internal) {
  1385. atomic_fetch_add_u(&arena->nthreads[internal], 1, ATOMIC_RELAXED);
  1386. }
  1387. void
  1388. arena_nthreads_dec(arena_t *arena, bool internal) {
  1389. atomic_fetch_sub_u(&arena->nthreads[internal], 1, ATOMIC_RELAXED);
  1390. }
  1391. arena_t *
  1392. arena_new(tsdn_t *tsdn, unsigned ind, const arena_config_t *config) {
  1393. arena_t *arena;
  1394. base_t *base;
  1395. unsigned i;
  1396. if (ind == 0) {
  1397. base = b0get();
  1398. } else {
  1399. base = base_new(tsdn, ind, config->extent_hooks,
  1400. config->metadata_use_hooks);
  1401. if (base == NULL) {
  1402. return NULL;
  1403. }
  1404. }
  1405. size_t arena_size = sizeof(arena_t) + sizeof(bin_t) * nbins_total;
  1406. arena = (arena_t *)base_alloc(tsdn, base, arena_size, CACHELINE);
  1407. if (arena == NULL) {
  1408. goto label_error;
  1409. }
  1410. atomic_store_u(&arena->nthreads[0], 0, ATOMIC_RELAXED);
  1411. atomic_store_u(&arena->nthreads[1], 0, ATOMIC_RELAXED);
  1412. arena->last_thd = NULL;
  1413. if (config_stats) {
  1414. if (arena_stats_init(tsdn, &arena->stats)) {
  1415. goto label_error;
  1416. }
  1417. ql_new(&arena->tcache_ql);
  1418. ql_new(&arena->cache_bin_array_descriptor_ql);
  1419. if (malloc_mutex_init(&arena->tcache_ql_mtx, "tcache_ql",
  1420. WITNESS_RANK_TCACHE_QL, malloc_mutex_rank_exclusive)) {
  1421. goto label_error;
  1422. }
  1423. }
  1424. atomic_store_u(&arena->dss_prec, (unsigned)extent_dss_prec_get(),
  1425. ATOMIC_RELAXED);
  1426. edata_list_active_init(&arena->large);
  1427. if (malloc_mutex_init(&arena->large_mtx, "arena_large",
  1428. WITNESS_RANK_ARENA_LARGE, malloc_mutex_rank_exclusive)) {
  1429. goto label_error;
  1430. }
  1431. nstime_t cur_time;
  1432. nstime_init_update(&cur_time);
  1433. if (pa_shard_init(tsdn, &arena->pa_shard, &arena_pa_central_global,
  1434. &arena_emap_global, base, ind, &arena->stats.pa_shard_stats,
  1435. LOCKEDINT_MTX(arena->stats.mtx), &cur_time, oversize_threshold,
  1436. arena_dirty_decay_ms_default_get(),
  1437. arena_muzzy_decay_ms_default_get())) {
  1438. goto label_error;
  1439. }
  1440. /* Initialize bins. */
  1441. atomic_store_u(&arena->binshard_next, 0, ATOMIC_RELEASE);
  1442. for (i = 0; i < nbins_total; i++) {
  1443. bool err = bin_init(&arena->bins[i]);
  1444. if (err) {
  1445. goto label_error;
  1446. }
  1447. }
  1448. arena->base = base;
  1449. /* Set arena before creating background threads. */
  1450. arena_set(ind, arena);
  1451. arena->ind = ind;
  1452. nstime_init_update(&arena->create_time);
  1453. /*
  1454. * We turn on the HPA if set to. There are two exceptions:
  1455. * - Custom extent hooks (we should only return memory allocated from
  1456. * them in that case).
  1457. * - Arena 0 initialization. In this case, we're mid-bootstrapping, and
  1458. * so arena_hpa_global is not yet initialized.
  1459. */
  1460. if (opt_hpa && ehooks_are_default(base_ehooks_get(base)) && ind != 0) {
  1461. hpa_shard_opts_t hpa_shard_opts = opt_hpa_opts;
  1462. hpa_shard_opts.deferral_allowed = background_thread_enabled();
  1463. if (pa_shard_enable_hpa(tsdn, &arena->pa_shard,
  1464. &hpa_shard_opts, &opt_hpa_sec_opts)) {
  1465. goto label_error;
  1466. }
  1467. }
  1468. /* We don't support reentrancy for arena 0 bootstrapping. */
  1469. if (ind != 0) {
  1470. /*
  1471. * If we're here, then arena 0 already exists, so bootstrapping
  1472. * is done enough that we should have tsd.
  1473. */
  1474. assert(!tsdn_null(tsdn));
  1475. pre_reentrancy(tsdn_tsd(tsdn), arena);
  1476. if (test_hooks_arena_new_hook) {
  1477. test_hooks_arena_new_hook();
  1478. }
  1479. post_reentrancy(tsdn_tsd(tsdn));
  1480. }
  1481. return arena;
  1482. label_error:
  1483. if (ind != 0) {
  1484. base_delete(tsdn, base);
  1485. }
  1486. return NULL;
  1487. }
  1488. arena_t *
  1489. arena_choose_huge(tsd_t *tsd) {
  1490. /* huge_arena_ind can be 0 during init (will use a0). */
  1491. if (huge_arena_ind == 0) {
  1492. assert(!malloc_initialized());
  1493. }
  1494. arena_t *huge_arena = arena_get(tsd_tsdn(tsd), huge_arena_ind, false);
  1495. if (huge_arena == NULL) {
  1496. /* Create the huge arena on demand. */
  1497. assert(huge_arena_ind != 0);
  1498. huge_arena = arena_get(tsd_tsdn(tsd), huge_arena_ind, true);
  1499. if (huge_arena == NULL) {
  1500. return NULL;
  1501. }
  1502. /*
  1503. * Purge eagerly for huge allocations, because: 1) number of
  1504. * huge allocations is usually small, which means ticker based
  1505. * decay is not reliable; and 2) less immediate reuse is
  1506. * expected for huge allocations.
  1507. */
  1508. if (arena_dirty_decay_ms_default_get() > 0) {
  1509. arena_decay_ms_set(tsd_tsdn(tsd), huge_arena,
  1510. extent_state_dirty, 0);
  1511. }
  1512. if (arena_muzzy_decay_ms_default_get() > 0) {
  1513. arena_decay_ms_set(tsd_tsdn(tsd), huge_arena,
  1514. extent_state_muzzy, 0);
  1515. }
  1516. }
  1517. return huge_arena;
  1518. }
  1519. bool
  1520. arena_init_huge(void) {
  1521. bool huge_enabled;
  1522. /* The threshold should be large size class. */
  1523. if (opt_oversize_threshold > SC_LARGE_MAXCLASS ||
  1524. opt_oversize_threshold < SC_LARGE_MINCLASS) {
  1525. opt_oversize_threshold = 0;
  1526. oversize_threshold = SC_LARGE_MAXCLASS + PAGE;
  1527. huge_enabled = false;
  1528. } else {
  1529. /* Reserve the index for the huge arena. */
  1530. huge_arena_ind = narenas_total_get();
  1531. oversize_threshold = opt_oversize_threshold;
  1532. huge_enabled = true;
  1533. }
  1534. return huge_enabled;
  1535. }
  1536. bool
  1537. arena_is_huge(unsigned arena_ind) {
  1538. if (huge_arena_ind == 0) {
  1539. return false;
  1540. }
  1541. return (arena_ind == huge_arena_ind);
  1542. }
  1543. bool
  1544. arena_boot(sc_data_t *sc_data, base_t *base, bool hpa) {
  1545. arena_dirty_decay_ms_default_set(opt_dirty_decay_ms);
  1546. arena_muzzy_decay_ms_default_set(opt_muzzy_decay_ms);
  1547. for (unsigned i = 0; i < SC_NBINS; i++) {
  1548. sc_t *sc = &sc_data->sc[i];
  1549. div_init(&arena_binind_div_info[i],
  1550. (1U << sc->lg_base) + (sc->ndelta << sc->lg_delta));
  1551. }
  1552. uint32_t cur_offset = (uint32_t)offsetof(arena_t, bins);
  1553. for (szind_t i = 0; i < SC_NBINS; i++) {
  1554. arena_bin_offsets[i] = cur_offset;
  1555. nbins_total += bin_infos[i].n_shards;
  1556. cur_offset += (uint32_t)(bin_infos[i].n_shards * sizeof(bin_t));
  1557. }
  1558. return pa_central_init(&arena_pa_central_global, base, hpa,
  1559. &hpa_hooks_default);
  1560. }
  1561. void
  1562. arena_prefork0(tsdn_t *tsdn, arena_t *arena) {
  1563. pa_shard_prefork0(tsdn, &arena->pa_shard);
  1564. }
  1565. void
  1566. arena_prefork1(tsdn_t *tsdn, arena_t *arena) {
  1567. if (config_stats) {
  1568. malloc_mutex_prefork(tsdn, &arena->tcache_ql_mtx);
  1569. }
  1570. }
  1571. void
  1572. arena_prefork2(tsdn_t *tsdn, arena_t *arena) {
  1573. pa_shard_prefork2(tsdn, &arena->pa_shard);
  1574. }
  1575. void
  1576. arena_prefork3(tsdn_t *tsdn, arena_t *arena) {
  1577. pa_shard_prefork3(tsdn, &arena->pa_shard);
  1578. }
  1579. void
  1580. arena_prefork4(tsdn_t *tsdn, arena_t *arena) {
  1581. pa_shard_prefork4(tsdn, &arena->pa_shard);
  1582. }
  1583. void
  1584. arena_prefork5(tsdn_t *tsdn, arena_t *arena) {
  1585. pa_shard_prefork5(tsdn, &arena->pa_shard);
  1586. }
  1587. void
  1588. arena_prefork6(tsdn_t *tsdn, arena_t *arena) {
  1589. base_prefork(tsdn, arena->base);
  1590. }
  1591. void
  1592. arena_prefork7(tsdn_t *tsdn, arena_t *arena) {
  1593. malloc_mutex_prefork(tsdn, &arena->large_mtx);
  1594. }
  1595. void
  1596. arena_prefork8(tsdn_t *tsdn, arena_t *arena) {
  1597. for (unsigned i = 0; i < nbins_total; i++) {
  1598. bin_prefork(tsdn, &arena->bins[i]);
  1599. }
  1600. }
  1601. void
  1602. arena_postfork_parent(tsdn_t *tsdn, arena_t *arena) {
  1603. for (unsigned i = 0; i < nbins_total; i++) {
  1604. bin_postfork_parent(tsdn, &arena->bins[i]);
  1605. }
  1606. malloc_mutex_postfork_parent(tsdn, &arena->large_mtx);
  1607. base_postfork_parent(tsdn, arena->base);
  1608. pa_shard_postfork_parent(tsdn, &arena->pa_shard);
  1609. if (config_stats) {
  1610. malloc_mutex_postfork_parent(tsdn, &arena->tcache_ql_mtx);
  1611. }
  1612. }
  1613. void
  1614. arena_postfork_child(tsdn_t *tsdn, arena_t *arena) {
  1615. atomic_store_u(&arena->nthreads[0], 0, ATOMIC_RELAXED);
  1616. atomic_store_u(&arena->nthreads[1], 0, ATOMIC_RELAXED);
  1617. if (tsd_arena_get(tsdn_tsd(tsdn)) == arena) {
  1618. arena_nthreads_inc(arena, false);
  1619. }
  1620. if (tsd_iarena_get(tsdn_tsd(tsdn)) == arena) {
  1621. arena_nthreads_inc(arena, true);
  1622. }
  1623. if (config_stats) {
  1624. ql_new(&arena->tcache_ql);
  1625. ql_new(&arena->cache_bin_array_descriptor_ql);
  1626. tcache_slow_t *tcache_slow = tcache_slow_get(tsdn_tsd(tsdn));
  1627. if (tcache_slow != NULL && tcache_slow->arena == arena) {
  1628. tcache_t *tcache = tcache_slow->tcache;
  1629. ql_elm_new(tcache_slow, link);
  1630. ql_tail_insert(&arena->tcache_ql, tcache_slow, link);
  1631. cache_bin_array_descriptor_init(
  1632. &tcache_slow->cache_bin_array_descriptor,
  1633. tcache->bins);
  1634. ql_tail_insert(&arena->cache_bin_array_descriptor_ql,
  1635. &tcache_slow->cache_bin_array_descriptor, link);
  1636. }
  1637. }
  1638. for (unsigned i = 0; i < nbins_total; i++) {
  1639. bin_postfork_child(tsdn, &arena->bins[i]);
  1640. }
  1641. malloc_mutex_postfork_child(tsdn, &arena->large_mtx);
  1642. base_postfork_child(tsdn, arena->base);
  1643. pa_shard_postfork_child(tsdn, &arena->pa_shard);
  1644. if (config_stats) {
  1645. malloc_mutex_postfork_child(tsdn, &arena->tcache_ql_mtx);
  1646. }
  1647. }