Files
hakmem/core/box/ss_hot_prewarm_box.c
Moe Charm (CI) 7adbcdfcb6 Phase 54-60: Memory-Lean mode, Balanced mode stabilization, M1 (50%) achievement
## Summary

Completed Phase 54-60 optimization work:

**Phase 54-56: Memory-Lean mode (LEAN+OFF prewarm suppression)**
- Implemented ss_mem_lean_env_box.h with ENV gates
- Balanced mode (LEAN+OFF) promoted as production default
- Result: +1.2% throughput, better stability, zero syscall overhead
- Added to bench_profile.h: MIXED_TINYV3_C7_BALANCED preset

**Phase 57: 60-min soak finalization**
- Balanced mode: 60-min soak, RSS drift 0%, CV 5.38%
- Speed-first mode: 60-min soak, RSS drift 0%, CV 1.58%
- Syscall budget: 1.25e-7/op (800× under target)
- Status: PRODUCTION-READY

**Phase 59: 50% recovery baseline rebase**
- hakmem FAST (Balanced): 59.184M ops/s, CV 1.31%
- mimalloc: 120.466M ops/s, CV 3.50%
- Ratio: 49.13% (M1 ACHIEVED within statistical noise)
- Superior stability: 2.68× better CV than mimalloc

**Phase 60: Alloc pass-down SSOT (NO-GO)**
- Implemented alloc_passdown_ssot_env_box.h
- Modified malloc_tiny_fast.h for SSOT pattern
- Result: -0.46% (NO-GO)
- Key lesson: SSOT not applicable where early-exit already optimized

## Key Metrics

- Performance: 49.13% of mimalloc (M1 effectively achieved)
- Stability: CV 1.31% (superior to mimalloc 3.50%)
- Syscall budget: 1.25e-7/op (excellent)
- RSS: 33MB stable, 0% drift over 60 minutes

## Files Added/Modified

New boxes:
- core/box/ss_mem_lean_env_box.h
- core/box/ss_release_policy_box.{h,c}
- core/box/alloc_passdown_ssot_env_box.h

Scripts:
- scripts/soak_mixed_single_process.sh
- scripts/analyze_epoch_tail_csv.py
- scripts/soak_mixed_rss.sh
- scripts/calculate_percentiles.py
- scripts/analyze_soak.py

Documentation: Phase 40-60 analysis documents

## Design Decisions

1. Profile separation (core/bench_profile.h):
   - MIXED_TINYV3_C7_SAFE: Speed-first (no LEAN)
   - MIXED_TINYV3_C7_BALANCED: Balanced mode (LEAN+OFF)

2. Box Theory compliance:
   - All ENV gates reversible (HAKMEM_SS_MEM_LEAN, HAKMEM_ALLOC_PASSDOWN_SSOT)
   - Single conversion points maintained
   - No physical deletions (compile-out only)

3. Lessons learned:
   - SSOT effective only where redundancy exists (Phase 60 showed limits)
   - Branch prediction extremely effective (~0 cycles for well-predicted branches)
   - Early-exit pattern valuable even when seemingly redundant

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

Co-Authored-By: Claude Sonnet 4.5 <noreply@anthropic.com>
2025-12-17 06:24:01 +09:00

159 lines
5.2 KiB
C

// ss_hot_prewarm_box.c - Box SS-HotPrewarm Implementation
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "../hakmem_tiny.h" // MUST BE FIRST: Base types
#include "../hakmem_tiny_config.h" // TINY_NUM_CLASSES
#include "ss_hot_prewarm_box.h"
#include "prewarm_box.h" // box_prewarm_tls()
#include "ss_mem_lean_env_box.h" // Memory-Lean mode check
// Per-class prewarm targets (cached from ENV)
static int g_ss_hot_prewarm_targets[TINY_NUM_CLASSES] = {0};
static int g_ss_hot_prewarm_initialized = 0;
// Default aggressive targets (ChatGPT Phase 20 strategy)
// Classes 0-1 (tiny): 0 (no prewarm)
// Classes 2-3 (33-128B): 128 blocks (hot path)
// Classes 4-5 (129-512B): 64 blocks (medium hot)
// Classes 6-7 (513-1024B): 0 (rare)
static const int g_ss_hot_prewarm_defaults[TINY_NUM_CLASSES] = {
0, // C0 (16B) - not used
0, // C1 (17-32B) - not used
128, // C2 (33-64B) - HOT
128, // C3 (65-128B) - HOT
64, // C4 (129-256B) - MEDIUM
64, // C5 (257-512B) - MEDIUM
0, // C6 (513-1024B) - rare
0 // C7 (1024B) - rare
};
// ============================================================================
// Internal Helpers
// ============================================================================
static void ss_hot_prewarm_init_targets(void) {
if (g_ss_hot_prewarm_initialized) return;
// Step 1: Copy defaults
for (int i = 0; i < TINY_NUM_CLASSES; i++) {
g_ss_hot_prewarm_targets[i] = g_ss_hot_prewarm_defaults[i];
}
// Step 2: Check for global override
const char* all_env = getenv("HAKMEM_TINY_PREWARM_ALL");
if (all_env && *all_env) {
int all_count = atoi(all_env);
if (all_count >= 0) {
for (int i = 0; i < TINY_NUM_CLASSES; i++) {
g_ss_hot_prewarm_targets[i] = all_count;
}
#if !HAKMEM_BUILD_RELEASE
fprintf(stderr, "[BOX_SS_HOT_PREWARM] Global override: HAKMEM_TINY_PREWARM_ALL=%d\n", all_count);
#endif
}
}
// Step 3: Parse per-class ENV overrides
const char* class_env_names[TINY_NUM_CLASSES] = {
"HAKMEM_TINY_PREWARM_C0",
"HAKMEM_TINY_PREWARM_C1",
"HAKMEM_TINY_PREWARM_C2",
"HAKMEM_TINY_PREWARM_C3",
"HAKMEM_TINY_PREWARM_C4",
"HAKMEM_TINY_PREWARM_C5",
"HAKMEM_TINY_PREWARM_C6",
"HAKMEM_TINY_PREWARM_C7"
};
for (int i = 0; i < TINY_NUM_CLASSES; i++) {
const char* env = getenv(class_env_names[i]);
if (env && *env) {
int count = atoi(env);
if (count >= 0) {
g_ss_hot_prewarm_targets[i] = count;
#if !HAKMEM_BUILD_RELEASE
fprintf(stderr, "[BOX_SS_HOT_PREWARM] Class %d override: %s=%d\n",
i, class_env_names[i], count);
#endif
}
}
}
// Step 4: Report final configuration (debug only)
#if !HAKMEM_BUILD_RELEASE
fprintf(stderr, "[BOX_SS_HOT_PREWARM] Final targets: ");
for (int i = 0; i < TINY_NUM_CLASSES; i++) {
if (g_ss_hot_prewarm_targets[i] > 0) {
fprintf(stderr, "C%d=%d ", i, g_ss_hot_prewarm_targets[i]);
}
}
fprintf(stderr, "\n");
#endif
g_ss_hot_prewarm_initialized = 1;
}
// ============================================================================
// Public API
// ============================================================================
int box_ss_hot_prewarm_target(int class_idx) {
if (class_idx < 0 || class_idx >= TINY_NUM_CLASSES) return 0;
if (!g_ss_hot_prewarm_initialized) {
ss_hot_prewarm_init_targets();
}
return g_ss_hot_prewarm_targets[class_idx];
}
int box_ss_hot_prewarm_all(void) {
// Phase 54: Memory-Lean mode suppresses prewarm (reduce RSS)
if (ss_mem_lean_enabled()) {
#if !HAKMEM_BUILD_RELEASE
fprintf(stderr, "[BOX_SS_HOT_PREWARM] Memory-Lean mode enabled: skipping prewarm\n");
#endif
return 0; // No prewarm in lean mode
}
// Initialize targets from ENV
ss_hot_prewarm_init_targets();
int total_prewarmed = 0;
// Prewarm each class with non-zero target
for (int class_idx = 0; class_idx < TINY_NUM_CLASSES; class_idx++) {
int target = g_ss_hot_prewarm_targets[class_idx];
if (target <= 0) continue;
#if !HAKMEM_BUILD_RELEASE
fprintf(stderr, "[BOX_SS_HOT_PREWARM] Prewarming C%d with %d blocks...\n",
class_idx, target);
#endif
// Use Box Prewarm API to safely warm TLS SLL
// This will automatically:
// - Allocate SuperSlab if needed
// - Populate pages (touch memory)
// - Fill TLS SLL with blocks
int actual = box_prewarm_tls(class_idx, target);
#if !HAKMEM_BUILD_RELEASE
if (actual < target) {
fprintf(stderr, "[BOX_SS_HOT_PREWARM] C%d: requested=%d actual=%d (capacity limited)\n",
class_idx, target, actual);
}
#endif
total_prewarmed += actual;
}
// Phase 20-1: Log prewarm summary (DEBUG ONLY to avoid perf impact)
#if !HAKMEM_BUILD_RELEASE
fprintf(stderr, "[BOX_SS_HOT_PREWARM] Total blocks pre-warmed: %d\n", total_prewarmed);
#endif
return total_prewarmed;
}