From: Willy Tarreau Date: Mon, 14 Apr 2025 15:46:18 +0000 (+0200) Subject: DEBUG: counters: make COUNT_IF() only appear at DEBUG_COUNTERS>=1 X-Git-Tag: v3.2-dev11~68 X-Git-Url: http://git.ipfire.org/?a=commitdiff_plain;h=a142adaba017cfedd498f715a9b9c4d73137fefa;p=thirdparty%2Fhaproxy.git DEBUG: counters: make COUNT_IF() only appear at DEBUG_COUNTERS>=1 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. --- diff --git a/Makefile b/Makefile index fee5a8952..6b8fa5bca 100644 --- 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 diff --git a/include/haproxy/bug.h b/include/haproxy/bug.h index a2c543826..f583e093d 100644 --- a/include/haproxy/bug.h +++ b/include/haproxy/bug.h @@ -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