prof_active.c 3.5 KB

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