diff --git a/core/box/carve_push_box.d b/core/box/carve_push_box.d index b10d30d9..53eb5a47 100644 --- a/core/box/carve_push_box.d +++ b/core/box/carve_push_box.d @@ -17,15 +17,17 @@ core/box/carve_push_box.o: core/box/carve_push_box.c \ core/box/../ptr_track.h core/box/../hakmem_super_registry.h \ core/box/../tiny_debug_api.h core/box/carve_push_box.h \ core/box/capacity_box.h core/box/tls_sll_box.h \ - core/box/../hakmem_build_flags.h core/box/../tiny_remote.h \ - core/box/../ptr_track.h core/box/../ptr_trace.h \ - core/box/../box/tiny_next_ptr_box.h core/hakmem_tiny_config.h \ - core/tiny_nextptr.h core/hakmem_build_flags.h core/tiny_region_id.h \ - core/superslab/superslab_inline.h core/box/../tiny_debug_ring.h \ - core/box/../superslab/superslab_inline.h core/box/../tiny_refill_opt.h \ - core/box/../tiny_region_id.h core/box/../box/slab_freelist_atomic.h \ + core/box/../hakmem_build_flags.h core/box/../hakmem_debug_master.h \ + core/box/../tiny_remote.h core/box/../ptr_track.h \ + core/box/../ptr_trace.h core/box/../box/tiny_next_ptr_box.h \ + core/hakmem_tiny_config.h core/tiny_nextptr.h core/hakmem_build_flags.h \ + core/tiny_region_id.h core/superslab/superslab_inline.h \ + core/box/../tiny_debug_ring.h core/box/../superslab/superslab_inline.h \ + core/box/../tiny_refill_opt.h core/box/../tiny_region_id.h \ + core/box/../box/slab_freelist_atomic.h \ core/box/../box/../superslab/superslab_types.h \ - core/box/../box/tls_sll_box.h core/box/../tiny_box_geometry.h + core/box/../hakmem_debug_master.h core/box/../box/tls_sll_box.h \ + core/box/../tiny_box_geometry.h core/box/../hakmem_tiny.h: core/box/../hakmem_build_flags.h: core/box/../hakmem_trace.h: @@ -55,6 +57,7 @@ core/box/carve_push_box.h: core/box/capacity_box.h: core/box/tls_sll_box.h: core/box/../hakmem_build_flags.h: +core/box/../hakmem_debug_master.h: core/box/../tiny_remote.h: core/box/../ptr_track.h: core/box/../ptr_trace.h: @@ -70,5 +73,6 @@ core/box/../tiny_refill_opt.h: core/box/../tiny_region_id.h: core/box/../box/slab_freelist_atomic.h: core/box/../box/../superslab/superslab_types.h: +core/box/../hakmem_debug_master.h: core/box/../box/tls_sll_box.h: core/box/../tiny_box_geometry.h: diff --git a/core/box/front_gate_box.d b/core/box/front_gate_box.d index 41b4e6b1..d79b8b80 100644 --- a/core/box/front_gate_box.d +++ b/core/box/front_gate_box.d @@ -11,11 +11,12 @@ core/box/front_gate_box.o: core/box/front_gate_box.c \ core/superslab/superslab_types.h core/superslab/../tiny_box_geometry.h \ core/tiny_debug_ring.h core/tiny_remote.h core/tiny_debug_api.h \ core/box/tls_sll_box.h core/box/../hakmem_tiny_config.h \ - core/box/../hakmem_build_flags.h core/box/../tiny_remote.h \ - core/box/../tiny_region_id.h core/box/../hakmem_tiny_integrity.h \ - core/box/../hakmem_tiny.h core/box/../ptr_track.h \ - core/box/../ptr_trace.h core/box/../tiny_debug_ring.h \ - core/box/../superslab/superslab_inline.h core/box/ptr_conversion_box.h + core/box/../hakmem_build_flags.h core/box/../hakmem_debug_master.h \ + core/box/../tiny_remote.h core/box/../tiny_region_id.h \ + core/box/../hakmem_tiny_integrity.h core/box/../hakmem_tiny.h \ + core/box/../ptr_track.h core/box/../ptr_trace.h \ + core/box/../tiny_debug_ring.h core/box/../superslab/superslab_inline.h \ + core/box/ptr_conversion_box.h core/box/front_gate_box.h: core/hakmem_tiny.h: core/hakmem_build_flags.h: @@ -44,6 +45,7 @@ core/tiny_debug_api.h: core/box/tls_sll_box.h: core/box/../hakmem_tiny_config.h: core/box/../hakmem_build_flags.h: +core/box/../hakmem_debug_master.h: core/box/../tiny_remote.h: core/box/../tiny_region_id.h: core/box/../hakmem_tiny_integrity.h: diff --git a/core/box/tls_sll_box.h b/core/box/tls_sll_box.h index d7ada1df..0c48d26d 100644 --- a/core/box/tls_sll_box.h +++ b/core/box/tls_sll_box.h @@ -26,6 +26,7 @@ #include "../hakmem_tiny_config.h" #include "../hakmem_build_flags.h" +#include "../hakmem_debug_master.h" // For unified debug level control #include "../tiny_remote.h" #include "../tiny_region_id.h" #include "../hakmem_tiny_integrity.h" @@ -296,15 +297,15 @@ static inline bool tls_sll_push_impl(int class_idx, void* ptr, uint32_t capacity #if HAKMEM_TINY_HEADER_CLASSIDX // Validate header on push - detect blocks pushed without header write - // Enabled via HAKMEM_TINY_SLL_VALIDATE_HDR=1 or in debug builds + // Enabled via HAKMEM_DEBUG_LEVEL >= 3 (INFO level) or in debug builds + // Legacy: HAKMEM_TINY_SLL_VALIDATE_HDR=1 still works for compatibility do { static int g_validate_hdr = -1; if (__builtin_expect(g_validate_hdr == -1, 0)) { #if !HAKMEM_BUILD_RELEASE g_validate_hdr = 1; // Always on in debug #else - const char* env = getenv("HAKMEM_TINY_SLL_VALIDATE_HDR"); - g_validate_hdr = (env && *env && *env != '0') ? 1 : 0; + g_validate_hdr = hak_debug_check_level("HAKMEM_TINY_SLL_VALIDATE_HDR", 3); #endif } diff --git a/core/box/unified_batch_box.d b/core/box/unified_batch_box.d index 408ea967..de16a488 100644 --- a/core/box/unified_batch_box.d +++ b/core/box/unified_batch_box.d @@ -1,8 +1,9 @@ core/box/unified_batch_box.o: core/box/unified_batch_box.c \ core/box/unified_batch_box.h core/box/carve_push_box.h \ core/box/../box/tls_sll_box.h core/box/../box/../hakmem_tiny_config.h \ - core/box/../box/../hakmem_build_flags.h core/box/../box/../tiny_remote.h \ - core/box/../box/../tiny_region_id.h \ + core/box/../box/../hakmem_build_flags.h \ + core/box/../box/../hakmem_debug_master.h \ + core/box/../box/../tiny_remote.h core/box/../box/../tiny_region_id.h \ core/box/../box/../hakmem_build_flags.h \ core/box/../box/../tiny_box_geometry.h \ core/box/../box/../hakmem_tiny_superslab_constants.h \ @@ -29,6 +30,7 @@ core/box/carve_push_box.h: core/box/../box/tls_sll_box.h: core/box/../box/../hakmem_tiny_config.h: core/box/../box/../hakmem_build_flags.h: +core/box/../box/../hakmem_debug_master.h: core/box/../box/../tiny_remote.h: core/box/../box/../tiny_region_id.h: core/box/../box/../hakmem_build_flags.h: diff --git a/core/hakmem_debug_master.h b/core/hakmem_debug_master.h index df17f564..269e96df 100644 --- a/core/hakmem_debug_master.h +++ b/core/hakmem_debug_master.h @@ -1,12 +1,21 @@ // hakmem_debug_master.h - Master Debug Control // // ENV Cleanup Phase 4b: Add master debug level control +// Updated 2025-11-29: Expanded to 0-5 levels, replaces individual debug ENVs // // 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_DEBUG_LEVEL=N Set debug level (0=off, 1=error, 2=warn, 3=info, 4=debug, 5=trace) // HAKMEM_QUIET=1 Suppress all debug output (overrides DEBUG_ALL/LEVEL) // +// Level Definitions: +// 0 = OFF Production mode (all debug disabled) +// 1 = ERROR Critical errors only +// 2 = WARN Warnings + errors +// 3 = INFO Allocation paths, header validation, stats +// 4 = DEBUG Guard instrumentation, failfast +// 5 = TRACE Verbose tracing (all debug output) +// // 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 @@ -30,7 +39,7 @@ #include // Master debug state (cached at first access) -// Level: -1 = uninitialized, 0 = off, 1 = critical, 2 = normal, 3 = verbose +// Level: -1 = uninitialized, 0-5 = off/error/warn/info/debug/trace static int g_debug_master_level = -1; static int g_debug_all = -1; static int g_debug_quiet = -1; @@ -52,10 +61,10 @@ static inline void hak_debug_master_init(void) { if (lvl && *lvl) { int v = atoi(lvl); if (v < 0) v = 0; - if (v > 3) v = 3; + if (v > 5) v = 5; // Expanded to 0-5 range g_debug_master_level = v; } else { - g_debug_master_level = g_debug_all ? 2 : 0; + g_debug_master_level = g_debug_all ? 3 : 0; // DEBUG_ALL → INFO level } } diff --git a/core/hakmem_tiny.c b/core/hakmem_tiny.c index 0850eec7..6201d3d3 100644 --- a/core/hakmem_tiny.c +++ b/core/hakmem_tiny.c @@ -16,6 +16,7 @@ #include "tiny_api.h" // Consolidated: stats_api, query_api, rss_api, registry_api #include "tiny_tls.h" #include "tiny_debug.h" +#include "hakmem_debug_master.h" // For unified debug level control #include "tiny_mmap_gate.h" #include "tiny_debug_ring.h" #include "tiny_route.h" diff --git a/core/hakmem_tiny_ace_guard_box.inc b/core/hakmem_tiny_ace_guard_box.inc index 02821773..ba729f45 100644 --- a/core/hakmem_tiny_ace_guard_box.inc +++ b/core/hakmem_tiny_ace_guard_box.inc @@ -57,8 +57,11 @@ static __thread int g_tiny_guard_seen = 0; static inline int tiny_guard_enabled_runtime(void) { if (__builtin_expect(g_tiny_guard_enabled == -1, 0)) { - const char* e = getenv("HAKMEM_TINY_GUARD"); - g_tiny_guard_enabled = (e && *e && *e != '0') ? 1 : 0; + // Enabled via HAKMEM_DEBUG_LEVEL >= 4 (DEBUG level) + // Legacy: HAKMEM_TINY_GUARD=1 still works for compatibility + g_tiny_guard_enabled = hak_debug_check_level("HAKMEM_TINY_GUARD", 4); + + // Special-purpose parameters (kept for targeted debugging) const char* ec = getenv("HAKMEM_TINY_GUARD_CLASS"); if (ec && *ec) g_tiny_guard_class = atoi(ec); const char* el = getenv("HAKMEM_TINY_GUARD_MAX"); diff --git a/core/hakmem_tiny_superslab_internal.h b/core/hakmem_tiny_superslab_internal.h index cb392fb1..5923a726 100644 --- a/core/hakmem_tiny_superslab_internal.h +++ b/core/hakmem_tiny_superslab_internal.h @@ -10,6 +10,7 @@ #include "box/ss_hot_cold_box.h" #include "box/ss_allocation_box.h" // CRITICAL: For superslab_allocate() declaration (fixes implicit int assumption) #include "hakmem_super_registry.h" +#include "hakmem_debug_master.h" // For unified debug level control #include "hakmem_tiny.h" #include "hakmem_tiny_config.h" #include "hakmem_shared_pool.h" @@ -59,13 +60,13 @@ #endif // HAK_RET_ALLOC_BLOCK_TRACED - Same as above but with path tracking -// Enabled via HAKMEM_ALLOC_PATH_TRACE=1 +// Enabled via HAKMEM_DEBUG_LEVEL >= 3 (INFO level) +// Legacy: HAKMEM_ALLOC_PATH_TRACE=1 still works for compatibility #if HAKMEM_TINY_HEADER_CLASSIDX #define HAK_RET_ALLOC_BLOCK_TRACED(cls, base_ptr, path_id) do { \ static int g_trace_enabled = -1; \ if (__builtin_expect(g_trace_enabled == -1, 0)) { \ - const char* env = getenv("HAKMEM_ALLOC_PATH_TRACE"); \ - g_trace_enabled = (env && *env && *env != '0') ? 1 : 0; \ + g_trace_enabled = hak_debug_check_level("HAKMEM_ALLOC_PATH_TRACE", 3); \ } \ if (__builtin_expect(g_trace_enabled, 0)) { \ static _Atomic uint32_t g_trace_count = 0; \ diff --git a/core/tiny_alloc_fast_push.d b/core/tiny_alloc_fast_push.d index 12de668c..110123ec 100644 --- a/core/tiny_alloc_fast_push.d +++ b/core/tiny_alloc_fast_push.d @@ -1,8 +1,9 @@ core/tiny_alloc_fast_push.o: core/tiny_alloc_fast_push.c \ core/hakmem_tiny_config.h core/box/tls_sll_box.h \ core/box/../hakmem_tiny_config.h core/box/../hakmem_build_flags.h \ - core/box/../tiny_remote.h core/box/../tiny_region_id.h \ - core/box/../hakmem_build_flags.h core/box/../tiny_box_geometry.h \ + core/box/../hakmem_debug_master.h core/box/../tiny_remote.h \ + core/box/../tiny_region_id.h core/box/../hakmem_build_flags.h \ + core/box/../tiny_box_geometry.h \ core/box/../hakmem_tiny_superslab_constants.h \ core/box/../hakmem_tiny_config.h core/box/../ptr_track.h \ core/box/../hakmem_super_registry.h core/box/../hakmem_tiny_superslab.h \ @@ -24,6 +25,7 @@ core/hakmem_tiny_config.h: core/box/tls_sll_box.h: core/box/../hakmem_tiny_config.h: core/box/../hakmem_build_flags.h: +core/box/../hakmem_debug_master.h: core/box/../tiny_remote.h: core/box/../tiny_region_id.h: core/box/../hakmem_build_flags.h: diff --git a/core/tiny_failfast.c b/core/tiny_failfast.c index 922d3d49..339d4e96 100644 --- a/core/tiny_failfast.c +++ b/core/tiny_failfast.c @@ -7,17 +7,22 @@ #include #include #include "hakmem_tiny_superslab.h" // For SuperSlab/TinySlabMeta and hak_now_ns +#include "hakmem_debug_master.h" // For unified debug level control // Runtime-configurable fail-fast level // 0 = disabled, 1 = log only, 2 = log + raise(SIGUSR2), 3 = abort() +// Enabled via HAKMEM_DEBUG_LEVEL >= 4 (DEBUG level) +// Legacy: HAKMEM_TINY_REFILL_FAILFAST still works for compatibility int tiny_refill_failfast_level(void) { static _Atomic int lvl = -1; int v = atomic_load_explicit(&lvl, memory_order_relaxed); if (__builtin_expect(v != -1, 1)) return v; - const char* e = getenv("HAKMEM_TINY_REFILL_FAILFAST"); - int parsed = (e && *e) ? atoi(e) : 0; - if (parsed < 0) parsed = 0; if (parsed > 3) parsed = 3; + + // Use unified debug level (>=4 maps to failfast level 1) + int debug_lvl = hak_debug_check_level("HAKMEM_TINY_REFILL_FAILFAST", 4); + int parsed = debug_lvl ? 1 : 0; // Simple on/off based on debug level + atomic_store_explicit(&lvl, parsed, memory_order_relaxed); return parsed; } diff --git a/core/tiny_failfast.d b/core/tiny_failfast.d index a01725a1..956f5eef 100644 --- a/core/tiny_failfast.d +++ b/core/tiny_failfast.d @@ -5,7 +5,7 @@ core/tiny_failfast.o: core/tiny_failfast.c core/hakmem_tiny_superslab.h \ core/superslab/../hakmem_tiny_superslab_constants.h \ core/superslab/../hakmem_tiny_config.h core/tiny_debug_ring.h \ core/hakmem_build_flags.h core/tiny_remote.h \ - core/hakmem_tiny_superslab_constants.h + core/hakmem_tiny_superslab_constants.h core/hakmem_debug_master.h core/hakmem_tiny_superslab.h: core/superslab/superslab_types.h: core/hakmem_tiny_superslab_constants.h: @@ -18,3 +18,4 @@ core/tiny_debug_ring.h: core/hakmem_build_flags.h: core/tiny_remote.h: core/hakmem_tiny_superslab_constants.h: +core/hakmem_debug_master.h: diff --git a/core/tiny_refill_opt.h b/core/tiny_refill_opt.h index 40c66468..fc2b258c 100644 --- a/core/tiny_refill_opt.h +++ b/core/tiny_refill_opt.h @@ -10,6 +10,7 @@ #include "ptr_track.h" // Pointer tracking for debugging header corruption #include "box/tiny_next_ptr_box.h" // Box API: Next pointer read/write #include "box/slab_freelist_atomic.h" // Phase 1: Atomic freelist accessor +#include "hakmem_debug_master.h" // For unified debug level control #ifndef HAKMEM_TINY_REFILL_OPT #define HAKMEM_TINY_REFILL_OPT 1 @@ -168,20 +169,20 @@ static inline void trc_splice_to_sll(int class_idx, TinyRefillChain* c, static inline int trc_refill_guard_enabled(void) { // FIX: Allow runtime override even in release builds for debugging + // Enabled via HAKMEM_DEBUG_LEVEL >= 4 (DEBUG level) + // Legacy: HAKMEM_TINY_REFILL_FAILFAST still works for compatibility static int g_trc_guard = -1; if (__builtin_expect(g_trc_guard == -1, 0)) { - const char* env = getenv("HAKMEM_TINY_REFILL_FAILFAST"); #if HAKMEM_BUILD_RELEASE - // Release: Default OFF, but allow explicit enable - g_trc_guard = (env && *env && *env != '0') ? 1 : 0; + // Release: Use unified debug level + g_trc_guard = hak_debug_check_level("HAKMEM_TINY_REFILL_FAILFAST", 4); #else - // Debug: Default ON, but allow explicit disable - g_trc_guard = (env && *env) ? ((*env != '0') ? 1 : 0) : 1; + // Debug: Default ON + g_trc_guard = 1; #endif #if !HAKMEM_BUILD_RELEASE - fprintf(stderr, "[TRC_GUARD] failfast=%d env=%s mode=%s\n", - g_trc_guard, env ? env : "(null)", - HAKMEM_BUILD_RELEASE ? "release" : "debug"); + fprintf(stderr, "[TRC_GUARD] failfast=%d mode=%s\n", + g_trc_guard, HAKMEM_BUILD_RELEASE ? "release" : "debug"); fflush(stderr); #endif } diff --git a/hakmem.d b/hakmem.d index 0a23e039..369955bf 100644 --- a/hakmem.d +++ b/hakmem.d @@ -27,8 +27,9 @@ hakmem.o: core/hakmem.c core/hakmem.h core/hakmem_build_flags.h \ core/box/../tiny_free_fast_v2.inc.h core/box/../tiny_region_id.h \ core/box/../hakmem_build_flags.h core/box/../hakmem_tiny_config.h \ core/box/../box/tls_sll_box.h core/box/../box/../hakmem_tiny_config.h \ - core/box/../box/../hakmem_build_flags.h core/box/../box/../tiny_remote.h \ - core/box/../box/../tiny_region_id.h \ + core/box/../box/../hakmem_build_flags.h \ + core/box/../box/../hakmem_debug_master.h \ + core/box/../box/../tiny_remote.h core/box/../box/../tiny_region_id.h \ core/box/../box/../hakmem_tiny_integrity.h \ core/box/../box/../hakmem_tiny.h core/box/../box/../ptr_track.h \ core/box/../box/../tiny_debug_ring.h \ @@ -116,6 +117,7 @@ core/box/../hakmem_tiny_config.h: core/box/../box/tls_sll_box.h: core/box/../box/../hakmem_tiny_config.h: core/box/../box/../hakmem_build_flags.h: +core/box/../box/../hakmem_debug_master.h: core/box/../box/../tiny_remote.h: core/box/../box/../tiny_region_id.h: core/box/../box/../hakmem_tiny_integrity.h: diff --git a/hakmem_shared_pool.d b/hakmem_shared_pool.d index e770c377..e34b3aad 100644 --- a/hakmem_shared_pool.d +++ b/hakmem_shared_pool.d @@ -15,12 +15,13 @@ hakmem_shared_pool.o: core/hakmem_shared_pool.c core/hakmem_shared_pool.h \ core/box/ss_hot_cold_box.h core/box/pagefault_telemetry_box.h \ core/box/tls_sll_drain_box.h core/box/tls_sll_box.h \ core/box/../hakmem_tiny_config.h core/box/../hakmem_build_flags.h \ - core/box/../tiny_remote.h core/box/../tiny_region_id.h \ - core/box/../hakmem_tiny_integrity.h core/box/../hakmem_tiny.h \ - core/box/../ptr_track.h core/box/../ptr_trace.h \ - core/box/../tiny_debug_ring.h core/box/../superslab/superslab_inline.h \ - core/box/free_local_box.h core/hakmem_tiny_superslab.h \ - core/box/tls_slab_reuse_guard_box.h core/hakmem_policy.h + core/box/../hakmem_debug_master.h core/box/../tiny_remote.h \ + core/box/../tiny_region_id.h core/box/../hakmem_tiny_integrity.h \ + core/box/../hakmem_tiny.h core/box/../ptr_track.h \ + core/box/../ptr_trace.h core/box/../tiny_debug_ring.h \ + core/box/../superslab/superslab_inline.h core/box/free_local_box.h \ + core/hakmem_tiny_superslab.h core/box/tls_slab_reuse_guard_box.h \ + core/hakmem_policy.h core/hakmem_shared_pool.h: core/superslab/superslab_types.h: core/hakmem_tiny_superslab_constants.h: @@ -56,6 +57,7 @@ core/box/tls_sll_drain_box.h: core/box/tls_sll_box.h: core/box/../hakmem_tiny_config.h: core/box/../hakmem_build_flags.h: +core/box/../hakmem_debug_master.h: core/box/../tiny_remote.h: core/box/../tiny_region_id.h: core/box/../hakmem_tiny_integrity.h: diff --git a/hakmem_tiny_sfc.d b/hakmem_tiny_sfc.d index f16b9661..6a0a7469 100644 --- a/hakmem_tiny_sfc.d +++ b/hakmem_tiny_sfc.d @@ -10,10 +10,11 @@ hakmem_tiny_sfc.o: core/hakmem_tiny_sfc.c core/tiny_alloc_fast_sfc.inc.h \ core/tiny_debug_ring.h core/tiny_remote.h core/tiny_debug_api.h \ core/hakmem_stats_master.h core/tiny_tls.h core/box/tls_sll_box.h \ core/box/../hakmem_tiny_config.h core/box/../hakmem_build_flags.h \ - core/box/../tiny_remote.h core/box/../tiny_region_id.h \ - core/box/../hakmem_tiny_integrity.h core/box/../hakmem_tiny.h \ - core/box/../ptr_track.h core/box/../ptr_trace.h \ - core/box/../tiny_debug_ring.h core/box/../superslab/superslab_inline.h + core/box/../hakmem_debug_master.h core/box/../tiny_remote.h \ + core/box/../tiny_region_id.h core/box/../hakmem_tiny_integrity.h \ + core/box/../hakmem_tiny.h core/box/../ptr_track.h \ + core/box/../ptr_trace.h core/box/../tiny_debug_ring.h \ + core/box/../superslab/superslab_inline.h core/tiny_alloc_fast_sfc.inc.h: core/hakmem_tiny.h: core/hakmem_build_flags.h: @@ -42,6 +43,7 @@ core/tiny_tls.h: core/box/tls_sll_box.h: core/box/../hakmem_tiny_config.h: core/box/../hakmem_build_flags.h: +core/box/../hakmem_debug_master.h: core/box/../tiny_remote.h: core/box/../tiny_region_id.h: core/box/../hakmem_tiny_integrity.h: