diff --git a/core/box/ss_hot_cold_box.h b/core/box/ss_hot_cold_box.h new file mode 100644 index 00000000..d60960bb --- /dev/null +++ b/core/box/ss_hot_cold_box.h @@ -0,0 +1,96 @@ +// ss_hot_cold_box.h - Phase 3d-C: Hot/Cold Slab Split Box +// Purpose: Cache locality optimization via hot/cold slab separation +// License: MIT +// Date: 2025-11-20 + +#ifndef SS_HOT_COLD_BOX_H +#define SS_HOT_COLD_BOX_H + +#include "../superslab/superslab_types.h" +#include + +// ============================================================================ +// Phase 3d-C: Hot/Cold Split Box API +// ============================================================================ +// +// Goal: Improve L1D cache hit rate by separating hot (high utilization) and +// cold (low utilization) slabs within a SuperSlab. +// +// Strategy: +// - Hot slabs (used > 50%): Prioritized for allocation → better cache locality +// - Cold slabs (used ≤ 50%): Used as fallback → delayed deallocation +// +// Expected: +8-12% throughput from improved cache line locality +// +// Box Contract: +// - ss_is_slab_hot(): Returns true if slab should be considered "hot" +// - ss_update_hot_cold_indices(): Rebuilds hot/cold index arrays +// - ss_init_hot_cold(): Initializes hot/cold fields on SuperSlab creation +// +// ============================================================================ + +// Phase 3d-C: Hot/Cold判定閾値 +#define HOT_UTILIZATION_THRESHOLD 50 // 使用率50%以上でホット判定 + +// Phase 3d-C: Hot判定ロジック +// Returns: true if slab is "hot" (high utilization, should be prioritized) +static inline bool ss_is_slab_hot(const TinySlabMeta* meta) { + // ヒューリスティック: 使用率 > 50% → ホット + // 理由: 使用率が高い = 頻繁にアクセスされている = キャッシュに載せたい + if (meta->capacity == 0) { + return false; // Uninitialized slab + } + return (meta->used * 100 / meta->capacity) > HOT_UTILIZATION_THRESHOLD; +} + +// Phase 3d-C: Hot/Cold インデックス更新 +// Rebuilds hot_indices[] and cold_indices[] arrays based on current slab state +static inline void ss_update_hot_cold_indices(SuperSlab* ss) { + if (!ss) return; + + ss->hot_count = 0; + ss->cold_count = 0; + + uint32_t max_slabs = (1u << ss->lg_size) / SLAB_SIZE; + if (max_slabs > SLABS_PER_SUPERSLAB_MAX) { + max_slabs = SLABS_PER_SUPERSLAB_MAX; + } + + // Scan active slabs and classify as hot or cold + for (uint32_t i = 0; i < max_slabs && i < ss->active_slabs; i++) { + TinySlabMeta* meta = &ss->slabs[i]; + + // Skip uninitialized slabs (capacity == 0) + if (meta->capacity == 0) { + continue; + } + + if (ss_is_slab_hot(meta)) { + // Hot slab: high utilization + if (ss->hot_count < 16) { + ss->hot_indices[ss->hot_count++] = (uint8_t)i; + } + } else { + // Cold slab: low utilization + if (ss->cold_count < 16) { + ss->cold_indices[ss->cold_count++] = (uint8_t)i; + } + } + } +} + +// Phase 3d-C: SuperSlab初期化時にhot/cold fieldsをゼロクリア +static inline void ss_init_hot_cold(SuperSlab* ss) { + if (!ss) return; + + ss->hot_count = 0; + ss->cold_count = 0; + + // Initialize index arrays to 0 (defensive programming) + for (int i = 0; i < 16; i++) { + ss->hot_indices[i] = 0; + ss->cold_indices[i] = 0; + } +} + +#endif // SS_HOT_COLD_BOX_H diff --git a/core/hakmem_tiny_superslab.c b/core/hakmem_tiny_superslab.c index b374f14d..1f4efd01 100644 --- a/core/hakmem_tiny_superslab.c +++ b/core/hakmem_tiny_superslab.c @@ -4,6 +4,7 @@ // Date: 2025-10-24 #include "hakmem_tiny_superslab.h" +#include "box/ss_hot_cold_box.h" // Phase 3d-C: Hot/Cold Split #include "hakmem_super_registry.h" // Phase 1: Registry integration #include "hakmem_tiny.h" // For tiny_self_u32 #include "hakmem_tiny_config.h" // For extern g_tiny_class_sizes @@ -783,6 +784,14 @@ SuperSlab* superslab_allocate(uint8_t size_class) { ss->lru_prev = NULL; ss->lru_next = NULL; + // Phase 3d-C: Initialize Hot/Cold Split fields + ss->hot_count = 0; + ss->cold_count = 0; + for (int i = 0; i < 16; i++) { + ss->hot_indices[i] = 0; + ss->cold_indices[i] = 0; + } + // Initialize all slab metadata (only up to max slabs for this size) int max_slabs = (int)(ss_size / SLAB_SIZE); @@ -1116,6 +1125,9 @@ void superslab_activate_slab(SuperSlab* ss, int slab_idx) { if ((ss->slab_bitmap & mask) == 0) { ss->slab_bitmap |= mask; ss->active_slabs++; + + // Phase 3d-C: Update hot/cold indices after activating new slab + ss_update_hot_cold_indices(ss); } } diff --git a/core/superslab/superslab_types.h b/core/superslab/superslab_types.h index 8bf78590..67d46f42 100644 --- a/core/superslab/superslab_types.h +++ b/core/superslab/superslab_types.h @@ -78,6 +78,12 @@ typedef struct SuperSlab { _Atomic uint32_t remote_counts[SLABS_PER_SUPERSLAB_MAX]; _Atomic uint32_t slab_listed[SLABS_PER_SUPERSLAB_MAX]; + // Phase 3d-C: Hot/Cold Split - Cache locality optimization + uint8_t hot_count; // Number of hot slabs (high utilization) + uint8_t cold_count; // Number of cold slabs (low utilization) + uint8_t hot_indices[16]; // Indices of hot slabs (max 16) + uint8_t cold_indices[16]; // Indices of cold slabs (max 16) + // Per-slab metadata array TinySlabMeta slabs[SLABS_PER_SUPERSLAB_MAX]; } SuperSlab;