Tiny: fix header/stride mismatch and harden refill paths

- Root cause: header-based class indexing (HEADER_CLASSIDX=1) wrote a 1-byte
  header during allocation, but linear carve/refill and initial slab capacity
  still used bare class block sizes. This mismatch could overrun slab usable
  space and corrupt freelists, causing reproducible SEGV at ~100k iters.

Changes
- Superslab: compute capacity with effective stride (block_size + header for
  classes 0..6; class7 remains headerless) in superslab_init_slab(). Add a
  debug-only bound check in superslab_alloc_from_slab() to fail fast if carve
  would exceed usable bytes.
- Refill (non-P0 and P0): use header-aware stride for all linear carving and
  TLS window bump operations. Ensure alignment/validation in tiny_refill_opt.h
  also uses stride, not raw class size.
- Drain: keep existing defense-in-depth for remote sentinel and sanitize nodes
  before splicing into freelist (already present).

Notes
- This unifies the memory layout across alloc/linear-carve/refill with a single
  stride definition and keeps class7 (1024B) headerless as designed.
- Debug builds add fail-fast checks; release builds remain lean.

Next
- Re-run Tiny benches (256/1024B) in debug to confirm stability, then in
  release. If any remaining crash persists, bisect with HAKMEM_TINY_P0_BATCH_REFILL=0
  to isolate P0 batch carve, and continue reducing branch-miss as planned.
This commit is contained in:
Moe Charm (CI)
2025-11-09 18:55:50 +09:00
parent ab68ee536d
commit 1010a961fb
171 changed files with 10238 additions and 634 deletions

View File

@ -1,74 +1,18 @@
#!/bin/bash
# HAKMEM Comprehensive Benchmark Runner
# Tests all major performance categories
set -e
echo "========================================"
echo " HAKMEM Comprehensive Benchmark Suite"
echo "========================================"
echo ""
# Check if executables exist
if [ ! -f "./bench_mid_large_mt_hakmem" ]; then
echo "❌ Benchmarks not built! Run ./build_hakmem.sh first"
exit 1
fi
RESULTS_DIR="benchmarks/results/pool_tls_phase1_$(date +%Y%m%d_%H%M%S)"
mkdir -p "${RESULTS_DIR}"
echo "Results will be saved to: ${RESULTS_DIR}"
echo ""
# 1. Mid-Large MT (Pool TLS Phase 1 showcase)
echo "[1/4] Mid-Large MT Benchmark (8-32KB, Pool TLS Phase 1)..."
echo "========================================"
./bench_mid_large_mt_hakmem | tee "${RESULTS_DIR}/mid_large_mt.txt"
echo ""
# 2. Tiny Random Mixed (Phase 7 showcase)
echo "[2/4] Tiny Random Mixed (128B-1024B, Phase 7)..."
echo "========================================"
for size in 128 256 512 1024; do
echo "Size: ${size}B"
./bench_random_mixed_hakmem 10000 ${size} 12345 | tee "${RESULTS_DIR}/random_mixed_${size}B.txt"
echo "=== Size: ${size}B ==="
echo "HAKMEM (3 runs):"
for i in 1 2 3; do
./bench_random_mixed_hakmem 100000 $size 42 2>/dev/null | grep "Throughput"
done
echo ""
echo "System (3 runs):"
for i in 1 2 3; do
./bench_random_mixed_system 100000 $size 42 2>/dev/null | grep "Throughput"
done
echo "---"
done
# 3. Larson Multi-threaded (Stability + MT performance)
echo "[3/4] Larson Multi-threaded (1T, 4T)..."
echo "========================================"
echo "1 Thread:"
./larson_hakmem 2 8 128 1024 1 12345 1 | tee "${RESULTS_DIR}/larson_1T.txt"
echo ""
echo "4 Threads:"
./larson_hakmem 2 8 128 1024 1 12345 4 | tee "${RESULTS_DIR}/larson_4T.txt"
echo ""
# 4. Quick comparison with System malloc
echo "[4/4] Quick System malloc comparison..."
echo "========================================"
if [ -f "./bench_mid_large_mt_system" ]; then
echo "System malloc (Mid-Large):"
./bench_mid_large_mt_system | tee "${RESULTS_DIR}/mid_large_mt_system.txt"
else
echo "⚠️ System benchmark not built, skipping comparison"
fi
echo ""
# Summary
echo ""
echo "========================================"
echo " Benchmark Complete!"
echo "========================================"
echo ""
echo "Results saved to: ${RESULTS_DIR}"
echo ""
echo "Key files:"
ls -lh "${RESULTS_DIR}"/*.txt | awk '{print " - " $9}'
echo ""
echo "To analyze results:"
echo " cat ${RESULTS_DIR}/mid_large_mt.txt"
echo " cat ${RESULTS_DIR}/random_mixed_*.txt"
echo ""