Phase 70-0: Infrastructure prep for refill tuning (Observability + WarmPool Sizing)

- Observability Fix: Enabled unified cache hit/miss stats in release builds when HAKMEM_UNIFIED_CACHE_STATS_COMPILED is set.
- WarmPool Sizing: Decoupled hardcoded '12' from prefill logic; now uses TINY_WARM_POOL_DEFAULT_PER_CLASS macro and respects ENV overrides.
- Increased TINY_WARM_POOL_MAX_PER_CLASS to 32 to support wider ENV sweeps.
- Added unified_cache_auto_stats destructor to dump metrics at exit (replacing debug print hack).
This commit is contained in:
Moe Charm (CI)
2025-12-18 03:02:40 +09:00
parent b6212bbe31
commit a6ab262ad2
5 changed files with 32 additions and 26 deletions

View File

@ -92,7 +92,7 @@ extern __thread TinyUnifiedCache g_unified_cache[TINY_NUM_CLASSES];
// Metrics (Phase 23, optional for debugging)
// ============================================================================
#if !HAKMEM_BUILD_RELEASE
#if !HAKMEM_BUILD_RELEASE || HAKMEM_UNIFIED_CACHE_STATS_COMPILED
extern __thread uint64_t g_unified_cache_hit[TINY_NUM_CLASSES]; // Alloc hits
extern __thread uint64_t g_unified_cache_miss[TINY_NUM_CLASSES]; // Alloc misses
extern __thread uint64_t g_unified_cache_push[TINY_NUM_CLASSES]; // Free pushes
@ -143,7 +143,7 @@ static inline size_t unified_capacity(int class_idx) {
while (pow2 < g_cap[class_idx]) pow2 *= 2;
g_cap[class_idx] = pow2;
#if !HAKMEM_BUILD_RELEASE
#if !HAKMEM_BUILD_RELEASE || HAKMEM_UNIFIED_CACHE_STATS_COMPILED
fprintf(stderr, "[Unified-INIT] C%d capacity = %zu (power of 2)\n", class_idx, g_cap[class_idx]);
fflush(stderr);
#endif
@ -197,7 +197,7 @@ static inline hak_base_ptr_t unified_cache_pop(int class_idx) {
// Empty check
if (__builtin_expect(cache->head == cache->tail, 0)) {
#if !HAKMEM_BUILD_RELEASE
#if !HAKMEM_BUILD_RELEASE || HAKMEM_UNIFIED_CACHE_STATS_COMPILED
g_unified_cache_miss[class_idx]++;
#endif
return HAK_BASE_FROM_RAW(NULL); // Empty
@ -207,7 +207,7 @@ static inline hak_base_ptr_t unified_cache_pop(int class_idx) {
void* base = cache->slots[cache->head]; // 1 cache miss (array access)
cache->head = (cache->head + 1) & cache->mask; // Fast modulo (power of 2)
#if !HAKMEM_BUILD_RELEASE
#if !HAKMEM_BUILD_RELEASE || HAKMEM_UNIFIED_CACHE_STATS_COMPILED
g_unified_cache_hit[class_idx]++;
#endif
@ -251,7 +251,7 @@ static inline int unified_cache_push(int class_idx, hak_base_ptr_t base) {
// Full check (leave 1 slot empty to distinguish full/empty)
if (__builtin_expect(next_tail == cache->head, 0)) {
#if !HAKMEM_BUILD_RELEASE
#if !HAKMEM_BUILD_RELEASE || HAKMEM_UNIFIED_CACHE_STATS_COMPILED
g_unified_cache_full[class_idx]++;
#endif
return 0; // Full
@ -261,7 +261,7 @@ static inline int unified_cache_push(int class_idx, hak_base_ptr_t base) {
cache->slots[cache->tail] = base_raw; // 1 cache miss (array write)
cache->tail = next_tail;
#if !HAKMEM_BUILD_RELEASE
#if !HAKMEM_BUILD_RELEASE || HAKMEM_UNIFIED_CACHE_STATS_COMPILED
g_unified_cache_push[class_idx]++;
#endif
@ -300,7 +300,7 @@ static inline hak_base_ptr_t unified_cache_pop_or_refill(int class_idx) {
// Phase 22: Compile-out when disabled (default OFF)
void* tcache_base = tiny_tcache_try_pop(class_idx);
if (tcache_base != NULL) {
#if !HAKMEM_BUILD_RELEASE
#if !HAKMEM_BUILD_RELEASE || HAKMEM_UNIFIED_CACHE_STATS_COMPILED
g_unified_cache_hit[class_idx]++;
#endif
#if HAKMEM_TINY_UNIFIED_CACHE_MEASURE_COMPILED
@ -320,7 +320,7 @@ static inline hak_base_ptr_t unified_cache_pop_or_refill(int class_idx) {
if (__builtin_expect(cache->head != cache->tail, 1)) {
void* base = cache->slots[cache->head]; // 1 cache miss (array access)
cache->head = (cache->head + 1) & cache->mask;
#if !HAKMEM_BUILD_RELEASE
#if !HAKMEM_BUILD_RELEASE || HAKMEM_UNIFIED_CACHE_STATS_COMPILED
g_unified_cache_hit[class_idx]++;
#endif
#if HAKMEM_TINY_UNIFIED_CACHE_MEASURE_COMPILED
@ -337,7 +337,7 @@ static inline hak_base_ptr_t unified_cache_pop_or_refill(int class_idx) {
}
// Cache miss → Batch refill from SuperSlab
#if !HAKMEM_BUILD_RELEASE
#if !HAKMEM_BUILD_RELEASE || HAKMEM_UNIFIED_CACHE_STATS_COMPILED
g_unified_cache_miss[class_idx]++;
#endif
return unified_cache_refill(class_idx); // Refill + return first block (BASE)