diff --git a/core/hakmem_shared_pool.c b/core/hakmem_shared_pool.c index 92beb38c..89ac6c16 100644 --- a/core/hakmem_shared_pool.c +++ b/core/hakmem_shared_pool.c @@ -542,12 +542,17 @@ sp_acquire_from_empty_scan(int class_idx, SuperSlab** ss_out, int* slab_idx_out, int reg_size = (class_idx < TINY_NUM_CLASSES) ? g_super_reg_class_size[class_idx] : 0; static int scan_limit = -1; - if (__builtin_expect(scan_limit == -1, 0)) { - const char* e = getenv("HAKMEM_SS_EMPTY_SCAN_LIMIT"); - scan_limit = (e && *e) ? atoi(e) : 16; // default: scan first 16 SuperSlabs - } + if (__builtin_expect(scan_limit == -1, 0)) { + const char* e = getenv("HAKMEM_SS_EMPTY_SCAN_LIMIT"); + scan_limit = (e && *e) ? atoi(e) : 32; // default: scan first 32 SuperSlabs (Phase 9-2 tuning) + } if (scan_limit > reg_size) scan_limit = reg_size; + // Stage 0.5 hit counter for visualization + static _Atomic uint64_t stage05_hits = 0; + static _Atomic uint64_t stage05_attempts = 0; + atomic_fetch_add_explicit(&stage05_attempts, 1, memory_order_relaxed); + for (int i = 0; i < scan_limit; i++) { SuperSlab* ss = g_super_reg_by_class[class_idx][i]; if (!(ss && ss->magic == SUPERSLAB_MAGIC)) continue; @@ -582,6 +587,15 @@ sp_acquire_from_empty_scan(int class_idx, SuperSlab** ss_out, int* slab_idx_out, if (g_sp_stage_stats_enabled) { atomic_fetch_add(&g_sp_stage1_hits[class_idx], 1); } + atomic_fetch_add_explicit(&stage05_hits, 1, memory_order_relaxed); + + // Stage 0.5 hit rate visualization (every 100 hits) + uint64_t hits = atomic_load_explicit(&stage05_hits, memory_order_relaxed); + if (hits % 100 == 1) { + uint64_t attempts = atomic_load_explicit(&stage05_attempts, memory_order_relaxed); + fprintf(stderr, "[STAGE0.5_STATS] hits=%lu attempts=%lu rate=%.1f%% (scan_limit=%d)\n", + hits, attempts, (double)hits * 100.0 / attempts, scan_limit); + } return 0; } }