Files
hakmem/build.sh
Moe Charm (CI) 1010a961fb 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.
2025-11-09 18:55:50 +09:00

147 lines
4.4 KiB
Bash
Executable File
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

#!/usr/bin/env bash
# build.sh - Unified build wrapper (Phase 7 + Pool TLS) with discoverable help
#
# Quick use:
# ./build.sh bench_pool_tls_hakmem # Recommended target
# ./build.sh help # Show usage/hints/ENV
# ./build.sh verify bench_pool_tls_hakmem # Check freshness
#
# Notes:
# - Flags are pinned to avoid drift (see below). You can pass extra make flags via
# EXTRA_MAKEFLAGS, e.g. EXTRA_MAKEFLAGS="HAKMEM_DEBUG_VERBOSE=1" ./build.sh <target>
# - Arena ENV (Pool TLS): HAKMEM_POOL_TLS_ARENA_MB_INIT/MAX/GROWTH_LEVELS
# - See also: docs/BUILDING_QUICKSTART.md
set -euo pipefail
FLAVOR="release"
if [[ $# -gt 0 && ( "$1" == "release" || "$1" == "debug" ) ]]; then
FLAVOR="$1"; shift
fi
TARGET="${1:-bench_mid_large_mt_hakmem}"
usage() {
cat <<'USAGE'
=========================================
HAKMEM Build Script (help)
=========================================
Usage:
./build.sh <target>
./build.sh help # Show this help
./build.sh list # Show common targets
./build.sh verify <bin> # Verify binary freshness
Common targets (curated):
- bench_random_mixed_hakmem
- bench_pool_tls_hakmem
- bench_mid_large_mt_hakmem
- larson_hakmem
Pinned build flags (by default):
POOL_TLS_PHASE1=1 HEADER_CLASSIDX=1 AGGRESSIVE_INLINE=1 PREWARM_TLS=1 POOL_TLS_PREWARM=1
Extra flags (optional):
Use environment var EXTRA_MAKEFLAGS, e.g.:
EXTRA_MAKEFLAGS="HAKMEM_DEBUG_VERBOSE=1" ./build.sh bench_pool_tls_hakmem
EXTRA_MAKEFLAGS="HAKMEM_TINY_SAFE_FREE=1" ./build.sh bench_random_mixed_hakmem
Pool TLS Arena ENV (A/B friendly):
export HAKMEM_POOL_TLS_ARENA_MB_INIT=2 # default 1
export HAKMEM_POOL_TLS_ARENA_MB_MAX=16 # default 8
export HAKMEM_POOL_TLS_ARENA_GROWTH_LEVELS=4 # default 3
Verify & perf tips:
make print-flags
./verify_build.sh <bin>
perf stat -e cycles,instructions,branches,branch-misses,cache-misses -r 3 -- ./<bin> ...
strace -e trace=mmap,madvise,munmap -c ./<bin> ...
USAGE
}
list_targets() {
cat <<'LIST'
Common build targets:
bench_random_mixed_hakmem # Tiny 1T mixed
bench_pool_tls_hakmem # Pool TLS (852KB)
bench_mid_large_mt_hakmem # Mid-Large MT (832KB)
larson_hakmem # Larson mixed
bench_random_mixed_system # glibc baseline
bench_pool_tls_system # glibc baseline (PoolTLS workload)
bench_mid_large_mt_system # glibc baseline (Mid-Large workload)
LIST
}
if [[ "${TARGET}" == "help" || "${TARGET}" == "-h" || "${TARGET}" == "--help" ]]; then
usage
exit 0
fi
if [[ "${TARGET}" == "list" ]]; then
list_targets
exit 0
fi
if [[ "${TARGET}" == "verify" ]]; then
BIN="${2:-}"
if [[ -z "${BIN}" ]]; then
echo "Usage: ./build.sh verify <binary>" >&2
exit 2
fi
./verify_build.sh "${BIN}"
exit 0
fi
echo "========================================="
echo " HAKMEM Build Script"
echo " Flavor: ${FLAVOR}"
echo " Target: ${TARGET}"
echo " Flags: POOL_TLS_PHASE1=1 POOL_TLS_PREWARM=1 HEADER_CLASSIDX=1 AGGRESSIVE_INLINE=1 PREWARM_TLS=1 ${EXTRA_MAKEFLAGS:-}"
echo "========================================="
# Always clean to avoid stale objects when toggling flags
make clean >/dev/null 2>&1 || true
# Phase 7 + Pool TLS defaults (pinned) + user extras
MAKE_ARGS=(
BUILD_FLAVOR=${FLAVOR} \
POOL_TLS_PHASE1=1 \
POOL_TLS_PREWARM=1 \
HEADER_CLASSIDX=1 \
AGGRESSIVE_INLINE=1 \
PREWARM_TLS=1 \
)
# Apply debug flavor extras (non-invasive): verbose + safe-free optional
if [[ "${FLAVOR}" == "debug" ]]; then
MAKE_ARGS+=(HAKMEM_DEBUG_VERBOSE=1)
MAKE_ARGS+=(BUILD_RELEASE_DEFAULT=0) # Disable release mode for debug builds
# Uncomment to enable extra safety by default for debug runs (may slow hot path)
# MAKE_ARGS+=(HAKMEM_TINY_SAFE_FREE=1)
else
MAKE_ARGS+=(BUILD_RELEASE_DEFAULT=1) # Enable release mode for release builds
fi
# Append user-provided extras and target
if [[ -n "${EXTRA_MAKEFLAGS:-}" ]]; then
# shellcheck disable=SC2206
MAKE_ARGS+=(${EXTRA_MAKEFLAGS})
fi
MAKE_ARGS+=("${TARGET}")
make "${MAKE_ARGS[@]}"
echo ""
echo "========================================="
echo " ✅ Build successful"
echo " Run: ./${TARGET}"
echo "-----------------------------------------"
# Place artifacts under out/<flavor>/
OUTDIR="out/${FLAVOR}"
mkdir -p "${OUTDIR}"
if [[ -x "./${TARGET}" ]]; then
cp -f "./${TARGET}" "${OUTDIR}/${TARGET}"
echo " Saved: ${OUTDIR}/${TARGET}"
fi
echo " Tip: ./build.sh help # flags, ENV, targets"
echo "========================================="