diff --git a/CURRENT_TASK.md b/CURRENT_TASK.md index 64ac06c9..f0cec7cf 100644 --- a/CURRENT_TASK.md +++ b/CURRENT_TASK.md @@ -1619,3 +1619,166 @@ if (unlikely(tls->seg_base == 0)) { - C5 (256B) / C4 (128B) 対応 → coverage 拡大で相対 overhead 削減 - Page metadata layout 最適化 → cache line alignment - Remote free 対応 → multi-thread workload 準備 + +--- + +## Phase v7-4: Policy Box 導入 (フロント芯の作り直し) ✅ + +### 完成 + +**実装箇所**: +- `/mnt/workdisk/public_share/hakmem/core/box/smallobject_policy_v7_box.h`: Policy Box ヘッダー (新規作成) +- `/mnt/workdisk/public_share/hakmem/core/smallobject_policy_v7.c`: Policy Box 実装 (新規作成) +- `/mnt/workdisk/public_share/hakmem/core/front/malloc_tiny_fast.h`: v7 routing を Policy Box 経由に変更 +- `/mnt/workdisk/public_share/hakmem/Makefile`: `core/smallobject_policy_v7.o` を OBJS リストに追加 + +**設計意図**: +フロントを「size→class→route_kind→switch」の1層だけにして、ルート決定を Policy Box に集約。Box Theory の L3 に SmallPolicyV7 を配置し、ULTRA/v7/MID_v3/LEGACY の選択を一元化。 + +### Policy Box 実装 + +**1. Route Kind Enum (L0/L1/L1' layer selection)**: +```c +typedef enum { + SMALL_ROUTE_ULTRA, // L0: C4-C7 ULTRA (FROZEN) + SMALL_ROUTE_V7, // L1: SmallObject v7 (research box) + SMALL_ROUTE_MID_V3, // L1': MID v3 (257-768B mid/small) + SMALL_ROUTE_LEGACY, // L1': TinyHeap v1 / Pool v1 (fallback) +} SmallRouteKind; +``` + +**2. Policy Snapshot Structure**: +```c +typedef struct SmallPolicyV7 { + SmallRouteKind route_kind[8]; // C0-C7 routing decision +} SmallPolicyV7; +``` + +**3. Policy API**: +```c +/// Get policy snapshot (read-only, TLS cached) +const SmallPolicyV7* small_policy_v7_snapshot(void); + +/// Initialize policy from ENV variables (called once at startup) +/// Priority: ULTRA > v7 > MID_v3 > LEGACY +void small_policy_v7_init_from_env(SmallPolicyV7* policy); + +/// Get route kind name for debugging +const char* small_route_kind_name(SmallRouteKind kind); +``` + +### ENV 優先順位 (固定) + +**Priority 1: ULTRA (highest)** +- `HAKMEM_TINY_C7_ULTRA_ENABLED` (default ON) → C7 ULTRA +- `HAKMEM_TINY_C6_ULTRA_FREE_ENABLED` → C6 ULTRA (free-only, 将来拡張用) +- Future: `HAKMEM_TINY_C4_ULTRA_ENABLED` / `C5_ULTRA_ENABLED` + +**Priority 2: SmallObject v7 (research box)** +- `HAKMEM_SMALL_HEAP_V7_ENABLED` → v7 有効化 +- `HAKMEM_SMALL_HEAP_V7_CLASSES` (default 0x40 = C6) → v7 対象クラス + +**Priority 3: MID_v3 (mid/small range)** +- `HAKMEM_MID_V3_ENABLED` → MID_v3 有効化 +- `HAKMEM_MID_V3_CLASSES` (default 0x60 = C5-C6) → MID_v3 対象クラス + +**Priority 4: LEGACY (fallback)** +- Default for all classes not covered by above + +### フロント段階移行 + +**alloc path** (malloc_tiny_fast.h, line 227-235): +```c +// Phase v7-4: Check Policy Box for v7 routing (before switch) +const SmallPolicyV7* policy = small_policy_v7_snapshot(); +if (policy->route_kind[class_idx] == SMALL_ROUTE_V7) { + void* v7p = small_heap_alloc_fast_v7_stub(size, (uint8_t)class_idx); + if (TINY_HOT_LIKELY(v7p != NULL)) { + return v7p; + } + // v7 stub returned NULL -> fallback to legacy +} +``` + +**free path** (malloc_tiny_fast.h, line 408-416): +```c +// Phase v7-4: Check Policy Box for v7 routing (before route lookup) +const SmallPolicyV7* policy = small_policy_v7_snapshot(); +if (class_idx == 6 && policy->route_kind[class_idx] == SMALL_ROUTE_V7) { + if (small_heap_free_fast_v7_stub(ptr, (uint8_t)class_idx)) { + FREE_PATH_STAT_INC(smallheap_v7_fast); + return 1; + } + // v7 returned false (ptr not in v7 segment) -> fallback to legacy below +} +``` + +### Box Theory 層構造 + +**L0: ULTRA (frozen, C4-C7)** +- C7 ULTRA: Phase ULTRA-1~6 (production) +- C6/C5/C4 ULTRA: Phase ULTRA-7~9 (future) + +**L1: SmallObject v7 (research box)** +- C6-only (Phase v7-1~4) +- Future: C5/C4 expansion + +**L1': MID_v3 / LEGACY (fallback)** +- MID_v3: 257-768B (C5-C6 range) +- LEGACY: TinyHeap v1 / Pool v1 + +**L2: Segment / RegionId** +- SmallSegment_v7 (64MB mmap region) +- RegionIdBox v6 (ptr → segment lookup) + +**L3: Policy / Stats / Learner** +- **SmallPolicyV7** (this phase): Route decision +- Stats: FreePathStatsBox / AllocGateStatsBox +- Learner: (future) dynamic route selection + +### 段階移行戦略 + +**Phase v7-4 現状**: +- v7 関連のみ Policy box 経由に変更 +- ULTRA/MID_v3/LEGACY は既存の `tiny_route_env_box.h` を併用(後で統合予定) + +**将来の統一**: +- `tiny_route_env_box.h` の ULTRA/MID_v3/LEGACY ルート判定を Policy box に統合 +- クラスごとの柔軟な優先順位設定 +- Learner 連携による動的ルート選択 (ENV override) + +### Debug Output + +初回 TLS 初期化時に stderr に出力: +``` +[POLICY_V7_INIT] Route assignments: + C0: LEGACY + C1: LEGACY + C2: LEGACY + C3: LEGACY + C4: LEGACY + C5: LEGACY + C6: V7 (if HAKMEM_SMALL_HEAP_V7_ENABLED=1) + C7: ULTRA (if HAKMEM_TINY_C7_ULTRA_ENABLED=1, default ON) +``` + +### ビルド確認 + +**コンパイル**: 成功 +``` +gcc -O3 ... -c -o core/smallobject_policy_v7.o core/smallobject_policy_v7.c +gcc -o bench_tiny_hot_hakmem ... core/smallobject_policy_v7.o ... -lm -lpthread -flto +``` + +**リンク**: 成功 (all object lists updated) +- `OBJS_BASE` +- `BENCH_HAKMEM_OBJS_BASE` +- `TINY_BENCH_OBJS_BASE` + +### 次の拡張 + +**Phase v7-5 候補**: +1. **ULTRA/MID_v3/LEGACY 統合**: `tiny_route_env_box.h` → Policy box に移行 +2. **Learner 連携**: ENV defaults + runtime learning override +3. **クラスごとの柔軟な優先順位**: ENV で ULTRA vs v7 の順序を逆転可能に +4. **Multi-class v7**: C5/C4 追加 → coverage 拡大 diff --git a/Makefile b/Makefile index 5291ac38..c26eccd1 100644 --- a/Makefile +++ b/Makefile @@ -218,7 +218,7 @@ LDFLAGS += $(EXTRA_LDFLAGS) # Targets TARGET = test_hakmem -OBJS_BASE = hakmem.o hakmem_config.o hakmem_tiny_config.o hakmem_ucb1.o hakmem_bigcache.o hakmem_pool.o hakmem_l25_pool.o hakmem_site_rules.o hakmem_tiny.o core/box/ss_allocation_box.o superslab_stats.o superslab_cache.o superslab_ace.o superslab_slab.o superslab_backend.o core/superslab_head_stub.o hakmem_smallmid.o tiny_sticky.o tiny_remote.o tiny_publish.o tiny_debug_ring.o hakmem_tiny_magazine.o hakmem_tiny_stats.o hakmem_tiny_sfc.o hakmem_tiny_query.o hakmem_tiny_rss.o hakmem_tiny_registry.o hakmem_tiny_remote_target.o hakmem_tiny_bg_spill.o tiny_adaptive_sizing.o hakmem_super_registry.o hakmem_shared_pool.o hakmem_shared_pool_acquire.o hakmem_shared_pool_release.o hakmem_elo.o hakmem_batch.o hakmem_p2.o hakmem_sizeclass_dist.o hakmem_evo.o hakmem_debug.o hakmem_sys.o hakmem_whale.o hakmem_policy.o hakmem_ace.o hakmem_ace_stats.o hakmem_prof.o hakmem_learner.o hakmem_size_hist.o hakmem_learn_log.o hakmem_syscall.o hakmem_ace_metrics.o hakmem_ace_ucb1.o hakmem_ace_controller.o tiny_fastcache.o core/box/superslab_expansion_box.o core/box/integrity_box.o core/box/free_publish_box.o core/box/mailbox_box.o core/box/front_gate_box.o core/box/front_gate_classifier.o core/box/capacity_box.o core/box/carve_push_box.o core/box/prewarm_box.o core/box/ss_hot_prewarm_box.o core/box/front_metrics_box.o core/box/bench_fast_box.o core/box/ss_addr_map_box.o core/box/slab_recycling_box.o core/box/pagefault_telemetry_box.o core/box/tiny_sizeclass_hist_box.o core/box/tiny_env_box.o core/box/tiny_route_box.o core/box/free_front_v3_env_box.o core/box/free_path_stats_box.o core/box/free_dispatch_stats_box.o core/box/alloc_gate_stats_box.o core/box/tiny_c6_ultra_free_box.o core/box/tiny_c5_ultra_free_box.o core/box/tiny_c4_ultra_free_box.o core/box/tiny_page_box.o core/box/tiny_class_policy_box.o core/box/tiny_class_stats_box.o core/box/tiny_policy_learner_box.o core/box/ss_budget_box.o core/box/tiny_mem_stats_box.o core/box/c7_meta_used_counter_box.o core/box/wrapper_env_box.o core/box/madvise_guard_box.o core/box/libm_reloc_guard_box.o core/box/ptr_trace_box.o core/box/link_missing_stubs.o core/box/super_reg_box.o core/box/shared_pool_box.o core/box/remote_side_box.o core/page_arena.o core/front/tiny_unified_cache.o core/tiny_alloc_fast_push.o core/tiny_c7_ultra_segment.o core/tiny_c7_ultra.o core/link_stubs.o core/tiny_failfast.o core/tiny_destructors.o core/smallobject_hotbox_v3.o core/smallobject_hotbox_v4.o core/smallobject_hotbox_v5.o core/smallsegment_v5.o core/smallobject_cold_iface_v5.o core/smallsegment_v6.o core/smallobject_cold_iface_v6.o core/smallobject_core_v6.o core/region_id_v6.o core/smallsegment_v7.o core/smallobject_cold_iface_v7.o core/mid_hotbox_v3.o +OBJS_BASE = hakmem.o hakmem_config.o hakmem_tiny_config.o hakmem_ucb1.o hakmem_bigcache.o hakmem_pool.o hakmem_l25_pool.o hakmem_site_rules.o hakmem_tiny.o core/box/ss_allocation_box.o superslab_stats.o superslab_cache.o superslab_ace.o superslab_slab.o superslab_backend.o core/superslab_head_stub.o hakmem_smallmid.o tiny_sticky.o tiny_remote.o tiny_publish.o tiny_debug_ring.o hakmem_tiny_magazine.o hakmem_tiny_stats.o hakmem_tiny_sfc.o hakmem_tiny_query.o hakmem_tiny_rss.o hakmem_tiny_registry.o hakmem_tiny_remote_target.o hakmem_tiny_bg_spill.o tiny_adaptive_sizing.o hakmem_super_registry.o hakmem_shared_pool.o hakmem_shared_pool_acquire.o hakmem_shared_pool_release.o hakmem_elo.o hakmem_batch.o hakmem_p2.o hakmem_sizeclass_dist.o hakmem_evo.o hakmem_debug.o hakmem_sys.o hakmem_whale.o hakmem_policy.o hakmem_ace.o hakmem_ace_stats.o hakmem_prof.o hakmem_learner.o hakmem_size_hist.o hakmem_learn_log.o hakmem_syscall.o hakmem_ace_metrics.o hakmem_ace_ucb1.o hakmem_ace_controller.o tiny_fastcache.o core/box/superslab_expansion_box.o core/box/integrity_box.o core/box/free_publish_box.o core/box/mailbox_box.o core/box/front_gate_box.o core/box/front_gate_classifier.o core/box/capacity_box.o core/box/carve_push_box.o core/box/prewarm_box.o core/box/ss_hot_prewarm_box.o core/box/front_metrics_box.o core/box/bench_fast_box.o core/box/ss_addr_map_box.o core/box/slab_recycling_box.o core/box/pagefault_telemetry_box.o core/box/tiny_sizeclass_hist_box.o core/box/tiny_env_box.o core/box/tiny_route_box.o core/box/free_front_v3_env_box.o core/box/free_path_stats_box.o core/box/free_dispatch_stats_box.o core/box/alloc_gate_stats_box.o core/box/tiny_c6_ultra_free_box.o core/box/tiny_c5_ultra_free_box.o core/box/tiny_c4_ultra_free_box.o core/box/tiny_page_box.o core/box/tiny_class_policy_box.o core/box/tiny_class_stats_box.o core/box/tiny_policy_learner_box.o core/box/ss_budget_box.o core/box/tiny_mem_stats_box.o core/box/c7_meta_used_counter_box.o core/box/wrapper_env_box.o core/box/madvise_guard_box.o core/box/libm_reloc_guard_box.o core/box/ptr_trace_box.o core/box/link_missing_stubs.o core/box/super_reg_box.o core/box/shared_pool_box.o core/box/remote_side_box.o core/page_arena.o core/front/tiny_unified_cache.o core/tiny_alloc_fast_push.o core/tiny_c7_ultra_segment.o core/tiny_c7_ultra.o core/link_stubs.o core/tiny_failfast.o core/tiny_destructors.o core/smallobject_hotbox_v3.o core/smallobject_hotbox_v4.o core/smallobject_hotbox_v5.o core/smallsegment_v5.o core/smallobject_cold_iface_v5.o core/smallsegment_v6.o core/smallobject_cold_iface_v6.o core/smallobject_core_v6.o core/region_id_v6.o core/smallsegment_v7.o core/smallobject_cold_iface_v7.o core/mid_hotbox_v3.o core/smallobject_policy_v7.o OBJS = $(OBJS_BASE) # Shared library @@ -250,7 +250,7 @@ endif # Benchmark targets BENCH_HAKMEM = bench_allocators_hakmem BENCH_SYSTEM = bench_allocators_system -BENCH_HAKMEM_OBJS_BASE = hakmem.o hakmem_config.o hakmem_tiny_config.o hakmem_ucb1.o hakmem_bigcache.o hakmem_pool.o hakmem_l25_pool.o hakmem_site_rules.o hakmem_tiny.o core/box/ss_allocation_box.o superslab_stats.o superslab_cache.o superslab_ace.o superslab_slab.o superslab_backend.o core/superslab_head_stub.o hakmem_smallmid.o tiny_sticky.o tiny_remote.o tiny_publish.o tiny_debug_ring.o hakmem_tiny_magazine.o hakmem_tiny_stats.o hakmem_tiny_sfc.o hakmem_tiny_query.o hakmem_tiny_rss.o hakmem_tiny_registry.o hakmem_tiny_remote_target.o hakmem_tiny_bg_spill.o tiny_adaptive_sizing.o hakmem_super_registry.o hakmem_shared_pool.o hakmem_shared_pool_acquire.o hakmem_shared_pool_release.o hakmem_elo.o hakmem_batch.o hakmem_p2.o hakmem_sizeclass_dist.o hakmem_evo.o hakmem_debug.o hakmem_sys.o hakmem_whale.o hakmem_policy.o hakmem_ace.o hakmem_ace_stats.o hakmem_prof.o hakmem_learner.o hakmem_size_hist.o hakmem_learn_log.o hakmem_syscall.o hakmem_ace_metrics.o hakmem_ace_ucb1.o hakmem_ace_controller.o tiny_fastcache.o core/box/superslab_expansion_box.o core/box/integrity_box.o core/box/free_publish_box.o core/box/mailbox_box.o core/box/front_gate_box.o core/box/front_gate_classifier.o core/box/capacity_box.o core/box/carve_push_box.o core/box/prewarm_box.o core/box/ss_hot_prewarm_box.o core/box/front_metrics_box.o core/box/bench_fast_box.o core/box/ss_addr_map_box.o core/box/slab_recycling_box.o core/box/pagefault_telemetry_box.o core/box/tiny_sizeclass_hist_box.o core/box/tiny_env_box.o core/box/tiny_route_box.o core/box/free_front_v3_env_box.o core/box/free_path_stats_box.o core/box/free_dispatch_stats_box.o core/box/alloc_gate_stats_box.o core/box/tiny_c6_ultra_free_box.o core/box/tiny_c5_ultra_free_box.o core/box/tiny_c4_ultra_free_box.o core/box/tiny_page_box.o core/box/tiny_class_policy_box.o core/box/tiny_class_stats_box.o core/box/tiny_policy_learner_box.o core/box/ss_budget_box.o core/box/tiny_mem_stats_box.o core/box/c7_meta_used_counter_box.o core/box/wrapper_env_box.o core/box/madvise_guard_box.o core/box/libm_reloc_guard_box.o core/box/ptr_trace_box.o core/box/link_missing_stubs.o core/box/super_reg_box.o core/box/shared_pool_box.o core/box/remote_side_box.o core/page_arena.o core/front/tiny_unified_cache.o core/tiny_alloc_fast_push.o core/tiny_c7_ultra_segment.o core/tiny_c7_ultra.o core/link_stubs.o core/tiny_failfast.o core/tiny_destructors.o core/smallobject_hotbox_v3.o core/smallobject_hotbox_v4.o core/smallobject_hotbox_v5.o core/smallsegment_v5.o core/smallobject_cold_iface_v5.o core/smallsegment_v6.o core/smallobject_cold_iface_v6.o core/smallobject_core_v6.o core/region_id_v6.o core/smallsegment_v7.o core/smallobject_cold_iface_v7.o core/mid_hotbox_v3.o bench_allocators_hakmem.o +BENCH_HAKMEM_OBJS_BASE = hakmem.o hakmem_config.o hakmem_tiny_config.o hakmem_ucb1.o hakmem_bigcache.o hakmem_pool.o hakmem_l25_pool.o hakmem_site_rules.o hakmem_tiny.o core/box/ss_allocation_box.o superslab_stats.o superslab_cache.o superslab_ace.o superslab_slab.o superslab_backend.o core/superslab_head_stub.o hakmem_smallmid.o tiny_sticky.o tiny_remote.o tiny_publish.o tiny_debug_ring.o hakmem_tiny_magazine.o hakmem_tiny_stats.o hakmem_tiny_sfc.o hakmem_tiny_query.o hakmem_tiny_rss.o hakmem_tiny_registry.o hakmem_tiny_remote_target.o hakmem_tiny_bg_spill.o tiny_adaptive_sizing.o hakmem_super_registry.o hakmem_shared_pool.o hakmem_shared_pool_acquire.o hakmem_shared_pool_release.o hakmem_elo.o hakmem_batch.o hakmem_p2.o hakmem_sizeclass_dist.o hakmem_evo.o hakmem_debug.o hakmem_sys.o hakmem_whale.o hakmem_policy.o hakmem_ace.o hakmem_ace_stats.o hakmem_prof.o hakmem_learner.o hakmem_size_hist.o hakmem_learn_log.o hakmem_syscall.o hakmem_ace_metrics.o hakmem_ace_ucb1.o hakmem_ace_controller.o tiny_fastcache.o core/box/superslab_expansion_box.o core/box/integrity_box.o core/box/free_publish_box.o core/box/mailbox_box.o core/box/front_gate_box.o core/box/front_gate_classifier.o core/box/capacity_box.o core/box/carve_push_box.o core/box/prewarm_box.o core/box/ss_hot_prewarm_box.o core/box/front_metrics_box.o core/box/bench_fast_box.o core/box/ss_addr_map_box.o core/box/slab_recycling_box.o core/box/pagefault_telemetry_box.o core/box/tiny_sizeclass_hist_box.o core/box/tiny_env_box.o core/box/tiny_route_box.o core/box/free_front_v3_env_box.o core/box/free_path_stats_box.o core/box/free_dispatch_stats_box.o core/box/alloc_gate_stats_box.o core/box/tiny_c6_ultra_free_box.o core/box/tiny_c5_ultra_free_box.o core/box/tiny_c4_ultra_free_box.o core/box/tiny_page_box.o core/box/tiny_class_policy_box.o core/box/tiny_class_stats_box.o core/box/tiny_policy_learner_box.o core/box/ss_budget_box.o core/box/tiny_mem_stats_box.o core/box/c7_meta_used_counter_box.o core/box/wrapper_env_box.o core/box/madvise_guard_box.o core/box/libm_reloc_guard_box.o core/box/ptr_trace_box.o core/box/link_missing_stubs.o core/box/super_reg_box.o core/box/shared_pool_box.o core/box/remote_side_box.o core/page_arena.o core/front/tiny_unified_cache.o core/tiny_alloc_fast_push.o core/tiny_c7_ultra_segment.o core/tiny_c7_ultra.o core/link_stubs.o core/tiny_failfast.o core/tiny_destructors.o core/smallobject_hotbox_v3.o core/smallobject_hotbox_v4.o core/smallobject_hotbox_v5.o core/smallsegment_v5.o core/smallobject_cold_iface_v5.o core/smallsegment_v6.o core/smallobject_cold_iface_v6.o core/smallobject_core_v6.o core/region_id_v6.o core/smallsegment_v7.o core/smallobject_cold_iface_v7.o core/mid_hotbox_v3.o core/smallobject_policy_v7.o bench_allocators_hakmem.o BENCH_HAKMEM_OBJS = $(BENCH_HAKMEM_OBJS_BASE) ifeq ($(POOL_TLS_PHASE1),1) BENCH_HAKMEM_OBJS += pool_tls.o pool_refill.o pool_tls_arena.o pool_tls_registry.o pool_tls_remote.o @@ -427,7 +427,7 @@ test-box-refactor: box-refactor ./larson_hakmem 10 8 128 1024 1 12345 4 # Phase 4: Tiny Pool benchmarks (properly linked with hakmem) -TINY_BENCH_OBJS_BASE = hakmem.o hakmem_config.o hakmem_tiny_config.o hakmem_ucb1.o hakmem_bigcache.o hakmem_pool.o hakmem_l25_pool.o hakmem_site_rules.o hakmem_tiny.o core/box/ss_allocation_box.o superslab_stats.o superslab_cache.o superslab_ace.o superslab_slab.o superslab_backend.o core/superslab_head_stub.o hakmem_smallmid.o core/box/superslab_expansion_box.o core/box/integrity_box.o core/box/mailbox_box.o core/box/front_gate_box.o core/box/front_gate_classifier.o core/box/free_publish_box.o core/box/capacity_box.o core/box/carve_push_box.o core/box/prewarm_box.o core/box/ss_hot_prewarm_box.o core/box/front_metrics_box.o core/box/bench_fast_box.o core/box/ss_addr_map_box.o core/box/slab_recycling_box.o core/box/pagefault_telemetry_box.o core/box/tiny_sizeclass_hist_box.o core/box/tiny_env_box.o core/box/tiny_route_box.o core/box/free_front_v3_env_box.o core/box/free_path_stats_box.o core/box/free_dispatch_stats_box.o core/box/alloc_gate_stats_box.o core/box/tiny_c6_ultra_free_box.o core/box/tiny_c5_ultra_free_box.o core/box/tiny_c4_ultra_free_box.o core/box/tiny_page_box.o core/box/tiny_class_policy_box.o core/box/tiny_class_stats_box.o core/box/tiny_policy_learner_box.o core/box/ss_budget_box.o core/box/tiny_mem_stats_box.o core/box/c7_meta_used_counter_box.o core/box/wrapper_env_box.o core/box/madvise_guard_box.o core/box/libm_reloc_guard_box.o core/box/ptr_trace_box.o core/box/link_missing_stubs.o core/box/super_reg_box.o core/box/shared_pool_box.o core/box/remote_side_box.o core/page_arena.o core/front/tiny_unified_cache.o tiny_sticky.o tiny_remote.o tiny_publish.o tiny_debug_ring.o hakmem_tiny_magazine.o hakmem_tiny_stats.o hakmem_tiny_sfc.o hakmem_tiny_query.o hakmem_tiny_rss.o hakmem_tiny_registry.o hakmem_tiny_remote_target.o hakmem_tiny_bg_spill.o tiny_adaptive_sizing.o hakmem_super_registry.o hakmem_shared_pool.o hakmem_shared_pool_acquire.o hakmem_shared_pool_release.o hakmem_elo.o hakmem_batch.o hakmem_p2.o hakmem_sizeclass_dist.o hakmem_evo.o hakmem_debug.o hakmem_sys.o hakmem_whale.o hakmem_policy.o hakmem_ace.o hakmem_ace_stats.o hakmem_prof.o hakmem_learner.o hakmem_size_hist.o hakmem_learn_log.o hakmem_syscall.o hakmem_ace_metrics.o hakmem_ace_ucb1.o hakmem_ace_controller.o tiny_fastcache.o core/tiny_alloc_fast_push.o core/tiny_c7_ultra_segment.o core/tiny_c7_ultra.o core/link_stubs.o core/tiny_failfast.o core/tiny_destructors.o core/smallobject_hotbox_v3.o core/smallobject_hotbox_v4.o core/smallobject_hotbox_v5.o core/smallsegment_v5.o core/smallobject_cold_iface_v5.o core/smallsegment_v6.o core/smallobject_cold_iface_v6.o core/smallobject_core_v6.o core/region_id_v6.o core/smallsegment_v7.o core/smallobject_cold_iface_v7.o core/mid_hotbox_v3.o +TINY_BENCH_OBJS_BASE = hakmem.o hakmem_config.o hakmem_tiny_config.o hakmem_ucb1.o hakmem_bigcache.o hakmem_pool.o hakmem_l25_pool.o hakmem_site_rules.o hakmem_tiny.o core/box/ss_allocation_box.o superslab_stats.o superslab_cache.o superslab_ace.o superslab_slab.o superslab_backend.o core/superslab_head_stub.o hakmem_smallmid.o core/box/superslab_expansion_box.o core/box/integrity_box.o core/box/mailbox_box.o core/box/front_gate_box.o core/box/front_gate_classifier.o core/box/free_publish_box.o core/box/capacity_box.o core/box/carve_push_box.o core/box/prewarm_box.o core/box/ss_hot_prewarm_box.o core/box/front_metrics_box.o core/box/bench_fast_box.o core/box/ss_addr_map_box.o core/box/slab_recycling_box.o core/box/pagefault_telemetry_box.o core/box/tiny_sizeclass_hist_box.o core/box/tiny_env_box.o core/box/tiny_route_box.o core/box/free_front_v3_env_box.o core/box/free_path_stats_box.o core/box/free_dispatch_stats_box.o core/box/alloc_gate_stats_box.o core/box/tiny_c6_ultra_free_box.o core/box/tiny_c5_ultra_free_box.o core/box/tiny_c4_ultra_free_box.o core/box/tiny_page_box.o core/box/tiny_class_policy_box.o core/box/tiny_class_stats_box.o core/box/tiny_policy_learner_box.o core/box/ss_budget_box.o core/box/tiny_mem_stats_box.o core/box/c7_meta_used_counter_box.o core/box/wrapper_env_box.o core/box/madvise_guard_box.o core/box/libm_reloc_guard_box.o core/box/ptr_trace_box.o core/box/link_missing_stubs.o core/box/super_reg_box.o core/box/shared_pool_box.o core/box/remote_side_box.o core/page_arena.o core/front/tiny_unified_cache.o tiny_sticky.o tiny_remote.o tiny_publish.o tiny_debug_ring.o hakmem_tiny_magazine.o hakmem_tiny_stats.o hakmem_tiny_sfc.o hakmem_tiny_query.o hakmem_tiny_rss.o hakmem_tiny_registry.o hakmem_tiny_remote_target.o hakmem_tiny_bg_spill.o tiny_adaptive_sizing.o hakmem_super_registry.o hakmem_shared_pool.o hakmem_shared_pool_acquire.o hakmem_shared_pool_release.o hakmem_elo.o hakmem_batch.o hakmem_p2.o hakmem_sizeclass_dist.o hakmem_evo.o hakmem_debug.o hakmem_sys.o hakmem_whale.o hakmem_policy.o hakmem_ace.o hakmem_ace_stats.o hakmem_prof.o hakmem_learner.o hakmem_size_hist.o hakmem_learn_log.o hakmem_syscall.o hakmem_ace_metrics.o hakmem_ace_ucb1.o hakmem_ace_controller.o tiny_fastcache.o core/tiny_alloc_fast_push.o core/tiny_c7_ultra_segment.o core/tiny_c7_ultra.o core/link_stubs.o core/tiny_failfast.o core/tiny_destructors.o core/smallobject_hotbox_v3.o core/smallobject_hotbox_v4.o core/smallobject_hotbox_v5.o core/smallsegment_v5.o core/smallobject_cold_iface_v5.o core/smallsegment_v6.o core/smallobject_cold_iface_v6.o core/smallobject_core_v6.o core/region_id_v6.o core/smallsegment_v7.o core/smallobject_cold_iface_v7.o core/mid_hotbox_v3.o core/smallobject_policy_v7.o TINY_BENCH_OBJS = $(TINY_BENCH_OBJS_BASE) ifeq ($(POOL_TLS_PHASE1),1) TINY_BENCH_OBJS += pool_tls.o pool_refill.o core/pool_tls_arena.o pool_tls_registry.o pool_tls_remote.o diff --git a/core/box/smallobject_policy_v7_box.h b/core/box/smallobject_policy_v7_box.h new file mode 100644 index 00000000..c07ef645 --- /dev/null +++ b/core/box/smallobject_policy_v7_box.h @@ -0,0 +1,49 @@ +// smallobject_policy_v7_box.h - SmallObject Policy v7 (Phase v7-4) +// +// Purpose: +// - Centralized routing policy for ULTRA / v7 / MID_v3 / LEGACY +// - Single source of truth for class → route_kind mapping +// - ENV configuration managed in one place (L3 Policy layer) + +#ifndef HAKMEM_SMALLOBJECT_POLICY_V7_BOX_H +#define HAKMEM_SMALLOBJECT_POLICY_V7_BOX_H + +#include +#include + +// ============================================================================ +// Route Kind Enum (L0/L1/L1' layer selection) +// ============================================================================ + +typedef enum { + SMALL_ROUTE_ULTRA, // L0: C4-C7 ULTRA (FROZEN) + SMALL_ROUTE_V7, // L1: SmallObject v7 (research box) + SMALL_ROUTE_MID_V3, // L1': MID v3 (257-768B mid/small) + SMALL_ROUTE_LEGACY, // L1': TinyHeap v1 / Pool v1 (fallback) +} SmallRouteKind; + +// ============================================================================ +// Policy Snapshot Structure +// ============================================================================ + +typedef struct SmallPolicyV7 { + SmallRouteKind route_kind[8]; // C0-C7 routing decision +} SmallPolicyV7; + +// ============================================================================ +// Policy API +// ============================================================================ + +/// Get policy snapshot (read-only, TLS cached) +/// Frontend calls this to determine route_kind[class_idx] +const SmallPolicyV7* small_policy_v7_snapshot(void); + +/// Initialize policy from ENV variables (called once at startup) +/// Priority: ULTRA > v7 > MID_v3 > LEGACY +/// @param policy: Policy structure to initialize +void small_policy_v7_init_from_env(SmallPolicyV7* policy); + +/// Get route kind name for debugging +const char* small_route_kind_name(SmallRouteKind kind); + +#endif // HAKMEM_SMALLOBJECT_POLICY_V7_BOX_H diff --git a/core/front/malloc_tiny_fast.h b/core/front/malloc_tiny_fast.h index 239c1aea..facfdddb 100644 --- a/core/front/malloc_tiny_fast.h +++ b/core/front/malloc_tiny_fast.h @@ -46,6 +46,7 @@ #include "../box/smallobject_core_v6_box.h" // SmallObject Core v6 (Phase V6-HDR-2) #include "../box/smallobject_v6_env_box.h" // SmallObject v6 ENV control (Phase V6-HDR-2) #include "../box/smallobject_hotbox_v7_box.h" // SmallObject HotBox v7 stub (Phase v7-1) +#include "../box/smallobject_policy_v7_box.h" // Phase v7-4: Policy Box #include "../box/tiny_c7_ultra_box.h" // C7 ULTRA stub (UF-1, delegates to v3) #include "../box/tiny_c6_ultra_free_box.h" // Phase 4-2: C6 ULTRA-free (free-only, C6-only) #include "../box/tiny_c5_ultra_free_box.h" // Phase 5-1/5-2: C5 ULTRA-free + alloc integration @@ -223,9 +224,19 @@ static inline void* malloc_tiny_fast(size_t size) { } } + // Phase v7-4: Check Policy Box for v7 routing (before switch) + const SmallPolicyV7* policy = small_policy_v7_snapshot(); + if (policy->route_kind[class_idx] == SMALL_ROUTE_V7) { + void* v7p = small_heap_alloc_fast_v7_stub(size, (uint8_t)class_idx); + if (TINY_HOT_LIKELY(v7p != NULL)) { + return v7p; + } + // v7 stub returned NULL -> fallback to legacy + } + switch (route) { case TINY_ROUTE_SMALL_HEAP_V7: { - // Phase v7-1: C6-only v7 stub (MID v3 fallback) + // Phase v7-4: v7 routing now handled by Policy Box above (kept for legacy compatibility) void* v7p = small_heap_alloc_fast_v7_stub(size, (uint8_t)class_idx); if (TINY_HOT_LIKELY(v7p != NULL)) { return v7p; @@ -394,10 +405,20 @@ static inline int free_tiny_fast(void* ptr) { return 1; } + // Phase v7-4: Check Policy Box for v7 routing (before route lookup) + const SmallPolicyV7* policy = small_policy_v7_snapshot(); + if (class_idx == 6 && policy->route_kind[class_idx] == SMALL_ROUTE_V7) { + if (small_heap_free_fast_v7_stub(ptr, (uint8_t)class_idx)) { + FREE_PATH_STAT_INC(smallheap_v7_fast); + return 1; + } + // v7 returned false (ptr not in v7 segment) -> fallback to legacy below + } + tiny_route_kind_t route = tiny_route_for_class((uint8_t)class_idx); - // Phase v7-2: v7 early-exit for C6 (v7 uses separate mmap segment, not SuperSlab) - // Must check BEFORE ss_fast_lookup since v7 pointers won't be in SuperSlab registry + // Phase v7-2: v7 early-exit for C6 (legacy path, now handled by Policy Box above) + // Kept for compatibility with old routing system if (class_idx == 6 && route == TINY_ROUTE_SMALL_HEAP_V7) { if (small_heap_free_fast_v7_stub(ptr, (uint8_t)class_idx)) { FREE_PATH_STAT_INC(smallheap_v7_fast); diff --git a/core/smallobject_policy_v7.c b/core/smallobject_policy_v7.c new file mode 100644 index 00000000..1181bc26 --- /dev/null +++ b/core/smallobject_policy_v7.c @@ -0,0 +1,118 @@ +// smallobject_policy_v7.c - Policy Box implementation + +#include +#include +#include +#include "box/smallobject_policy_v7_box.h" + +#ifndef likely +#define likely(x) __builtin_expect(!!(x), 1) +#define unlikely(x) __builtin_expect(!!(x), 0) +#endif + +// ============================================================================ +// TLS Policy Snapshot +// ============================================================================ + +static __thread SmallPolicyV7 g_small_policy_v7; +static __thread int g_small_policy_v7_init = 0; + +const SmallPolicyV7* small_policy_v7_snapshot(void) { + if (unlikely(!g_small_policy_v7_init)) { + small_policy_v7_init_from_env(&g_small_policy_v7); + g_small_policy_v7_init = 1; + } + return &g_small_policy_v7; +} + +// ============================================================================ +// ENV Helpers +// ============================================================================ + +static inline bool env_enabled(const char* name) { + const char* e = getenv(name); + return (e && *e && *e != '0'); +} + +static inline uint32_t env_class_mask(const char* name, uint32_t default_mask) { + const char* e = getenv(name); + if (e && *e) { + return (uint32_t)strtoul(e, NULL, 0); + } + return default_mask; +} + +// ============================================================================ +// Policy Initialization from ENV +// ============================================================================ + +void small_policy_v7_init_from_env(SmallPolicyV7* policy) { + if (!policy) return; + + // Default: all classes go to LEGACY + for (int i = 0; i < 8; i++) { + policy->route_kind[i] = SMALL_ROUTE_LEGACY; + } + + // Priority 3: MID_v3 (257-768B, C5-C6 range) + // ENV: HAKMEM_MID_V3_ENABLED, HAKMEM_MID_V3_CLASSES + if (env_enabled("HAKMEM_MID_V3_ENABLED")) { + uint32_t mid_mask = env_class_mask("HAKMEM_MID_V3_CLASSES", 0x60); // C5-C6 default + for (int i = 0; i < 8; i++) { + if (mid_mask & (1u << i)) { + policy->route_kind[i] = SMALL_ROUTE_MID_V3; + } + } + } + + // Priority 2: SmallObject v7 (research box, C6-only for now) + // ENV: HAKMEM_SMALL_HEAP_V7_ENABLED, HAKMEM_SMALL_HEAP_V7_CLASSES + if (env_enabled("HAKMEM_SMALL_HEAP_V7_ENABLED")) { + uint32_t v7_mask = env_class_mask("HAKMEM_SMALL_HEAP_V7_CLASSES", 0x40); // C6 default + for (int i = 0; i < 8; i++) { + if (v7_mask & (1u << i)) { + policy->route_kind[i] = SMALL_ROUTE_V7; + } + } + } + + // Priority 1: ULTRA (highest priority, C4-C7) + // ENV: HAKMEM_TINY_C7_ULTRA_ENABLED (C7), HAKMEM_TINY_C6_ULTRA_FREE_ENABLED (C6), etc. + // Note: For now, we check individual ULTRA ENV vars + + // C7 ULTRA (default ON) + if (env_enabled("HAKMEM_TINY_C7_ULTRA_ENABLED")) { + policy->route_kind[7] = SMALL_ROUTE_ULTRA; + } + + // C6 ULTRA (if C6 ULTRA free is enabled, route to ULTRA) + // Note: This is a free-only optimization, not full ULTRA for C6 yet + // Keep C6 routing as-is (v7 or MID_v3) for now + + // C4-C5 ULTRA (if enabled via ENV) + // TODO: Add HAKMEM_TINY_C4_ULTRA_ENABLED / C5 when implemented + + // Debug output (if needed) + static int g_debug_once = 0; + if (!g_debug_once) { + g_debug_once = 1; + fprintf(stderr, "[POLICY_V7_INIT] Route assignments:\n"); + for (int i = 0; i < 8; i++) { + fprintf(stderr, " C%d: %s\n", i, small_route_kind_name(policy->route_kind[i])); + } + } +} + +// ============================================================================ +// Utility +// ============================================================================ + +const char* small_route_kind_name(SmallRouteKind kind) { + switch (kind) { + case SMALL_ROUTE_ULTRA: return "ULTRA"; + case SMALL_ROUTE_V7: return "V7"; + case SMALL_ROUTE_MID_V3: return "MID_V3"; + case SMALL_ROUTE_LEGACY: return "LEGACY"; + default: return "UNKNOWN"; + } +} diff --git a/docs/analysis/SMALLOBJECT_V7_DESIGN.md b/docs/analysis/SMALLOBJECT_V7_DESIGN.md index 9db1cc6a..de8cd1ba 100644 --- a/docs/analysis/SMALLOBJECT_V7_DESIGN.md +++ b/docs/analysis/SMALLOBJECT_V7_DESIGN.md @@ -340,3 +340,51 @@ RegionIdBox / SegmentBox_v7 / PageStatsBox --- +## 7. Phase v7-4: Policy Box (L3 層の明確化) + +### Policy Box の役割 + +SmallPolicyV7 Box を L3 に配置し、「どのクラスをどの層に送るか」を一元管理: + +```c +typedef struct SmallPolicyV7 { + SmallRouteKind route_kind[8]; // C0-C7 +} SmallPolicyV7; + +const SmallPolicyV7* small_policy_v7_snapshot(void); +``` + +### フロントの責務 + +フロントは Policy Snapshot を読んで route を選ぶだけ: + +```c +const SmallPolicyV7* policy = small_policy_v7_snapshot(); +SmallRouteKind route = policy->route_kind[class_idx]; + +switch (route) { + case SMALL_ROUTE_ULTRA: // L0 + case SMALL_ROUTE_V7: // L1 + case SMALL_ROUTE_MID_V3: // L1' + case SMALL_ROUTE_LEGACY: // L1' +} +``` + +### ENV の一元化 + +ENV 変数は Policy init で一度だけ読む: +- `HAKMEM_TINY_C7_ULTRA_ENABLED` +- `HAKMEM_SMALL_HEAP_V7_ENABLED` + `HAKMEM_SMALL_HEAP_V7_CLASSES` +- `HAKMEM_MID_V3_ENABLED` + `HAKMEM_MID_V3_CLASSES` + +優先順位: ULTRA > v7 > MID_v3 > LEGACY (固定) + +将来的にはクラスごとの柔軟な優先順位設定や、Learner 連携による動的ルート選択も可能。 + +### 段階移行 + +Phase v7-4 では v7 関連のみ Policy box 経由に変更。 +ULTRA/MID_v3/LEGACY は既存の `tiny_route_env_box.h` を併用(後で統合予定)。 + +--- + diff --git a/hakmem.d b/hakmem.d index af412cff..e1851e0d 100644 --- a/hakmem.d +++ b/hakmem.d @@ -116,6 +116,7 @@ hakmem.o: core/hakmem.c core/hakmem.h core/hakmem_build_flags.h \ core/box/../front/../box/smallsegment_v7_box.h \ core/box/../front/../box/smallobject_cold_iface_v7_box.h \ core/box/../front/../box/region_id_v6_box.h \ + core/box/../front/../box/smallobject_policy_v7_box.h \ core/box/../front/../box/tiny_c7_ultra_box.h \ core/box/../front/../box/tiny_c7_ultra_segment_box.h \ core/box/../front/../box/tiny_c6_ultra_free_box.h \ @@ -329,6 +330,7 @@ core/box/../front/../box/smallobject_hotbox_v7_box.h: core/box/../front/../box/smallsegment_v7_box.h: core/box/../front/../box/smallobject_cold_iface_v7_box.h: core/box/../front/../box/region_id_v6_box.h: +core/box/../front/../box/smallobject_policy_v7_box.h: core/box/../front/../box/tiny_c7_ultra_box.h: core/box/../front/../box/tiny_c7_ultra_segment_box.h: core/box/../front/../box/tiny_c6_ultra_free_box.h: