Files
hakmem/core/box/ss_cache_box.h
Moe Charm (CI) 9b0d746407 Phase 3d-B: TLS Cache Merge - Unified g_tls_sll[] structure (+12-18% expected)
Merge separate g_tls_sll_head[] and g_tls_sll_count[] arrays into unified
TinyTLSSLL struct to improve L1D cache locality. Expected performance gain:
+12-18% from reducing cache line splits (2 loads → 1 load per operation).

Changes:
- core/hakmem_tiny.h: Add TinyTLSSLL type (16B aligned, head+count+pad)
- core/hakmem_tiny.c: Replace separate arrays with g_tls_sll[8]
- core/box/tls_sll_box.h: Update Box API (13 sites) for unified access
- Updated 32+ files: All g_tls_sll_head[i] → g_tls_sll[i].head
- Updated 32+ files: All g_tls_sll_count[i] → g_tls_sll[i].count
- core/hakmem_tiny_integrity.h: Unified canary guards
- core/box/integrity_box.c: Simplified canary validation
- Makefile: Added core/box/tiny_sizeclass_hist_box.o to link

Build:  PASS (10K ops sanity test)
Warnings: Only pre-existing LTO type mismatches (unrelated)

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude <noreply@anthropic.com>
2025-11-20 07:32:30 +09:00

87 lines
3.3 KiB
C

// ss_cache_box.h - SuperSlab Cache Management Box
// Purpose: LRU cache and prewarm/precharge cache for SuperSlab reuse
// Box Theory: Lazy deallocation strategy to minimize mmap/munmap syscalls
//
// Responsibilities:
// - Per-class SuperSlab cache (prewarm/precharge)
// - Cache initialization and configuration
// - Runtime tuning API (learner integration)
// - Cache hit/miss statistics
//
// Dependencies: ss_os_acquire_box (for precharge allocation)
//
// License: MIT
// Date: 2025-11-19
#ifndef HAKMEM_SS_CACHE_BOX_H
#define HAKMEM_SS_CACHE_BOX_H
#include <stdint.h>
#include <stddef.h>
#include "../superslab/superslab_types.h"
// ============================================================================
// Cache Statistics (external visibility for monitoring)
// ============================================================================
extern uint64_t g_ss_cache_hits[8]; // Cache hits per class
extern uint64_t g_ss_cache_misses[8]; // Cache misses per class
extern uint64_t g_ss_cache_puts[8]; // Cache stores per class
extern uint64_t g_ss_cache_drops[8]; // Cache evictions per class
extern uint64_t g_ss_cache_precharged[8]; // Precharge count per class
// ============================================================================
// Cache Management API
// ============================================================================
// Initialize cache system (called once per process)
// Thread-safe: pthread_once protected
void ss_cache_ensure_init(void);
// Pop SuperSlab from cache (returns NULL if cache empty)
// Thread-safe: mutex protected
// Returns: SuperSlab pointer (cast from SuperslabCacheEntry) or NULL
void* ss_cache_pop(uint8_t size_class);
// Push SuperSlab to cache (for lazy deallocation)
// Thread-safe: mutex protected
// Returns: 1 if cached, 0 if cache full (caller should munmap)
int ss_cache_push(uint8_t size_class, SuperSlab* ss);
// Precharge cache with N SuperSlabs (startup optimization)
// Thread-safe: mutex protected, one-shot per class
// Populates cache to reduce first-allocation latency
void ss_cache_precharge(uint8_t size_class, size_t ss_size, uintptr_t ss_mask);
// Cache capacity and precharge target arrays (for direct access from allocation box)
extern size_t g_ss_cache_cap[8];
extern size_t g_ss_precharge_target[8];
// ============================================================================
// Runtime Tuning API (Learner Integration)
// ============================================================================
// Set per-class cache capacity (runtime tunable)
// If new_cap < old_cap, excess cached SuperSlabs are munmapped
// Thread-safe: mutex protected
//
// Parameters:
// class_idx: Tiny class (0..7)
// new_cap: Maximum cached SuperSlabs for this class (0 = disable cache)
//
// Used by: TinyPageAuto learner for adaptive cache sizing
void tiny_ss_cache_set_class_cap(int class_idx, size_t new_cap);
// Set per-class precharge target (runtime tunable)
// If target > 0, precharge will run on next allocation
// Thread-safe: mutex protected
//
// Parameters:
// class_idx: Tiny class (0..7)
// target: Number of SuperSlabs to precharge (0 = disable precharge)
//
// Used by: TinyPageAuto learner based on PageFaultTelemetry
void tiny_ss_precharge_set_class_target(int class_idx, size_t target);
#endif // HAKMEM_SS_CACHE_BOX_H