background_thread.c 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. #include "test/jemalloc_test.h"
  2. #include "jemalloc/internal/util.h"
  3. static void
  4. test_switch_background_thread_ctl(bool new_val) {
  5. bool e0, e1;
  6. size_t sz = sizeof(bool);
  7. e1 = new_val;
  8. expect_d_eq(mallctl("background_thread", (void *)&e0, &sz,
  9. &e1, sz), 0, "Unexpected mallctl() failure");
  10. expect_b_eq(e0, !e1,
  11. "background_thread should be %d before.\n", !e1);
  12. if (e1) {
  13. expect_zu_gt(n_background_threads, 0,
  14. "Number of background threads should be non zero.\n");
  15. } else {
  16. expect_zu_eq(n_background_threads, 0,
  17. "Number of background threads should be zero.\n");
  18. }
  19. }
  20. static void
  21. test_repeat_background_thread_ctl(bool before) {
  22. bool e0, e1;
  23. size_t sz = sizeof(bool);
  24. e1 = before;
  25. expect_d_eq(mallctl("background_thread", (void *)&e0, &sz,
  26. &e1, sz), 0, "Unexpected mallctl() failure");
  27. expect_b_eq(e0, before,
  28. "background_thread should be %d.\n", before);
  29. if (e1) {
  30. expect_zu_gt(n_background_threads, 0,
  31. "Number of background threads should be non zero.\n");
  32. } else {
  33. expect_zu_eq(n_background_threads, 0,
  34. "Number of background threads should be zero.\n");
  35. }
  36. }
  37. TEST_BEGIN(test_background_thread_ctl) {
  38. test_skip_if(!have_background_thread);
  39. bool e0, e1;
  40. size_t sz = sizeof(bool);
  41. expect_d_eq(mallctl("opt.background_thread", (void *)&e0, &sz,
  42. NULL, 0), 0, "Unexpected mallctl() failure");
  43. expect_d_eq(mallctl("background_thread", (void *)&e1, &sz,
  44. NULL, 0), 0, "Unexpected mallctl() failure");
  45. expect_b_eq(e0, e1,
  46. "Default and opt.background_thread does not match.\n");
  47. if (e0) {
  48. test_switch_background_thread_ctl(false);
  49. }
  50. expect_zu_eq(n_background_threads, 0,
  51. "Number of background threads should be 0.\n");
  52. for (unsigned i = 0; i < 4; i++) {
  53. test_switch_background_thread_ctl(true);
  54. test_repeat_background_thread_ctl(true);
  55. test_repeat_background_thread_ctl(true);
  56. test_switch_background_thread_ctl(false);
  57. test_repeat_background_thread_ctl(false);
  58. test_repeat_background_thread_ctl(false);
  59. }
  60. }
  61. TEST_END
  62. TEST_BEGIN(test_background_thread_running) {
  63. test_skip_if(!have_background_thread);
  64. test_skip_if(!config_stats);
  65. #if defined(JEMALLOC_BACKGROUND_THREAD)
  66. tsd_t *tsd = tsd_fetch();
  67. background_thread_info_t *info = &background_thread_info[0];
  68. test_repeat_background_thread_ctl(false);
  69. test_switch_background_thread_ctl(true);
  70. expect_b_eq(info->state, background_thread_started,
  71. "Background_thread did not start.\n");
  72. nstime_t start;
  73. nstime_init_update(&start);
  74. bool ran = false;
  75. while (true) {
  76. malloc_mutex_lock(tsd_tsdn(tsd), &info->mtx);
  77. if (info->tot_n_runs > 0) {
  78. ran = true;
  79. }
  80. malloc_mutex_unlock(tsd_tsdn(tsd), &info->mtx);
  81. if (ran) {
  82. break;
  83. }
  84. nstime_t now;
  85. nstime_init_update(&now);
  86. nstime_subtract(&now, &start);
  87. expect_u64_lt(nstime_sec(&now), 1000,
  88. "Background threads did not run for 1000 seconds.");
  89. sleep(1);
  90. }
  91. test_switch_background_thread_ctl(false);
  92. #endif
  93. }
  94. TEST_END
  95. int
  96. main(void) {
  97. /* Background_thread creation tests reentrancy naturally. */
  98. return test_no_reentrancy(
  99. test_background_thread_ctl,
  100. test_background_thread_running);
  101. }