diff --git a/core/box/front_metrics_box.c b/core/box/front_metrics_box.c index eb69c63b..45afed2a 100644 --- a/core/box/front_metrics_box.c +++ b/core/box/front_metrics_box.c @@ -36,8 +36,12 @@ extern unsigned long long g_front_sll_hit[TINY_NUM_CLASSES]; int front_metrics_enabled(void) { static int g_enabled = -1; if (__builtin_expect(g_enabled == -1, 0)) { +#if HAKMEM_BUILD_RELEASE + g_enabled = 0; +#else const char* env = getenv("HAKMEM_TINY_FRONT_METRICS"); g_enabled = (env && *env && *env != '0') ? 1 : 0; +#endif } return g_enabled; } @@ -51,10 +55,16 @@ void hak_tiny_front_metrics_dump(void) { return; } - const char* dump_env = getenv("HAKMEM_TINY_FRONT_DUMP"); + const char* dump_env = +#if HAKMEM_BUILD_RELEASE + NULL; + if (1) { return; } +#else + getenv("HAKMEM_TINY_FRONT_DUMP"); if (!(dump_env && *dump_env && *dump_env != '0')) { return; } +#endif fprintf(stderr, "\n========== Box FrontMetrics: Layer Hit Rates ==========\n"); fprintf(stderr, "Purpose: Identify which frontend layers are doing real work\n"); diff --git a/core/box/mailbox_box.c b/core/box/mailbox_box.c index 0e43082c..5acaa1a3 100644 --- a/core/box/mailbox_box.c +++ b/core/box/mailbox_box.c @@ -56,11 +56,15 @@ void mailbox_box_register(int class_idx) { if (g_tls_mailbox_registered[class_idx]) return; g_mailbox_register_calls[class_idx]++; // One-shot visibility trace (env: HAKMEM_TINY_RF_TRACE) +#if HAKMEM_BUILD_RELEASE + static const int trace_en = 0; +#else static int trace_en = -1; if (__builtin_expect(trace_en == -1, 0)) { const char* e = getenv("HAKMEM_TINY_RF_TRACE"); trace_en = (e && atoi(e) != 0) ? 1 : 0; } +#endif pthread_once(&g_mailbox_tls_once, mailbox_tls_init); pthread_setspecific(g_mailbox_tls_key, (void*)1); @@ -125,6 +129,10 @@ void mailbox_box_publish(int class_idx, SuperSlab* ss, int slab_idx) { uintptr_t mailbox_box_peek_one(int class_idx) { // Optional slow-discovery (triage only) to expand used when >0 int slow_en, period; +#if HAKMEM_BUILD_RELEASE + slow_en = 0; + period = 0; +#else if (__builtin_expect(g_mailbox_slowdisc_en == -1, 0)) { const char* e = getenv("HAKMEM_TINY_MAILBOX_SLOWDISC"); g_mailbox_slowdisc_en = (!e || atoi(e) != 0) ? 1 : 0; // default ON @@ -147,6 +155,7 @@ uintptr_t mailbox_box_peek_one(int class_idx) { } } } +#endif // Non-destructive peek of first non-zero entry uint32_t used = atomic_load_explicit(&g_pub_mailbox_used[class_idx], memory_order_acquire); @@ -158,6 +167,11 @@ uintptr_t mailbox_box_peek_one(int class_idx) { } uintptr_t mailbox_box_fetch(int class_idx) { +#if HAKMEM_BUILD_RELEASE + if (__builtin_expect(g_mailbox_trace_en == -1, 0)) g_mailbox_trace_en = 0; + if (__builtin_expect(g_mailbox_slowdisc_en == -1, 0)) g_mailbox_slowdisc_en = 0; + if (__builtin_expect(g_mailbox_slowdisc_period == -1, 0)) g_mailbox_slowdisc_period = 256; +#else if (__builtin_expect(g_mailbox_trace_en == -1, 0)) { const char* e = getenv("HAKMEM_TINY_MAILBOX_TRACE"); g_mailbox_trace_en = (e && atoi(e) != 0) ? 1 : 0; @@ -166,7 +180,6 @@ uintptr_t mailbox_box_fetch(int class_idx) { if (v > 0) g_mailbox_trace_limit = v; } - uint32_t used = atomic_load_explicit(&g_pub_mailbox_used[class_idx], memory_order_acquire); // Optional slow discovery if (__builtin_expect(g_mailbox_slowdisc_en == -1, 0)) { const char* e = getenv("HAKMEM_TINY_MAILBOX_SLOWDISC"); @@ -176,6 +189,9 @@ uintptr_t mailbox_box_fetch(int class_idx) { const char* p = getenv("HAKMEM_TINY_MAILBOX_SLOWDISC_PERIOD"); int v = p ? atoi(p) : 256; g_mailbox_slowdisc_period = v; } +#endif + + uint32_t used = atomic_load_explicit(&g_pub_mailbox_used[class_idx], memory_order_acquire); if (g_mailbox_slowdisc_en && used < MAILBOX_SHARDS) { uint32_t t = ++g_mailbox_fetch_tick[class_idx]; int period = g_mailbox_slowdisc_period; diff --git a/core/box/ss_allocation_box.c b/core/box/ss_allocation_box.c index 269a3579..38eed400 100644 --- a/core/box/ss_allocation_box.c +++ b/core/box/ss_allocation_box.c @@ -177,10 +177,14 @@ SuperSlab* superslab_allocate(uint8_t size_class) { // Debug logging flag (lazy init) static __thread int dbg = -1; +#if HAKMEM_BUILD_RELEASE + dbg = 0; +#else if (__builtin_expect(dbg == -1, 0)) { const char* e = getenv("HAKMEM_SS_PREWARM_DEBUG"); dbg = (e && *e && *e != '0') ? 1 : 0; } +#endif // Phase 9: Try LRU cache first (lazy deallocation) SuperSlab* cached_ss = hak_ss_lru_pop(size_class); @@ -296,10 +300,14 @@ void superslab_free(SuperSlab* ss) { // ADD DEBUG LOGGING static __thread int dbg = -1; +#if HAKMEM_BUILD_RELEASE + dbg = 0; +#else if (__builtin_expect(dbg == -1, 0)) { const char* e = getenv("HAKMEM_SS_FREE_DEBUG"); dbg = (e && *e && *e != '0') ? 1 : 0; } +#endif if (dbg == 1) { fprintf(stderr, "[SS_FREE] CALLED: ss=%p lg_size=%d active_slabs=%u\n", (void*)ss, ss->lg_size, ss->active_slabs); diff --git a/core/hakmem_tiny_stats.c b/core/hakmem_tiny_stats.c index f8e68796..17d99319 100644 --- a/core/hakmem_tiny_stats.c +++ b/core/hakmem_tiny_stats.c @@ -237,8 +237,14 @@ void hak_tiny_path_debug_dump(void) { // Debug print for extended counters (slow/bin/bump/spec) void hak_tiny_debug_counters_dump(void) { #if HAKMEM_DEBUG_COUNTERS - const char* on = getenv("HAKMEM_TINY_COUNTERS_DUMP"); + const char* on = +#if HAKMEM_BUILD_RELEASE + NULL; + if (1) return; +#else + getenv("HAKMEM_TINY_COUNTERS_DUMP"); if (!(on && atoi(on) != 0)) return; +#endif // NOTE: Extended counters (alloc_slow, bitmap_scans, etc.) are currently not tracked // Uncomment when these variables are implemented @@ -561,9 +567,13 @@ void hak_tiny_debug_counters_dump(void) { // Always-available: Refill stage counters dump (env: HAKMEM_TINY_REFILL_DUMP=1 or reuse HAKMEM_TINY_COUNTERS_DUMP) static void hak_tiny_refill_counters_dump(void) { hak_tiny_stats_init_flags(); +#if HAKMEM_BUILD_RELEASE + if (1) return; +#else const char* on1 = getenv("HAKMEM_TINY_REFILL_DUMP"); const char* on2 = getenv("HAKMEM_TINY_COUNTERS_DUMP"); if (!((on1 && atoi(on1)!=0) || (on2 && atoi(on2)!=0))) return; +#endif extern unsigned long long g_rf_total_calls[]; extern unsigned long long g_rf_hit_bench[]; extern unsigned long long g_rf_hit_hot[]; diff --git a/core/hakmem_tiny_superslab.c b/core/hakmem_tiny_superslab.c index aa4e8d79..a537ed97 100644 --- a/core/hakmem_tiny_superslab.c +++ b/core/hakmem_tiny_superslab.c @@ -797,10 +797,14 @@ SuperSlab* superslab_allocate(uint8_t size_class) { // Debug logging flag (lazy init) static __thread int dbg = -1; +#if HAKMEM_BUILD_RELEASE + dbg = 0; +#else if (__builtin_expect(dbg == -1, 0)) { const char* e = getenv("HAKMEM_SS_PREWARM_DEBUG"); dbg = (e && *e && *e != '0') ? 1 : 0; } +#endif // Phase 9: Try LRU cache first (lazy deallocation) SuperSlab* cached_ss = hak_ss_lru_pop(size_class); @@ -1089,10 +1093,14 @@ void superslab_free(SuperSlab* ss) { // ADD DEBUG LOGGING static __thread int dbg = -1; +#if HAKMEM_BUILD_RELEASE + dbg = 0; +#else if (__builtin_expect(dbg == -1, 0)) { const char* e = getenv("HAKMEM_SS_FREE_DEBUG"); dbg = (e && *e && *e != '0') ? 1 : 0; } +#endif if (dbg == 1) { fprintf(stderr, "[SS_FREE] CALLED: ss=%p lg_size=%d active_slabs=%u\n", (void*)ss, ss->lg_size, ss->active_slabs); diff --git a/core/ptr_trace.h b/core/ptr_trace.h index a1846bf6..bdc99a6d 100644 --- a/core/ptr_trace.h +++ b/core/ptr_trace.h @@ -65,17 +65,25 @@ static inline void ptr_trace_dump(void) { } static inline void ptr_trace_try_register_dump(void) { +#if HAKMEM_BUILD_RELEASE + return; +#else if (g_ptr_trace_dump_registered) return; g_ptr_trace_dump_registered = 1; const char* env = getenv("HAKMEM_PTR_TRACE_DUMP"); if (!(env && *env && *env != '0')) return; atexit(ptr_trace_dump); +#endif } // Immediate dump (Debug only) — static inline to avoid ODR/link conflicts under LTO // Only dumps if HAKMEM_PTR_TRACE_VERBOSE=1 to avoid excessive output in debug builds #if HAKMEM_PTR_TRACE static inline void ptr_trace_dump_now(const char* reason) { +#if HAKMEM_BUILD_RELEASE + (void)reason; + return; +#else static int verbose_mode = -1; if (verbose_mode == -1) { const char* env = getenv("HAKMEM_PTR_TRACE_VERBOSE"); @@ -91,6 +99,7 @@ static inline void ptr_trace_dump_now(const char* reason) { fprintf(stderr, "[%3u] tag=%s cls=%d node=%p val=%p off=%zu\n", k, e->tag ? e->tag : "?", e->class_idx, e->node, e->val, e->off); } +#endif } #else static inline void ptr_trace_dump_now(const char* reason) { (void)reason; } diff --git a/core/tiny_debug_ring.c b/core/tiny_debug_ring.c index 19bacd8a..9df04c7e 100644 --- a/core/tiny_debug_ring.c +++ b/core/tiny_debug_ring.c @@ -191,6 +191,9 @@ static void tiny_debug_ring_sigusr(int signo, siginfo_t* info, void* uctx) { } void tiny_debug_ring_init(void) { +#if HAKMEM_BUILD_RELEASE + return; // No env reads or hooks in release builds +#endif if (g_tiny_ring_enabled) return; const char* env = getenv("HAKMEM_TINY_TRACE_RING"); if (!(env && *env && env[0] != '0')) { @@ -228,6 +231,9 @@ static void tiny_debug_ring_ctor(void) { __attribute__((destructor)) static void tiny_debug_ring_dtor(void) { +#if HAKMEM_BUILD_RELEASE + return; // Skip env access in release builds +#endif const char* e = getenv("HAKMEM_TINY_DUMP_RING_ATEXIT"); if (e && *e && e[0] != '0') { tiny_debug_ring_dump(STDERR_FILENO, 0); diff --git a/core/tiny_refill.h b/core/tiny_refill.h index b5102587..11bc1da9 100644 --- a/core/tiny_refill.h +++ b/core/tiny_refill.h @@ -86,7 +86,8 @@ static inline SuperSlab* tiny_refill_try_fast(int class_idx, TinyTLSSlab* tls) { } } } - // One-shot entry trace (env: HAKMEM_TINY_RF_TRACE) + // One-shot entry trace (env: HAKMEM_TINY_RF_TRACE), disabled in release builds +#if !HAKMEM_BUILD_RELEASE do { static int en = -1; static _Atomic int printed[8]; if (__builtin_expect(en == -1, 0)) { @@ -101,6 +102,7 @@ static inline SuperSlab* tiny_refill_try_fast(int class_idx, TinyTLSSlab* tls) { } } } while (0); +#endif // For hot tiny classes (0..3), try mailbox first to avoid deeper scans if (class_idx <= 3) { uint32_t self_tid = tiny_self_u32(); diff --git a/core/tiny_superslab_free.inc.h b/core/tiny_superslab_free.inc.h index c24609c9..8c135fde 100644 --- a/core/tiny_superslab_free.inc.h +++ b/core/tiny_superslab_free.inc.h @@ -225,10 +225,14 @@ static inline void hak_tiny_free_superslab(void* ptr, SuperSlab* ss) { #include "box/free_local_box.h" // DEBUG LOGGING - Track freelist operations static __thread int dbg = -1; +#if HAKMEM_BUILD_RELEASE + dbg = 0; +#else if (__builtin_expect(dbg == -1, 0)) { const char* e = getenv("HAKMEM_SS_FREE_DEBUG"); dbg = (e && *e && *e != '0') ? 1 : 0; } +#endif static __thread int free_count = 0; if (dbg == 1 && (free_count++ % 1000) == 0) { fprintf(stderr, "[FREE_LOCAL] cls=%u slab=%d meta->used=%u (before dec)\n", @@ -262,10 +266,14 @@ static inline void hak_tiny_free_superslab(void* ptr, SuperSlab* ss) { if (meta->used == 0) { // DEBUG LOGGING static __thread int dbg = -1; +#if HAKMEM_BUILD_RELEASE + dbg = 0; +#else if (__builtin_expect(dbg == -1, 0)) { const char* e = getenv("HAKMEM_SS_FREE_DEBUG"); dbg = (e && *e && *e != '0') ? 1 : 0; } +#endif if (dbg == 1) { fprintf(stderr, "[FREE_PATH] meta->used=0 detected: cls=%u ss=%p slab_idx=%d\n", cls, (void*)ss, slab_idx); diff --git a/docs/analysis/ENV_CLEANUP_PLAN.md b/docs/analysis/ENV_CLEANUP_PLAN.md index 829179bb..4de25537 100644 --- a/docs/analysis/ENV_CLEANUP_PLAN.md +++ b/docs/analysis/ENV_CLEANUP_PLAN.md @@ -49,6 +49,10 @@ - Doc-only list was confirmed with `rg` diff (`comm -23 docs_env code_env`); zero getenv hits. - Count drift (221 → ~279) comes from `.inc` helpers now included in the scan; keep 221 as the baseline metric but track the expanded map below. +## Completed in this session (release gating of debug env) +- Disabled getenv for debug/trace in release builds: `HAKMEM_TINY_TRACE_RING`, `HAKMEM_TINY_DUMP_RING_ATEXIT`, `HAKMEM_TINY_RF_TRACE`, `HAKMEM_TINY_MAILBOX_TRACE(_LIMIT/_SLOWDISC/_PERIOD)`, `HAKMEM_SS_PREWARM_DEBUG`, `HAKMEM_SS_FREE_DEBUG`, `HAKMEM_TINY_FRONT_METRICS`, `HAKMEM_TINY_FRONT_DUMP`, `HAKMEM_TINY_COUNTERS_DUMP`, `HAKMEM_TINY_REFILL_DUMP`, `HAKMEM_PTR_TRACE_DUMP`, `HAKMEM_PTR_TRACE_VERBOSE`. +- SFC_DEBUG getenv consolidated to init boundary (`g_sfc_debug`). + ## Appendix: Complete getenv() map (sorted by path) ``` core/box/ace_pool_connector.c:30: const char* ace_env = getenv("HAKMEM_ACE_ENABLED"); diff --git a/docs/analysis/ENV_CLEANUP_SUMMARY.md b/docs/analysis/ENV_CLEANUP_SUMMARY.md index cf8f2300..e3157973 100644 --- a/docs/analysis/ENV_CLEANUP_SUMMARY.md +++ b/docs/analysis/ENV_CLEANUP_SUMMARY.md @@ -24,10 +24,20 @@ - **BG system**: `HAKMEM_BATCH_BG`, `HAKMEM_L25_BG_DRAIN`, `HAKMEM_L25_BG_MS` — enabled by default; removal would change behavior. - **HeapV2/FrontV2**: `HAKMEM_TINY_HEAP_V2_*`, `HAKMEM_TINY_FRONT_V2/SLIM/DIRECT/METRICS/DUMP` — active path for tiny frontend; keep. +## 最新アクション(このセッション) +- `HAKMEM_SFC_DEBUG` を init 1回読みへ集約(ホットパス getenv を排除)。 +- リリースビルドで以下の DEBUG/TRACE/STATS 系 ENV を無効化(getenv しない): + - `HAKMEM_TINY_TRACE_RING`, `HAKMEM_TINY_DUMP_RING_ATEXIT` + - `HAKMEM_TINY_RF_TRACE`, `HAKMEM_TINY_MAILBOX_TRACE`, `HAKMEM_TINY_MAILBOX_TRACE_LIMIT`, `HAKMEM_TINY_MAILBOX_SLOWDISC(_PERIOD)` + - `HAKMEM_SS_PREWARM_DEBUG`, `HAKMEM_SS_FREE_DEBUG` + - `HAKMEM_TINY_FRONT_METRICS`, `HAKMEM_TINY_FRONT_DUMP` + - `HAKMEM_TINY_COUNTERS_DUMP`, `HAKMEM_TINY_REFILL_DUMP` + - `HAKMEM_PTR_TRACE_DUMP`, `HAKMEM_PTR_TRACE_VERBOSE` + ## Next Steps -1) Implement `HAKMEM_SFC_DEBUG` consolidation (init boundary only). -2) Sweep docs to drop the 21 docs-only variables (no code changes). -3) Maintain A/B gates for Ultra/BG/HeapV2; any deprecation must be feature-flagged with rollback. +1) 残る DEBUG/TRACE/STATS 系 ENV を同様にリリースビルドで無効化 or init 1回読みへ移動。 +2) 21個のドキュメント専用変数を `docs/specs/ENV_VARS*.md` から削除。 +3) Ultra/BG/HeapV2 は現役のため A/B ゲートを維持したまま、削除は別フェーズで判断。 See `ENV_CLEANUP_PLAN.md` for the full getenv map (file+line) and `ENV_CONSOLIDATION_PLAN.md` for the long-term policy reduction (149 → ~30).