Files
hakmem/core/hakmem_shared_pool.h

158 lines
6.4 KiB
C
Raw Normal View History

Phase 12: Shared SuperSlab Pool implementation (WIP - runtime crash) ## Summary Implemented Phase 12 Shared SuperSlab Pool (mimalloc-style) to address SuperSlab allocation churn (877 SuperSlabs → 100-200 target). ## Implementation (ChatGPT + Claude) 1. **Metadata changes** (superslab_types.h): - Added class_idx to TinySlabMeta (per-slab dynamic class) - Removed size_class from SuperSlab (no longer per-SuperSlab) - Changed owner_tid (16-bit) → owner_tid_low (8-bit) 2. **Shared Pool** (hakmem_shared_pool.{h,c}): - Global pool shared by all size classes - shared_pool_acquire_slab() - Get free slab for class_idx - shared_pool_release_slab() - Return slab when empty - Per-class hints for fast path optimization 3. **Integration** (23 files modified): - Updated all ss->size_class → meta->class_idx - Updated all meta->owner_tid → meta->owner_tid_low - superslab_refill() now uses shared pool - Free path releases empty slabs back to pool 4. **Build system** (Makefile): - Added hakmem_shared_pool.o to OBJS_BASE and TINY_BENCH_OBJS_BASE ## Status: ⚠️ Build OK, Runtime CRASH **Build**: ✅ SUCCESS - All 23 files compile without errors - Only warnings: superslab_allocate type mismatch (legacy code) **Runtime**: ❌ SEGFAULT - Crash location: sll_refill_small_from_ss() - Exit code: 139 (SIGSEGV) - Test case: ./bench_random_mixed_hakmem 1000 256 42 ## Known Issues 1. **SEGFAULT in refill path** - Likely shared_pool_acquire_slab() issue 2. **Legacy superslab_allocate()** still exists (type mismatch warning) 3. **Remaining TODOs** from design doc: - SuperSlab physical layout integration - slab_handle.h cleanup - Remove old per-class head implementation ## Next Steps 1. Debug SEGFAULT (gdb backtrace shows sll_refill_small_from_ss) 2. Fix shared_pool_acquire_slab() or superslab_init_slab() 3. Basic functionality test (1K → 100K iterations) 4. Measure SuperSlab count reduction (877 → 100-200) 5. Performance benchmark (+650-860% expected) ## Files Changed (25 files) core/box/free_local_box.c core/box/free_remote_box.c core/box/front_gate_classifier.c core/hakmem_super_registry.c core/hakmem_tiny.c core/hakmem_tiny_bg_spill.c core/hakmem_tiny_free.inc core/hakmem_tiny_lifecycle.inc core/hakmem_tiny_magazine.c core/hakmem_tiny_query.c core/hakmem_tiny_refill.inc.h core/hakmem_tiny_superslab.c core/hakmem_tiny_superslab.h core/hakmem_tiny_tls_ops.h core/slab_handle.h core/superslab/superslab_inline.h core/superslab/superslab_types.h core/tiny_debug.h core/tiny_free_fast.inc.h core/tiny_free_magazine.inc.h core/tiny_remote.c core/tiny_superslab_alloc.inc.h core/tiny_superslab_free.inc.h Makefile ## New Files (3 files) PHASE12_SHARED_SUPERSLAB_POOL_DESIGN.md core/hakmem_shared_pool.c core/hakmem_shared_pool.h 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude <noreply@anthropic.com> Co-Authored-By: ChatGPT <chatgpt@openai.com>
2025-11-13 16:33:03 +09:00
#pragma once
#include <stdint.h>
#include <pthread.h>
#include <stdatomic.h>
#include "superslab/superslab_types.h"
// Shared SuperSlab Pool (Phase 12-2 skeleton)
// Multiple tiny size classes share a global set of SuperSlab instances.
// This header exposes the minimal API used by refill/free hot paths in Phase 12.
#ifdef __cplusplus
extern "C" {
#endif
// ============================================================================
// Phase 12: SP-SLOT Box - Per-Slot State Management
// ============================================================================
//
// Problem:
// - Current design: 1 SuperSlab mixes multiple classes (C0-C7)
// - SuperSlab freed only when ALL classes empty (active_slabs==0)
// - Result: SuperSlabs rarely freed, LRU cache unused
//
// Solution:
// - Track each slab slot's state individually (UNUSED/ACTIVE/EMPTY)
// - Maintain per-class free slot lists for reuse
// - Free SuperSlab only when ALL slots empty
//
// Benefits:
// - Empty slabs from one class can be reused by same class immediately
// - Reduces mmap/munmap churn significantly
// - Enables LRU cache for fully empty SuperSlabs
// Slot state for each (SuperSlab, slab_idx) pair
typedef enum {
SLOT_UNUSED = 0, // Never used yet
SLOT_ACTIVE, // Assigned to a class (meta->used > 0 or freelist non-empty)
SLOT_EMPTY // Was assigned, now empty (meta->used==0, remote==0)
} SlotState;
// Per-slot metadata
// P0-5: state is atomic for lock-free claiming
typedef struct {
_Atomic SlotState state; // Atomic for lock-free CAS (UNUSED→ACTIVE)
uint8_t class_idx; // Valid when state != SLOT_UNUSED (0-7)
uint8_t slab_idx; // SuperSlab-internal index (0-31)
} SharedSlot;
// Per-SuperSlab metadata for slot management
#define MAX_SLOTS_PER_SS 32 // Typical: 1MB SS has 32 slabs of 32KB each
typedef struct SharedSSMeta {
Fix: Larson multi-threaded crash - 3 critical race conditions in SharedSuperSlabPool Root Cause Analysis (via Task agent investigation): Larson benchmark crashed with SEGV due to 3 separate race conditions between lock-free Stage 2 readers and mutex-protected writers in shared_pool_acquire_slab(). Race Condition 1: Non-Atomic Counter - **Problem**: `ss_meta_count` was `uint32_t` (non-atomic) but read atomically via cast - **Impact**: Thread A reads partially-updated count, accesses uninitialized metadata[N] - **Fix**: Changed to `_Atomic uint32_t`, use memory_order_release/acquire Race Condition 2: Non-Atomic Pointer - **Problem**: `meta->ss` was plain pointer, read lock-free but freed under mutex - **Impact**: Thread A loads `meta->ss` after Thread B frees SuperSlab → use-after-free - **Fix**: Changed to `_Atomic(SuperSlab*)`, set NULL before free, check for NULL Race Condition 3: realloc() vs Lock-Free Readers (CRITICAL) - **Problem**: `sp_meta_ensure_capacity()` used `realloc()` which MOVES the array - **Impact**: Thread B reallocs `ss_metadata`, Thread A accesses OLD (freed) array - **Fix**: **Removed realloc entirely** - use fixed-size array `ss_metadata[2048]` Fixes Applied: 1. **core/hakmem_shared_pool.h** (Line 53, 125-126): - `SuperSlab* ss` → `_Atomic(SuperSlab*) ss` - `uint32_t ss_meta_count` → `_Atomic uint32_t ss_meta_count` - `SharedSSMeta* ss_metadata` → `SharedSSMeta ss_metadata[MAX_SS_METADATA_ENTRIES]` - Removed `ss_meta_capacity` (no longer needed) 2. **core/hakmem_shared_pool.c** (Lines 223-233, 248-287, 577, 631-635, 812-815, 872): - **sp_meta_ensure_capacity()**: Replaced realloc with capacity check - **sp_meta_find_or_create()**: atomic_load/store for count and ss pointer - **Stage 1 (line 577)**: atomic_load for meta->ss - **Stage 2 (line 631-635)**: atomic_load with NULL check + skip - **shared_pool_release_slab()**: atomic_store(NULL) BEFORE superslab_free() - All metadata searches: atomic_load for consistency Memory Ordering: - **Release** (line 285): `atomic_fetch_add(&ss_meta_count, 1, memory_order_release)` → Publishes all metadata[N] writes before count increment is visible - **Acquire** (line 620, 631): `atomic_load(..., memory_order_acquire)` → Synchronizes-with release, ensures initialized metadata is seen - **Release** (line 872): `atomic_store(&meta->ss, NULL, memory_order_release)` → Prevents Stage 2 from seeing dangling pointer Test Results: - **Before**: SEGV crash (1 thread, 2 threads, any iteration count) - **After**: No crashes, stable execution - 1 thread: 266K ops/sec (stable, no SEGV) - 2 threads: 193K ops/sec (stable, no SEGV) - Warning: `[SP_META_CAPACITY_ERROR] Exceeded MAX_SS_METADATA_ENTRIES=2048` → Non-fatal, indicates metadata recycling needed (future optimization) Known Limitation: - Fixed array size (2048) may be insufficient for extreme workloads - Workaround: Increase MAX_SS_METADATA_ENTRIES if needed - Proper solution: Implement metadata recycling when SuperSlabs are freed Performance Note: - Larson still slow (~200K ops/sec vs System 20M ops/sec, 100x slower) - This is due to lock contention (separate issue, not race condition) - Crash bug is FIXED, performance optimization is next step Related Issues: - Original report: Commit 93cc23450 claimed to fix 500K SEGV but crashes persisted - This fix addresses the ROOT CAUSE, not just symptoms 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude <noreply@anthropic.com>
2025-11-14 23:16:54 +09:00
_Atomic(SuperSlab*) ss; // Physical SuperSlab pointer (atomic for lock-free Stage 2)
SharedSlot slots[MAX_SLOTS_PER_SS]; // Slot state for each slab
uint8_t active_slots; // Number of SLOT_ACTIVE slots
uint8_t total_slots; // Total available slots (from ss_slabs_capacity)
struct SharedSSMeta* next; // For free list linking
} SharedSSMeta;
// ============================================================================
// P0-4: Lock-Free Free Slot List (LIFO Stack)
// ============================================================================
// Free slot node for lock-free linked list
typedef struct FreeSlotNode {
SharedSSMeta* meta; // Which SuperSlab metadata
uint8_t slot_idx; // Which slot within that SuperSlab
struct FreeSlotNode* next; // Next node in LIFO stack
} FreeSlotNode;
// Lock-free per-class free slot list (LIFO stack with atomic head)
typedef struct {
_Atomic(FreeSlotNode*) head; // Atomic stack head pointer
} LockFreeFreeList;
// Node pool for lock-free allocation (avoid malloc/free)
#define MAX_FREE_NODES_PER_CLASS 4096 // Pre-allocated nodes per class (increased for 500K+ iterations)
extern FreeSlotNode g_free_node_pool[TINY_NUM_CLASSES_SS][MAX_FREE_NODES_PER_CLASS];
extern _Atomic uint32_t g_node_alloc_index[TINY_NUM_CLASSES_SS];
// ============================================================================
// Legacy Free Slot List (for comparison, will be removed after P0-4)
// ============================================================================
// Free slot entry for per-class reuse lists
typedef struct {
SharedSSMeta* meta; // Which SuperSlab metadata
uint8_t slot_idx; // Which slot within that SuperSlab
} FreeSlotEntry;
// Per-class free slot list (max capacity for now: 256 entries per class)
#define MAX_FREE_SLOTS_PER_CLASS 256
typedef struct {
FreeSlotEntry entries[MAX_FREE_SLOTS_PER_CLASS];
uint32_t count; // Number of free slots available
} FreeSlotList;
Phase 12: Shared SuperSlab Pool implementation (WIP - runtime crash) ## Summary Implemented Phase 12 Shared SuperSlab Pool (mimalloc-style) to address SuperSlab allocation churn (877 SuperSlabs → 100-200 target). ## Implementation (ChatGPT + Claude) 1. **Metadata changes** (superslab_types.h): - Added class_idx to TinySlabMeta (per-slab dynamic class) - Removed size_class from SuperSlab (no longer per-SuperSlab) - Changed owner_tid (16-bit) → owner_tid_low (8-bit) 2. **Shared Pool** (hakmem_shared_pool.{h,c}): - Global pool shared by all size classes - shared_pool_acquire_slab() - Get free slab for class_idx - shared_pool_release_slab() - Return slab when empty - Per-class hints for fast path optimization 3. **Integration** (23 files modified): - Updated all ss->size_class → meta->class_idx - Updated all meta->owner_tid → meta->owner_tid_low - superslab_refill() now uses shared pool - Free path releases empty slabs back to pool 4. **Build system** (Makefile): - Added hakmem_shared_pool.o to OBJS_BASE and TINY_BENCH_OBJS_BASE ## Status: ⚠️ Build OK, Runtime CRASH **Build**: ✅ SUCCESS - All 23 files compile without errors - Only warnings: superslab_allocate type mismatch (legacy code) **Runtime**: ❌ SEGFAULT - Crash location: sll_refill_small_from_ss() - Exit code: 139 (SIGSEGV) - Test case: ./bench_random_mixed_hakmem 1000 256 42 ## Known Issues 1. **SEGFAULT in refill path** - Likely shared_pool_acquire_slab() issue 2. **Legacy superslab_allocate()** still exists (type mismatch warning) 3. **Remaining TODOs** from design doc: - SuperSlab physical layout integration - slab_handle.h cleanup - Remove old per-class head implementation ## Next Steps 1. Debug SEGFAULT (gdb backtrace shows sll_refill_small_from_ss) 2. Fix shared_pool_acquire_slab() or superslab_init_slab() 3. Basic functionality test (1K → 100K iterations) 4. Measure SuperSlab count reduction (877 → 100-200) 5. Performance benchmark (+650-860% expected) ## Files Changed (25 files) core/box/free_local_box.c core/box/free_remote_box.c core/box/front_gate_classifier.c core/hakmem_super_registry.c core/hakmem_tiny.c core/hakmem_tiny_bg_spill.c core/hakmem_tiny_free.inc core/hakmem_tiny_lifecycle.inc core/hakmem_tiny_magazine.c core/hakmem_tiny_query.c core/hakmem_tiny_refill.inc.h core/hakmem_tiny_superslab.c core/hakmem_tiny_superslab.h core/hakmem_tiny_tls_ops.h core/slab_handle.h core/superslab/superslab_inline.h core/superslab/superslab_types.h core/tiny_debug.h core/tiny_free_fast.inc.h core/tiny_free_magazine.inc.h core/tiny_remote.c core/tiny_superslab_alloc.inc.h core/tiny_superslab_free.inc.h Makefile ## New Files (3 files) PHASE12_SHARED_SUPERSLAB_POOL_DESIGN.md core/hakmem_shared_pool.c core/hakmem_shared_pool.h 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude <noreply@anthropic.com> Co-Authored-By: ChatGPT <chatgpt@openai.com>
2025-11-13 16:33:03 +09:00
typedef struct SharedSuperSlabPool {
SuperSlab** slabs; // Dynamic array of SuperSlab*
uint32_t capacity; // Allocated entries in slabs[]
uint32_t total_count; // Total SuperSlabs ever allocated (<= capacity)
uint32_t active_count; // SuperSlabs that have >0 active slabs
pthread_mutex_t alloc_lock; // Protects pool metadata and grow/scan operations
// Per-class hints: last known SuperSlab with a free slab for that class.
// Read lock-free (best-effort), updated under alloc_lock.
SuperSlab* class_hints[TINY_NUM_CLASSES_SS];
// Approximate per-class ACTIVE slot counts (Tiny classes 0..7).
// Updated under alloc_lock; read by learning layer and stats snapshot.
uint32_t class_active_slots[TINY_NUM_CLASSES_SS];
Phase 12: Shared SuperSlab Pool implementation (WIP - runtime crash) ## Summary Implemented Phase 12 Shared SuperSlab Pool (mimalloc-style) to address SuperSlab allocation churn (877 SuperSlabs → 100-200 target). ## Implementation (ChatGPT + Claude) 1. **Metadata changes** (superslab_types.h): - Added class_idx to TinySlabMeta (per-slab dynamic class) - Removed size_class from SuperSlab (no longer per-SuperSlab) - Changed owner_tid (16-bit) → owner_tid_low (8-bit) 2. **Shared Pool** (hakmem_shared_pool.{h,c}): - Global pool shared by all size classes - shared_pool_acquire_slab() - Get free slab for class_idx - shared_pool_release_slab() - Return slab when empty - Per-class hints for fast path optimization 3. **Integration** (23 files modified): - Updated all ss->size_class → meta->class_idx - Updated all meta->owner_tid → meta->owner_tid_low - superslab_refill() now uses shared pool - Free path releases empty slabs back to pool 4. **Build system** (Makefile): - Added hakmem_shared_pool.o to OBJS_BASE and TINY_BENCH_OBJS_BASE ## Status: ⚠️ Build OK, Runtime CRASH **Build**: ✅ SUCCESS - All 23 files compile without errors - Only warnings: superslab_allocate type mismatch (legacy code) **Runtime**: ❌ SEGFAULT - Crash location: sll_refill_small_from_ss() - Exit code: 139 (SIGSEGV) - Test case: ./bench_random_mixed_hakmem 1000 256 42 ## Known Issues 1. **SEGFAULT in refill path** - Likely shared_pool_acquire_slab() issue 2. **Legacy superslab_allocate()** still exists (type mismatch warning) 3. **Remaining TODOs** from design doc: - SuperSlab physical layout integration - slab_handle.h cleanup - Remove old per-class head implementation ## Next Steps 1. Debug SEGFAULT (gdb backtrace shows sll_refill_small_from_ss) 2. Fix shared_pool_acquire_slab() or superslab_init_slab() 3. Basic functionality test (1K → 100K iterations) 4. Measure SuperSlab count reduction (877 → 100-200) 5. Performance benchmark (+650-860% expected) ## Files Changed (25 files) core/box/free_local_box.c core/box/free_remote_box.c core/box/front_gate_classifier.c core/hakmem_super_registry.c core/hakmem_tiny.c core/hakmem_tiny_bg_spill.c core/hakmem_tiny_free.inc core/hakmem_tiny_lifecycle.inc core/hakmem_tiny_magazine.c core/hakmem_tiny_query.c core/hakmem_tiny_refill.inc.h core/hakmem_tiny_superslab.c core/hakmem_tiny_superslab.h core/hakmem_tiny_tls_ops.h core/slab_handle.h core/superslab/superslab_inline.h core/superslab/superslab_types.h core/tiny_debug.h core/tiny_free_fast.inc.h core/tiny_free_magazine.inc.h core/tiny_remote.c core/tiny_superslab_alloc.inc.h core/tiny_superslab_free.inc.h Makefile ## New Files (3 files) PHASE12_SHARED_SUPERSLAB_POOL_DESIGN.md core/hakmem_shared_pool.c core/hakmem_shared_pool.h 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude <noreply@anthropic.com> Co-Authored-By: ChatGPT <chatgpt@openai.com>
2025-11-13 16:33:03 +09:00
// LRU cache integration hooks (Phase 9/12, optional for now)
SuperSlab* lru_head;
SuperSlab* lru_tail;
uint32_t lru_count;
// ========== Phase 12: SP-SLOT Management ==========
// P0-4: Lock-free per-class free slot lists (atomic LIFO stacks)
LockFreeFreeList free_slots_lockfree[TINY_NUM_CLASSES_SS];
// Legacy: Per-class free slot lists (mutex-protected, for comparison)
FreeSlotList free_slots[TINY_NUM_CLASSES_SS];
// SharedSSMeta array for all SuperSlabs in pool
Fix: Larson multi-threaded crash - 3 critical race conditions in SharedSuperSlabPool Root Cause Analysis (via Task agent investigation): Larson benchmark crashed with SEGV due to 3 separate race conditions between lock-free Stage 2 readers and mutex-protected writers in shared_pool_acquire_slab(). Race Condition 1: Non-Atomic Counter - **Problem**: `ss_meta_count` was `uint32_t` (non-atomic) but read atomically via cast - **Impact**: Thread A reads partially-updated count, accesses uninitialized metadata[N] - **Fix**: Changed to `_Atomic uint32_t`, use memory_order_release/acquire Race Condition 2: Non-Atomic Pointer - **Problem**: `meta->ss` was plain pointer, read lock-free but freed under mutex - **Impact**: Thread A loads `meta->ss` after Thread B frees SuperSlab → use-after-free - **Fix**: Changed to `_Atomic(SuperSlab*)`, set NULL before free, check for NULL Race Condition 3: realloc() vs Lock-Free Readers (CRITICAL) - **Problem**: `sp_meta_ensure_capacity()` used `realloc()` which MOVES the array - **Impact**: Thread B reallocs `ss_metadata`, Thread A accesses OLD (freed) array - **Fix**: **Removed realloc entirely** - use fixed-size array `ss_metadata[2048]` Fixes Applied: 1. **core/hakmem_shared_pool.h** (Line 53, 125-126): - `SuperSlab* ss` → `_Atomic(SuperSlab*) ss` - `uint32_t ss_meta_count` → `_Atomic uint32_t ss_meta_count` - `SharedSSMeta* ss_metadata` → `SharedSSMeta ss_metadata[MAX_SS_METADATA_ENTRIES]` - Removed `ss_meta_capacity` (no longer needed) 2. **core/hakmem_shared_pool.c** (Lines 223-233, 248-287, 577, 631-635, 812-815, 872): - **sp_meta_ensure_capacity()**: Replaced realloc with capacity check - **sp_meta_find_or_create()**: atomic_load/store for count and ss pointer - **Stage 1 (line 577)**: atomic_load for meta->ss - **Stage 2 (line 631-635)**: atomic_load with NULL check + skip - **shared_pool_release_slab()**: atomic_store(NULL) BEFORE superslab_free() - All metadata searches: atomic_load for consistency Memory Ordering: - **Release** (line 285): `atomic_fetch_add(&ss_meta_count, 1, memory_order_release)` → Publishes all metadata[N] writes before count increment is visible - **Acquire** (line 620, 631): `atomic_load(..., memory_order_acquire)` → Synchronizes-with release, ensures initialized metadata is seen - **Release** (line 872): `atomic_store(&meta->ss, NULL, memory_order_release)` → Prevents Stage 2 from seeing dangling pointer Test Results: - **Before**: SEGV crash (1 thread, 2 threads, any iteration count) - **After**: No crashes, stable execution - 1 thread: 266K ops/sec (stable, no SEGV) - 2 threads: 193K ops/sec (stable, no SEGV) - Warning: `[SP_META_CAPACITY_ERROR] Exceeded MAX_SS_METADATA_ENTRIES=2048` → Non-fatal, indicates metadata recycling needed (future optimization) Known Limitation: - Fixed array size (2048) may be insufficient for extreme workloads - Workaround: Increase MAX_SS_METADATA_ENTRIES if needed - Proper solution: Implement metadata recycling when SuperSlabs are freed Performance Note: - Larson still slow (~200K ops/sec vs System 20M ops/sec, 100x slower) - This is due to lock contention (separate issue, not race condition) - Crash bug is FIXED, performance optimization is next step Related Issues: - Original report: Commit 93cc23450 claimed to fix 500K SEGV but crashes persisted - This fix addresses the ROOT CAUSE, not just symptoms 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude <noreply@anthropic.com>
2025-11-14 23:16:54 +09:00
// RACE FIX: Fixed-size array (no realloc!) to avoid race with lock-free Stage 2
Phase 23 Unified Cache + PageFaultTelemetry generalization: Mid/VM page-fault bottleneck identified Summary: - Phase 23 Unified Cache: +30% improvement (Random Mixed 256B: 18.18M → 23.68M ops/s) - PageFaultTelemetry: Extended to generic buckets (C0-C7, MID, L25, SSM) - Measurement-driven decision: Mid/VM page-faults (80-100K) >> Tiny (6K) → prioritize Mid/VM optimization Phase 23 Changes: 1. Unified Cache implementation (core/front/tiny_unified_cache.{c,h}) - Direct SuperSlab carve (TLS SLL bypass) - Self-contained pop-or-refill pattern - ENV: HAKMEM_TINY_UNIFIED_CACHE=1, HAKMEM_TINY_UNIFIED_C{0-7}=128 2. Fast path pruning (tiny_alloc_fast.inc.h, tiny_free_fast_v2.inc.h) - Unified ON → direct cache access (skip all intermediate layers) - Alloc: unified_cache_pop_or_refill() → immediate fail to slow - Free: unified_cache_push() → fallback to SLL only if full PageFaultTelemetry Changes: 3. Generic bucket architecture (core/box/pagefault_telemetry_box.{c,h}) - PF_BUCKET_{C0-C7, MID, L25, SSM} for domain-specific measurement - Integration: hak_pool_try_alloc(), l25_alloc_new_run(), shared_pool_allocate_superslab_unlocked() 4. Measurement results (Random Mixed 500K / 256B): - Tiny C2-C7: 2-33 pages, high reuse (64-3.8 touches/page) - SSM: 512 pages (initialization footprint) - MID/L25: 0 (unused in this workload) - Mid/Large VM benchmarks: 80-100K page-faults (13-16x higher than Tiny) Ring Cache Enhancements: 5. Hot Ring Cache (core/front/tiny_ring_cache.{c,h}) - ENV: HAKMEM_TINY_HOT_RING_ENABLE=1, HAKMEM_TINY_HOT_RING_C{0-7}=size - Conditional compilation cleanup Documentation: 6. Analysis reports - RANDOM_MIXED_BOTTLENECK_ANALYSIS.md: Page-fault breakdown - RANDOM_MIXED_SUMMARY.md: Phase 23 summary - RING_CACHE_ACTIVATION_GUIDE.md: Ring cache usage - CURRENT_TASK.md: Updated with Phase 23 results and Phase 24 plan Next Steps (Phase 24): - Target: Mid/VM PageArena/HotSpanBox (page-fault reduction 80-100K → 30-40K) - Tiny SSM optimization deferred (low ROI, ~6K page-faults already optimal) - Expected improvement: +30-50% for Mid/Large workloads Generated with Claude Code Co-Authored-By: Claude <noreply@anthropic.com>
2025-11-17 02:47:58 +09:00
// LARSON FIX (2025-11-16): Increased from 2048 → 8192 for MT churn workloads
#define MAX_SS_METADATA_ENTRIES 8192
Fix: Larson multi-threaded crash - 3 critical race conditions in SharedSuperSlabPool Root Cause Analysis (via Task agent investigation): Larson benchmark crashed with SEGV due to 3 separate race conditions between lock-free Stage 2 readers and mutex-protected writers in shared_pool_acquire_slab(). Race Condition 1: Non-Atomic Counter - **Problem**: `ss_meta_count` was `uint32_t` (non-atomic) but read atomically via cast - **Impact**: Thread A reads partially-updated count, accesses uninitialized metadata[N] - **Fix**: Changed to `_Atomic uint32_t`, use memory_order_release/acquire Race Condition 2: Non-Atomic Pointer - **Problem**: `meta->ss` was plain pointer, read lock-free but freed under mutex - **Impact**: Thread A loads `meta->ss` after Thread B frees SuperSlab → use-after-free - **Fix**: Changed to `_Atomic(SuperSlab*)`, set NULL before free, check for NULL Race Condition 3: realloc() vs Lock-Free Readers (CRITICAL) - **Problem**: `sp_meta_ensure_capacity()` used `realloc()` which MOVES the array - **Impact**: Thread B reallocs `ss_metadata`, Thread A accesses OLD (freed) array - **Fix**: **Removed realloc entirely** - use fixed-size array `ss_metadata[2048]` Fixes Applied: 1. **core/hakmem_shared_pool.h** (Line 53, 125-126): - `SuperSlab* ss` → `_Atomic(SuperSlab*) ss` - `uint32_t ss_meta_count` → `_Atomic uint32_t ss_meta_count` - `SharedSSMeta* ss_metadata` → `SharedSSMeta ss_metadata[MAX_SS_METADATA_ENTRIES]` - Removed `ss_meta_capacity` (no longer needed) 2. **core/hakmem_shared_pool.c** (Lines 223-233, 248-287, 577, 631-635, 812-815, 872): - **sp_meta_ensure_capacity()**: Replaced realloc with capacity check - **sp_meta_find_or_create()**: atomic_load/store for count and ss pointer - **Stage 1 (line 577)**: atomic_load for meta->ss - **Stage 2 (line 631-635)**: atomic_load with NULL check + skip - **shared_pool_release_slab()**: atomic_store(NULL) BEFORE superslab_free() - All metadata searches: atomic_load for consistency Memory Ordering: - **Release** (line 285): `atomic_fetch_add(&ss_meta_count, 1, memory_order_release)` → Publishes all metadata[N] writes before count increment is visible - **Acquire** (line 620, 631): `atomic_load(..., memory_order_acquire)` → Synchronizes-with release, ensures initialized metadata is seen - **Release** (line 872): `atomic_store(&meta->ss, NULL, memory_order_release)` → Prevents Stage 2 from seeing dangling pointer Test Results: - **Before**: SEGV crash (1 thread, 2 threads, any iteration count) - **After**: No crashes, stable execution - 1 thread: 266K ops/sec (stable, no SEGV) - 2 threads: 193K ops/sec (stable, no SEGV) - Warning: `[SP_META_CAPACITY_ERROR] Exceeded MAX_SS_METADATA_ENTRIES=2048` → Non-fatal, indicates metadata recycling needed (future optimization) Known Limitation: - Fixed array size (2048) may be insufficient for extreme workloads - Workaround: Increase MAX_SS_METADATA_ENTRIES if needed - Proper solution: Implement metadata recycling when SuperSlabs are freed Performance Note: - Larson still slow (~200K ops/sec vs System 20M ops/sec, 100x slower) - This is due to lock contention (separate issue, not race condition) - Crash bug is FIXED, performance optimization is next step Related Issues: - Original report: Commit 93cc23450 claimed to fix 500K SEGV but crashes persisted - This fix addresses the ROOT CAUSE, not just symptoms 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude <noreply@anthropic.com>
2025-11-14 23:16:54 +09:00
SharedSSMeta ss_metadata[MAX_SS_METADATA_ENTRIES]; // Fixed-size array
_Atomic uint32_t ss_meta_count; // Used entries (atomic for lock-free Stage 2)
Phase 12: Shared SuperSlab Pool implementation (WIP - runtime crash) ## Summary Implemented Phase 12 Shared SuperSlab Pool (mimalloc-style) to address SuperSlab allocation churn (877 SuperSlabs → 100-200 target). ## Implementation (ChatGPT + Claude) 1. **Metadata changes** (superslab_types.h): - Added class_idx to TinySlabMeta (per-slab dynamic class) - Removed size_class from SuperSlab (no longer per-SuperSlab) - Changed owner_tid (16-bit) → owner_tid_low (8-bit) 2. **Shared Pool** (hakmem_shared_pool.{h,c}): - Global pool shared by all size classes - shared_pool_acquire_slab() - Get free slab for class_idx - shared_pool_release_slab() - Return slab when empty - Per-class hints for fast path optimization 3. **Integration** (23 files modified): - Updated all ss->size_class → meta->class_idx - Updated all meta->owner_tid → meta->owner_tid_low - superslab_refill() now uses shared pool - Free path releases empty slabs back to pool 4. **Build system** (Makefile): - Added hakmem_shared_pool.o to OBJS_BASE and TINY_BENCH_OBJS_BASE ## Status: ⚠️ Build OK, Runtime CRASH **Build**: ✅ SUCCESS - All 23 files compile without errors - Only warnings: superslab_allocate type mismatch (legacy code) **Runtime**: ❌ SEGFAULT - Crash location: sll_refill_small_from_ss() - Exit code: 139 (SIGSEGV) - Test case: ./bench_random_mixed_hakmem 1000 256 42 ## Known Issues 1. **SEGFAULT in refill path** - Likely shared_pool_acquire_slab() issue 2. **Legacy superslab_allocate()** still exists (type mismatch warning) 3. **Remaining TODOs** from design doc: - SuperSlab physical layout integration - slab_handle.h cleanup - Remove old per-class head implementation ## Next Steps 1. Debug SEGFAULT (gdb backtrace shows sll_refill_small_from_ss) 2. Fix shared_pool_acquire_slab() or superslab_init_slab() 3. Basic functionality test (1K → 100K iterations) 4. Measure SuperSlab count reduction (877 → 100-200) 5. Performance benchmark (+650-860% expected) ## Files Changed (25 files) core/box/free_local_box.c core/box/free_remote_box.c core/box/front_gate_classifier.c core/hakmem_super_registry.c core/hakmem_tiny.c core/hakmem_tiny_bg_spill.c core/hakmem_tiny_free.inc core/hakmem_tiny_lifecycle.inc core/hakmem_tiny_magazine.c core/hakmem_tiny_query.c core/hakmem_tiny_refill.inc.h core/hakmem_tiny_superslab.c core/hakmem_tiny_superslab.h core/hakmem_tiny_tls_ops.h core/slab_handle.h core/superslab/superslab_inline.h core/superslab/superslab_types.h core/tiny_debug.h core/tiny_free_fast.inc.h core/tiny_free_magazine.inc.h core/tiny_remote.c core/tiny_superslab_alloc.inc.h core/tiny_superslab_free.inc.h Makefile ## New Files (3 files) PHASE12_SHARED_SUPERSLAB_POOL_DESIGN.md core/hakmem_shared_pool.c core/hakmem_shared_pool.h 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude <noreply@anthropic.com> Co-Authored-By: ChatGPT <chatgpt@openai.com>
2025-11-13 16:33:03 +09:00
} SharedSuperSlabPool;
// Global singleton
extern SharedSuperSlabPool g_shared_pool;
// Initialize shared pool (idempotent, thread-safe wrt multiple callers on startup paths)
void shared_pool_init(void);
// Get/allocate a SuperSlab registered in the pool.
// Returns non-NULL on success, NULL on failure.
SuperSlab* shared_pool_acquire_superslab(void);
// Acquire a slab for class_idx from shared pool.
// On success:
// *ss_out = SuperSlab containing slab
// *slab_idx_out = slab index [0, SLABS_PER_SUPERSLAB_MAX)
// Returns 0 on success, non-zero on failure.
int shared_pool_acquire_slab(int class_idx, SuperSlab** ss_out, int* slab_idx_out);
// Release an empty slab back to pool (mark as unassigned).
// Caller must ensure TinySlabMeta.used == 0.
void shared_pool_release_slab(SuperSlab* ss, int slab_idx);
#ifdef __cplusplus
}
#endif