// superslab_stats.c - Statistics and debugging for SuperSlab allocator // Purpose: Tracking and reporting allocation statistics // License: MIT // Date: 2025-11-28 #include "hakmem_tiny_superslab_internal.h" // ============================================================================ // Global Statistics // ============================================================================ pthread_mutex_t g_superslab_lock = PTHREAD_MUTEX_INITIALIZER; uint64_t g_superslabs_allocated = 0; // Non-static for debugging uint64_t g_superslabs_freed = 0; // Phase 7.6: Non-static for test access uint64_t g_bytes_allocated = 0; // Non-static for debugging // Debug counters _Atomic uint64_t g_ss_active_dec_calls = 0; _Atomic uint64_t g_hak_tiny_free_calls = 0; _Atomic uint64_t g_ss_remote_push_calls = 0; // Free path instrumentation (lightweight, for OOM/route diagnosis) _Atomic uint64_t g_free_ss_enter = 0; // hak_tiny_free_superslab() entries _Atomic uint64_t g_free_local_box_calls = 0; // same-thread freelist pushes _Atomic uint64_t g_free_remote_box_calls = 0; // cross-thread remote pushes // Per-class counters for gating/metrics (Tiny classes = 8) uint64_t g_ss_alloc_by_class[8] = {0}; uint64_t g_ss_freed_by_class[8] = {0}; // Global counters for debugging (non-static for external access) _Atomic uint64_t g_ss_mmap_count = 0; _Atomic uint64_t g_final_fallback_mmap_count = 0; // ============================================================================ // Statistics Functions // ============================================================================ void ss_stats_os_alloc(uint8_t size_class, size_t ss_size) { pthread_mutex_lock(&g_superslab_lock); g_superslabs_allocated++; if (size_class < 8) { g_ss_alloc_by_class[size_class]++; } g_bytes_allocated += ss_size; pthread_mutex_unlock(&g_superslab_lock); } void ss_stats_cache_reuse(void) { pthread_mutex_lock(&g_superslab_lock); g_superslabs_reused++; pthread_mutex_unlock(&g_superslab_lock); } void ss_stats_cache_store(void) { pthread_mutex_lock(&g_superslab_lock); g_superslabs_cached++; pthread_mutex_unlock(&g_superslab_lock); } // ============================================================================ // Diagnostics // ============================================================================ void log_superslab_oom_once(size_t ss_size, size_t alloc_size, int err) { static int logged = 0; if (logged) return; logged = 1; // CRITICAL FIX: Increment lock depth FIRST before any LIBC calls // fopen/fclose/getrlimit/fprintf all may call malloc internally // Must bypass HAKMEM wrapper to avoid header mismatch crash extern __thread int g_hakmem_lock_depth; g_hakmem_lock_depth++; // Force wrapper to use __libc_malloc struct rlimit rl = {0}; if (getrlimit(RLIMIT_AS, &rl) != 0) { rl.rlim_cur = RLIM_INFINITY; rl.rlim_max = RLIM_INFINITY; } unsigned long vm_size_kb = 0; unsigned long vm_rss_kb = 0; FILE* status = fopen("/proc/self/status", "r"); if (status) { char line[256]; while (fgets(line, sizeof(line), status)) { if (strncmp(line, "VmSize:", 7) == 0) { (void)sscanf(line + 7, "%lu", &vm_size_kb); } else if (strncmp(line, "VmRSS:", 6) == 0) { (void)sscanf(line + 6, "%lu", &vm_rss_kb); } } fclose(status); } // CRITICAL FIX: Do NOT decrement lock_depth yet! // fprintf() below may call malloc for buffering char rl_cur_buf[32]; char rl_max_buf[32]; if (rl.rlim_cur == RLIM_INFINITY) { strcpy(rl_cur_buf, "inf"); } else { snprintf(rl_cur_buf, sizeof(rl_cur_buf), "%llu", (unsigned long long)rl.rlim_cur); } if (rl.rlim_max == RLIM_INFINITY) { strcpy(rl_max_buf, "inf"); } else { 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 " "RLIMIT_AS(cur=%s max=%s) VmSize=%lu kB VmRSS=%lu kB\n", err, ss_size, alloc_size, (unsigned long long)g_superslabs_allocated, (unsigned long long)g_superslabs_freed, (unsigned long long)g_bytes_allocated, rl_cur_buf, rl_max_buf, vm_size_kb, vm_rss_kb); #endif g_hakmem_lock_depth--; // Now safe to restore (all libc calls complete) } // ============================================================================ // Statistics / Debugging // ============================================================================ void superslab_print_stats(SuperSlab* ss) { if (!ss || ss->magic != SUPERSLAB_MAGIC) { printf("Invalid SuperSlab\n"); return; } printf("=== SuperSlab Stats ===\n"); printf("Address: %p\n", (void*)ss); // Phase 12: per-SS size_class removed; classes are per-slab via meta->class_idx. printf("Active slabs: %u / %d\n", ss->active_slabs, ss_slabs_capacity(ss)); printf("Bitmap: 0x%08X\n", ss->slab_bitmap); printf("\nPer-slab details:\n"); for (int i = 0; i < ss_slabs_capacity(ss); i++) { if (ss->slab_bitmap & (1u << i)) { TinySlabMeta* meta = &ss->slabs[i]; printf(" Slab %2d: used=%u/%u freelist=%p class=%u owner_tid_low=%u\n", i, meta->used, meta->capacity, meta->freelist, (unsigned)meta->class_idx, (unsigned)meta->owner_tid_low); } } printf("\n"); } // Global statistics void superslab_print_global_stats(void) { pthread_mutex_lock(&g_superslab_lock); printf("=== Global SuperSlab Stats ===\n"); printf("SuperSlabs allocated: %lu\n", g_superslabs_allocated); printf("SuperSlabs freed: %lu\n", g_superslabs_freed); printf("SuperSlabs active: %lu\n", g_superslabs_allocated - g_superslabs_freed); printf("Total bytes allocated: %lu MB\n", g_bytes_allocated / (1024 * 1024)); pthread_mutex_unlock(&g_superslab_lock); }