2
0

prof_active.c 3.6 KB

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