diff --git a/core/hakmem_debug_master.h b/core/hakmem_debug_master.h new file mode 100644 index 00000000..df17f564 --- /dev/null +++ b/core/hakmem_debug_master.h @@ -0,0 +1,108 @@ +// hakmem_debug_master.h - Master Debug Control +// +// ENV Cleanup Phase 4b: Add master debug level control +// +// Usage: +// HAKMEM_DEBUG_ALL=1 Enable ALL debug output modules +// HAKMEM_DEBUG_LEVEL=N Set debug level (0=off, 1=critical, 2=normal, 3=verbose) +// HAKMEM_QUIET=1 Suppress all debug output (overrides DEBUG_ALL/LEVEL) +// +// Priority (highest to lowest): +// 1. HAKMEM_QUIET=1 → suppress all (returns 0) +// 2. Specific module ENV (e.g., HAKMEM_SFC_DEBUG=1) → use that value +// 3. HAKMEM_DEBUG_ALL=1 → enable all (returns 1) +// 4. HAKMEM_DEBUG_LEVEL >= threshold → enable (returns 1) +// 5. Default → disabled (returns 0) +// +// Integration example: +// Old: static int dbg = -1; +// if (dbg==-1) { const char* e = getenv("HAKMEM_FOO_DEBUG"); dbg = (e && *e != '0') ? 1 : 0; } +// if (dbg) { fprintf(stderr, "..."); } +// +// New: static int dbg = -1; +// if (dbg==-1) { dbg = hak_debug_check("HAKMEM_FOO_DEBUG"); } +// if (dbg) { fprintf(stderr, "..."); } + +#ifndef HAKMEM_DEBUG_MASTER_H +#define HAKMEM_DEBUG_MASTER_H + +#include +#include + +// Master debug state (cached at first access) +// Level: -1 = uninitialized, 0 = off, 1 = critical, 2 = normal, 3 = verbose +static int g_debug_master_level = -1; +static int g_debug_all = -1; +static int g_debug_quiet = -1; + +// Initialize master debug settings (called once, lazily) +static inline void hak_debug_master_init(void) { + if (__builtin_expect(g_debug_master_level >= 0, 1)) return; + + // Check HAKMEM_QUIET first (highest priority) + const char* quiet = getenv("HAKMEM_QUIET"); + g_debug_quiet = (quiet && *quiet && *quiet != '0') ? 1 : 0; + + // Check HAKMEM_DEBUG_ALL + const char* all = getenv("HAKMEM_DEBUG_ALL"); + g_debug_all = (all && *all && *all != '0') ? 1 : 0; + + // Check HAKMEM_DEBUG_LEVEL + const char* lvl = getenv("HAKMEM_DEBUG_LEVEL"); + if (lvl && *lvl) { + int v = atoi(lvl); + if (v < 0) v = 0; + if (v > 3) v = 3; + g_debug_master_level = v; + } else { + g_debug_master_level = g_debug_all ? 2 : 0; + } +} + +// Check if a specific debug module should be enabled +// Returns: 1 if debug should be enabled, 0 otherwise +static inline int hak_debug_check(const char* env_name) { + hak_debug_master_init(); + + // HAKMEM_QUIET overrides everything + if (g_debug_quiet) return 0; + + // Check specific ENV first (allows explicit enable/disable) + const char* e = getenv(env_name); + if (e && *e) { + return (*e != '0') ? 1 : 0; + } + + // Fall back to master controls + if (g_debug_all) return 1; + if (g_debug_master_level >= 2) return 1; + + return 0; +} + +// Check if debug should be enabled at a given level +// level: 1=critical, 2=normal, 3=verbose +static inline int hak_debug_check_level(const char* env_name, int level) { + hak_debug_master_init(); + + // HAKMEM_QUIET overrides everything + if (g_debug_quiet) return 0; + + // Check specific ENV first + const char* e = getenv(env_name); + if (e && *e) { + int v = atoi(e); + return (v >= level) ? 1 : 0; + } + + // Fall back to master level + return (g_debug_master_level >= level) ? 1 : 0; +} + +// Quick check if quiet mode is enabled (for performance-critical paths) +static inline int hak_is_quiet(void) { + hak_debug_master_init(); + return g_debug_quiet; +} + +#endif // HAKMEM_DEBUG_MASTER_H diff --git a/core/hakmem_elo.c b/core/hakmem_elo.c index c70eab8a..f34ad7f6 100644 --- a/core/hakmem_elo.c +++ b/core/hakmem_elo.c @@ -4,6 +4,7 @@ // Composite score: 40% CPU + 30% PageFaults + 30% Memory #include "hakmem_elo.h" +#include "hakmem_debug_master.h" // Phase 4b: Master debug control #include #include #include @@ -17,15 +18,9 @@ static int g_initialized = 0; static int g_current_strategy = 0; static uint64_t g_total_selections = 0; -// ENV Cleanup: Cache HAKMEM_QUIET flag (was calling getenv 10+ times in loops!) -static int g_quiet_mode = -1; // -1 = uninitialized, 0 = verbose, 1 = quiet -static inline int is_quiet(void) { - if (__builtin_expect(g_quiet_mode < 0, 0)) { - const char* q = getenv("HAKMEM_QUIET"); - g_quiet_mode = (q && strcmp(q, "1") == 0) ? 1 : 0; - } - return g_quiet_mode; -} +// ENV Cleanup Phase 4b: Use centralized quiet mode from hakmem_debug_master.h +// This replaces the previous local is_quiet() implementation +#define is_quiet() hak_is_quiet() // Strategy threshold presets (geometric progression from 512KB to 8MB) static const size_t STRATEGY_THRESHOLDS[] = { diff --git a/core/hakmem_shared_pool.c b/core/hakmem_shared_pool.c index c09f4649..60d54b45 100644 --- a/core/hakmem_shared_pool.c +++ b/core/hakmem_shared_pool.c @@ -1,6 +1,7 @@ #include "hakmem_shared_pool.h" #include "hakmem_tiny_superslab.h" #include "hakmem_tiny_superslab_constants.h" +#include "hakmem_debug_master.h" // Phase 4b: Master debug control #include "box/ss_slab_meta_box.h" // Phase 3d-A: SlabMeta Box boundary #include "box/ss_hot_cold_box.h" // Phase 12-1.1: EMPTY slab marking #include "box/pagefault_telemetry_box.h" // Box PageFaultTelemetry (PF_BUCKET_SS_META) @@ -25,10 +26,10 @@ static int g_lock_stats_enabled = -1; // -1=uninitialized, 0=of #if !HAKMEM_BUILD_RELEASE // Initialize lock stats from environment variable +// Phase 4b: Now uses hak_debug_check() for master debug control support static inline void lock_stats_init(void) { if (__builtin_expect(g_lock_stats_enabled == -1, 0)) { - const char* env = getenv("HAKMEM_SHARED_POOL_LOCK_STATS"); - g_lock_stats_enabled = (env && *env && *env != '0') ? 1 : 0; + g_lock_stats_enabled = hak_debug_check("HAKMEM_SHARED_POOL_LOCK_STATS"); } }