// front_metrics_box.c - Box FrontMetrics Implementation // Purpose: Collect and report frontend layer hit rates #include "front_metrics_box.h" #include "../hakmem_tiny_stats_api.h" #include "../hakmem_stats_master.h" // Phase 4d: Master stats control #include #include #include // ============================================================================ // Per-thread counters (NEW - declared in header, defined here) // ============================================================================ __thread uint64_t g_front_ultrahot_hit[TINY_NUM_CLASSES] = {0}; __thread uint64_t g_front_ultrahot_miss[TINY_NUM_CLASSES] = {0}; __thread uint64_t g_front_heapv2_hit[TINY_NUM_CLASSES] = {0}; __thread uint64_t g_front_heapv2_miss[TINY_NUM_CLASSES] = {0}; __thread uint64_t g_front_class5_hit[TINY_NUM_CLASSES] = {0}; __thread uint64_t g_front_class5_miss[TINY_NUM_CLASSES] = {0}; // ============================================================================ // Existing counters (defined in hakmem_tiny.c, extern here for reading) // ============================================================================ extern unsigned long long g_front_fc_hit[TINY_NUM_CLASSES]; extern unsigned long long g_front_fc_miss[TINY_NUM_CLASSES]; extern unsigned long long g_front_sfc_hit[TINY_NUM_CLASSES]; extern unsigned long long g_front_sll_hit[TINY_NUM_CLASSES]; // ============================================================================ // Enable flag (cached) // ============================================================================ int front_metrics_enabled(void) { static int g_enabled = -1; if (__builtin_expect(g_enabled == -1, 0)) { #if HAKMEM_BUILD_RELEASE g_enabled = 0; #else const char* env = getenv("HAKMEM_TINY_FRONT_METRICS"); g_enabled = (env && *env && *env != '0') ? 1 : 0; #endif } return g_enabled; } // ============================================================================ // Dump frontend metrics (CSV format) // ============================================================================ void hak_tiny_front_metrics_dump(void) { if (!front_metrics_enabled()) { return; } #if !HAKMEM_BUILD_RELEASE if (!hak_stats_check("HAKMEM_TINY_FRONT_DUMP", "front")) { return; } #endif fprintf(stderr, "\n========== Box FrontMetrics: Layer Hit Rates ==========\n"); fprintf(stderr, "Purpose: Identify which frontend layers are doing real work\n"); fprintf(stderr, "Legend: UH=UltraHot, HV2=HeapV2, C5=Class5, FC=FastCache, SFC=SuperFrontCache, SLL=TLS_SLL\n\n"); fprintf(stderr, "%-5s %10s %10s %10s %10s %10s %10s %12s | %6s %6s %6s %6s %6s %6s\n", "Class", "UH_hit", "HV2_hit", "C5_hit", "FC_hit", "SFC_hit", "SLL_hit", "Total", "UH%", "HV2%", "C5%", "FC%", "SFC%", "SLL%"); fprintf(stderr, "------|----------|----------|----------|----------|----------|----------|-------------|"); fprintf(stderr, "-------|-------|-------|-------|-------|-------\n"); for (int cls = 0; cls < TINY_NUM_CLASSES; cls++) { uint64_t uh_hit = g_front_ultrahot_hit[cls]; uint64_t hv2_hit = g_front_heapv2_hit[cls]; uint64_t c5_hit = g_front_class5_hit[cls]; uint64_t fc_hit = g_front_fc_hit[cls]; uint64_t sfc_hit = g_front_sfc_hit[cls]; uint64_t sll_hit = g_front_sll_hit[cls]; uint64_t total = uh_hit + hv2_hit + c5_hit + fc_hit + sfc_hit + sll_hit; if (total == 0) { fprintf(stderr, "C%-4d %10s %10s %10s %10s %10s %10s %12s | %6s %6s %6s %6s %6s %6s\n", cls, "-", "-", "-", "-", "-", "-", "-", "-", "-", "-", "-", "-", "-"); continue; } double uh_pct = (double)uh_hit / total * 100.0; double hv2_pct = (double)hv2_hit / total * 100.0; double c5_pct = (double)c5_hit / total * 100.0; double fc_pct = (double)fc_hit / total * 100.0; double sfc_pct = (double)sfc_hit / total * 100.0; double sll_pct = (double)sll_hit / total * 100.0; fprintf(stderr, "C%-4d %10lu %10lu %10lu %10lu %10lu %10lu %12lu | %5.1f%% %5.1f%% %5.1f%% %5.1f%% %5.1f%% %5.1f%%\n", cls, (unsigned long)uh_hit, (unsigned long)hv2_hit, (unsigned long)c5_hit, (unsigned long)fc_hit, (unsigned long)sfc_hit, (unsigned long)sll_hit, (unsigned long)total, uh_pct, hv2_pct, c5_pct, fc_pct, sfc_pct, sll_pct); } fprintf(stderr, "=======================================================\n\n"); // Analysis recommendations fprintf(stderr, "Analysis Recommendations:\n"); fprintf(stderr, " - Layers with >80%% hit rate: Keep and optimize (hot path)\n"); fprintf(stderr, " - Layers with <5%% hit rate: Consider pruning (dead weight)\n"); fprintf(stderr, " - Multiple layers >20%%: Potential redundancy, test pruning\n\n"); } // Register dump at shutdown static void front_metrics_atexit(void) __attribute__((destructor)); static void front_metrics_atexit(void) { hak_tiny_front_metrics_dump(); }