background_thread_enable.c 3.0 KB

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