Files
hakmem/core/hakmem_debug_master.h
Moe Charm (CI) 3f461ba25f Cleanup: Consolidate debug ENV vars to HAKMEM_DEBUG_LEVEL
Integrated 4 new debug environment variables added during bug fixes
into the existing unified HAKMEM_DEBUG_LEVEL system (expanded to 0-5 levels).

Changes:

1. Expanded HAKMEM_DEBUG_LEVEL from 0-3 to 0-5 levels:
   - 0 = OFF (production)
   - 1 = ERROR (critical errors)
   - 2 = WARN (warnings)
   - 3 = INFO (allocation paths, header validation, stats)
   - 4 = DEBUG (guard instrumentation, failfast)
   - 5 = TRACE (verbose tracing)

2. Integrated 4 environment variables:
   - HAKMEM_ALLOC_PATH_TRACE → HAKMEM_DEBUG_LEVEL >= 3 (INFO)
   - HAKMEM_TINY_SLL_VALIDATE_HDR → HAKMEM_DEBUG_LEVEL >= 3 (INFO)
   - HAKMEM_TINY_REFILL_FAILFAST → HAKMEM_DEBUG_LEVEL >= 4 (DEBUG)
   - HAKMEM_TINY_GUARD → HAKMEM_DEBUG_LEVEL >= 4 (DEBUG)

3. Kept 2 special-purpose variables (fine-grained control):
   - HAKMEM_TINY_GUARD_CLASS (target class for guard)
   - HAKMEM_TINY_GUARD_MAX (max guard events)

4. Backward compatibility:
   - Legacy ENV vars still work via hak_debug_check_level()
   - New code uses unified system
   - No behavior changes for existing users

Updated files:
- core/hakmem_debug_master.h (level 0-5 expansion)
- core/hakmem_tiny_superslab_internal.h (alloc path trace)
- core/box/tls_sll_box.h (header validation)
- core/tiny_failfast.c (failfast level)
- core/tiny_refill_opt.h (failfast guard)
- core/hakmem_tiny_ace_guard_box.inc (guard enable)
- core/hakmem_tiny.c (include hakmem_debug_master.h)

Impact:
- Simpler debug control: HAKMEM_DEBUG_LEVEL=3 instead of 4 separate ENVs
- Easier to discover/use
- Consistent debug levels across codebase
- Reduces ENV variable proliferation (43+ vars surveyed)

Future work:
- Consolidate remaining 39+ debug variables (documented in survey)
- Gradual migration over 2-3 releases

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude <noreply@anthropic.com>
2025-11-29 06:57:03 +09:00

118 lines
3.8 KiB
C

// 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=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
// 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 <stdlib.h>
#include <string.h>
// Master debug state (cached at first access)
// 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;
// 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 > 5) v = 5; // Expanded to 0-5 range
g_debug_master_level = v;
} else {
g_debug_master_level = g_debug_all ? 3 : 0; // DEBUG_ALL → INFO level
}
}
// 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