Files
hakmem/core/hakmem_tiny_config.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

191 lines
7.2 KiB
C

/**
* hakmem_tiny_config.h
*
* Centralized Configuration for TinyPool (≤1KB allocations)
* All tunable constants and defaults in one place
*
* Created: 2025-11-01
* Purpose: Simplify tuning and avoid scattered magic numbers
*/
#ifndef HAKMEM_TINY_CONFIG_H
#define HAKMEM_TINY_CONFIG_H
#include <stdint.h>
#include <stddef.h>
#ifdef __cplusplus
extern "C" {
#endif
// ============================================================================
// Size Classes (8 classes: 8B, 16B, 32B, 64B, 128B, 256B, 512B, 1KB)
// ============================================================================
#define TINY_NUM_CLASSES 8
// Size class boundaries (defined in hakmem_tiny.h, referenced here)
extern const size_t g_tiny_class_sizes[TINY_NUM_CLASSES];
// ============================================================================
// Fast Cache Configuration (per-class front-end cache)
// ============================================================================
// Default fast cache capacities per class (mutable so presets/env can tweak)
extern uint16_t g_fast_cap_defaults[TINY_NUM_CLASSES];
// Reset fast cache defaults back to the factory baseline
void tiny_config_reset_defaults(void);
// ============================================================================
// TLS Magazine Configuration (thread-local cache)
// ============================================================================
// Global magazine capacity limit (can be overridden by HAKMEM_TINY_MAG_CAP)
#define TINY_TLS_MAG_CAP 2048
// Default TLS magazine capacities per class
// These are the initial/default values before ACE learning adjusts them
// Implemented in hakmem_tiny_config.c
int tiny_default_cap(int class_idx);
int tiny_mag_default_cap(int class_idx); // Alias for tiny_default_cap
// Maximum allowed TLS magazine capacities per class
// These limits prevent ACE from growing caches too large
// Implemented in hakmem_tiny_config.c
int tiny_cap_max_for_class(int class_idx);
// ============================================================================
// SuperSlab Configuration (1MB aligned chunks)
// ============================================================================
// SuperSlab constants are defined in hakmem_tiny_superslab.h to avoid duplication
// - SUPERSLAB_SIZE: 1MB (default)
// - SLABS_PER_SUPERSLAB: 256 (for 1MB SuperSlab)
// - SUPERSLAB_MAGIC: Magic number for validation
// ============================================================================
// Partial SuperSlab Release Configuration
// ============================================================================
// Enable partial SuperSlab release by default
// When enabled, SuperSlabs with low active block count are released via madvise
#define TINY_SS_PARTIAL_ENABLE_DEFAULT 1
// Partial release interval (every N refills)
#define TINY_SS_PARTIAL_INTERVAL_DEFAULT 4
// Active block threshold for partial release (percentage)
// If active_blocks / capacity < threshold, release the SuperSlab
#define TINY_SS_PARTIAL_THRESHOLD_PCT_DEFAULT 10 // 10%
// ============================================================================
// Refill/Drain Configuration
// ============================================================================
// Number of blocks to refill from SuperSlab to magazine
#define TINY_REFILL_BATCH_SIZE 16
// Number of blocks to drain from magazine to SuperSlab
#define TINY_DRAIN_BATCH_SIZE 16
// ============================================================================
// Remote Free Configuration (cross-thread free)
// ============================================================================
// Remote free list capacity per class
#define TINY_REMOTE_FREE_CAP 64
// Batch size for draining remote free list
#define TINY_REMOTE_DRAIN_BATCH 32
// ============================================================================
// Memory Efficiency Presets
// ============================================================================
// Preset: Balanced (default)
// - Moderate cache sizes
// - Partial release enabled
// - Good balance between performance and RSS
#define TINY_PRESET_BALANCED() tiny_config_reset_defaults()
// Preset: Tight (low memory)
// - Smaller cache sizes
// - Aggressive partial release
// - Optimized for RSS at slight performance cost
#define TINY_PRESET_TIGHT() do { \
g_fast_cap_defaults[0] = 64; /* 8B */ \
g_fast_cap_defaults[1] = 64; /* 16B */ \
g_fast_cap_defaults[2] = 64; /* 32B */ \
g_fast_cap_defaults[3] = 64; /* 64B */ \
g_fast_cap_defaults[4] = 64; /* 128B */ \
g_fast_cap_defaults[5] = 64; /* 256B */ \
g_fast_cap_defaults[6] = 64; /* 512B */ \
} while(0)
// Preset: Ultra Tight (minimal memory)
// - Minimal cache sizes
// - Maximum RSS reduction
// - Use for memory-constrained environments
#define TINY_PRESET_ULTRA_TIGHT() do { \
g_fast_cap_defaults[0] = 32; /* 8B */ \
g_fast_cap_defaults[1] = 32; /* 16B */ \
g_fast_cap_defaults[2] = 32; /* 32B */ \
g_fast_cap_defaults[3] = 32; /* 64B */ \
g_fast_cap_defaults[4] = 32; /* 128B */ \
g_fast_cap_defaults[5] = 32; /* 256B */ \
g_fast_cap_defaults[6] = 32; /* 512B */ \
} while(0)
// ============================================================================
// Super Front Cache (SFC) Configuration - Box 5-NEW (Phase 1)
// ============================================================================
// SFC Feature Flag (A/B testing)
// ENV: HAKMEM_SFC_ENABLE (default: 0, OFF)
extern int g_sfc_enabled;
// SFC Default Configuration (can be overridden via ENV)
// ENV: HAKMEM_SFC_CAPACITY (default: 128, range: 16-256)
// ENV: HAKMEM_SFC_REFILL_COUNT (default: 64, range: 8-256)
#define SFC_DEFAULT_CAPACITY 128
#define SFC_DEFAULT_REFILL_COUNT 64
// SFC Per-Class Overrides (optional)
// ENV: HAKMEM_SFC_CAPACITY_CLASS{0..7} (per-class capacity)
// ENV: HAKMEM_SFC_REFILL_COUNT_CLASS{0..7} (per-class refill count)
// SFC Statistics Dump (optional)
// ENV: HAKMEM_SFC_STATS_DUMP=1 (print stats at exit)
// ENV: HAKMEM_SFC_DEBUG=1 (enable debug logging)
// ============================================================================
// Environment Variable Overrides
// ============================================================================
// The following environment variables can override defaults:
//
// - HAKMEM_TINY_MAG_CAP: Global magazine cap limit
// - HAKMEM_TINY_MAG_CAP_C{0..7}: Per-class magazine cap override
// - HAKMEM_TINY_SS_PARTIAL: Enable/disable partial release (0/1)
// - HAKMEM_TINY_SS_PARTIAL_INT: Partial release interval
// - HAKMEM_TINY_SS_PARTIAL_PCT: Partial release threshold percentage
//
// - HAKMEM_SFC_ENABLE: Enable Super Front Cache (0/1, default: 0)
// - HAKMEM_SFC_CAPACITY: Default SFC capacity (16-256, default: 128)
// - HAKMEM_SFC_REFILL_COUNT: Default refill count (8-256, default: 64)
// - HAKMEM_SFC_CAPACITY_CLASS{0..7}: Per-class capacity override
// - HAKMEM_SFC_REFILL_COUNT_CLASS{0..7}: Per-class refill count override
// - HAKMEM_SFC_STATS_DUMP: Print SFC stats at exit (0/1, default: 0)
// - HAKMEM_SFC_DEBUG: Enable SFC debug logging (0/1, default: 0)
//
// Example:
// HAKMEM_TINY_MAG_CAP=512 HAKMEM_TINY_SS_PARTIAL=1 ./my_app
// HAKMEM_SFC_ENABLE=1 HAKMEM_SFC_CAPACITY=192 ./my_app # Test SFC Phase 1
#ifdef __cplusplus
}
#endif
#endif // HAKMEM_TINY_CONFIG_H