log.c 4.1 KB

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