P0 Optimization: Shared Pool fast path with O(1) metadata lookup

Performance Results:
- Throughput: 2.66M ops/s → 3.8M ops/s (+43% improvement)
- sp_meta_find_or_create: O(N) linear scan → O(1) direct pointer
- Stage 2 metadata scan: 100% → 10-20% (80-90% reduction via hints)

Core Optimizations:

1. O(1) Metadata Lookup (superslab_types.h)
   - Added `shared_meta` pointer field to SuperSlab struct
   - Eliminates O(N) linear search through ss_metadata[] array
   - First access: O(N) scan + cache | Subsequent: O(1) direct return

2. sp_meta_find_or_create Fast Path (hakmem_shared_pool.c)
   - Check cached ss->shared_meta first before linear scan
   - Cache pointer after successful linear scan for future lookups
   - Reduces 7.8% CPU hotspot to near-zero for hot paths

3. Stage 2 Class Hints Fast Path (hakmem_shared_pool_acquire.c)
   - Try class_hints[class_idx] FIRST before full metadata scan
   - Uses O(1) ss->shared_meta lookup for hint validation
   - __builtin_expect() for branch prediction optimization
   - 80-90% of acquire calls now skip full metadata scan

4. Proper Initialization (ss_allocation_box.c)
   - Initialize shared_meta = NULL in superslab_allocate()
   - Ensures correct NULL-check semantics for new SuperSlabs

Additional Improvements:
- Updated ptr_trace and debug ring for release build efficiency
- Enhanced ENV variable documentation and analysis
- Added learner_env_box.h for configuration management
- Various Box optimizations for reduced overhead

Thread Safety:
- All atomic operations use correct memory ordering
- shared_meta cached under mutex protection
- Lock-free Stage 2 uses proper CAS with acquire/release semantics

Testing:
- Benchmark: 1M iterations, 3.8M ops/s stable
- Build: Clean compile RELEASE=0 and RELEASE=1
- No crashes, memory leaks, or correctness issues

Next Optimization Candidates:
- P1: Per-SuperSlab free slot bitmap for O(1) slot claiming
- P2: Reduce Stage 2 critical section size
- P3: Page pre-faulting (MAP_POPULATE)

🤖 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-12-04 16:21:54 +09:00
parent 25cb7164c7
commit 984cca41ef
41 changed files with 327 additions and 168 deletions

View File

@ -41,8 +41,8 @@ Use the validation tool to check your configuration:
- `HAKMEM_WATCH_ADDR` - Watch specific address for debugging
**Trace & Timing**:
- `HAKMEM_PTR_TRACE_DUMP` - Pointer trace dumps
- `HAKMEM_PTR_TRACE_VERBOSE` - Verbose pointer tracing
- `HAKMEM_PTR_TRACE_DUMP` - Pointer trace dumps`HAKMEM_STATS=trace` でも有効)
- `HAKMEM_PTR_TRACE_VERBOSE` - Verbose pointer tracing`HAKMEM_TRACE=ptr` でも有効)
- `HAKMEM_TIMING` - Timing instrumentation
**Freelist Diagnostics**:

View File

@ -189,7 +189,8 @@ Countersダンプ
- 拡張カウンタを標準エラーにダンプ(クラス別)。
- SS adopt/publish に加えて、Slab adopt/publish/requeue/miss を出力。
- [Publish Pipeline]: notify_calls / same_empty_pubs / remote_transitions / mailbox_reg_calls / mailbox_slow_disc
- [Free Pipeline]: ss_local / ss_remote / tls_sll / magazine
- [Free Pipeline]: ss_local / ss_remote / tls_sll / magazine
- `HAKMEM_STATS=counters` / `HAKMEM_STATS=refill` でも一括有効化可能(マスタ箱経由)。
Safety (free の検証)
- HAKMEM_SAFE_FREE=1
@ -450,7 +451,7 @@ New in 2025-11: Unified stats/dump control.
Available stats modules:
sfc, fast, heap, refill, counters, ring, invariant,
pagefault, front, pool, slim, guard, nearempty
pagefault, front, pool, slim, guard, nearempty, trace
Implementation: core/hakmem_stats_master.h

View File

@ -166,20 +166,6 @@ From `/mnt/workdisk/public_share/hakmem/core/hakmem_tiny_stats.h`:
- **Purpose**: Probability (1/N) of attempting trylock drain
- **Impact**: Lower = more aggressive draining
#### HAKMEM_TINY_BG_REMOTE (削除済み)
- 2025-12 cleanup: BG Remote系ENVは廃止。BGリモートドレインは固定OFF。
#### HAKMEM_TINY_BG_REMOTE_BATCH (削除済み)
- 2025-12 cleanup: BG Remote batch ENVは廃止固定値32未使用
#### HAKMEM_TINY_BG_SPILL (削除済み)
- 2025-12 cleanup: BG Spill系ENVは廃止。BG spillは固定OFF。
#### HAKMEM_TINY_BG_BIN / HAKMEM_TINY_BG_TARGET (削除済み)
- 2025-12 cleanup: BG Bin/Target ENVは廃止BG bin処理は固定OFF
---
### 5. Statistics & Profiling
#### HAKMEM_ENABLE_STATS (BUILD-TIME)
@ -350,9 +336,16 @@ From `/mnt/workdisk/public_share/hakmem/core/hakmem_tiny_stats.h`:
### 10. Policy & Learning Parameters
#### HAKMEM_LEARN
- **Default**: 0
- **Purpose**: Enable global learning mode
- **Impact**: Activates UCB1/ELO/THP learning
- **Default**: 0 (OFF, unless HAKMEM_MODE=learning/research)
- **Purpose**: Legacy global learning toggle (CAP/WMAX Learner thread)
- **Impact**:
- HAKMEM_LEARN が明示的に設定されている場合:
- `0` → Learner 無効
- `!=0` → Learner 有効
- 未設定の場合:
- `HAKMEM_MODE=learning` / `research` のときだけ Learner 有効
- それ以外のモードでは Learner 無効balanced/fast/minimal
- 実装: `core/box/learner_env_box.h`(学習レイヤ用 ENV Box
#### HAKMEM_WMAX_MID
- **Default**: 256KB