log.c 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  1. #include "test/jemalloc_test.h"
  2. #include "jemalloc/internal/log.h"
  3. static void
  4. expect_no_logging(const char *names) {
  5. log_var_t log_l1 = LOG_VAR_INIT("l1");
  6. log_var_t log_l2 = LOG_VAR_INIT("l2");
  7. log_var_t log_l2_a = LOG_VAR_INIT("l2.a");
  8. strcpy(log_var_names, names);
  9. int count = 0;
  10. for (int i = 0; i < 10; i++) {
  11. log_do_begin(log_l1)
  12. count++;
  13. log_do_end(log_l1)
  14. log_do_begin(log_l2)
  15. count++;
  16. log_do_end(log_l2)
  17. log_do_begin(log_l2_a)
  18. count++;
  19. log_do_end(log_l2_a)
  20. }
  21. assert_d_eq(count, 0, "Disabled logging not ignored!");
  22. }
  23. TEST_BEGIN(test_log_disabled) {
  24. test_skip_if(!config_log);
  25. atomic_store_b(&log_init_done, true, ATOMIC_RELAXED);
  26. expect_no_logging("");
  27. expect_no_logging("abc");
  28. expect_no_logging("a.b.c");
  29. expect_no_logging("l12");
  30. expect_no_logging("l123|a456|b789");
  31. expect_no_logging("|||");
  32. }
  33. TEST_END
  34. TEST_BEGIN(test_log_enabled_direct) {
  35. test_skip_if(!config_log);
  36. atomic_store_b(&log_init_done, true, ATOMIC_RELAXED);
  37. log_var_t log_l1 = LOG_VAR_INIT("l1");
  38. log_var_t log_l1_a = LOG_VAR_INIT("l1.a");
  39. log_var_t log_l2 = LOG_VAR_INIT("l2");
  40. int count;
  41. count = 0;
  42. strcpy(log_var_names, "l1");
  43. for (int i = 0; i < 10; i++) {
  44. log_do_begin(log_l1)
  45. count++;
  46. log_do_end(log_l1)
  47. }
  48. assert_d_eq(count, 10, "Mis-logged!");
  49. count = 0;
  50. strcpy(log_var_names, "l1.a");
  51. for (int i = 0; i < 10; i++) {
  52. log_do_begin(log_l1_a)
  53. count++;
  54. log_do_end(log_l1_a)
  55. }
  56. assert_d_eq(count, 10, "Mis-logged!");
  57. count = 0;
  58. strcpy(log_var_names, "l1.a|abc|l2|def");
  59. for (int i = 0; i < 10; i++) {
  60. log_do_begin(log_l1_a)
  61. count++;
  62. log_do_end(log_l1_a)
  63. log_do_begin(log_l2)
  64. count++;
  65. log_do_end(log_l2)
  66. }
  67. assert_d_eq(count, 20, "Mis-logged!");
  68. }
  69. TEST_END
  70. TEST_BEGIN(test_log_enabled_indirect) {
  71. test_skip_if(!config_log);
  72. atomic_store_b(&log_init_done, true, ATOMIC_RELAXED);
  73. strcpy(log_var_names, "l0|l1|abc|l2.b|def");
  74. /* On. */
  75. log_var_t log_l1 = LOG_VAR_INIT("l1");
  76. /* Off. */
  77. log_var_t log_l1a = LOG_VAR_INIT("l1a");
  78. /* On. */
  79. log_var_t log_l1_a = LOG_VAR_INIT("l1.a");
  80. /* Off. */
  81. log_var_t log_l2_a = LOG_VAR_INIT("l2.a");
  82. /* On. */
  83. log_var_t log_l2_b_a = LOG_VAR_INIT("l2.b.a");
  84. /* On. */
  85. log_var_t log_l2_b_b = LOG_VAR_INIT("l2.b.b");
  86. /* 4 are on total, so should sum to 40. */
  87. int count = 0;
  88. for (int i = 0; i < 10; i++) {
  89. log_do_begin(log_l1)
  90. count++;
  91. log_do_end(log_l1)
  92. log_do_begin(log_l1a)
  93. count++;
  94. log_do_end(log_l1a)
  95. log_do_begin(log_l1_a)
  96. count++;
  97. log_do_end(log_l1_a)
  98. log_do_begin(log_l2_a)
  99. count++;
  100. log_do_end(log_l2_a)
  101. log_do_begin(log_l2_b_a)
  102. count++;
  103. log_do_end(log_l2_b_a)
  104. log_do_begin(log_l2_b_b)
  105. count++;
  106. log_do_end(log_l2_b_b)
  107. }
  108. assert_d_eq(count, 40, "Mis-logged!");
  109. }
  110. TEST_END
  111. TEST_BEGIN(test_log_enabled_global) {
  112. test_skip_if(!config_log);
  113. atomic_store_b(&log_init_done, true, ATOMIC_RELAXED);
  114. strcpy(log_var_names, "abc|.|def");
  115. log_var_t log_l1 = LOG_VAR_INIT("l1");
  116. log_var_t log_l2_a_a = LOG_VAR_INIT("l2.a.a");
  117. int count = 0;
  118. for (int i = 0; i < 10; i++) {
  119. log_do_begin(log_l1)
  120. count++;
  121. log_do_end(log_l1)
  122. log_do_begin(log_l2_a_a)
  123. count++;
  124. log_do_end(log_l2_a_a)
  125. }
  126. assert_d_eq(count, 20, "Mis-logged!");
  127. }
  128. TEST_END
  129. TEST_BEGIN(test_logs_if_no_init) {
  130. test_skip_if(!config_log);
  131. atomic_store_b(&log_init_done, false, ATOMIC_RELAXED);
  132. log_var_t l = LOG_VAR_INIT("definitely.not.enabled");
  133. int count = 0;
  134. for (int i = 0; i < 10; i++) {
  135. log_do_begin(l)
  136. count++;
  137. log_do_end(l)
  138. }
  139. assert_d_eq(count, 0, "Logging shouldn't happen if not initialized.");
  140. }
  141. TEST_END
  142. /*
  143. * This really just checks to make sure that this usage compiles; we don't have
  144. * any test code to run.
  145. */
  146. TEST_BEGIN(test_log_only_format_string) {
  147. if (false) {
  148. LOG("log_str", "No arguments follow this format string.");
  149. }
  150. }
  151. TEST_END
  152. int
  153. main(void) {
  154. return test(
  155. test_log_disabled,
  156. test_log_enabled_direct,
  157. test_log_enabled_indirect,
  158. test_log_enabled_global,
  159. test_logs_if_no_init,
  160. test_log_only_format_string);
  161. }