Priority 1-3のBox Modulesを実装し、安全なpre-warming APIを提供。
既存の複雑なprewarmコードを1行のBox API呼び出しに置き換え。
## 新規Box Modules
1. **Box Capacity Manager** (capacity_box.h/c)
- TLS SLL容量の一元管理
- adaptive_sizing初期化保証
- Double-free バグ防止
2. **Box Carve-And-Push** (carve_push_box.h/c)
- アトミックなblock carve + TLS SLL push
- All-or-nothing semantics
- Rollback保証(partial failure防止)
3. **Box Prewarm** (prewarm_box.h/c)
- 安全なTLS cache pre-warming
- 初期化依存性を隠蔽
- シンプルなAPI (1関数呼び出し)
## コード簡略化
hakmem_tiny_init.inc: 20行 → 1行
```c
// BEFORE: 複雑なP0分岐とエラー処理
adaptive_sizing_init();
if (prewarm > 0) {
#if HAKMEM_TINY_P0_BATCH_REFILL
int taken = sll_refill_batch_from_ss(5, prewarm);
#else
int taken = sll_refill_small_from_ss(5, prewarm);
#endif
}
// AFTER: Box API 1行
int taken = box_prewarm_tls(5, prewarm);
```
## シンボルExport修正
hakmem_tiny.c: 5つのシンボルをstatic → non-static
- g_tls_slabs[] (TLS slab配列)
- g_sll_multiplier (SLL容量乗数)
- g_sll_cap_override[] (容量オーバーライド)
- superslab_refill() (SuperSlab再充填)
- ss_active_add() (アクティブカウンタ)
## ビルドシステム
Makefile: TINY_BENCH_OBJS_BASEに3つのBox modules追加
- core/box/capacity_box.o
- core/box/carve_push_box.o
- core/box/prewarm_box.o
## 動作確認
✅ Debug build成功
✅ Box Prewarm API動作確認
[PREWARM] class=5 requested=128 taken=32
## 次のステップ
- Box Refill Manager (Priority 4)
- Box SuperSlab Allocator (Priority 5)
- Release build修正(tiny_debug_ring_record)
🤖 Generated with [Claude Code](https://claude.com/claude-code)
Co-Authored-By: Claude <noreply@anthropic.com>
90 lines
2.8 KiB
C
90 lines
2.8 KiB
C
// prewarm_box.c - Box Prewarm Implementation
|
|
#include <stdio.h>
|
|
#include <stdlib.h>
|
|
#include "../hakmem_tiny.h" // MUST BE FIRST: Base types
|
|
#include "../tiny_tls.h" // TinyTLSSlab type definition
|
|
#include "../hakmem_tiny_config.h" // TINY_NUM_CLASSES
|
|
#include "../hakmem_tiny_superslab.h" // SuperSlab
|
|
#include "../hakmem_tiny_integrity.h" // HAK_CHECK_CLASS_IDX
|
|
#include "prewarm_box.h"
|
|
#include "capacity_box.h" // box_cap_init(), box_cap_avail()
|
|
#include "carve_push_box.h" // box_carve_and_push()
|
|
|
|
// External declarations
|
|
extern __thread TinyTLSSlab g_tls_slabs[TINY_NUM_CLASSES];
|
|
extern __thread uint32_t g_tls_sll_count[TINY_NUM_CLASSES];
|
|
extern SuperSlab* superslab_refill(int class_idx);
|
|
|
|
// ============================================================================
|
|
// Box Prewarm API Implementation
|
|
// ============================================================================
|
|
|
|
int box_prewarm_tls(int class_idx, int count) {
|
|
// PRIORITY 1: Bounds check
|
|
HAK_CHECK_CLASS_IDX(class_idx, "box_prewarm_tls");
|
|
|
|
if (count <= 0) return 0;
|
|
|
|
// Step 1: Ensure capacity system is initialized
|
|
// This is critical to prevent the double-free bug
|
|
box_cap_init();
|
|
|
|
// Step 2: Check available capacity
|
|
uint32_t avail = box_cap_avail(class_idx);
|
|
if (avail == 0) {
|
|
// TLS SLL already at capacity
|
|
return 0;
|
|
}
|
|
|
|
// Limit count to available capacity
|
|
uint32_t want = (uint32_t)count;
|
|
if (want > avail) {
|
|
want = avail;
|
|
}
|
|
|
|
// Step 3: Ensure SuperSlab is available
|
|
TinyTLSSlab* tls = &g_tls_slabs[class_idx];
|
|
if (!tls->ss) {
|
|
// Try to allocate SuperSlab
|
|
if (superslab_refill(class_idx) == NULL) {
|
|
#if !HAKMEM_BUILD_RELEASE
|
|
fprintf(stderr, "[BOX_PREWARM] Failed to allocate SuperSlab for class %d\n",
|
|
class_idx);
|
|
#endif
|
|
return 0;
|
|
}
|
|
// Reload tls pointer after superslab_refill
|
|
tls = &g_tls_slabs[class_idx];
|
|
}
|
|
|
|
// Step 4: Atomically carve and push blocks
|
|
// This uses Box Carve-Push which guarantees no orphaned blocks
|
|
uint32_t pushed = box_carve_and_push(class_idx, want);
|
|
|
|
#if !HAKMEM_BUILD_RELEASE
|
|
if (pushed < want) {
|
|
fprintf(stderr, "[BOX_PREWARM] Partial prewarm: requested=%u pushed=%u class=%d\n",
|
|
want, pushed, class_idx);
|
|
}
|
|
#endif
|
|
|
|
return (int)pushed;
|
|
}
|
|
|
|
int box_prewarm_needed(int class_idx, int target_count) {
|
|
// PRIORITY 1: Bounds check
|
|
HAK_CHECK_CLASS_IDX(class_idx, "box_prewarm_needed");
|
|
|
|
if (target_count <= 0) return 0;
|
|
|
|
// Check current count
|
|
uint32_t current = g_tls_sll_count[class_idx];
|
|
if (current >= (uint32_t)target_count) {
|
|
// Already at or above target
|
|
return 0;
|
|
}
|
|
|
|
// Return how many more blocks needed
|
|
return (target_count - (int)current);
|
|
}
|