1#include "test/jemalloc_test.h" 2 3const char *malloc_conf = "background_thread:false,narenas:1,max_background_threads:20"; 4 5TEST_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} 31TEST_END 32 33TEST_BEGIN(test_max_background_threads) { 34 test_skip_if(!have_background_thread); 35 36 size_t maxt; 37 size_t opt_maxt; 38 size_t sz_m = sizeof(maxt); 39 assert_d_eq(mallctl("opt.max_background_threads", 40 &opt_maxt, &sz_m, NULL, 0), 0, 41 "Failed to get opt.max_background_threads"); 42 assert_d_eq(mallctl("max_background_threads", &maxt, &sz_m, NULL, 0), 0, 43 "Failed to get max background threads"); 44 assert_zu_eq(20, maxt, "should be ncpus"); 45 assert_zu_eq(opt_maxt, maxt, 46 "max_background_threads and " 47 "opt.max_background_threads should match"); 48 assert_d_eq(mallctl("max_background_threads", NULL, NULL, &maxt, sz_m), 49 0, "Failed to set max background threads"); 50 51 unsigned id; 52 size_t sz_u = sizeof(unsigned); 53 54 for (unsigned i = 0; i < 10 * ncpus; i++) { 55 assert_d_eq(mallctl("arenas.create", &id, &sz_u, NULL, 0), 0, 56 "Failed to create arena"); 57 } 58 59 bool enable = true; 60 size_t sz_b = sizeof(bool); 61 assert_d_eq(mallctl("background_thread", NULL, NULL, &enable, sz_b), 0, 62 "Failed to enable background threads"); 63 assert_zu_eq(n_background_threads, maxt, 64 "Number of background threads should be 3.\n"); 65 maxt = 10; 66 assert_d_eq(mallctl("max_background_threads", NULL, NULL, &maxt, sz_m), 67 0, "Failed to set max background threads"); 68 assert_zu_eq(n_background_threads, maxt, 69 "Number of background threads should be 10.\n"); 70 maxt = 3; 71 assert_d_eq(mallctl("max_background_threads", NULL, NULL, &maxt, sz_m), 72 0, "Failed to set max background threads"); 73 assert_zu_eq(n_background_threads, maxt, 74 "Number of background threads should be 3.\n"); 75} 76TEST_END 77 78int 79main(void) { 80 return test_no_reentrancy( 81 test_deferred, 82 test_max_background_threads); 83} 84