2
0

prof_active.c 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. #include "test/jemalloc_test.h"
  2. static void
  3. mallctl_bool_get(const char *name, bool expected, const char *func, int line) {
  4. bool old;
  5. size_t sz;
  6. sz = sizeof(old);
  7. assert_d_eq(mallctl(name, (void *)&old, &sz, NULL, 0), 0,
  8. "%s():%d: Unexpected mallctl failure reading %s", func, line, name);
  9. assert_b_eq(old, expected, "%s():%d: Unexpected %s value", func, line,
  10. name);
  11. }
  12. static void
  13. mallctl_bool_set(const char *name, bool old_expected, bool val_new,
  14. const char *func, int line) {
  15. bool old;
  16. size_t sz;
  17. sz = sizeof(old);
  18. assert_d_eq(mallctl(name, (void *)&old, &sz, (void *)&val_new,
  19. sizeof(val_new)), 0,
  20. "%s():%d: Unexpected mallctl failure reading/writing %s", func,
  21. line, name);
  22. assert_b_eq(old, old_expected, "%s():%d: Unexpected %s value", func,
  23. line, name);
  24. }
  25. static void
  26. mallctl_prof_active_get_impl(bool prof_active_old_expected, const char *func,
  27. int line) {
  28. mallctl_bool_get("prof.active", prof_active_old_expected, func, line);
  29. }
  30. #define mallctl_prof_active_get(a) \
  31. mallctl_prof_active_get_impl(a, __func__, __LINE__)
  32. static void
  33. mallctl_prof_active_set_impl(bool prof_active_old_expected,
  34. bool prof_active_new, const char *func, int line) {
  35. mallctl_bool_set("prof.active", prof_active_old_expected,
  36. prof_active_new, func, line);
  37. }
  38. #define mallctl_prof_active_set(a, b) \
  39. mallctl_prof_active_set_impl(a, b, __func__, __LINE__)
  40. static void
  41. mallctl_thread_prof_active_get_impl(bool thread_prof_active_old_expected,
  42. const char *func, int line) {
  43. mallctl_bool_get("thread.prof.active", thread_prof_active_old_expected,
  44. func, line);
  45. }
  46. #define mallctl_thread_prof_active_get(a) \
  47. mallctl_thread_prof_active_get_impl(a, __func__, __LINE__)
  48. static void
  49. mallctl_thread_prof_active_set_impl(bool thread_prof_active_old_expected,
  50. bool thread_prof_active_new, const char *func, int line) {
  51. mallctl_bool_set("thread.prof.active", thread_prof_active_old_expected,
  52. thread_prof_active_new, func, line);
  53. }
  54. #define mallctl_thread_prof_active_set(a, b) \
  55. mallctl_thread_prof_active_set_impl(a, b, __func__, __LINE__)
  56. static void
  57. prof_sampling_probe_impl(bool expect_sample, const char *func, int line) {
  58. void *p;
  59. size_t expected_backtraces = expect_sample ? 1 : 0;
  60. assert_zu_eq(prof_bt_count(), 0, "%s():%d: Expected 0 backtraces", func,
  61. line);
  62. p = mallocx(1, 0);
  63. assert_ptr_not_null(p, "Unexpected mallocx() failure");
  64. assert_zu_eq(prof_bt_count(), expected_backtraces,
  65. "%s():%d: Unexpected backtrace count", func, line);
  66. dallocx(p, 0);
  67. }
  68. #define prof_sampling_probe(a) \
  69. prof_sampling_probe_impl(a, __func__, __LINE__)
  70. TEST_BEGIN(test_prof_active) {
  71. test_skip_if(!config_prof);
  72. mallctl_prof_active_get(true);
  73. mallctl_thread_prof_active_get(false);
  74. mallctl_prof_active_set(true, true);
  75. mallctl_thread_prof_active_set(false, false);
  76. /* prof.active, !thread.prof.active. */
  77. prof_sampling_probe(false);
  78. mallctl_prof_active_set(true, false);
  79. mallctl_thread_prof_active_set(false, false);
  80. /* !prof.active, !thread.prof.active. */
  81. prof_sampling_probe(false);
  82. mallctl_prof_active_set(false, false);
  83. mallctl_thread_prof_active_set(false, true);
  84. /* !prof.active, thread.prof.active. */
  85. prof_sampling_probe(false);
  86. mallctl_prof_active_set(false, true);
  87. mallctl_thread_prof_active_set(true, true);
  88. /* prof.active, thread.prof.active. */
  89. prof_sampling_probe(true);
  90. /* Restore settings. */
  91. mallctl_prof_active_set(true, true);
  92. mallctl_thread_prof_active_set(true, false);
  93. }
  94. TEST_END
  95. int
  96. main(void) {
  97. return test_no_reentrancy(
  98. test_prof_active);
  99. }