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