Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 23 additions & 0 deletions crates/solverforge-solver/benches/selector_cursor_gate.rs
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,29 @@ static LIVE_BYTES: AtomicIsize = AtomicIsize::new(0);
static PEAK_LIVE_BYTES: AtomicIsize = AtomicIsize::new(0);
static ITERATIONS: OnceLock<usize> = OnceLock::new();

/* Data placed ahead of the rest of the bench's read-only data, sized per
gate layout (`SOLVERFORGE_BENCH_DATA_PAD`, bytes). Cache simulation depends
on where the linker places data, so each layout moves it by a different
amount on both sides and the gate's medians sample several placements. */
#[used]
static DATA_PAD: [u8; DATA_PAD_LEN] = [1; DATA_PAD_LEN];

const DATA_PAD_LEN: usize = match option_env!("SOLVERFORGE_BENCH_DATA_PAD") {
Some(len) => parse_data_pad(len.as_bytes()),
None => 0,
};

const fn parse_data_pad(digits: &[u8]) -> usize {
let mut value = 0;
let mut position = 0;
while position < digits.len() {
assert!(digits[position].is_ascii_digit(), "data pad is a decimal byte count");
value = value * 10 + (digits[position] - b'0') as usize;
position += 1;
}
value
}

#[global_allocator]
static GLOBAL_ALLOCATOR: MeasuringAllocator = MeasuringAllocator;

Expand Down
54 changes: 40 additions & 14 deletions scripts/bench-zero-regression.sh
Original file line number Diff line number Diff line change
Expand Up @@ -34,10 +34,20 @@ BENCH_ISOLATE=${BENCH_ISOLATE:-1}
# The simulation repeats exactly for one binary, but it depends on where the
# linker places each function, and that placement follows symbol hashes: any
# change to a generic type name reorders functions whose bodies are untouched.
# Each side is therefore built in SIM_LAYOUTS layouts that differ only in
# `-C metadata`, and every simulated metric is taken as its median across
# them; layout 0 is the binary the timed trials run. SIM_JOBS simulations run
# at once, which does not change their counts.
# It also depends on where the linker places data: on v0.31.4, 896 bytes of
# unused read-only data moved construction_first_fit's data-L1 misses -15% on
# one layout and +0.5% on another. Each side is therefore built in
# SIM_LAYOUTS layouts that differ in `-C metadata` and in a read-only pad of
# (layout * 1984) mod 4096 bytes compiled into the bench
# (SOLVERFORGE_BENCH_DATA_PAD), the same for both sides, so the layouts sample
# code and data placement; every simulated metric is taken as its median
# across them, and layout 0 is the binary the timed trials run. SIM_JOBS
# simulations run at once, which does not change their counts.
# construction_first_fit's data-L1 misses spread 3.9% across placements of
# one source: the medians of two identical-code sides over 5 placements
# differed by more than 5.6% in 5% of resamples, over 21 by more than 2.5%.
# It is built in FIRST_FIT_SIM_LAYOUTS layouts (21); the extra layouts only
# feed the simulation.
# Across layouts of one source, simulated instructions, data-L1 misses and
# last-level misses hold within a fraction of a percent, so they are graded.
# Simulated instruction-L1 misses and branch mispredictions are reported only:
Expand All @@ -58,6 +68,7 @@ BENCH_ISOLATE=${BENCH_ISOLATE:-1}
# by 32 bytes read +10% wall time (ruin) and +9.9% hardware branch misses
# (nearby_swap), while the medians over five layouts moved +1.2% and +1.0%.
SIM_LAYOUTS=${SIM_LAYOUTS:-5}
FIRST_FIT_SIM_LAYOUTS=${FIRST_FIT_SIM_LAYOUTS:-21}
SIM_JOBS=${SIM_JOBS:-8}
# Each trial's hardware counters are the mean of PERF_REPEATS process runs.
# cache-misses counts last-level misses, and the last-level cache is shared
Expand Down Expand Up @@ -108,8 +119,9 @@ if [[ $BENCH_ISOLATE == 1 ]] && ! sudo -n true 2>/dev/null; then
echo "BENCH_ISOLATE=1 needs passwordless sudo to isolate CPU ${BENCH_CPU}" >&2
exit 2
fi
if ! [[ $SIM_LAYOUTS =~ ^[1-9][0-9]*$ && $SIM_JOBS =~ ^[1-9][0-9]*$ ]]; then
echo "SIM_LAYOUTS and SIM_JOBS must be positive integers" >&2
if ! [[ $SIM_LAYOUTS =~ ^[1-9][0-9]*$ && $SIM_JOBS =~ ^[1-9][0-9]*$ \
&& $FIRST_FIT_SIM_LAYOUTS =~ ^[1-9][0-9]*$ ]] || ((FIRST_FIT_SIM_LAYOUTS < SIM_LAYOUTS)); then
echo "SIM_LAYOUTS, FIRST_FIT_SIM_LAYOUTS (>= SIM_LAYOUTS) and SIM_JOBS must be positive integers" >&2
exit 2
fi
if [[ $BASELINE_CURSOR_API != 0 && $BASELINE_CURSOR_API != 1 ]]; then
Expand Down Expand Up @@ -283,6 +295,14 @@ for case_name in "${CASES[@]}"; do
fi
done

sim_layouts_for_case() {
if [[ $1 == construction_first_fit ]]; then
printf '%s\n' "$FIRST_FIT_SIM_LAYOUTS"
else
printf '%s\n' "$SIM_LAYOUTS"
fi
}

build_case_binary() {
local side=$1
local case_name=$2
Expand All @@ -303,6 +323,7 @@ build_case_binary() {
feature_args=(--features candidate)
fi
SOLVERFORGE_BENCH_CASE="$case_name" CARGO_TARGET_DIR="$target" RUSTFLAGS="$rustflags" \
SOLVERFORGE_BENCH_DATA_PAD=$((layout * 1984 % 4096)) \
cargo "+$TOOLCHAIN" build --manifest-path "$manifest" --release --locked \
"${feature_args[@]}" >/dev/null
local binary
Expand All @@ -312,7 +333,8 @@ build_case_binary() {
}

for case_name in "${CASES[@]}"; do
for ((layout = 0; layout < SIM_LAYOUTS; layout++)); do
case_layouts=$(sim_layouts_for_case "$case_name")
for ((layout = 0; layout < case_layouts; layout++)); do
build_case_binary baseline "$case_name" "$layout"
build_case_binary candidate "$case_name" "$layout"
done
Expand All @@ -337,7 +359,8 @@ simulate() {
}
running=0
for case_name in "${CASES[@]}"; do
for ((layout = 0; layout < SIM_LAYOUTS; layout++)); do
case_layouts=$(sim_layouts_for_case "$case_name")
for ((layout = 0; layout < case_layouts; layout++)); do
for side in baseline candidate; do
simulate "$side" "$case_name" "$layout" &
running=$((running + 1))
Expand Down Expand Up @@ -401,7 +424,7 @@ if [[ $BENCH_ISOLATE == 1 ]]; then
fi

cat >"$RESULTS/environment.json" <<EOF
{"baseline_requested":"$BASELINE","baseline_commit":"$BASELINE_COMMIT","baseline_cursor_api":$BASELINE_CURSOR_API,"candidate_head":"$(git -C "$ROOT" rev-parse HEAD)","candidate_dirty":$CANDIDATE_DIRTY,"candidate_tree_sha256":"$CANDIDATE_TREE_SHA256","profile":"release(codegen-units=1,incremental=false)","case_isolated_binaries":true,"cpu_model":"$CPU_MODEL","affinity_cpu":$BENCH_CPU,"thread_siblings":"$SIBLINGS","governor":"$GOVERNOR","toolchain":"$TOOLCHAIN","warmups":$WARMUPS,"trials":$TRIALS,"measure_iterations":{"default":$MEASURE_ITERATIONS,"cartesian":$CARTESIAN_MEASURE_ITERATIONS,"construction_first_fit":$FIRST_FIT_MEASURE_ITERATIONS,"full_list":$FULL_LIST_MEASURE_ITERATIONS,"route_rows":$ROUTE_ROWS_MEASURE_ITERATIONS,"group_ruin":$GROUP_RUIN_MEASURE_ITERATIONS,"assignment_neighborhood":$ASSIGNMENT_NEIGHBORHOOD_MEASURE_ITERATIONS},"perf_available":$PERF_AVAILABLE,"isolated_cpus":"$ISOLATED_CPUS","perf_repeats":$PERF_REPEATS,"sim_layouts":$SIM_LAYOUTS,"tolerance_percent":$BENCH_TOLERANCE_PERCENT}
{"baseline_requested":"$BASELINE","baseline_commit":"$BASELINE_COMMIT","baseline_cursor_api":$BASELINE_CURSOR_API,"candidate_head":"$(git -C "$ROOT" rev-parse HEAD)","candidate_dirty":$CANDIDATE_DIRTY,"candidate_tree_sha256":"$CANDIDATE_TREE_SHA256","profile":"release(codegen-units=1,incremental=false)","case_isolated_binaries":true,"cpu_model":"$CPU_MODEL","affinity_cpu":$BENCH_CPU,"thread_siblings":"$SIBLINGS","governor":"$GOVERNOR","toolchain":"$TOOLCHAIN","warmups":$WARMUPS,"trials":$TRIALS,"measure_iterations":{"default":$MEASURE_ITERATIONS,"cartesian":$CARTESIAN_MEASURE_ITERATIONS,"construction_first_fit":$FIRST_FIT_MEASURE_ITERATIONS,"full_list":$FULL_LIST_MEASURE_ITERATIONS,"route_rows":$ROUTE_ROWS_MEASURE_ITERATIONS,"group_ruin":$GROUP_RUIN_MEASURE_ITERATIONS,"assignment_neighborhood":$ASSIGNMENT_NEIGHBORHOOD_MEASURE_ITERATIONS},"perf_available":$PERF_AVAILABLE,"isolated_cpus":"$ISOLATED_CPUS","perf_repeats":$PERF_REPEATS,"sim_layouts":$SIM_LAYOUTS,"first_fit_sim_layouts":$FIRST_FIT_SIM_LAYOUTS,"data_pad":"(layout * 1984) mod 4096","tolerance_percent":$BENCH_TOLERANCE_PERCENT}
EOF

for case_name in "${CASES[@]}"; do
Expand Down Expand Up @@ -454,7 +477,7 @@ for case_name in "${CASES[@]}"; do
done
done

python3 - "$RESULTS" "$PERF_AVAILABLE" "$BENCH_TOLERANCE_PERCENT" "$BENCH_OWN_SOURCES" "$SIM_LAYOUTS" "${CASES[@]}" <<'PY'
python3 - "$RESULTS" "$PERF_AVAILABLE" "$BENCH_TOLERANCE_PERCENT" "$BENCH_OWN_SOURCES" "$SIM_LAYOUTS" "$FIRST_FIT_SIM_LAYOUTS" "${CASES[@]}" <<'PY'
import hashlib
import json
import math
Expand All @@ -469,7 +492,8 @@ perf_available = sys.argv[2] == "1"
tolerance_percent = sys.argv[3]
own_sources = sys.argv[4] == "1"
sim_layouts = int(sys.argv[5])
cases = sys.argv[6:]
first_fit_sim_layouts = int(sys.argv[6])
cases = sys.argv[7:]
metrics = ["wall_ns", "allocations", "allocated_bytes", "peak_live_bytes", "max_rss_kb"]
perf_metrics = [
"instructions",
Expand Down Expand Up @@ -530,10 +554,12 @@ def load_cachegrind_layout(side, case, layout):
}

# Each simulated metric is graded on its median across the side's layouts
# (builds that differ only in symbol hashes), so function placement luck does
# not decide the verdict; every layout's count stays in the report.
# (builds that differ in symbol hashes and the bench's data pad), so code and
# data placement luck does not decide the verdict; every layout's count stays
# in the report. A case simulates every layout it was built in.
def load_cachegrind(side, case):
layouts = [load_cachegrind_layout(side, case, layout) for layout in range(sim_layouts)]
case_layouts = first_fit_sim_layouts if case == "construction_first_fit" else sim_layouts
layouts = [load_cachegrind_layout(side, case, layout) for layout in range(case_layouts)]
return {
metric: (statistics.median(values[metric] for values in layouts), [values[metric] for values in layouts])
for metric in layouts[0]
Expand Down
Loading