]> git.ipfire.org Git - thirdparty/haproxy.git/commitdiff
DEBUG: counters: make COUNT_IF() only appear at DEBUG_COUNTERS>=1
authorWilly Tarreau <w@1wt.eu>
Mon, 14 Apr 2025 15:46:18 +0000 (17:46 +0200)
committerWilly Tarreau <w@1wt.eu>
Mon, 14 Apr 2025 17:02:13 +0000 (19:02 +0200)
COUNT_IF() is convenient but can be heavy since some of them were found
to trigger often (roughly 1 counter per request on avg). This might even
have an impact on large setups due to the cost of a shared cache line
bouncing between multiple cores. For now there's no way to disable it,
so let's only enable it when DEBUG_COUNTERS is 1 or above. A future
change will make it configurable.

Makefile
include/haproxy/bug.h

index fee5a8952c1006d1d69aef5c19d76919c4d32889..6b8fa5bca90df3021bcfb01d23d803903bf6cd5f 100644 (file)
--- a/Makefile
+++ b/Makefile
@@ -263,7 +263,7 @@ endif
 # DEBUG_NO_POOLS, DEBUG_FAIL_ALLOC, DEBUG_STRICT_ACTION=[0-3], DEBUG_HPACK,
 # DEBUG_AUTH, DEBUG_SPOE, DEBUG_UAF, DEBUG_THREAD, DEBUG_STRICT, DEBUG_DEV,
 # DEBUG_TASK, DEBUG_MEMORY_POOLS, DEBUG_POOL_TRACING, DEBUG_QPACK, DEBUG_LIST,
-# DEBUG_COUNTERS, DEBUG_STRESS, DEBUG_UNIT.
+# DEBUG_COUNTERS=[0-1], DEBUG_STRESS, DEBUG_UNIT.
 DEBUG =
 
 #### Trace options
index a2c5438264688d590098f23ead93d56a699586e8..f583e093dd3fa070e03a2a865a67e379713e1211 100644 (file)
@@ -222,14 +222,24 @@ extern __attribute__((__weak__)) struct debug_count __stop_dbg_cnt  HA_SECTION_S
                _HA_ATOMIC_INC(&__dbg_cnt_##_line.count);                       \
        } while (0)
 
+
+/* Matrix for DEBUG_COUNTERS:
+ *    0 : only BUG_ON() and CHECK_IF() are reported (super rare)
+ *    1 : COUNT_GLITCH() and COUNT_IF() are also reported (rare)
+ */
+
 /* Core of the COUNT_IF() macro, checks the condition and counts one hit if
- * true.
+ * true. It's only enabled at DEBUG_COUNTERS >= 1.
  */
-#define _COUNT_IF(cond, file, line, ...)                                       \
+# if defined(DEBUG_COUNTERS) && (DEBUG_COUNTERS >= 1)
+#  define _COUNT_IF(cond, file, line, ...)                                     \
        (unlikely(cond) ? ({                                                    \
                __DBG_COUNT(cond, file, line, DBG_COUNT_IF, __VA_ARGS__);       \
                1; /* let's return the true condition */                        \
        }) : 0)
+# else
+#  define _COUNT_IF(cond, file, line, ...) DISGUISE(unlikely(cond) ? 1 : 0)
+# endif
 
 /* DEBUG_COUNTERS enables counting the number of glitches per line of code. The
  * condition is empty (nothing to write there), except maybe __VA_ARGS at the
@@ -245,7 +255,7 @@ extern __attribute__((__weak__)) struct debug_count __stop_dbg_cnt  HA_SECTION_S
 
 #else /* USE_OBSOLETE_LINKER not defined below  */
 # define __DBG_COUNT(cond, file, line, type, ...) do { } while (0)
-# define _COUNT_IF(cond, file, line, ...) DISGUISE(cond)
+# define _COUNT_IF(cond, file, line, ...) DISGUISE(unlikely(cond) ? 1 : 0)
 # define _COUNT_GLITCH(file, line, ...) do { } while (0)
 #endif