Add perf analysis reports from Task agent

Generated comprehensive performance analysis reports:
- PERF_ANALYSIS_EXECUTIVE_SUMMARY.md: Executive summary with key findings
- README_PERF_ANALYSIS.md: Index and navigation guide
- perf_analysis_summary.md: Detailed bottleneck analysis

Key findings:
- HAKMEM: 55.7M ops/s vs System: 86.7M ops/s (-35.7%)
- Top bottleneck: SuperSlab memset (23.83% CPU time)
- Quick win: Remove redundant memset → +10-15% throughput
- Phase 1 optimizations target: 65M ops/s (+17%)
This commit is contained in:
Moe Charm (CI)
2025-11-28 17:51:00 +09:00
parent 3df38074a2
commit 53bc92842b
3 changed files with 632 additions and 0 deletions

View File

@ -0,0 +1,212 @@
# HAKMEM アロケータ パフォーマンス分析 - エグゼクティブサマリー
## 📊 現状評価
### スループット比較
- **HAKMEM**: 55.7M ops/s
- **System (glibc)**: 86.7M ops/s
- **差分**: -35.7% (HAKMEM が遅い)
### 効率指標
**良好な指標**:
- IPC (Instructions Per Cycle): 2.12 vs 2.09 (HAKMEM が若干上)
- Branch Miss率: 2.56% vs 3.39% (HAKMEM が良好)
- Cache Miss率: 9.28% vs 13.26% (HAKMEM が良好)
**問題のある指標**:
- Instructions: 167M vs 94M (+78%, HAKMEM が1.8倍多い)
- Cache References: 1.86M vs 340K (+448%, HAKMEM が5.5倍多い)
- Branches: 36M vs 16.6M (+117%, HAKMEM が2.2倍多い)
### 🔍 本質的な問題
**HAKMEM は「仕事の質」は高いが「仕事の量」が多すぎる**
- 分岐予測・キャッシュ効率は優秀
- しかし、不要な処理が多く、全体のスループットを低下させている
---
## 🔥 トップ3ボトルネック
### 1位: SuperSlab 初期化の Page Fault (23.83% CPU時間)
**症状**:
```
shared_pool_acquire_slab()
→ memset(1MB-2MB)
→ page fault
→ kernel clear_page_erms (4.46%)
```
**原因**:
- SuperSlab 獲得時に 1MB-2MB のメモリをゼロクリア
- mmap は既にゼロページを返すのに、4つの memset() を実行
- Lines 912-915 in hakmem_tiny_superslab.c
**即効性のある対策**:
```bash
# 4つの memset() をコメントアウト
# 理由: mmap(MAP_ANONYMOUS) は既にゼロ初期化されている
# 推定効果: +10-15% throughput
```
---
### 2位: 多層分岐ルーティング (36M branches, 2.2倍)
**症状**:
- malloc 1回あたり 36 branches (system は 16.6)
- smallmid → tiny → mid → ACE の順次チェック
**原因**:
- Front Gate の domain 分類が複雑
- サイズレンジごとに個別判定
**対策**:
```c
// Dispatch table で一発判定
void* (*allocator)(size_t) = size_dispatch_table[size >> 8];
return allocator(size);
```
**推定効果**: +5-8% throughput
---
### 3位: 過剰なキャッシュアクセス (1.86M refs, 5.5倍)
**症状**:
- Cache references が system の 5.5倍
- Miss率は良好だが、アクセス回数自体が多すぎる
**原因**:
- SuperSlab メタデータが大きい
- Hot/Cold フィールドが混在
**対策**:
```c
// Hot fields を先頭64バイトに集約
struct SuperSlab {
// Cache line 0 (HOT)
uint64_t magic;
void* freelist;
uint32_t used;
uint32_t capacity;
// ...
// Cache line N (COLD)
uint64_t last_used_ns;
uint64_t generation;
// ...
} __attribute__((aligned(64)));
```
**推定効果**: +3-5% cache efficiency
---
## 🎯 最適化ロードマップ
### Phase 1: Quick Wins (1-2日, +17% 目標)
1.**memset 削除** (lines 912-915)
- 4つの memset() をコメントアウト
- 効果: +12%
2.**Front Gate に `__builtin_expect()` 追加**
- Tiny path を likely に設定
- 効果: +2-3%
3.**HAKMEM_DISABLE_MINCORE_CHECK=1 でビルド**
- mincore() syscall を削減
- 効果: +2-3%
**Phase 1 合計**: 55M → **65M ops/s** (system の 75%)
---
### Phase 2: 構造的改善 (1週間, +24% 追加目標)
1. **Front Gate dispatch table 実装**
- サイズ → アロケータの直接マッピング
- 効果: +5-8%
2. **SuperSlab サイズを 512KB に縮小**
- 初期化コストを半減
- 効果: +5-7%
3. **LTO 有効化**
- `-flto -fwhole-program`
- 効果: +3-5%
4. **Cache line 最適化**
- SuperSlab 構造体の再配置
- 効果: +3-5%
**Phase 2 合計**: 65M → **77M ops/s** (system の 89%)
---
### Phase 3: 詳細分析 (2週間)
- Valgrind/heaptrack でメモリプロファイリング
- マルチスレッドベンチマーク
- Workload 別の最適化
---
## 📈 予測効果まとめ
| シナリオ | スループット | System比 | 実装期間 |
|---------|------------|---------|---------|
| 現状 | 55.7M ops/s | 64.3% | - |
| Phase 1 (保守的) | 65.2M ops/s | 75.2% | 1-2日 |
| Phase 2 (楽観的) | 77.5M ops/s | 89.4% | 1週間 |
---
## 🛠️ すぐに試せるコマンド
### 1. memset 削除版のビルド
```bash
cd /mnt/workdisk/public_share/hakmem
# Backup
cp core/hakmem_tiny_superslab.c core/hakmem_tiny_superslab.c.bak
# Edit lines 912-915 (comment out memset)
sed -i '912,915s/^/\/\/ PERF_OPT: /' core/hakmem_tiny_superslab.c
# Rebuild
make clean && make
# Benchmark
./bench_random_mixed_hakmem 1000000 256 42
```
### 2. mincore 無効化版のビルド
```bash
make clean
make CFLAGS="-DHAKMEM_DISABLE_MINCORE_CHECK=1"
./bench_random_mixed_hakmem 1000000 256 42
```
### 3. 両方を適用
```bash
make clean
make CFLAGS="-DHAKMEM_DISABLE_MINCORE_CHECK=1"
# + memset コメントアウト済み
./bench_random_mixed_hakmem 1000000 256 42
```
**期待される結果**: 55M → **63-67M ops/s** (+13-20%)
---
## 📁 生成ファイル一覧
1. **perf_analysis_summary.md** - 詳細分析レポート (このファイル)
2. **perf_comparison_chart.txt** - 視覚的比較チャート
3. **perf_hakmem_hotspots.txt** - ホットスポット詳細
4. **perf_hakmem_stats.txt** - HAKMEM 統計
5. **perf_system_stats.txt** - System malloc 統計
6. **perf_hakmem_callgraph.txt** - コールグラフ
7. **perf_annotate_malloc.txt** - malloc 関数のアノテーション
8. **PERF_ANALYSIS_EXECUTIVE_SUMMARY.md** - エグゼクティブサマリー
---
**分析完了日時**: 2025-11-28
**使用ツール**: perf record/stat/report
**ベンチマーク**: bench_random_mixed (1M ops, WS=256)
**分析者**: Claude Code (Sonnet 4.5)

156
README_PERF_ANALYSIS.md Normal file
View File

@ -0,0 +1,156 @@
# HAKMEM Allocator Performance Analysis Results
**分析実施日**: 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)

264
perf_analysis_summary.md Normal file
View File

@ -0,0 +1,264 @@
# HAKMEM アロケータ パフォーマンス分析レポート
## 1. 実行環境
- ベンチマーク: bench_random_mixed (1,000,000 ops, working set=256)
- プラットフォーム: Linux x86_64
- perf_event_paranoid: 1
## 2. パフォーマンス統計比較
### HAKMEM アロケータ
```
Throughput: 55,705,335 ops/s
Time: 0.018s
Cycles: 78,648,007
Instructions: 167,028,675
IPC: 2.12
Cache References: 1,861,523
Cache Misses: 172,740 (9.28%)
Branches: 35,993,722
Branch Misses: 921,505 (2.56%)
```
### System malloc (glibc)
```
Throughput: 86,683,664 ops/s (+55.6% faster)
Time: 0.012s
Cycles: 44,872,364 (-43.0% fewer cycles)
Instructions: 93,643,012 (-43.9% fewer instructions)
IPC: 2.09 (similar)
Cache References: 339,504 (-81.8% fewer)
Cache Misses: 45,009 (-73.9% fewer)
Branches: 16,572,153 (-53.9% fewer)
Branch Misses: 561,229 (-39.1% fewer)
```
## 3. ホットスポット分析
### CPU時間消費 (Top Functions)
1. **malloc**: 26.10% (self) / 39.22% (children)
2. **free**: 19.47% (self) / 40.23% (children)
3. **shared_pool_acquire_slab**: 23.83% (children only)
4. **Page fault handling**: 25.55% (kernel time)
### 主要な発見
- **Page faults が顕著**: 25.55% が asm_exc_page_fault 経由
- `shared_pool_acquire_slab``__memset_avx2_unaligned_erms` → page fault
- `clear_page_erms` が 4.46% を消費
- **memset overhead**: `__memset_avx2_unaligned_erms` が 6.41%
- **メモリ管理のオーバーヘッド**: kernel の folio/page 管理が目立つ
## 4. ボトルネック特定
### 1. shared_pool_acquire_slab() の初期化コスト
- **問題**: memset → page fault → kernel page allocation
- **影響**: 23.83% の CPU 時間
- **原因**: SuperSlab 獲得時の large memory clear (1MB/2MB)
### 2. 過剰なキャッシュ参照
- **HAKMEM**: 1,861,523 cache refs (system の 5.5倍)
- **Cache miss率**: 9.28% (system は 13.26% だがアクセス量が少ない)
- **分析**: データ構造のメモリフットプリントが大きい
### 3. 分岐数の多さ
- **HAKMEM**: 35,993,722 branches (system の 2.2倍)
- **Branch miss率**: 2.56% (system は 3.39%)
- **分析**: 複雑な分岐ロジック (Front Gate, domain routing, etc.)
### 4. 命令数の多さ
- **HAKMEM**: 167M instructions (system の 1.8倍)
- **分析**: 多層的なアロケーション戦略 (Tiny/Mid/ACE/BigCache)
## 5. 最適化提案(優先度順)
### 優先度1: SuperSlab 初期化の遅延化・削減
**問題**: `shared_pool_acquire_slab()` で 23.83% の CPU 時間を消費
- memset による 1MB-2MB のゼロクリア
- Page fault による kernel page allocation (clear_page_erms: 4.46%)
**提案**:
1. **Lazy Initialization**: メタデータのゼロクリアを遅延化
- mmap(MAP_ANONYMOUS) は既にゼロページを返すため、明示的な memset は不要
- 4つの memset() 呼び出しlines 912-915を削除可能
- 推定効果: **10-15% throughput 向上**
2. **On-Demand Page Faulting**:
- MADV_DONTNEED または MADV_FREE を活用
- 実際に使用される slab だけ page fault を起こす
- 推定効果: **5-10% throughput 向上**
3. **SuperSlab Size 調整**:
- 現状: 1MB (lg=20) or 2MB (lg=21)
- 提案: 512KB (lg=19) or 256KB (lg=18) をデフォルトに
- 推定効果: **初期化コスト 50-75% 削減**
**実装箇所**: `/mnt/workdisk/public_share/hakmem/core/hakmem_tiny_superslab.c:912-915`
---
### 優先度2: Front Gate 分岐最適化
**問題**: 35,993,722 branches (system の 2.2倍)
- Branch miss率は良好 (2.56%) だが絶対数が多い
- malloc/free の多層ルーティング (Tiny/Mid/ACE/BigCache)
**提案**:
1. **Fast Path 統合**:
- 現状: smallmid → tiny → mid → ACE の順でチェック
- 提案: サイズレンジを統合して判定を1回に削減
```c
// Before: 4 separate checks
if (smallmid_is_in_range(size)) { ... }
if (size <= tiny_get_max_size()) { ... }
if (mid_is_in_range(size)) { ... }
if (size < threshold) { ... }
// After: Single dispatch table
void* (*allocator)(size_t) = size_dispatch_table[size >> 8];
return allocator(size);
```
- 推定効果: **5-8% throughput 向上**
2. **Likely/Unlikely Hints 追加**:
- `/mnt/workdisk/public_share/hakmem/core/box/hak_alloc_api.inc.h:43-78`
- 使用頻度が高い Tiny path に `__builtin_expect()`
- 推定効果: **2-3% throughput 向上**
---
### 優先度3: キャッシュライン最適化
**問題**: 1,861,523 cache refs (system の 5.5倍)
- Cache miss率は 9.28% と許容範囲だが、アクセス回数が多すぎる
**提案**:
1. **SuperSlab メタデータ構造の再配置**:
- Hot fieldsfreelist, used, capacityを先頭 64 bytes に集約
- Cold fieldsLRU, statisticsを末尾に移動
- `__attribute__((aligned(64)))` でキャッシュライン境界に整列
- 推定効果: **3-5% cache miss 削減**
2. **TLS Hot Slot の積極活用**:
- 現状: Stage 0 (L0 reuse) は既に実装済み (lines 802-832)
- 提案: L0 hit rate をモニタリングし、閾値調整
- ENV: `HAKMEM_SS_L0_CAPACITY` でキャッシュサイズ拡大
- 推定効果: **Stage 1-3 の呼び出し 20-30% 削減**
---
### 優先度4: インライン化の最適化
**問題**: 167M instructions (system の 1.8倍)
- 多層の関数呼び出しによる overhead
**提案**:
1. **Critical Path のインライン化**:
- `fg_classify_domain()` - 既に static inline?
- `hak_tiny_free_fast_v2()` - 既に always_inline 化されている
- `smallmid_is_in_range()` / `mid_is_in_range()` を強制インライン
- 推定効果: **2-4% call overhead 削減**
2. **LTO (Link Time Optimization) の有効化**:
- `-flto -fwhole-program` でコンパイル
- クロスモジュールのインライン化が可能に
- 推定効果: **5-8% code size 削減、2-3% throughput 向上**
---
### 優先度5: mincore() 呼び出しの削減
**問題**: free() 内の mincore() によるシステムコール overhead
- `/mnt/workdisk/public_share/hakmem/core/box/hak_free_api.inc.h:224-272`
- TLS page cache で緩和されているが、まだ overhead がある
**提案**:
1. **Registry First Policy**:
- mincore() の前に必ず SuperSlab registry を先にチェック
- Registry hit ならヘッダー読み取り不要
- 推定効果: **free() の 5-10% を高速化**
2. **Compile-Time Option**:
- `HAKMEM_DISABLE_MINCORE_CHECK=1` を production default に
- Invalid pointer は極稀なので、クラッシュリスクは許容範囲
- 推定効果: **free() の 10-15% を高速化**
---
## 6. 実測効果の予測
### 楽観的シナリオ(全提案を実装)
| 最適化項目 | 予測効果 |
|-----------|---------|
| SuperSlab lazy init | +15% |
| Front Gate 統合 | +8% |
| Cache line 最適化 | +5% |
| Inline + LTO | +5% |
| mincore 削減 | +8% |
| **合計** | **+41%** (複合効果を考慮) |
**目標スループット**: 55M → **77M ops/s** (system 86M の 89%)
### 保守的シナリオ優先度1-2のみ実装
| 最適化項目 | 予測効果 |
|-----------|---------|
| SuperSlab lazy init | +12% |
| Front Gate 統合 | +5% |
| **合計** | **+17%** |
**目標スループット**: 55M → **65M ops/s** (system 86M の 75%)
---
## 7. 追加分析が必要な項目
### 7.1 メモリフットプリント
- **課題**: Cache refs が 5.5倍 → データ構造サイズが大きい可能性
- **調査**: `sizeof(SuperSlab)`, `sizeof(TinySlabMeta)` の確認
- **ツール**: Valgrind massif, heaptrack
### 7.2 マルチスレッド性能
- **課題**: 現在のベンチマークはシングルスレッド
- **調査**: pthread 環境での lock contention
- **ツール**: `perf record -e lock:contention_begin`
### 7.3 Workload 別の最適戦略
- **課題**: random_mixed 以外のパターンsequential, burst, etc.
- **調査**: ELO strategy の adaptive 効果測定
- **ベンチマーク**: bench_comprehensive の全パターン実行
---
## 8. 次のステップ
### Phase 1: Quick Wins (1-2日)
1. SuperSlab memset 削除 (lines 912-915)
2. HAKMEM_DISABLE_MINCORE_CHECK=1 でビルド・測定
3. Front Gate に `__builtin_expect()` 追加
### Phase 2: 構造的改善 (1週間)
1. Front Gate dispatch table 実装
2. SuperSlab size を 512KB に変更
3. LTO 有効化
### Phase 3: 詳細分析 (2週間)
1. Valgrind/heaptrack でメモリプロファイリング
2. マルチスレッドベンチマーク追加
3. Workload 別の最適化
---
## 9. 参考データ
### ファイルパス
- perf データ: `/mnt/workdisk/public_share/hakmem/perf.data`
- ホットスポットレポート: `/mnt/workdisk/public_share/hakmem/perf_hakmem_hotspots.txt`
- 統計データ: `/mnt/workdisk/public_share/hakmem/perf_hakmem_stats.txt`
- コールグラフ: `/mnt/workdisk/public_share/hakmem/perf_hakmem_callgraph.txt`
### 重要なソースファイル
- Alloc API: `/mnt/workdisk/public_share/hakmem/core/box/hak_alloc_api.inc.h`
- Free API: `/mnt/workdisk/public_share/hakmem/core/box/hak_free_api.inc.h`
- SuperSlab: `/mnt/workdisk/public_share/hakmem/core/hakmem_tiny_superslab.c`
- Shared Pool: `/mnt/workdisk/public_share/hakmem/core/hakmem_shared_pool.c`
---
**レポート生成日**: 2025-11-28
**分析対象**: HAKMEM allocator (commit 0ce20bb83)
**ベンチマーク**: bench_random_mixed_hakmem 1000000 256 42