Extract postfork-child tcache list relink into tcache_arena_postfork_child

This commit is contained in:
Slobodan Predolac 2026-05-08 10:01:00 -07:00
parent b6cfaa4fe2
commit 54ef51121b
3 changed files with 32 additions and 24 deletions

View file

@ -73,6 +73,7 @@ void tcaches_destroy(tsd_t *tsd, unsigned ind);
bool tcache_boot(tsdn_t *tsdn, base_t *base);
void tcache_arena_associate(
tsdn_t *tsdn, tcache_slow_t *tcache_slow, tcache_t *tcache, arena_t *arena);
void tcache_arena_postfork_child(tsdn_t *tsdn, arena_t *arena);
void tcache_prefork(tsdn_t *tsdn);
void tcache_postfork_parent(tsdn_t *tsdn);
void tcache_postfork_child(tsdn_t *tsdn);

View file

@ -2089,21 +2089,7 @@ arena_postfork_child(tsdn_t *tsdn, arena_t *arena) {
if (tsd_iarena_get(tsdn_tsd(tsdn)) == arena) {
arena_nthreads_inc(arena, true);
}
if (config_stats) {
ql_new(&arena->tcache_ql);
ql_new(&arena->cache_bin_array_descriptor_ql);
tcache_slow_t *tcache_slow = tcache_slow_get(tsdn_tsd(tsdn));
if (tcache_slow != NULL && tcache_slow->arena == arena) {
tcache_t *tcache = tcache_slow->tcache;
ql_elm_new(tcache_slow, link);
ql_tail_insert(&arena->tcache_ql, tcache_slow, link);
cache_bin_array_descriptor_init(
&tcache_slow->cache_bin_array_descriptor,
tcache->bins);
ql_tail_insert(&arena->cache_bin_array_descriptor_ql,
&tcache_slow->cache_bin_array_descriptor, link);
}
}
tcache_arena_postfork_child(tsdn, arena);
for (unsigned i = 0; i < nbins_total; i++) {
JEMALLOC_SUPPRESS_WARN_ON_USAGE(

View file

@ -720,6 +720,22 @@ tcache_bin_ncached_max_read(
return false;
}
/*
* Insert tcache_slow into arena's tcache list. Caller must hold
* arena->tcache_ql_mtx, OR be in the postfork-child path (single-threaded,
* mutex re-init pending). config_stats must be true.
*/
static void
tcache_arena_link(arena_t *arena, tcache_slow_t *tcache_slow,
tcache_t *tcache) {
ql_elm_new(tcache_slow, link);
ql_tail_insert(&arena->tcache_ql, tcache_slow, link);
cache_bin_array_descriptor_init(
&tcache_slow->cache_bin_array_descriptor, tcache->bins);
ql_tail_insert(&arena->cache_bin_array_descriptor_ql,
&tcache_slow->cache_bin_array_descriptor, link);
}
void
tcache_arena_associate(tsdn_t *tsdn, tcache_slow_t *tcache_slow,
tcache_t *tcache, arena_t *arena) {
@ -727,16 +743,8 @@ tcache_arena_associate(tsdn_t *tsdn, tcache_slow_t *tcache_slow,
tcache_slow->arena = arena;
if (config_stats) {
/* Link into list of extant tcaches. */
malloc_mutex_lock(tsdn, &arena->tcache_ql_mtx);
ql_elm_new(tcache_slow, link);
ql_tail_insert(&arena->tcache_ql, tcache_slow, link);
cache_bin_array_descriptor_init(
&tcache_slow->cache_bin_array_descriptor, tcache->bins);
ql_tail_insert(&arena->cache_bin_array_descriptor_ql,
&tcache_slow->cache_bin_array_descriptor, link);
tcache_arena_link(arena, tcache_slow, tcache);
malloc_mutex_unlock(tsdn, &arena->tcache_ql_mtx);
}
}
@ -776,6 +784,19 @@ tcache_arena_reassociate(tsdn_t *tsdn, tcache_slow_t *tcache_slow,
tcache_arena_associate(tsdn, tcache_slow, tcache, arena);
}
void
tcache_arena_postfork_child(tsdn_t *tsdn, arena_t *arena) {
if (!config_stats) {
return;
}
ql_new(&arena->tcache_ql);
ql_new(&arena->cache_bin_array_descriptor_ql);
tcache_slow_t *tcache_slow = tcache_slow_get(tsdn_tsd(tsdn));
if (tcache_slow != NULL && tcache_slow->arena == arena) {
tcache_arena_link(arena, tcache_slow, tcache_slow->tcache);
}
}
static void
tcache_default_settings_init(tcache_slow_t *tcache_slow) {
assert(tcache_slow != NULL);