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