Files
hakmem/core/box/ss_release_policy_box.c

78 lines
2.4 KiB
C
Raw Normal View History

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
// ss_release_policy_box.c - SuperSlab Release Policy Box Implementation
#include "ss_release_policy_box.h"
#include "ss_mem_lean_env_box.h"
#include "madvise_guard_box.h"
#include "ss_os_acquire_box.h"
#include "../superslab/superslab_types.h"
#include <sys/mman.h>
#include <errno.h>
// ============================================================================
// Release Policy Implementation
// ============================================================================
bool ss_should_keep_superslab(SuperSlab* ss, int class_idx) {
(void)ss; // Reserved for future budget logic
(void)class_idx; // Reserved for per-class policy
// In lean mode: allow release of empty superslabs
// In FAST mode (default): keep all superslabs (persistent backend)
if (ss_mem_lean_enabled()) {
// TODO: Add per-class budget logic here if needed
// For now: allow release (caller decides based on empty state)
return false; // Signal: OK to release in lean mode
}
// Default (FAST mode): keep all superslabs (speed-first)
return true;
}
int ss_maybe_decommit_superslab(void* ptr, size_t size) {
// Fast path: lean mode disabled → no-op
if (!ss_mem_lean_enabled()) {
return 0;
}
// Get decommit mode from ENV
ss_mem_lean_decommit_mode_t mode = ss_mem_lean_decommit_mode();
// If decommit is OFF, skip (only prewarm suppression active)
if (mode == SS_MEM_LEAN_DECOMMIT_OFF) {
return 0;
}
// Select madvise advice based on mode
int advice;
switch (mode) {
case SS_MEM_LEAN_DECOMMIT_FREE:
#ifdef MADV_FREE
advice = MADV_FREE; // Lazy reclaim (fast, kernel 4.5+)
#else
// Fallback to MADV_DONTNEED if MADV_FREE not available
advice = MADV_DONTNEED;
#endif
break;
case SS_MEM_LEAN_DECOMMIT_DONTNEED:
advice = MADV_DONTNEED; // Eager reclaim (slower, but universal)
break;
default:
return 0; // Unknown mode, skip
}
// Call DSO-guarded madvise (respects guard rules)
// This will:
// - Skip if ptr overlaps DSO (.fini_array safety)
// - Disable future calls on ENOMEM (fail-fast)
// - Update madvise counters
int ret = ss_os_madvise_guarded(ptr, size, advice, "ss_release_policy_decommit");
if (ret == 0) {
// Success: update lean_decommit counter
ss_os_stats_record_lean_decommit();
}
return ret;
}