Files
hakmem/README_PERF_ANALYSIS.md

161 lines
6.2 KiB
Markdown
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

# HAKMEM Allocator Performance Analysis Results
**最新メモ (2025-12-05)**: C7 Warm/TLS Bind は本番経路を Bind-only (mode=1) に統一。Debug では `HAKMEM_WARM_TLS_BIND_C7=0/1/2` で切替可能だが、Release は常に mode=1 固定。C7-only ワークロードでは mode=1 が legacy (mode=0) 比で ~410x 速く、mode=2 は TLS carve 実験として残置。
**追記 (2025-12-05, Release 修復)**: Release だけ C7 Warm が死んでいた原因は「満杯 C7 slab が Shared Pool に居残り、空スラブが Warm に渡っていなかった」こと。Acquire で C7 は空スラブ限定、Release でメタをリセットするガードを導入し、C7-only Release で ~18.8M ops/s、Random Mixed Release で ~2728M ops/s まで回復。
**追記 (2025-12-05, Policy Box)**: `TinyClassPolicyBox` を導入し、`HAKMEM_TINY_POLICY_PROFILE=legacy|c5_7_only|tinyplus_all` で Page/Warm ポリシーを切替可能にした。現状 legacyPageBox= C5C7, Warm= 全クラス cap 4/8でランダム混在 Release は ~4.9M ops/s と低下しており、Warm 道の有効化状態を追加調査中。
**分析実施日**: 2025-11-28
**分析対象**: HAKMEM allocator (commit 0ce20bb83)
**ベンチマーク**: bench_random_mixed (1,000,000 ops, working set=256)
**ツール**: Linux perf (record/stat/report/annotate)
---
## 📊 クイックサマリー
| 指標 | HAKMEM | System malloc | 差分 |
|------|--------|---------------|------|
| **Throughput** | 55.7M ops/s | 86.7M ops/s | -35.7% ❌ |
| **IPC** | 2.12 | 2.09 | +1.4% ✅ |
| **Branch Miss** | 2.56% | 3.39% | -24.5% ✅ |
| **Cache Miss** | 9.28% | 13.26% | -30.0% ✅ |
| **Instructions** | 167M | 94M | +78.4% ❌ |
| **Cache Refs** | 1.86M | 340K | +448% ❌ |
**結論**: 効率は良いが仕事量が多すぎる → 不要な処理の削減が最優先
---
## 📁 生成ファイル一覧
### メインレポート
1. **PERF_ANALYSIS_EXECUTIVE_SUMMARY.md**
- エグゼクティブサマリー
- トップ3ボトルネック
- 最適化ロードマップ (Phase 1-3)
- すぐに試せるコマンド
2. **perf_analysis_summary.md**
- 詳細分析レポート (9.4KB)
- 全ボトルネックの説明
- 優先度付き最適化提案 (5項目)
- 実測効果予測 (楽観的/保守的シナリオ)
3. **perf_comparison_chart.txt**
- 視覚的比較チャート
- ASCII バーグラフ形式
### 生データ
4. **perf_hakmem_stats.txt** - HAKMEM の perf stat 出力
5. **perf_system_stats.txt** - System malloc の perf stat 出力
6. **perf_hakmem_hotspots.txt** - ホットスポット詳細 (perf report)
7. **perf_hakmem_callgraph.txt** - コールグラフ (perf report -g)
8. **perf_annotate_malloc.txt** - malloc 関数のアセンブリアノテーション
---
## 🔥 トップ3ボトルネック
### 1. SuperSlab 初期化 (23.83% CPU時間)
- **原因**: 4つの memset() で 1MB-2MB をゼロクリア
- **対策**: memset 削除 (mmap は既にゼロ初期化済み)
- **効果**: +10-15% throughput
- **実装箇所**: `/mnt/workdisk/public_share/hakmem/core/hakmem_tiny_superslab.c:912-915`
### 2. 多層分岐ルーティング (36M branches)
- **原因**: smallmid → tiny → mid → ACE の順次チェック
- **対策**: Dispatch table で一発判定
- **効果**: +5-8% throughput
### 3. 過剰なキャッシュアクセス (1.86M refs)
- **原因**: SuperSlab メタデータが大きい、Hot/Cold 混在
- **対策**: Cache line 最適化 (hot fields を先頭64Bに集約)
- **効果**: +3-5% cache efficiency
---
## 🎯 推奨アクション
### すぐに試せる (1-2日)
```bash
cd /mnt/workdisk/public_share/hakmem
# 1. memset 削除版
cp core/hakmem_tiny_superslab.c core/hakmem_tiny_superslab.c.bak
sed -i '912,915s/^/\/\/ PERF_OPT: /' core/hakmem_tiny_superslab.c
make clean && make
./bench_random_mixed_hakmem 1000000 256 42
# 2. mincore 無効化版
make clean
make CFLAGS="-DHAKMEM_DISABLE_MINCORE_CHECK=1"
./bench_random_mixed_hakmem 1000000 256 42
```
**期待効果**: 55M → **63-67M ops/s** (+13-20%)
---
## 📈 最適化ロードマップ
| Phase | 実装期間 | 目標 | 施策 |
|-------|---------|------|------|
| **Phase 1** | 1-2日 | 65M ops/s (+17%) | memset削除, mincore無効化, likely hints |
| **Phase 2** | 1週間 | 77M ops/s (+39%) | dispatch table, 512KB SuperSlab, LTO, cache line |
| **Phase 3** | 2週間 | 詳細分析 | Valgrind, MT bench, workload別最適化 |
---
## 🛠️ 使用したコマンド
### プロファイリング
```bash
# ホットスポット分析
perf record -g ./bench_random_mixed_hakmem 1000000 256 42
perf report --stdio | head -100 > perf_hakmem_hotspots.txt
# 統計比較
perf stat -e cycles,instructions,cache-references,cache-misses,branches,branch-misses \
./bench_random_mixed_hakmem 1000000 256 42 2>&1 | tee perf_hakmem_stats.txt
perf stat -e cycles,instructions,cache-references,cache-misses,branches,branch-misses \
./bench_random_mixed_system 1000000 256 42 2>&1 | tee perf_system_stats.txt
# コールグラフ
perf report --stdio -g graph,0.5,caller --no-children | head -200 > perf_hakmem_callgraph.txt
# アノテーション
perf annotate --stdio malloc > perf_annotate_malloc.txt
```
---
## 📚 関連ファイル
### ソースコード
- `/mnt/workdisk/public_share/hakmem/core/box/hak_alloc_api.inc.h` - Alloc API
- `/mnt/workdisk/public_share/hakmem/core/box/hak_free_api.inc.h` - Free API
- `/mnt/workdisk/public_share/hakmem/core/hakmem_tiny_superslab.c` - SuperSlab 実装
- `/mnt/workdisk/public_share/hakmem/core/hakmem_shared_pool.c` - Shared Pool
### ベンチマーク
- `/mnt/workdisk/public_share/hakmem/bench_random_mixed_hakmem` - HAKMEM版
- `/mnt/workdisk/public_share/hakmem/bench_random_mixed_system` - System malloc版
---
## 🔍 詳細を読むには
1. **全体像を把握**: `PERF_ANALYSIS_EXECUTIVE_SUMMARY.md` を読む (5分)
2. **詳細な分析**: `perf_analysis_summary.md` を読む (15分)
3. **生データ確認**: `perf_hakmem_*.txt` ファイル群を参照
---
**次のステップ**:
1. `PERF_ANALYSIS_EXECUTIVE_SUMMARY.md` を開く
2. "すぐに試せるコマンド" を実行
3. スループット改善を確認
**質問・フィードバック**: 分析者 Claude Code (Sonnet 4.5)