Files
hakmem/core/box/pool_stats.inc.h
Moe Charm (CI) 1da8754d45 CRITICAL FIX: TLS 未初期化による 4T SEGV を完全解消
**問題:**
- Larson 4T で 100% SEGV (1T は 2.09M ops/s で完走)
- System/mimalloc は 4T で 33.52M ops/s 正常動作
- SS OFF + Remote OFF でも 4T で SEGV

**根本原因: (Task agent ultrathink 調査結果)**
```
CRASH: mov (%r15),%r13
R15 = 0x6261  ← ASCII "ba" (ゴミ値、未初期化TLS)
```

Worker スレッドの TLS 変数が未初期化:
- `__thread void* g_tls_sll_head[TINY_NUM_CLASSES];`  ← 初期化なし
- pthread_create() で生成されたスレッドでゼロ初期化されない
- NULL チェックが通過 (0x6261 != NULL) → dereference → SEGV

**修正内容:**
全 TLS 配列に明示的初期化子 `= {0}` を追加:

1. **core/hakmem_tiny.c:**
   - `g_tls_sll_head[TINY_NUM_CLASSES] = {0}`
   - `g_tls_sll_count[TINY_NUM_CLASSES] = {0}`
   - `g_tls_live_ss[TINY_NUM_CLASSES] = {0}`
   - `g_tls_bcur[TINY_NUM_CLASSES] = {0}`
   - `g_tls_bend[TINY_NUM_CLASSES] = {0}`

2. **core/tiny_fastcache.c:**
   - `g_tiny_fast_cache[TINY_FAST_CLASS_COUNT] = {0}`
   - `g_tiny_fast_count[TINY_FAST_CLASS_COUNT] = {0}`
   - `g_tiny_fast_free_head[TINY_FAST_CLASS_COUNT] = {0}`
   - `g_tiny_fast_free_count[TINY_FAST_CLASS_COUNT] = {0}`

3. **core/hakmem_tiny_magazine.c:**
   - `g_tls_mags[TINY_NUM_CLASSES] = {0}`

4. **core/tiny_sticky.c:**
   - `g_tls_sticky_ss[TINY_NUM_CLASSES][TINY_STICKY_RING] = {0}`
   - `g_tls_sticky_idx[TINY_NUM_CLASSES][TINY_STICKY_RING] = {0}`
   - `g_tls_sticky_pos[TINY_NUM_CLASSES] = {0}`

**効果:**
```
Before: 1T: 2.09M   |  4T: SEGV 💀
After:  1T: 2.41M   |  4T: 4.19M   (+15% 1T, SEGV解消)
```

**テスト:**
```bash
# 1 thread: 完走
./larson_hakmem 2 8 128 1024 1 12345 1
→ Throughput = 2,407,597 ops/s 

# 4 threads: 完走(以前は SEGV)
./larson_hakmem 2 8 128 1024 1 12345 4
→ Throughput = 4,192,155 ops/s 
```

**調査協力:** Task agent (ultrathink mode) による完璧な根本原因特定

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude <noreply@anthropic.com>
2025-11-07 01:27:04 +09:00

81 lines
3.1 KiB
C

// pool_stats.inc.h — Box: L2 Pool statistics and snapshots
#ifndef POOL_STATS_INC_H
#define POOL_STATS_INC_H
void hak_pool_print_stats(void) {
if (!g_pool.initialized) return;
printf("\n========================================\n");
printf("L2 Pool Statistics\n");
printf("========================================\n");
uint64_t total_hits = 0, total_misses = 0, total_refills = 0, total_frees = 0;
for (int i = 0; i < POOL_NUM_CLASSES; i++) {
if (g_class_sizes[i] == 0) continue; // skip disabled dynamic class
total_hits += g_pool.hits[i];
total_misses += g_pool.misses[i];
total_refills += g_pool.refills[i];
total_frees += g_pool.frees[i];
printf("Class %zu KB:\n", g_class_sizes[i] / 1024);
printf(" Hits: %lu\n", (unsigned long)g_pool.hits[i]);
printf(" Misses: %lu\n", (unsigned long)g_pool.misses[i]);
printf(" Refills: %lu\n", (unsigned long)g_pool.refills[i]);
printf(" Frees: %lu\n", (unsigned long)g_pool.frees[i]);
if (g_pool.hits[i] + g_pool.misses[i] > 0) {
double hit_rate = (double)g_pool.hits[i] / (g_pool.hits[i] + g_pool.misses[i]) * 100.0;
printf(" Hit rate: %.1f%%\n", hit_rate);
}
}
printf("\n----------------------------------------\n");
printf("Summary:\n");
printf(" Total hits: %lu\n", (unsigned long)total_hits);
printf(" Total misses: %lu\n", (unsigned long)total_misses);
printf(" Total refills: %lu\n", (unsigned long)total_refills);
printf(" Total frees: %lu\n", (unsigned long)total_frees);
printf(" Pages allocated: %lu\n", (unsigned long)g_pool.total_pages_allocated);
printf(" Bytes allocated: %lu KB\n", (unsigned long)(g_pool.total_bytes_allocated / 1024));
if (total_hits + total_misses > 0) {
double hit_rate = (double)total_hits / (total_hits + total_misses) * 100.0;
printf(" Overall hit rate: %.1f%%\n", hit_rate);
}
printf("========================================\n");
}
void hak_pool_stats_snapshot(uint64_t hits[], uint64_t misses[], uint64_t refills[], uint64_t frees[]) {
if (!g_pool.initialized) {
for (int i = 0; i < POOL_NUM_CLASSES; i++) {
if (hits) hits[i] = 0;
if (misses) misses[i] = 0;
if (refills) refills[i] = 0;
if (frees) frees[i] = 0;
}
return;
}
for (int i = 0; i < POOL_NUM_CLASSES; i++) {
if (hits) hits[i] = g_pool.hits[i];
if (misses) misses[i] = g_pool.misses[i];
if (refills) refills[i] = g_pool.refills[i];
if (frees) frees[i] = g_pool.frees[i];
}
}
void hak_pool_extra_metrics_snapshot(uint64_t* trylock_attempts, uint64_t* trylock_success, uint64_t* ring_underflow) {
if (trylock_attempts) {
*trylock_attempts = atomic_load_explicit(&g_pool.trylock_attempts, memory_order_relaxed);
}
if (trylock_success) {
*trylock_success = atomic_load_explicit(&g_pool.trylock_success, memory_order_relaxed);
}
if (ring_underflow) {
*ring_underflow = atomic_load_explicit(&g_pool.ring_underflow, memory_order_relaxed);
}
}
#endif // POOL_STATS_INC_H