1 #include "test/jemalloc_test.h"
2
3 const char *malloc_conf = "background_thread:false,narenas:1,max_background_threads:20";
4
TEST_BEGIN(test_deferred)5 TEST_BEGIN(test_deferred) {
6 test_skip_if(!have_background_thread);
7
8 unsigned id;
9 size_t sz_u = sizeof(unsigned);
10
11 /*
12 * 10 here is somewhat arbitrary, except insofar as we want to ensure
13 * that the number of background threads is smaller than the number of
14 * arenas. I'll ragequit long before we have to spin up 10 threads per
15 * cpu to handle background purging, so this is a conservative
16 * approximation.
17 */
18 for (unsigned i = 0; i < 10 * ncpus; i++) {
19 assert_d_eq(mallctl("arenas.create", &id, &sz_u, NULL, 0), 0,
20 "Failed to create arena");
21 }
22
23 bool enable = true;
24 size_t sz_b = sizeof(bool);
25 assert_d_eq(mallctl("background_thread", NULL, NULL, &enable, sz_b), 0,
26 "Failed to enable background threads");
27 enable = false;
28 assert_d_eq(mallctl("background_thread", NULL, NULL, &enable, sz_b), 0,
29 "Failed to disable background threads");
30 }
31 TEST_END
32
TEST_BEGIN(test_max_background_threads)33 TEST_BEGIN(test_max_background_threads) {
34 test_skip_if(!have_background_thread);
35
36 size_t max_n_thds;
37 size_t opt_max_n_thds;
38 size_t sz_m = sizeof(max_n_thds);
39 assert_d_eq(mallctl("opt.max_background_threads",
40 &opt_max_n_thds, &sz_m, NULL, 0), 0,
41 "Failed to get opt.max_background_threads");
42 assert_d_eq(mallctl("max_background_threads", &max_n_thds, &sz_m, NULL,
43 0), 0, "Failed to get max background threads");
44 assert_zu_eq(opt_max_n_thds, max_n_thds,
45 "max_background_threads and "
46 "opt.max_background_threads should match");
47 assert_d_eq(mallctl("max_background_threads", NULL, NULL, &max_n_thds,
48 sz_m), 0, "Failed to set max background threads");
49
50 unsigned id;
51 size_t sz_u = sizeof(unsigned);
52
53 for (unsigned i = 0; i < 10 * ncpus; i++) {
54 assert_d_eq(mallctl("arenas.create", &id, &sz_u, NULL, 0), 0,
55 "Failed to create arena");
56 }
57
58 bool enable = true;
59 size_t sz_b = sizeof(bool);
60 assert_d_eq(mallctl("background_thread", NULL, NULL, &enable, sz_b), 0,
61 "Failed to enable background threads");
62 assert_zu_eq(n_background_threads, max_n_thds,
63 "Number of background threads should not change.\n");
64 size_t new_max_thds = max_n_thds - 1;
65 if (new_max_thds > 0) {
66 assert_d_eq(mallctl("max_background_threads", NULL, NULL,
67 &new_max_thds, sz_m), 0,
68 "Failed to set max background threads");
69 assert_zu_eq(n_background_threads, new_max_thds,
70 "Number of background threads should decrease by 1.\n");
71 }
72 new_max_thds = 1;
73 assert_d_eq(mallctl("max_background_threads", NULL, NULL, &new_max_thds,
74 sz_m), 0, "Failed to set max background threads");
75 assert_zu_eq(n_background_threads, new_max_thds,
76 "Number of background threads should be 1.\n");
77 }
78 TEST_END
79
80 int
main(void)81 main(void) {
82 return test_no_reentrancy(
83 test_deferred,
84 test_max_background_threads);
85 }
86