Add background-thread state and toggle unit tests

New tests cover:
  - background thread states after arena_reset
  - background thread stats
  - toggle background thread on and off during parallel stress
    allocs/dallocs
  - background thread fork behavior

Add background-thread fork unit test

test_fork_background_thread (in fork.c): with the background thread enabled,
fork and assert the child comes up with it disabled yet usable (re-enable +
alloc round-trips), while the parent keeps its threads.  Placed in fork.c to
reuse its wait_for_child_exit helper and fork/WIN32 handling instead of
duplicating them in a separate file.
This commit is contained in:
guangli-dai 2026-07-07 18:08:13 -07:00 committed by Guangli Dai
parent e931730f51
commit afeda129b0
3 changed files with 216 additions and 4 deletions

View file

@ -107,9 +107,88 @@ TEST_BEGIN(test_background_thread_running) {
}
TEST_END
TEST_BEGIN(test_background_thread_arena_reset) {
test_skip_if(!have_background_thread);
test_switch_background_thread_ctl(true);
unsigned arena_ind;
size_t sz = sizeof(arena_ind);
expect_d_eq(mallctl("arenas.create", (void *)&arena_ind, &sz, NULL, 0),
0, "Unexpected arenas.create failure");
void *p = mallocx(PAGE,
MALLOCX_TCACHE_NONE | MALLOCX_ARENA(arena_ind));
expect_ptr_not_null(p, "Unexpected mallocx failure");
/*
* Resetting an arena takes its background thread through
* started -> paused -> started (background_thread_arena_reset_begin/
* finish). The reset frees p, so we must not touch it afterwards.
*/
size_t mib[3];
size_t miblen = sizeof(mib) / sizeof(mib[0]);
expect_d_eq(mallctlnametomib("arena.0.reset", mib, &miblen), 0,
"Unexpected mallctlnametomib failure");
mib[1] = arena_ind;
expect_d_eq(mallctlbymib(mib, miblen, NULL, NULL, NULL, 0), 0,
"Unexpected arena reset failure");
#if defined(JEMALLOC_BACKGROUND_THREAD)
background_thread_info_t *info = background_thread_info_get(arena_ind);
tsd_t *tsd = tsd_fetch();
malloc_mutex_lock(tsd_tsdn(tsd), &info->mtx);
background_thread_state_t st = info->state;
malloc_mutex_unlock(tsd_tsdn(tsd), &info->mtx);
expect_d_eq((int)st, (int)background_thread_started,
"Arena reset must leave the background thread started, not paused");
#endif
expect_zu_gt(n_background_threads, 0,
"Arena reset must not lose background threads");
test_switch_background_thread_ctl(false);
}
TEST_END
TEST_BEGIN(test_background_thread_stats_ctl) {
test_skip_if(!have_background_thread);
test_skip_if(!config_stats);
test_switch_background_thread_ctl(true);
uint64_t epoch = 1;
size_t sz = sizeof(epoch);
expect_d_eq(mallctl("epoch", (void *)&epoch, &sz, (void *)&epoch,
sizeof(epoch)),
0, "Unexpected epoch mallctl failure");
size_t num_threads;
sz = sizeof(num_threads);
expect_d_eq(mallctl("stats.background_thread.num_threads",
(void *)&num_threads, &sz, NULL, 0),
0, "Unexpected stats.background_thread.num_threads failure");
expect_zu_eq(num_threads, n_background_threads,
"Reported num_threads should match n_background_threads");
uint64_t num_runs;
sz = sizeof(num_runs);
expect_d_eq(mallctl("stats.background_thread.num_runs",
(void *)&num_runs, &sz, NULL, 0),
0, "Unexpected stats.background_thread.num_runs failure");
uint64_t run_interval;
sz = sizeof(run_interval);
expect_d_eq(mallctl("stats.background_thread.run_interval",
(void *)&run_interval, &sz, NULL, 0),
0, "Unexpected stats.background_thread.run_interval failure");
test_switch_background_thread_ctl(false);
}
TEST_END
int
main(void) {
/* Background_thread creation tests reentrancy naturally. */
return test_no_reentrancy(
test_background_thread_ctl, test_background_thread_running);
return test_no_reentrancy(test_background_thread_ctl,
test_background_thread_running, test_background_thread_arena_reset,
test_background_thread_stats_ctl);
}