2
0

background_thread_enable.c 2.7 KB

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