diff --git a/core/box/hak_core_init.inc.h b/core/box/hak_core_init.inc.h index 86375b6d..54add955 100644 --- a/core/box/hak_core_init.inc.h +++ b/core/box/hak_core_init.inc.h @@ -88,7 +88,7 @@ static void hak_init_impl(void) { if (evo_sample_str && atoi(evo_sample_str) > 0) { int freq = atoi(evo_sample_str); if (freq >= 64) { - fprintf(stderr, "[hakmem] Warning: HAKMEM_EVO_SAMPLE=%d too large, using 63\n", freq); + HAKMEM_LOG("Warning: HAKMEM_EVO_SAMPLE=%d too large, using 63\n", freq); freq = 63; } g_evo_sample_mask = (1ULL << freq) - 1; @@ -313,7 +313,7 @@ void hak_shutdown(void) { hkm_ace_controller_destroy(&g_ace_controller); if (!g_bench_tiny_only) { - printf("[hakmem] Shutting down...\n"); + HAKMEM_LOG("Shutting down...\n"); hak_print_stats(); } diff --git a/core/box/hak_free_api.inc.h b/core/box/hak_free_api.inc.h index 3c298e3d..bc00f01b 100644 --- a/core/box/hak_free_api.inc.h +++ b/core/box/hak_free_api.inc.h @@ -255,7 +255,7 @@ void hak_free_at(void* ptr, size_t size, hak_callsite_t site) { __libc_free(raw); #endif break; - default: fprintf(stderr, "[hakmem] ERROR: Unknown allocation method: %d\n", hdr->method); break; + default: HAKMEM_LOG("ERROR: Unknown allocation method: %d\n", hdr->method); break; } } diff --git a/core/box/hak_wrappers.inc.h b/core/box/hak_wrappers.inc.h index e7ef5477..3990f42c 100644 --- a/core/box/hak_wrappers.inc.h +++ b/core/box/hak_wrappers.inc.h @@ -139,28 +139,34 @@ void free(void* ptr) { g_hakmem_lock_depth--; return; } - // Front Gate libc bypass detection + // Front Gate libc bypass detection (quiet in release) static _Atomic uint64_t fg_libc_bypass_count = 0; - - if (g_hakmem_lock_depth > 0) { + + if (g_hakmem_lock_depth > 0) { +#if !HAKMEM_BUILD_RELEASE uint64_t count = atomic_fetch_add_explicit(&fg_libc_bypass_count, 1, memory_order_relaxed); - if (count < 10) { // Log first 10 occurrences + if (count < 10) { fprintf(stderr, "[FG_LIBC_BYPASS] lockdepth=%d count=%llu ptr=%p\n", g_hakmem_lock_depth, (unsigned long long)count, ptr); } - extern void __libc_free(void*); - ptr_trace_dump_now("wrap_libc_lockdepth"); - __libc_free(ptr); - return; +#else + (void)fg_libc_bypass_count; +#endif + extern void __libc_free(void*); + ptr_trace_dump_now("wrap_libc_lockdepth"); + __libc_free(ptr); + return; } - if (__builtin_expect(g_initializing != 0, 0)) { + if (__builtin_expect(g_initializing != 0, 0)) { +#if !HAKMEM_BUILD_RELEASE uint64_t count = atomic_fetch_add_explicit(&fg_libc_bypass_count, 1, memory_order_relaxed); - if (count < 10) { // Log first 10 occurrences + if (count < 10) { fprintf(stderr, "[FG_LIBC_BYPASS] init=%d count=%llu ptr=%p\n", g_initializing, (unsigned long long)count, ptr); } - extern void __libc_free(void*); - ptr_trace_dump_now("wrap_libc_init"); - __libc_free(ptr); - return; +#endif + extern void __libc_free(void*); + ptr_trace_dump_now("wrap_libc_init"); + __libc_free(ptr); + return; } if (__builtin_expect(hak_force_libc_alloc(), 0)) { extern void __libc_free(void*); ptr_trace_dump_now("wrap_libc_force"); __libc_free(ptr); return; } if (hak_ld_env_mode()) { diff --git a/core/box/pool_api.inc.h b/core/box/pool_api.inc.h index 83b1a5eb..ae15a002 100644 --- a/core/box/pool_api.inc.h +++ b/core/box/pool_api.inc.h @@ -5,52 +5,34 @@ void* hak_pool_try_alloc(size_t size, uintptr_t site_id) { // Debug: IMMEDIATE output to verify function is called static int first_call = 1; - if (first_call) { - fprintf(stderr, "[Pool] hak_pool_try_alloc FIRST CALL EVER!\n"); - first_call = 0; - } + if (first_call) { HAKMEM_LOG("[Pool] hak_pool_try_alloc FIRST CALL EVER!\n"); first_call = 0; } - if (size == 40960) { // Exactly 40KB - fprintf(stderr, "[Pool] hak_pool_try_alloc called with 40KB (Bridge class 5)\n"); - } + if (size == 40960) { HAKMEM_LOG("[Pool] hak_pool_try_alloc called with 40KB (Bridge class 5)\n"); } hak_pool_init(); // pthread_once() ensures thread-safe init (no data race!) // Debug for 33-41KB allocations - if (size >= 33000 && size <= 41000) { - fprintf(stderr, "[Pool] hak_pool_try_alloc: size=%zu (after init)\n", size); - } + if (size >= 33000 && size <= 41000) { HAKMEM_LOG("[Pool] hak_pool_try_alloc: size=%zu (after init)\n", size); } // P1.7 approach: Avoid using pool during ALL wrapper calls (conservative but safe) extern int hak_in_wrapper(void); if (hak_in_wrapper() && !g_wrap_l2_enabled) { - if (size >= 33000 && size <= 41000) { - fprintf(stderr, "[Pool] REJECTED: in_wrapper=%d, wrap_l2=%d\n", - hak_in_wrapper(), g_wrap_l2_enabled); - } + if (size >= 33000 && size <= 41000) { HAKMEM_LOG("[Pool] REJECTED: in_wrapper=%d, wrap_l2=%d\n", hak_in_wrapper(), g_wrap_l2_enabled); } return NULL; } if (!hak_pool_is_poolable(size)) { - if (size >= 33000 && size <= 41000) { - fprintf(stderr, "[Pool] REJECTED: not poolable (min=%d, max=%d)\n", - POOL_MIN_SIZE, POOL_MAX_SIZE); - } + if (size >= 33000 && size <= 41000) { HAKMEM_LOG("[Pool] REJECTED: not poolable (min=%d, max=%d)\n", POOL_MIN_SIZE, POOL_MAX_SIZE); } return NULL; } // Get class and shard indices int class_idx = hak_pool_get_class_index(size); if (class_idx < 0) { - if (size >= 33000 && size <= 41000) { - fprintf(stderr, "[Pool] REJECTED: class_idx=%d (size=%zu not mapped)\n", - class_idx, size); - } + if (size >= 33000 && size <= 41000) { HAKMEM_LOG("[Pool] REJECTED: class_idx=%d (size=%zu not mapped)\n", class_idx, size); } return NULL; } - if (size >= 33000 && size <= 41000) { - fprintf(stderr, "[Pool] ACCEPTED: class_idx=%d, proceeding with allocation\n", class_idx); - } + if (size >= 33000 && size <= 41000) { HAKMEM_LOG("[Pool] ACCEPTED: class_idx=%d, proceeding with allocation\n", class_idx); } // MF2: Per-Page Sharding path if (g_mf2_enabled) { diff --git a/core/box/pool_init_api.inc.h b/core/box/pool_init_api.inc.h index 92d93c7a..047e2de9 100644 --- a/core/box/pool_init_api.inc.h +++ b/core/box/pool_init_api.inc.h @@ -5,7 +5,7 @@ // Thread-safe initialization using pthread_once static pthread_once_t hak_pool_init_once_control = PTHREAD_ONCE_INIT; static void hak_pool_init_impl(void) { - fprintf(stderr, "[Pool] hak_pool_init_impl() EXECUTING - Bridge class fix applied\n"); + HAKMEM_LOG("[Pool] hak_pool_init_impl() EXECUTING - Bridge class fix applied\n"); const FrozenPolicy* pol = hkm_policy_get(); // Phase 6.21 CRITICAL FIX: Bridge classes are hardcoded in g_class_sizes, @@ -91,9 +91,9 @@ static void hak_pool_init_impl(void) { HAKMEM_LOG("[MF2] max_queues=%d, lease_ms=%d, idle_threshold_us=%d\n", g_mf2_max_queues, g_mf2_lease_ms, g_mf2_idle_threshold_us); } g_pool.initialized = 1; - fprintf(stderr, "[Pool] Initialized (L2 Hybrid Pool) - Bridge classes SHOULD be enabled\n"); - fprintf(stderr, "[Pool] Class 5 (40KB): %zu\n", g_class_sizes[5]); - fprintf(stderr, "[Pool] Class 6 (52KB): %zu\n", g_class_sizes[6]); + HAKMEM_LOG("[Pool] Initialized (L2 Hybrid Pool) - Bridge classes SHOULD be enabled\n"); + HAKMEM_LOG("[Pool] Class 5 (40KB): %zu\n", g_class_sizes[5]); + HAKMEM_LOG("[Pool] Class 6 (52KB): %zu\n", g_class_sizes[6]); HAKMEM_LOG("[Pool] Initialized (L2 Hybrid Pool)\n"); #ifdef HAKMEM_DEBUG_VERBOSE @@ -123,10 +123,10 @@ static void hak_pool_init_impl(void) { allocated++; } } - fprintf(stderr, "[Pool] Pre-allocated %d pages for Bridge class 5 (%zu KB) - Critical for 33KB allocs\n", + HAKMEM_LOG("[Pool] Pre-allocated %d pages for Bridge class 5 (%zu KB) - Critical for 33KB allocs\n", allocated, g_class_sizes[5]/1024); } else { - fprintf(stderr, "[Pool] WARNING: Bridge class 5 (40KB) is DISABLED - 33KB allocations will fail!\n"); + HAKMEM_LOG("[Pool] WARNING: Bridge class 5 (40KB) is DISABLED - 33KB allocations will fail!\n"); } // Pre-warm Bridge class 6 (52KB) @@ -137,17 +137,14 @@ static void hak_pool_init_impl(void) { allocated++; } } - fprintf(stderr, "[Pool] Pre-allocated %d pages for Bridge class 6 (%zu KB)\n", + HAKMEM_LOG("[Pool] Pre-allocated %d pages for Bridge class 6 (%zu KB)\n", allocated, g_class_sizes[6]/1024); } } void hak_pool_init(void) { - // Always print this to see if it's being called - static int called = 0; - if (called++ == 0) { - fprintf(stderr, "[Pool] hak_pool_init() called for the first time\n"); - } + // Debug-only trace + // static int called = 0; if (called++ == 0) { HAKMEM_LOG("[Pool] hak_pool_init() called for the first time\n"); } pthread_once(&hak_pool_init_once_control, hak_pool_init_impl); } diff --git a/core/hakmem.c b/core/hakmem.c index 1d60bf3b..19e18de5 100644 --- a/core/hakmem.c +++ b/core/hakmem.c @@ -263,7 +263,7 @@ static void bigcache_free_callback(void* ptr, size_t size) { // Verify magic before accessing method field if (hdr->magic != HAKMEM_MAGIC) { - fprintf(stderr, "[hakmem] BigCache eviction: invalid magic, fallback to free()\n"); + HAKMEM_LOG("BigCache eviction: invalid magic, fallback to free()\n"); // CRITICAL FIX: When magic is invalid, allocation came from LIBC (NO header) // Therefore ptr IS the allocated address, not raw (ptr - HEADER_SIZE) // MUST use __libc_free to avoid infinite recursion through free() wrapper @@ -302,7 +302,7 @@ static void bigcache_free_callback(void* ptr, size_t size) { break; default: - fprintf(stderr, "[hakmem] BigCache eviction: unknown method %d\n", hdr->method); + HAKMEM_LOG("BigCache eviction: unknown method %d\n", hdr->method); free(raw); // Fallback break; } diff --git a/core/hakmem_ace.c b/core/hakmem_ace.c index 59bcc729..20baff1f 100644 --- a/core/hakmem_ace.c +++ b/core/hakmem_ace.c @@ -1,4 +1,5 @@ #include +#include "hakmem_internal.h" #include "hakmem_ace.h" #include "hakmem_pool.h" #include "hakmem_l25_pool.h" @@ -52,12 +53,16 @@ void* hkm_ace_alloc(size_t size, uintptr_t site_id, const FrozenPolicy* pol) { // MidPool: 2–52KiB (Phase 6.21: with Bridge classes for W_MAX rounding) if (size >= 33000 && size <= 34000) { +#if !HAKMEM_BUILD_RELEASE fprintf(stderr, "[ACE] Processing 33KB: size=%zu, POOL_MAX_SIZE=%d\n", size, POOL_MAX_SIZE); +#endif } if (size <= POOL_MAX_SIZE) { size_t r = round_to_mid_class(size, wmax_mid, pol); if (size >= 33000 && size <= 34000) { +#if !HAKMEM_BUILD_RELEASE fprintf(stderr, "[ACE] round_to_mid_class returned: %zu (0 means no valid class)\n", r); +#endif } if (r != 0) { // Debug: Log 33KB allocation routing (only in debug builds) @@ -67,7 +72,9 @@ void* hkm_ace_alloc(size_t size, uintptr_t site_id, const FrozenPolicy* pol) { } #endif if (size >= 33000 && size <= 34000) { +#if !HAKMEM_BUILD_RELEASE fprintf(stderr, "[ACE] Calling hak_pool_try_alloc with size=%zu\n", r); +#endif } HKM_TIME_START(t_mid_get); void* p = hak_pool_try_alloc(r, site_id); diff --git a/core/hakmem_ace_controller.c b/core/hakmem_ace_controller.c index 7a735034..2f0525f9 100644 --- a/core/hakmem_ace_controller.c +++ b/core/hakmem_ace_controller.c @@ -23,15 +23,23 @@ static uint64_t getenv_uint64(const char *name, uint64_t default_value) { /* ========== ログマクロ ========== */ +#if HAKMEM_BUILD_RELEASE +#define ACE_LOG_INFO(ctrl, fmt, ...) do { (void)(ctrl); } while (0) +#else #define ACE_LOG_INFO(ctrl, fmt, ...) \ do { if (hkm_ace_log_info_enabled(ctrl)) { \ fprintf(stderr, "[ACE] " fmt "\n", ##__VA_ARGS__); \ } } while (0) +#endif +#if HAKMEM_BUILD_RELEASE +#define ACE_LOG_DEBUG(ctrl, fmt, ...) do { (void)(ctrl); } while (0) +#else #define ACE_LOG_DEBUG(ctrl, fmt, ...) \ do { if (hkm_ace_log_debug_enabled(ctrl)) { \ fprintf(stderr, "[ACE DEBUG] " fmt "\n", ##__VA_ARGS__); \ } } while (0) +#endif /* ========== 初期化 ========== */ diff --git a/core/hakmem_batch.c b/core/hakmem_batch.c index 6cd91c26..f8fb0377 100644 --- a/core/hakmem_batch.c +++ b/core/hakmem_batch.c @@ -15,6 +15,8 @@ #include #include #include +// Release-silent logging +#include "hakmem_internal.h" // For madvise (Linux) #ifdef __linux__ @@ -86,13 +88,10 @@ void hak_batch_init(void) { } g_initialized = 1; - { - const char* q = getenv("HAKMEM_QUIET"); - if (!(q && strcmp(q, "1") == 0)) { - fprintf(stderr, "[Batch] Initialized (threshold=%d MB, min_size=%d KB, bg=%s)\n", - BATCH_THRESHOLD / (1024 * 1024), BATCH_MIN_SIZE / 1024, g_bg_enabled?"on":"off"); - } - } +#if !HAKMEM_BUILD_RELEASE + fprintf(stderr, "[Batch] Initialized (threshold=%d MB, min_size=%d KB, bg=%s)\n", + BATCH_THRESHOLD / (1024 * 1024), BATCH_MIN_SIZE / 1024, g_bg_enabled?"on":"off"); +#endif } // Flush all batched blocks @@ -138,13 +137,10 @@ void hak_batch_flush(void) { #endif g_flush_count++; - { - const char* q = getenv("HAKMEM_QUIET"); - if (!(q && strcmp(q, "1") == 0)) { - fprintf(stderr, "[Batch] Flushed %d blocks (%.1f MB) - flush #%lu\n", - snap.count, snap.total_bytes / (1024.0 * 1024.0), g_flush_count); - } - } +#if !HAKMEM_BUILD_RELEASE + fprintf(stderr, "[Batch] Flushed %d blocks (%.1f MB) - flush #%lu\n", + snap.count, snap.total_bytes / (1024.0 * 1024.0), g_flush_count); +#endif } // Add block to batch @@ -203,10 +199,7 @@ void hak_batch_shutdown(void) { } else { // Flush any remaining blocks synchronously if (g_batch.count > 0) { - const char* q = getenv("HAKMEM_QUIET"); - if (!(q && strcmp(q, "1") == 0)) { - fprintf(stderr, "[Batch] Final flush of %d remaining blocks...\n", g_batch.count); - } + fprintf(stderr, "[Batch] Final flush of %d remaining blocks...\n", g_batch.count); hak_batch_flush(); } } @@ -219,6 +212,7 @@ void hak_batch_shutdown(void) { void hak_batch_print_stats(void) { if (!g_initialized) return; +#if !HAKMEM_BUILD_RELEASE fprintf(stderr, "\n========================================\n"); fprintf(stderr, "madvise Batching Statistics\n"); fprintf(stderr, "========================================\n"); @@ -236,6 +230,7 @@ void hak_batch_print_stats(void) { fprintf(stderr, "Pending blocks: %d\n", g_batch.count); fprintf(stderr, "Pending bytes: %.1f MB\n", g_batch.total_bytes / (1024.0 * 1024.0)); fprintf(stderr, "========================================\n\n"); +#endif } // Query pending bytes diff --git a/core/hakmem_config.c b/core/hakmem_config.c index 33797c98..36de0f71 100644 --- a/core/hakmem_config.c +++ b/core/hakmem_config.c @@ -4,6 +4,7 @@ #include #include #include +#include "hakmem_internal.h" // HAKMEM_LOG for release-silent logging // =========================================================================== // Global Configuration Instance @@ -158,7 +159,7 @@ void hak_config_apply_mode(HakemMode mode) { case HAKMEM_MODE_LEARNING: apply_learning_mode(&g_hakem_config); break; case HAKMEM_MODE_RESEARCH: apply_research_mode(&g_hakem_config); break; default: - fprintf(stderr, "[hakmem] Unknown mode %d, using BALANCED\n", mode); + HAKMEM_LOG("Unknown mode %d, using BALANCED\n", mode); apply_balanced_mode(&g_hakem_config); break; } @@ -177,7 +178,7 @@ static HakemMode parse_mode_env(const char* mode_str) { if (strcmp(mode_str, "learning") == 0) return HAKMEM_MODE_LEARNING; if (strcmp(mode_str, "research") == 0) return HAKMEM_MODE_RESEARCH; - fprintf(stderr, "[hakmem] Unknown HAKMEM_MODE='%s', using balanced\n", mode_str); + HAKMEM_LOG("Unknown HAKMEM_MODE='%s', using balanced\n", mode_str); return HAKMEM_MODE_BALANCED; } @@ -247,58 +248,58 @@ void hak_config_init(void) { // =========================================================================== void hak_config_print(void) { - fprintf(stderr, "\n========================================\n"); - fprintf(stderr, "hakmem Configuration\n"); - fprintf(stderr, "========================================\n"); - fprintf(stderr, "Mode: %s\n", g_hakem_config.mode_name); - fprintf(stderr, "\n"); + HAKMEM_LOG("\n========================================\n"); + HAKMEM_LOG("hakmem Configuration\n"); + HAKMEM_LOG("========================================\n"); + HAKMEM_LOG("Mode: %s\n", g_hakem_config.mode_name); + HAKMEM_LOG("\n"); - fprintf(stderr, "Features:\n"); - fprintf(stderr, " Allocation:\n"); - fprintf(stderr, " malloc: %s\n", (g_hakem_config.features.alloc & HAKMEM_FEATURE_MALLOC) ? "ON" : "OFF"); - fprintf(stderr, " mmap: %s\n", (g_hakem_config.features.alloc & HAKMEM_FEATURE_MMAP) ? "ON" : "OFF"); - fprintf(stderr, " pool: %s\n", (g_hakem_config.features.alloc & HAKMEM_FEATURE_POOL) ? "ON" : "OFF"); + HAKMEM_LOG("Features:\n"); + HAKMEM_LOG(" Allocation:\n"); + HAKMEM_LOG(" malloc: %s\n", (g_hakem_config.features.alloc & HAKMEM_FEATURE_MALLOC) ? "ON" : "OFF"); + HAKMEM_LOG(" mmap: %s\n", (g_hakem_config.features.alloc & HAKMEM_FEATURE_MMAP) ? "ON" : "OFF"); + HAKMEM_LOG(" pool: %s\n", (g_hakem_config.features.alloc & HAKMEM_FEATURE_POOL) ? "ON" : "OFF"); - fprintf(stderr, " Caching:\n"); - fprintf(stderr, " BigCache: %s\n", (g_hakem_config.features.cache & HAKMEM_FEATURE_BIGCACHE) ? "ON" : "OFF"); - fprintf(stderr, " TinyPool: %s\n", (g_hakem_config.features.cache & HAKMEM_FEATURE_TINYPOOL) ? "ON" : "OFF"); + HAKMEM_LOG(" Caching:\n"); + HAKMEM_LOG(" BigCache: %s\n", (g_hakem_config.features.cache & HAKMEM_FEATURE_BIGCACHE) ? "ON" : "OFF"); + HAKMEM_LOG(" TinyPool: %s\n", (g_hakem_config.features.cache & HAKMEM_FEATURE_TINYPOOL) ? "ON" : "OFF"); - fprintf(stderr, " Learning:\n"); - fprintf(stderr, " ELO: %s\n", (g_hakem_config.features.learning & HAKMEM_FEATURE_ELO) ? "ON" : "OFF"); - fprintf(stderr, " Evolution: %s\n", (g_hakem_config.features.learning & HAKMEM_FEATURE_EVOLUTION) ? "ON" : "OFF"); - fprintf(stderr, " Profiling: %s\n", (g_hakem_config.features.learning & HAKMEM_FEATURE_PROFILING) ? "ON" : "OFF"); + HAKMEM_LOG(" Learning:\n"); + HAKMEM_LOG(" ELO: %s\n", (g_hakem_config.features.learning & HAKMEM_FEATURE_ELO) ? "ON" : "OFF"); + HAKMEM_LOG(" Evolution: %s\n", (g_hakem_config.features.learning & HAKMEM_FEATURE_EVOLUTION) ? "ON" : "OFF"); + HAKMEM_LOG(" Profiling: %s\n", (g_hakem_config.features.learning & HAKMEM_FEATURE_PROFILING) ? "ON" : "OFF"); - fprintf(stderr, " Memory:\n"); - fprintf(stderr, " Batch madvise: %s\n", (g_hakem_config.features.memory & HAKMEM_FEATURE_BATCH_MADVISE) ? "ON" : "OFF"); - fprintf(stderr, " THP: %s\n", (g_hakem_config.features.memory & HAKMEM_FEATURE_THP) ? "ON" : "OFF"); - fprintf(stderr, " Free policy: %s\n", (g_hakem_config.features.memory & HAKMEM_FEATURE_FREE_POLICY) ? "ON" : "OFF"); + HAKMEM_LOG(" Memory:\n"); + HAKMEM_LOG(" Batch madvise: %s\n", (g_hakem_config.features.memory & HAKMEM_FEATURE_BATCH_MADVISE) ? "ON" : "OFF"); + HAKMEM_LOG(" THP: %s\n", (g_hakem_config.features.memory & HAKMEM_FEATURE_THP) ? "ON" : "OFF"); + HAKMEM_LOG(" Free policy: %s\n", (g_hakem_config.features.memory & HAKMEM_FEATURE_FREE_POLICY) ? "ON" : "OFF"); - fprintf(stderr, " Debug:\n"); - fprintf(stderr, " Logging: %s\n", (g_hakem_config.features.debug & HAKMEM_FEATURE_DEBUG_LOG) ? "ON" : "OFF"); - fprintf(stderr, " Statistics: %s\n", (g_hakem_config.features.debug & HAKMEM_FEATURE_STATISTICS) ? "ON" : "OFF"); - fprintf(stderr, " Trace: %s\n", (g_hakem_config.features.debug & HAKMEM_FEATURE_TRACE) ? "ON" : "OFF"); + HAKMEM_LOG(" Debug:\n"); + HAKMEM_LOG(" Logging: %s\n", (g_hakem_config.features.debug & HAKMEM_FEATURE_DEBUG_LOG) ? "ON" : "OFF"); + HAKMEM_LOG(" Statistics: %s\n", (g_hakem_config.features.debug & HAKMEM_FEATURE_STATISTICS) ? "ON" : "OFF"); + HAKMEM_LOG(" Trace: %s\n", (g_hakem_config.features.debug & HAKMEM_FEATURE_TRACE) ? "ON" : "OFF"); - fprintf(stderr, "\n"); - fprintf(stderr, "Policies:\n"); - fprintf(stderr, " Free policy: %s\n", + HAKMEM_LOG("\n"); + HAKMEM_LOG("Policies:\n"); + HAKMEM_LOG(" Free policy: %s\n", g_hakem_config.free_policy == FREE_POLICY_BATCH ? "batch" : g_hakem_config.free_policy == FREE_POLICY_KEEP ? "keep" : g_hakem_config.free_policy == FREE_POLICY_ADAPTIVE ? "adaptive" : "unknown"); - fprintf(stderr, " THP policy: %s\n", + HAKMEM_LOG(" THP policy: %s\n", g_hakem_config.thp_policy == THP_POLICY_OFF ? "off" : g_hakem_config.thp_policy == THP_POLICY_AUTO ? "auto" : g_hakem_config.thp_policy == THP_POLICY_ON ? "on" : "unknown"); - fprintf(stderr, " Evo phase: %s\n", + HAKMEM_LOG(" Evo phase: %s\n", g_hakem_config.evo_phase == EVO_PHASE_LEARN ? "learn" : g_hakem_config.evo_phase == EVO_PHASE_FROZEN ? "frozen" : g_hakem_config.evo_phase == EVO_PHASE_CANARY ? "canary" : "unknown"); - fprintf(stderr, "\n"); - fprintf(stderr, "Parameters:\n"); - fprintf(stderr, " ELO frozen strategy: %d\n", g_hakem_config.elo_frozen_strategy); - fprintf(stderr, " Batch threshold: %zu bytes\n", g_hakem_config.batch_threshold); - fprintf(stderr, " Verbose level: %d\n", g_hakem_config.verbose); - fprintf(stderr, "========================================\n\n"); + HAKMEM_LOG("\n"); + HAKMEM_LOG("Parameters:\n"); + HAKMEM_LOG(" ELO frozen strategy: %d\n", g_hakem_config.elo_frozen_strategy); + HAKMEM_LOG(" Batch threshold: %zu bytes\n", g_hakem_config.batch_threshold); + HAKMEM_LOG(" Verbose level: %d\n", g_hakem_config.verbose); + HAKMEM_LOG("========================================\n\n"); } // =========================================================================== @@ -323,12 +324,12 @@ HakemFeatureSet hak_features_for_mode(const char* mode_str) { } void hak_features_print(HakemFeatureSet* fs) { - fprintf(stderr, "Feature Set:\n"); - fprintf(stderr, " alloc: 0x%08x\n", fs->alloc); - fprintf(stderr, " cache: 0x%08x\n", fs->cache); - fprintf(stderr, " learning: 0x%08x\n", fs->learning); - fprintf(stderr, " memory: 0x%08x\n", fs->memory); - fprintf(stderr, " debug: 0x%08x\n", fs->debug); + HAKMEM_LOG("Feature Set:\n"); + HAKMEM_LOG(" alloc: 0x%08x\n", fs->alloc); + HAKMEM_LOG(" cache: 0x%08x\n", fs->cache); + HAKMEM_LOG(" learning: 0x%08x\n", fs->learning); + HAKMEM_LOG(" memory: 0x%08x\n", fs->memory); + HAKMEM_LOG(" debug: 0x%08x\n", fs->debug); } // Box Refactor line default: enable SuperSlab unless explicitly disabled by env or diet mode int g_use_superslab = 1; diff --git a/core/hakmem_elo.c b/core/hakmem_elo.c index 2b51d889..4fa49c31 100644 --- a/core/hakmem_elo.c +++ b/core/hakmem_elo.c @@ -71,10 +71,9 @@ void hak_elo_init(void) { g_current_strategy = g_num_strategies / 2; g_initialized = 1; - const char* q = getenv("HAKMEM_QUIET"); - if (!(q && strcmp(q, "1") == 0)) { - fprintf(stderr, "[ELO] Initialized %d strategies (thresholds: 512KB-32MB)\n", g_num_strategies); - } +#if !HAKMEM_BUILD_RELEASE + fprintf(stderr, "[ELO] Initialized %d strategies (thresholds: 512KB-32MB)\n", g_num_strategies); +#endif } // Shutdown ELO system @@ -189,7 +188,9 @@ void hak_elo_update_ratings(EloStrategyCandidate* a, EloStrategyCandidate* b, do void hak_elo_trigger_evolution(void) { if (!g_initialized) return; - { const char* q = getenv("HAKMEM_QUIET"); if (!(q && strcmp(q, "1") == 0)) fprintf(stderr, "[ELO] Triggering evolution (pairwise comparison)...\n"); } +#if !HAKMEM_BUILD_RELEASE + fprintf(stderr, "[ELO] Triggering evolution (pairwise comparison)...\n"); +#endif // Count active strategies with enough samples int eligible[ELO_MAX_STRATEGIES]; @@ -308,3 +309,5 @@ void hak_elo_print_leaderboard(void) { s->active ? "ACTIVE" : "eliminated"); } } } +// Release-silent logging +#include "hakmem_internal.h" diff --git a/core/hakmem_internal.h b/core/hakmem_internal.h index 4b859dc9..0a915ed4 100644 --- a/core/hakmem_internal.h +++ b/core/hakmem_internal.h @@ -38,7 +38,7 @@ extern int g_ldpreload_mode; // Debug: make debug → Logs enabled (unless HAKMEM_QUIET=1) // Debug quiet: HAKMEM_QUIET=1 ... → Logs suppressed at runtime -#ifdef HAKMEM_DEBUG_VERBOSE +#if HAKMEM_DEBUG_VERBOSE // Debug build: Check HAKMEM_QUIET at runtime #define HAKMEM_LOG(fmt, ...) do { \ static int quiet_checked = 0; \ diff --git a/core/hakmem_site_rules.c b/core/hakmem_site_rules.c index df0bb0c2..32f3781e 100644 --- a/core/hakmem_site_rules.c +++ b/core/hakmem_site_rules.c @@ -9,6 +9,7 @@ #include #include #include +#include "hakmem_internal.h" // =========================================================================== // Internal Data Structures @@ -88,7 +89,9 @@ void hak_site_rules_init(void) { memset(&g_site_rules, 0, sizeof(g_site_rules)); g_site_rules.initialized = 1; - printf("[SiteRules] Initialized (MVP: 4-probe hash, capacity=%d)\n", SITE_RULES_CAPACITY); + #if !HAKMEM_BUILD_RELEASE + fprintf(stderr, "[SiteRules] Initialized (MVP: 4-probe hash, capacity=%d)\n", SITE_RULES_CAPACITY); + #endif } void hak_site_rules_shutdown(void) { diff --git a/core/hakmem_tiny.h b/core/hakmem_tiny.h index 6e1a25ff..cbfe6f14 100644 --- a/core/hakmem_tiny.h +++ b/core/hakmem_tiny.h @@ -23,7 +23,7 @@ int hak_is_initializing(void); #define TINY_NUM_CLASSES 8 #define TINY_SLAB_SIZE (64 * 1024) // 64KB per slab -#define TINY_MAX_SIZE 1536 // Maximum allocation size (1.5KB, accommodate 1024B + header) +#define TINY_MAX_SIZE 1024 // Tiny handles up to 1024B (C7 headerless) // ============================================================================ // Size Classes @@ -236,18 +236,17 @@ void hkm_ace_set_drain_threshold(int class_idx, uint32_t threshold); // Convert size to class index (branchless lookup) // Quick Win #4: 2-3 cycles (table lookup) vs 5 cycles (branch chain) static inline int hak_tiny_size_to_class(size_t size) { - if (size == 0 || size > TINY_MAX_SIZE) return -1; + if (size == 0) return -1; #if HAKMEM_TINY_HEADER_CLASSIDX - // Phase 7 header adds +1 byte. Special-case 1024B to remain in Tiny (no header). - // Rationale: Avoid forcing 1024B to Mid/OS which causes frequent mmap/madvise. - if (size == TINY_MAX_SIZE) { - return g_size_to_class_lut_1k[size]; // class 7 (1024B blocks) - } - size_t alloc_size = size + 1; // Add header for other sizes - if (alloc_size > TINY_MAX_SIZE) return -1; + // C7: 1024B is headerless and maps directly to class 7 + if (size == 1024) return g_size_to_class_lut_1k[1024]; + // Other sizes must fit with +1 header within 1..1024 range + size_t alloc_size = size + 1; // header byte + if (alloc_size < 1 || alloc_size > 1024) return -1; return g_size_to_class_lut_1k[alloc_size]; #else - return g_size_to_class_lut_1k[size]; // 1..1024: single load + if (size > 1024) return -1; + return g_size_to_class_lut_1k[size]; // 1..1024 #endif } diff --git a/core/hakmem_tiny_refill_p0.inc.h b/core/hakmem_tiny_refill_p0.inc.h index d666a102..9b64893b 100644 --- a/core/hakmem_tiny_refill_p0.inc.h +++ b/core/hakmem_tiny_refill_p0.inc.h @@ -300,6 +300,7 @@ static inline int sll_refill_batch_from_ss(int class_idx, int max_take) { : tiny_slab_base_for(tls->ss, tls->slab_idx); // Diagnostic log (one-shot) + #if !HAKMEM_BUILD_RELEASE static _Atomic int g_carve_log_printed = 0; if (atomic_load(&g_carve_log_printed) == 0 && atomic_exchange(&g_carve_log_printed, 1) == 0) { @@ -308,6 +309,7 @@ static inline int sll_refill_batch_from_ss(int class_idx, int max_take) { (void*)slab_base, bs); fflush(stderr); } + #endif TinyRefillChain carve; trc_linear_carve(slab_base, bs, meta, batch, class_idx, &carve); diff --git a/core/hakmem_tiny_superslab.c b/core/hakmem_tiny_superslab.c index 36959602..84f4428d 100644 --- a/core/hakmem_tiny_superslab.c +++ b/core/hakmem_tiny_superslab.c @@ -16,6 +16,7 @@ #include #include // getrlimit for OOM diagnostics #include +#include "hakmem_internal.h" // HAKMEM_LOG for release-silent logging static int g_ss_force_lg = -1; static _Atomic int g_ss_populate_once = 0; @@ -166,6 +167,7 @@ static void log_superslab_oom_once(size_t ss_size, size_t alloc_size, int err) { snprintf(rl_max_buf, sizeof(rl_max_buf), "%llu", (unsigned long long)rl.rlim_max); } +#if !HAKMEM_BUILD_RELEASE fprintf(stderr, "[SS OOM] mmap failed: err=%d ss_size=%zu alloc_size=%zu " "alloc=%llu freed=%llu bytes=%llu " @@ -180,6 +182,7 @@ static void log_superslab_oom_once(size_t ss_size, size_t alloc_size, int err) { rl_max_buf, vm_size_kb, vm_rss_kb); +#endif g_hakmem_lock_depth--; // Now safe to restore (all libc calls complete) } @@ -229,11 +232,13 @@ static void* ss_os_acquire(uint8_t size_class, size_t ss_size, uintptr_t ss_mask -1, 0); if (raw != MAP_FAILED) { uint64_t count = atomic_fetch_add(&g_ss_mmap_count, 1) + 1; + #if !HAKMEM_BUILD_RELEASE if (log_count < 10) { fprintf(stderr, "[SUPERSLAB_MMAP] #%lu: class=%d size=%zu (total SuperSlab mmaps so far)\n", (unsigned long)count, size_class, ss_size); log_count++; } + #endif } if (raw == MAP_FAILED) { log_superslab_oom_once(ss_size, alloc_size, errno); @@ -562,8 +567,10 @@ SuperSlabHead* init_superslab_head(int class_idx) { extern __thread int g_hakmem_lock_depth; g_hakmem_lock_depth++; +#if !HAKMEM_BUILD_RELEASE fprintf(stderr, "[HAKMEM] Initialized SuperSlabHead for class %d: %zu initial chunks\n", class_idx, atomic_load_explicit(&head->total_chunks, memory_order_relaxed)); +#endif g_hakmem_lock_depth--; return head; @@ -745,13 +752,14 @@ void superslab_init_slab(SuperSlab* ss, int slab_idx, size_t block_size, uint32_ static _Atomic int g_cap_log_printed = 0; if (atomic_load(&g_cap_log_printed) == 0 && atomic_exchange(&g_cap_log_printed, 1) == 0) { +#if !HAKMEM_BUILD_RELEASE fprintf(stderr, "[SUPERSLAB_INIT] class 7 slab 0: usable_size=%zu stride=%zu capacity=%d\n", usable_size, stride, capacity); fprintf(stderr, "[SUPERSLAB_INIT] Expected: 63488 / 1024 = 62 blocks\n"); if (capacity != 62) { fprintf(stderr, "[SUPERSLAB_INIT] WARNING: capacity=%d (expected 62!)\n", capacity); } - fflush(stderr); +#endif } } diff --git a/core/link_stubs.c b/core/link_stubs.c index 18866ee2..3a3a41e4 100644 --- a/core/link_stubs.c +++ b/core/link_stubs.c @@ -1,5 +1,6 @@ #include #include +#include // Weak, no-op stubs to satisfy link in configurations where // optional components are compiled out or gated by flags. @@ -25,3 +26,11 @@ __attribute__((weak)) void pool_free(void* ptr) { // Fallback to free if Pool TLS not linked free(ptr); } + +// Pool TLS registry lookup stub (used by Front Gate classifier when POOL_TLS is enabled) +__attribute__((weak)) int pool_reg_lookup(void* ptr, pid_t* tid_out, int* class_idx_out) { + (void)ptr; if (tid_out) *tid_out = 0; if (class_idx_out) *class_idx_out = -1; return 0; // not found +} + +// Memory profile print stub (bench_comprehensive references this symbol) +__attribute__((weak)) void hak_tiny_print_memory_profile(void) {} diff --git a/core/tiny_refill_opt.h b/core/tiny_refill_opt.h index 754c8305..f023841c 100644 --- a/core/tiny_refill_opt.h +++ b/core/tiny_refill_opt.h @@ -102,10 +102,12 @@ static inline int trc_refill_guard_enabled(void) { // Debug: Default ON, but allow explicit disable g_trc_guard = (env && *env) ? ((*env != '0') ? 1 : 0) : 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"); fflush(stderr); + #endif } return g_trc_guard; } diff --git a/core/tiny_superslab_free.inc.h b/core/tiny_superslab_free.inc.h index c5feb3d9..d3251764 100644 --- a/core/tiny_superslab_free.inc.h +++ b/core/tiny_superslab_free.inc.h @@ -32,7 +32,9 @@ static inline void hak_tiny_free_superslab(void* ptr, SuperSlab* ss) { static _Atomic int c7_free_count = 0; int count = atomic_fetch_add_explicit(&c7_free_count, 1, memory_order_relaxed); if (count == 0) { + #if !HAKMEM_BUILD_RELEASE fprintf(stderr, "[C7_FIRST_FREE] ptr=%p base=%p slab_idx=%d\n", ptr, base, slab_idx); + #endif } } if (__builtin_expect(tiny_remote_watch_is(ptr), 0)) { diff --git a/hakmem_ace.d b/hakmem_ace.d index 73f23c9b..e56ee490 100644 --- a/hakmem_ace.d +++ b/hakmem_ace.d @@ -1,6 +1,15 @@ -hakmem_ace.o: core/hakmem_ace.c core/hakmem_ace.h core/hakmem_policy.h \ - core/hakmem_pool.h core/hakmem_l25_pool.h core/hakmem_ace_stats.h \ - core/hakmem_debug.h +hakmem_ace.o: core/hakmem_ace.c core/hakmem_internal.h core/hakmem.h \ + core/hakmem_build_flags.h core/hakmem_config.h core/hakmem_features.h \ + core/hakmem_sys.h core/hakmem_whale.h core/hakmem_ace.h \ + core/hakmem_policy.h core/hakmem_pool.h core/hakmem_l25_pool.h \ + core/hakmem_ace_stats.h core/hakmem_debug.h +core/hakmem_internal.h: +core/hakmem.h: +core/hakmem_build_flags.h: +core/hakmem_config.h: +core/hakmem_features.h: +core/hakmem_sys.h: +core/hakmem_whale.h: core/hakmem_ace.h: core/hakmem_policy.h: core/hakmem_pool.h: diff --git a/hakmem_batch.d b/hakmem_batch.d index 339e474f..03ecbb2d 100644 --- a/hakmem_batch.d +++ b/hakmem_batch.d @@ -1,5 +1,11 @@ hakmem_batch.o: core/hakmem_batch.c core/hakmem_batch.h core/hakmem_sys.h \ - core/hakmem_whale.h + core/hakmem_whale.h core/hakmem_internal.h core/hakmem.h \ + core/hakmem_build_flags.h core/hakmem_config.h core/hakmem_features.h core/hakmem_batch.h: core/hakmem_sys.h: core/hakmem_whale.h: +core/hakmem_internal.h: +core/hakmem.h: +core/hakmem_build_flags.h: +core/hakmem_config.h: +core/hakmem_features.h: diff --git a/hakmem_config.d b/hakmem_config.d index 408065f4..cc610fd6 100644 --- a/hakmem_config.d +++ b/hakmem_config.d @@ -1,4 +1,10 @@ hakmem_config.o: core/hakmem_config.c core/hakmem_config.h \ - core/hakmem_features.h + core/hakmem_features.h core/hakmem_internal.h core/hakmem.h \ + core/hakmem_build_flags.h core/hakmem_sys.h core/hakmem_whale.h core/hakmem_config.h: core/hakmem_features.h: +core/hakmem_internal.h: +core/hakmem.h: +core/hakmem_build_flags.h: +core/hakmem_sys.h: +core/hakmem_whale.h: diff --git a/hakmem_elo.d b/hakmem_elo.d index bf8eff45..70e18062 100644 --- a/hakmem_elo.d +++ b/hakmem_elo.d @@ -1,2 +1,11 @@ -hakmem_elo.o: core/hakmem_elo.c core/hakmem_elo.h +hakmem_elo.o: core/hakmem_elo.c core/hakmem_elo.h core/hakmem_internal.h \ + core/hakmem.h core/hakmem_build_flags.h core/hakmem_config.h \ + core/hakmem_features.h core/hakmem_sys.h core/hakmem_whale.h core/hakmem_elo.h: +core/hakmem_internal.h: +core/hakmem.h: +core/hakmem_build_flags.h: +core/hakmem_config.h: +core/hakmem_features.h: +core/hakmem_sys.h: +core/hakmem_whale.h: diff --git a/hakmem_site_rules.d b/hakmem_site_rules.d index 13d97f1d..aad5f77f 100644 --- a/hakmem_site_rules.d +++ b/hakmem_site_rules.d @@ -1,4 +1,13 @@ hakmem_site_rules.o: core/hakmem_site_rules.c core/hakmem_site_rules.h \ - core/hakmem_pool.h + core/hakmem_pool.h core/hakmem_internal.h core/hakmem.h \ + core/hakmem_build_flags.h core/hakmem_config.h core/hakmem_features.h \ + core/hakmem_sys.h core/hakmem_whale.h core/hakmem_site_rules.h: core/hakmem_pool.h: +core/hakmem_internal.h: +core/hakmem.h: +core/hakmem_build_flags.h: +core/hakmem_config.h: +core/hakmem_features.h: +core/hakmem_sys.h: +core/hakmem_whale.h: diff --git a/hakmem_tiny_superslab.d b/hakmem_tiny_superslab.d index b13b1987..570be1d2 100644 --- a/hakmem_tiny_superslab.d +++ b/hakmem_tiny_superslab.d @@ -7,7 +7,9 @@ hakmem_tiny_superslab.o: core/hakmem_tiny_superslab.c \ core/superslab/../hakmem_tiny_config.h core/tiny_debug_ring.h \ core/tiny_remote.h core/hakmem_tiny_superslab_constants.h \ core/hakmem_build_flags.h core/hakmem_super_registry.h \ - core/hakmem_tiny.h core/hakmem_trace.h core/hakmem_tiny_mini_mag.h + core/hakmem_tiny.h core/hakmem_trace.h core/hakmem_tiny_mini_mag.h \ + core/hakmem_internal.h core/hakmem.h core/hakmem_config.h \ + core/hakmem_features.h core/hakmem_sys.h core/hakmem_whale.h core/hakmem_tiny_superslab.h: core/superslab/superslab_types.h: core/hakmem_tiny_superslab_constants.h: @@ -26,3 +28,9 @@ core/hakmem_super_registry.h: core/hakmem_tiny.h: core/hakmem_trace.h: core/hakmem_tiny_mini_mag.h: +core/hakmem_internal.h: +core/hakmem.h: +core/hakmem_config.h: +core/hakmem_features.h: +core/hakmem_sys.h: +core/hakmem_whale.h: