2
0

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. assert_d_eq(mallctl("background_thread", (void *)&e0, &sz,
  9. &e1, sz), 0, "Unexpected mallctl() failure");
  10. assert_b_eq(e0, !e1,
  11. "background_thread should be %d before.\n", !e1);
  12. if (e1) {
  13. assert_zu_gt(n_background_threads, 0,
  14. "Number of background threads should be non zero.\n");
  15. } else {
  16. assert_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. assert_d_eq(mallctl("background_thread", (void *)&e0, &sz,
  26. &e1, sz), 0, "Unexpected mallctl() failure");
  27. assert_b_eq(e0, before,
  28. "background_thread should be %d.\n", before);
  29. if (e1) {
  30. assert_zu_gt(n_background_threads, 0,
  31. "Number of background threads should be non zero.\n");
  32. } else {
  33. assert_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. assert_d_eq(mallctl("opt.background_thread", (void *)&e0, &sz,
  42. NULL, 0), 0, "Unexpected mallctl() failure");
  43. assert_d_eq(mallctl("background_thread", (void *)&e1, &sz,
  44. NULL, 0), 0, "Unexpected mallctl() failure");
  45. assert_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. assert_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. assert_b_eq(info->state, background_thread_started,
  71. "Background_thread did not start.\n");
  72. nstime_t start, now;
  73. nstime_init(&start, 0);
  74. nstime_update(&start);
  75. bool ran = false;
  76. while (true) {
  77. malloc_mutex_lock(tsd_tsdn(tsd), &info->mtx);
  78. if (info->tot_n_runs > 0) {
  79. ran = true;
  80. }
  81. malloc_mutex_unlock(tsd_tsdn(tsd), &info->mtx);
  82. if (ran) {
  83. break;
  84. }
  85. nstime_init(&now, 0);
  86. nstime_update(&now);
  87. nstime_subtract(&now, &start);
  88. assert_u64_lt(nstime_sec(&now), 1000,
  89. "Background threads did not run for 1000 seconds.");
  90. sleep(1);
  91. }
  92. test_switch_background_thread_ctl(false);
  93. #endif
  94. }
  95. TEST_END
  96. int
  97. main(void) {
  98. /* Background_thread creation tests reentrancy naturally. */
  99. return test_no_reentrancy(
  100. test_background_thread_ctl,
  101. test_background_thread_running);
  102. }