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