Phase 3d-C: Hot/Cold Slab Split - SuperSlab cache locality optimization (baseline established)

Goal: Improve L1D cache hit rate via hot/cold slab separation

Implementation:
- Added hot/cold fields to SuperSlab (superslab_types.h)
  - hot_indices[16] / cold_indices[16]: Index arrays for hot/cold slabs
  - hot_count / cold_count: Number of slabs in each category
- Created ss_hot_cold_box.h: Hot/Cold Split Box API
  - ss_is_slab_hot(): Utilization-based hot判定 (>50% usage)
  - ss_update_hot_cold_indices(): Rebuild index arrays on slab activation
  - ss_init_hot_cold(): Initialize fields on SuperSlab creation
- Updated hakmem_tiny_superslab.c:
  - Initialize hot/cold fields in superslab creation (line 786-792)
  - Update hot/cold indices on slab activation (line 1130)
  - Include ss_hot_cold_box.h (line 7)

Architecture:
- Strategy: Hot slabs (high utilization) prioritized for allocation
- Expected: +8-12% from improved cache line locality
- Note: Refill path optimization (hot優先スキャン) deferred to future commit

Testing:
- Build: Success (LTO warnings are pre-existing)
- 10K ops sanity test: PASS (1.4M ops/s)
- Baseline established for Phase C-8 benchmark comparison

Phase 3d sequence:
- Phase A: SlabMeta Box boundary (38552c3f3) 
- Phase B: TLS Cache Merge (9b0d74640) 
- Phase C: Hot/Cold Split (current) 

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

Co-Authored-By: Claude <noreply@anthropic.com>
This commit is contained in:
Moe Charm (CI)
2025-11-20 07:44:07 +09:00
parent 9b0d746407
commit 23c0d95410
3 changed files with 114 additions and 0 deletions

View File

@ -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 <stdbool.h>
// ============================================================================
// 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

View File

@ -4,6 +4,7 @@
// Date: 2025-10-24 // Date: 2025-10-24
#include "hakmem_tiny_superslab.h" #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_super_registry.h" // Phase 1: Registry integration
#include "hakmem_tiny.h" // For tiny_self_u32 #include "hakmem_tiny.h" // For tiny_self_u32
#include "hakmem_tiny_config.h" // For extern g_tiny_class_sizes #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_prev = NULL;
ss->lru_next = 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) // Initialize all slab metadata (only up to max slabs for this size)
int max_slabs = (int)(ss_size / SLAB_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) { if ((ss->slab_bitmap & mask) == 0) {
ss->slab_bitmap |= mask; ss->slab_bitmap |= mask;
ss->active_slabs++; ss->active_slabs++;
// Phase 3d-C: Update hot/cold indices after activating new slab
ss_update_hot_cold_indices(ss);
} }
} }

View File

@ -78,6 +78,12 @@ typedef struct SuperSlab {
_Atomic uint32_t remote_counts[SLABS_PER_SUPERSLAB_MAX]; _Atomic uint32_t remote_counts[SLABS_PER_SUPERSLAB_MAX];
_Atomic uint32_t slab_listed[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 // Per-slab metadata array
TinySlabMeta slabs[SLABS_PER_SUPERSLAB_MAX]; TinySlabMeta slabs[SLABS_PER_SUPERSLAB_MAX];
} SuperSlab; } SuperSlab;