ENV Cleanup Step 7: Gate debug ENV vars in core/box/free_local_box.c

Changes:
- Gated HAKMEM_TINY_SLL_DIAG (2 call sites) behind #if !HAKMEM_BUILD_RELEASE
- Gated HAKMEM_TINY_FREELIST_MASK behind #if !HAKMEM_BUILD_RELEASE
- Gated HAKMEM_SS_FREE_DEBUG behind #if !HAKMEM_BUILD_RELEASE
- Entire diagnostic blocks wrapped (not just getenv) to avoid compilation errors
- ENV variables gated: HAKMEM_TINY_SLL_DIAG, HAKMEM_TINY_FREELIST_MASK, HAKMEM_SS_FREE_DEBUG

Performance: 30.4M ops/s Larson (baseline 30.4M, perfect match)
Build: Clean, pre-existing warnings only

FIX: Previous version had scoping issue with static variables inside do{}while blocks

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

Co-Authored-By: Claude <noreply@anthropic.com>
This commit is contained in:
Moe Charm (CI)
2025-11-28 01:06:15 +09:00
parent d0d2814f15
commit cfa5e4e91c

View File

@ -30,6 +30,7 @@ void tiny_free_local_box(SuperSlab* ss, int slab_idx, TinySlabMeta* meta, void*
void* base = (void*)((uint8_t*)ptr - 1); void* base = (void*)((uint8_t*)ptr - 1);
// Targeted header integrity check (env: HAKMEM_TINY_SLL_DIAG, C7 focus) // Targeted header integrity check (env: HAKMEM_TINY_SLL_DIAG, C7 focus)
#if !HAKMEM_BUILD_RELEASE
do { do {
static int g_free_diag_en = -1; static int g_free_diag_en = -1;
static _Atomic uint32_t g_free_diag_shot = 0; static _Atomic uint32_t g_free_diag_shot = 0;
@ -56,6 +57,7 @@ void tiny_free_local_box(SuperSlab* ss, int slab_idx, TinySlabMeta* meta, void*
} }
} }
} while (0); } while (0);
#endif
if (__builtin_expect(tiny_refill_failfast_level() >= 2, 0)) { if (__builtin_expect(tiny_refill_failfast_level() >= 2, 0)) {
int actual_idx = slab_index_for(ss, base); int actual_idx = slab_index_for(ss, base);
@ -77,6 +79,7 @@ void tiny_free_local_box(SuperSlab* ss, int slab_idx, TinySlabMeta* meta, void*
void* prev = meta->freelist; void* prev = meta->freelist;
// Detect suspicious prev before writing next (env-gated) // Detect suspicious prev before writing next (env-gated)
#if !HAKMEM_BUILD_RELEASE
do { do {
static int g_prev_diag_en = -1; static int g_prev_diag_en = -1;
static _Atomic uint32_t g_prev_diag_shot = 0; static _Atomic uint32_t g_prev_diag_shot = 0;
@ -100,6 +103,7 @@ void tiny_free_local_box(SuperSlab* ss, int slab_idx, TinySlabMeta* meta, void*
} }
} }
} while (0); } while (0);
#endif
// FREELIST CORRUPTION DEBUG: Validate pointer before writing // FREELIST CORRUPTION DEBUG: Validate pointer before writing
if (__builtin_expect(tiny_refill_failfast_level() >= 2, 0)) { if (__builtin_expect(tiny_refill_failfast_level() >= 2, 0)) {
@ -157,6 +161,7 @@ void tiny_free_local_box(SuperSlab* ss, int slab_idx, TinySlabMeta* meta, void*
atomic_thread_fence(memory_order_release); atomic_thread_fence(memory_order_release);
// Optional freelist mask update on first push // Optional freelist mask update on first push
#if !HAKMEM_BUILD_RELEASE
do { do {
static int g_mask_en = -1; static int g_mask_en = -1;
if (__builtin_expect(g_mask_en == -1, 0)) { if (__builtin_expect(g_mask_en == -1, 0)) {
@ -168,6 +173,7 @@ void tiny_free_local_box(SuperSlab* ss, int slab_idx, TinySlabMeta* meta, void*
atomic_fetch_or_explicit(&ss->freelist_mask, bit, memory_order_release); atomic_fetch_or_explicit(&ss->freelist_mask, bit, memory_order_release);
} }
} while (0); } while (0);
#endif
// Track local free (debug helpers may be no-op) // Track local free (debug helpers may be no-op)
tiny_remote_track_on_local_free(ss, slab_idx, ptr, "local_free", my_tid); tiny_remote_track_on_local_free(ss, slab_idx, ptr, "local_free", my_tid);
@ -180,11 +186,15 @@ void tiny_free_local_box(SuperSlab* ss, int slab_idx, TinySlabMeta* meta, void*
ss_mark_slab_empty(ss, slab_idx); ss_mark_slab_empty(ss, slab_idx);
// DEBUG LOGGING - Track when used reaches 0 // DEBUG LOGGING - Track when used reaches 0
#if !HAKMEM_BUILD_RELEASE
static int dbg = -1; static int dbg = -1;
if (__builtin_expect(dbg == -1, 0)) { if (__builtin_expect(dbg == -1, 0)) {
const char* e = getenv("HAKMEM_SS_FREE_DEBUG"); const char* e = getenv("HAKMEM_SS_FREE_DEBUG");
dbg = (e && *e && *e != '0') ? 1 : 0; dbg = (e && *e && *e != '0') ? 1 : 0;
} }
#else
const int dbg = 0;
#endif
if (dbg == 1) { if (dbg == 1) {
fprintf(stderr, "[FREE_LOCAL_BOX] EMPTY detected: cls=%u ss=%p slab=%d empty_mask=0x%x empty_count=%u\n", fprintf(stderr, "[FREE_LOCAL_BOX] EMPTY detected: cls=%u ss=%p slab=%d empty_mask=0x%x empty_count=%u\n",
cls, (void*)ss, slab_idx, ss->empty_mask, ss->empty_count); cls, (void*)ss, slab_idx, ss->empty_mask, ss->empty_count);