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