diff --git a/core/front/tiny_unified_cache.c b/core/front/tiny_unified_cache.c index 818989dc..58f2ef98 100644 --- a/core/front/tiny_unified_cache.c +++ b/core/front/tiny_unified_cache.c @@ -8,6 +8,7 @@ #include "../superslab/superslab_inline.h" // Phase 23-E: ss_active_add, slab_index_for, ss_slabs_capacity #include "../hakmem_super_registry.h" // For hak_super_lookup (pointer→SuperSlab) #include "../box/pagefault_telemetry_box.h" // Phase 24: Box PageFaultTelemetry (Tiny page touch stats) +#include "../hakmem_env_cache.h" // Priority-2: ENV cache (eliminate syscalls) #include #include @@ -37,10 +38,10 @@ __thread uint64_t g_unified_cache_full[TINY_NUM_CLASSES] = {0}; // Enable flag (default: ON, disable with HAKMEM_TINY_UNIFIED_CACHE=0) int unified_cache_enabled(void) { + // Priority-2: Use cached ENV (eliminate lazy-init static overhead) static int g_enable = -1; if (__builtin_expect(g_enable == -1, 0)) { - const char* e = getenv("HAKMEM_TINY_UNIFIED_CACHE"); - g_enable = (e && *e && *e == '0') ? 0 : 1; // default ON + g_enable = HAK_ENV_TINY_UNIFIED_CACHE(); #if !HAKMEM_BUILD_RELEASE if (g_enable) { fprintf(stderr, "[Unified-INIT] unified_cache_enabled() = %d\n", g_enable); diff --git a/core/hakmem_env_cache.h b/core/hakmem_env_cache.h index 5f9ab880..aa8a46fa 100644 --- a/core/hakmem_env_cache.h +++ b/core/hakmem_env_cache.h @@ -49,10 +49,12 @@ typedef struct { // ===== Warm Path: Lazy Init (1 variable) ===== int ss_map_trace; // HAKMEM_SS_MAP_TRACE (default: 0) - // ===== Warm Path: FastCache (3 variables) ===== + // ===== Warm Path: FastCache (5 variables) ===== int tiny_fast_debug; // HAKMEM_TINY_FAST_DEBUG (default: 0) int tiny_fast_debug_max; // HAKMEM_TINY_FAST_DEBUG_MAX (default: 0) int tiny_front_direct; // HAKMEM_TINY_FRONT_DIRECT (default: 0) + int tiny_fast_stats; // HAKMEM_TINY_FAST_STATS (default: 0) + int tiny_unified_cache; // HAKMEM_TINY_UNIFIED_CACHE (default: 1) // ===== Cold Path: Headers/Debug (5 variables) ===== int tiny_restore_header; // HAKMEM_TINY_RESTORE_HEADER (default: 0) @@ -155,6 +157,13 @@ static inline void hakmem_env_cache_init(void) { e = getenv("HAKMEM_TINY_FRONT_DIRECT"); g_hak_env_cache.tiny_front_direct = (e && *e && *e != '0') ? 1 : 0; + + e = getenv("HAKMEM_TINY_FAST_STATS"); + g_hak_env_cache.tiny_fast_stats = (e && *e && *e != '0') ? 1 : 0; + + e = getenv("HAKMEM_TINY_UNIFIED_CACHE"); + // Default: 1 (enabled), set HAKMEM_TINY_UNIFIED_CACHE=0 to disable + g_hak_env_cache.tiny_unified_cache = (e && *e && *e == '0') ? 0 : 1; } // ===== Cold Path: Headers/Debug ===== @@ -180,7 +189,7 @@ static inline void hakmem_env_cache_init(void) { #if !HAKMEM_BUILD_RELEASE // Debug: Print cache summary (stderr only) if (!g_hak_env_cache.quiet) { - fprintf(stderr, "[ENV_CACHE_INIT] Parsed %d ENV variables at startup\n", 28); + fprintf(stderr, "[ENV_CACHE_INIT] Parsed %d ENV variables at startup\n", 30); fprintf(stderr, "[ENV_CACHE_INIT] Hot path syscalls eliminated: ~2000/sec → 0/sec\n"); fflush(stderr); } @@ -212,6 +221,8 @@ static inline void hakmem_env_cache_init(void) { #define HAK_ENV_TINY_FAST_DEBUG() (g_hak_env_cache.tiny_fast_debug) #define HAK_ENV_TINY_FAST_DEBUG_MAX() (g_hak_env_cache.tiny_fast_debug_max) #define HAK_ENV_TINY_FRONT_DIRECT() (g_hak_env_cache.tiny_front_direct) +#define HAK_ENV_TINY_FAST_STATS() (g_hak_env_cache.tiny_fast_stats) +#define HAK_ENV_TINY_UNIFIED_CACHE() (g_hak_env_cache.tiny_unified_cache) // Cold path accessors #define HAK_ENV_TINY_RESTORE_HEADER() (g_hak_env_cache.tiny_restore_header) diff --git a/core/superslab_slab.c b/core/superslab_slab.c index dfbbe4bf..ef4f8742 100644 --- a/core/superslab_slab.c +++ b/core/superslab_slab.c @@ -5,6 +5,7 @@ #include "hakmem_tiny_superslab_internal.h" #include "box/slab_recycling_box.h" +#include "hakmem_env_cache.h" // Priority-2: ENV cache (eliminate syscalls) // ============================================================================ // Remote Drain (MPSC queue to freelist conversion) @@ -42,16 +43,14 @@ void _ss_remote_drain_to_freelist_unsafe(SuperSlab* ss, int slab_idx, TinySlabMe uintptr_t cur = head; while (cur != 0) { uintptr_t next = *(uintptr_t*)cur; // remote-next stored at offset 0 + // Priority-2: Use cached ENV (eliminate lazy-init static overhead) + if (__builtin_expect(g_remote_drain_diag_en == -1, 0)) { #if !HAKMEM_BUILD_RELEASE - if (__builtin_expect(g_remote_drain_diag_en == -1, 0)) { - const char* e = getenv("HAKMEM_TINY_SLL_DIAG"); - g_remote_drain_diag_en = (e && *e && *e != '0') ? 1 : 0; - } + g_remote_drain_diag_en = HAK_ENV_TINY_SLL_DIAG(); #else - if (__builtin_expect(g_remote_drain_diag_en == -1, 0)) { g_remote_drain_diag_en = 0; - } #endif + } if (__builtin_expect(g_remote_drain_diag_en, 0)) { uintptr_t addr = (uintptr_t)next; if (addr != 0 && (addr < 4096 || addr > 0x00007fffffffffffULL)) { diff --git a/core/tiny_fastcache.c b/core/tiny_fastcache.c index fde0d7ca..1a3cccd4 100644 --- a/core/tiny_fastcache.c +++ b/core/tiny_fastcache.c @@ -5,6 +5,7 @@ #include "hakmem_tiny.h" #include "hakmem_tiny_superslab.h" #include "box/tiny_next_ptr_box.h" // Phase E1-CORRECT: Box API +#include "hakmem_env_cache.h" // Priority-2: ENV cache (eliminate syscalls) #include #include @@ -76,9 +77,9 @@ static __thread uint64_t g_refill_total_blocks = 0; // Total blocks actually al int g_profile_enabled = -1; // -1: uninitialized, 0: off, 1: on (extern in header) static inline int profile_enabled(void) { + // Priority-2: Use cached ENV (eliminate lazy-init syscall overhead) if (__builtin_expect(g_profile_enabled == -1, 0)) { - const char* env = getenv("HAKMEM_TINY_PROFILE"); - g_profile_enabled = (env && *env && *env != '0') ? 1 : 0; + g_profile_enabled = HAK_ENV_TINY_PROFILE(); } return g_profile_enabled; } @@ -204,15 +205,8 @@ void tiny_fast_drain(int class_idx) { // ========== Debug Stats ========== void tiny_fast_print_stats(void) { - static const char* env = NULL; - static int checked = 0; - - if (!checked) { - env = getenv("HAKMEM_TINY_FAST_STATS"); - checked = 1; - } - - if (env && *env && *env != '0') { + // Priority-2: Use cached ENV (eliminate static lazy-init overhead) + if (HAK_ENV_TINY_FAST_STATS()) { fprintf(stderr, "[TINY_FAST] refills=%lu drains=%lu\n", (unsigned long)g_tiny_fast_refill_count, (unsigned long)g_tiny_fast_drain_count); diff --git a/core/tiny_fastcache.h b/core/tiny_fastcache.h index 2fa9f011..577040a9 100644 --- a/core/tiny_fastcache.h +++ b/core/tiny_fastcache.h @@ -6,8 +6,9 @@ #include #include #include -#include // For getenv() -#include "box/tiny_next_ptr_box.h" // Box API: Next pointer read/write +#include +#include "hakmem_env_cache.h" // Priority-2: ENV cache (eliminate syscalls) +#include "box/tiny_next_ptr_box.h" // Box API: Next pointer read/write // ========== Configuration ========== @@ -68,10 +69,10 @@ static inline uint64_t tiny_fast_rdtsc(void) { return 0; } extern int g_profile_enabled; static inline int tiny_fast_profile_enabled(void) { #if !HAKMEM_BUILD_RELEASE + // Priority-2: Use cached ENV (eliminate lazy-init inline overhead) extern int g_profile_enabled; if (__builtin_expect(g_profile_enabled == -1, 0)) { - const char* env = getenv("HAKMEM_TINY_PROFILE"); - g_profile_enabled = (env && *env && *env != '0') ? 1 : 0; + g_profile_enabled = HAK_ENV_TINY_PROFILE(); } return g_profile_enabled; #else