1*1208bc7eSAndroid Build Coastguard Worker #include "test/jemalloc_test.h"
2*1208bc7eSAndroid Build Coastguard Worker
3*1208bc7eSAndroid Build Coastguard Worker #include "jemalloc/internal/util.h"
4*1208bc7eSAndroid Build Coastguard Worker
5*1208bc7eSAndroid Build Coastguard Worker static void
test_switch_background_thread_ctl(bool new_val)6*1208bc7eSAndroid Build Coastguard Worker test_switch_background_thread_ctl(bool new_val) {
7*1208bc7eSAndroid Build Coastguard Worker bool e0, e1;
8*1208bc7eSAndroid Build Coastguard Worker size_t sz = sizeof(bool);
9*1208bc7eSAndroid Build Coastguard Worker
10*1208bc7eSAndroid Build Coastguard Worker e1 = new_val;
11*1208bc7eSAndroid Build Coastguard Worker assert_d_eq(mallctl("background_thread", (void *)&e0, &sz,
12*1208bc7eSAndroid Build Coastguard Worker &e1, sz), 0, "Unexpected mallctl() failure");
13*1208bc7eSAndroid Build Coastguard Worker assert_b_eq(e0, !e1,
14*1208bc7eSAndroid Build Coastguard Worker "background_thread should be %d before.\n", !e1);
15*1208bc7eSAndroid Build Coastguard Worker if (e1) {
16*1208bc7eSAndroid Build Coastguard Worker assert_zu_gt(n_background_threads, 0,
17*1208bc7eSAndroid Build Coastguard Worker "Number of background threads should be non zero.\n");
18*1208bc7eSAndroid Build Coastguard Worker } else {
19*1208bc7eSAndroid Build Coastguard Worker assert_zu_eq(n_background_threads, 0,
20*1208bc7eSAndroid Build Coastguard Worker "Number of background threads should be zero.\n");
21*1208bc7eSAndroid Build Coastguard Worker }
22*1208bc7eSAndroid Build Coastguard Worker }
23*1208bc7eSAndroid Build Coastguard Worker
24*1208bc7eSAndroid Build Coastguard Worker static void
test_repeat_background_thread_ctl(bool before)25*1208bc7eSAndroid Build Coastguard Worker test_repeat_background_thread_ctl(bool before) {
26*1208bc7eSAndroid Build Coastguard Worker bool e0, e1;
27*1208bc7eSAndroid Build Coastguard Worker size_t sz = sizeof(bool);
28*1208bc7eSAndroid Build Coastguard Worker
29*1208bc7eSAndroid Build Coastguard Worker e1 = before;
30*1208bc7eSAndroid Build Coastguard Worker assert_d_eq(mallctl("background_thread", (void *)&e0, &sz,
31*1208bc7eSAndroid Build Coastguard Worker &e1, sz), 0, "Unexpected mallctl() failure");
32*1208bc7eSAndroid Build Coastguard Worker assert_b_eq(e0, before,
33*1208bc7eSAndroid Build Coastguard Worker "background_thread should be %d.\n", before);
34*1208bc7eSAndroid Build Coastguard Worker if (e1) {
35*1208bc7eSAndroid Build Coastguard Worker assert_zu_gt(n_background_threads, 0,
36*1208bc7eSAndroid Build Coastguard Worker "Number of background threads should be non zero.\n");
37*1208bc7eSAndroid Build Coastguard Worker } else {
38*1208bc7eSAndroid Build Coastguard Worker assert_zu_eq(n_background_threads, 0,
39*1208bc7eSAndroid Build Coastguard Worker "Number of background threads should be zero.\n");
40*1208bc7eSAndroid Build Coastguard Worker }
41*1208bc7eSAndroid Build Coastguard Worker }
42*1208bc7eSAndroid Build Coastguard Worker
TEST_BEGIN(test_background_thread_ctl)43*1208bc7eSAndroid Build Coastguard Worker TEST_BEGIN(test_background_thread_ctl) {
44*1208bc7eSAndroid Build Coastguard Worker test_skip_if(!have_background_thread);
45*1208bc7eSAndroid Build Coastguard Worker
46*1208bc7eSAndroid Build Coastguard Worker bool e0, e1;
47*1208bc7eSAndroid Build Coastguard Worker size_t sz = sizeof(bool);
48*1208bc7eSAndroid Build Coastguard Worker
49*1208bc7eSAndroid Build Coastguard Worker assert_d_eq(mallctl("opt.background_thread", (void *)&e0, &sz,
50*1208bc7eSAndroid Build Coastguard Worker NULL, 0), 0, "Unexpected mallctl() failure");
51*1208bc7eSAndroid Build Coastguard Worker assert_d_eq(mallctl("background_thread", (void *)&e1, &sz,
52*1208bc7eSAndroid Build Coastguard Worker NULL, 0), 0, "Unexpected mallctl() failure");
53*1208bc7eSAndroid Build Coastguard Worker assert_b_eq(e0, e1,
54*1208bc7eSAndroid Build Coastguard Worker "Default and opt.background_thread does not match.\n");
55*1208bc7eSAndroid Build Coastguard Worker if (e0) {
56*1208bc7eSAndroid Build Coastguard Worker test_switch_background_thread_ctl(false);
57*1208bc7eSAndroid Build Coastguard Worker }
58*1208bc7eSAndroid Build Coastguard Worker assert_zu_eq(n_background_threads, 0,
59*1208bc7eSAndroid Build Coastguard Worker "Number of background threads should be 0.\n");
60*1208bc7eSAndroid Build Coastguard Worker
61*1208bc7eSAndroid Build Coastguard Worker for (unsigned i = 0; i < 4; i++) {
62*1208bc7eSAndroid Build Coastguard Worker test_switch_background_thread_ctl(true);
63*1208bc7eSAndroid Build Coastguard Worker test_repeat_background_thread_ctl(true);
64*1208bc7eSAndroid Build Coastguard Worker test_repeat_background_thread_ctl(true);
65*1208bc7eSAndroid Build Coastguard Worker
66*1208bc7eSAndroid Build Coastguard Worker test_switch_background_thread_ctl(false);
67*1208bc7eSAndroid Build Coastguard Worker test_repeat_background_thread_ctl(false);
68*1208bc7eSAndroid Build Coastguard Worker test_repeat_background_thread_ctl(false);
69*1208bc7eSAndroid Build Coastguard Worker }
70*1208bc7eSAndroid Build Coastguard Worker }
71*1208bc7eSAndroid Build Coastguard Worker TEST_END
72*1208bc7eSAndroid Build Coastguard Worker
TEST_BEGIN(test_background_thread_running)73*1208bc7eSAndroid Build Coastguard Worker TEST_BEGIN(test_background_thread_running) {
74*1208bc7eSAndroid Build Coastguard Worker test_skip_if(!have_background_thread);
75*1208bc7eSAndroid Build Coastguard Worker test_skip_if(!config_stats);
76*1208bc7eSAndroid Build Coastguard Worker
77*1208bc7eSAndroid Build Coastguard Worker #if defined(JEMALLOC_BACKGROUND_THREAD)
78*1208bc7eSAndroid Build Coastguard Worker tsd_t *tsd = tsd_fetch();
79*1208bc7eSAndroid Build Coastguard Worker background_thread_info_t *info = &background_thread_info[0];
80*1208bc7eSAndroid Build Coastguard Worker
81*1208bc7eSAndroid Build Coastguard Worker test_repeat_background_thread_ctl(false);
82*1208bc7eSAndroid Build Coastguard Worker test_switch_background_thread_ctl(true);
83*1208bc7eSAndroid Build Coastguard Worker assert_b_eq(info->state, background_thread_started,
84*1208bc7eSAndroid Build Coastguard Worker "Background_thread did not start.\n");
85*1208bc7eSAndroid Build Coastguard Worker
86*1208bc7eSAndroid Build Coastguard Worker nstime_t start, now;
87*1208bc7eSAndroid Build Coastguard Worker nstime_init(&start, 0);
88*1208bc7eSAndroid Build Coastguard Worker nstime_update(&start);
89*1208bc7eSAndroid Build Coastguard Worker
90*1208bc7eSAndroid Build Coastguard Worker bool ran = false;
91*1208bc7eSAndroid Build Coastguard Worker while (true) {
92*1208bc7eSAndroid Build Coastguard Worker malloc_mutex_lock(tsd_tsdn(tsd), &info->mtx);
93*1208bc7eSAndroid Build Coastguard Worker if (info->tot_n_runs > 0) {
94*1208bc7eSAndroid Build Coastguard Worker ran = true;
95*1208bc7eSAndroid Build Coastguard Worker }
96*1208bc7eSAndroid Build Coastguard Worker malloc_mutex_unlock(tsd_tsdn(tsd), &info->mtx);
97*1208bc7eSAndroid Build Coastguard Worker if (ran) {
98*1208bc7eSAndroid Build Coastguard Worker break;
99*1208bc7eSAndroid Build Coastguard Worker }
100*1208bc7eSAndroid Build Coastguard Worker
101*1208bc7eSAndroid Build Coastguard Worker nstime_init(&now, 0);
102*1208bc7eSAndroid Build Coastguard Worker nstime_update(&now);
103*1208bc7eSAndroid Build Coastguard Worker nstime_subtract(&now, &start);
104*1208bc7eSAndroid Build Coastguard Worker assert_u64_lt(nstime_sec(&now), 1000,
105*1208bc7eSAndroid Build Coastguard Worker "Background threads did not run for 1000 seconds.");
106*1208bc7eSAndroid Build Coastguard Worker sleep(1);
107*1208bc7eSAndroid Build Coastguard Worker }
108*1208bc7eSAndroid Build Coastguard Worker test_switch_background_thread_ctl(false);
109*1208bc7eSAndroid Build Coastguard Worker #endif
110*1208bc7eSAndroid Build Coastguard Worker }
111*1208bc7eSAndroid Build Coastguard Worker TEST_END
112*1208bc7eSAndroid Build Coastguard Worker
113*1208bc7eSAndroid Build Coastguard Worker int
main(void)114*1208bc7eSAndroid Build Coastguard Worker main(void) {
115*1208bc7eSAndroid Build Coastguard Worker /* Background_thread creation tests reentrancy naturally. */
116*1208bc7eSAndroid Build Coastguard Worker return test_no_reentrancy(
117*1208bc7eSAndroid Build Coastguard Worker test_background_thread_ctl,
118*1208bc7eSAndroid Build Coastguard Worker test_background_thread_running);
119*1208bc7eSAndroid Build Coastguard Worker }
120