#!/bin/bash # bench-splice.sh -- A/B benchmark of mergerfs splice # against the mergefs OFF baseline and a pristine-HEAD baseline binary. # # Run: bash tools/bench-splice.sh [REPS] # Env: BASE_REPO (path to pristine HEAD build dir), SIZE_MIB, REPS # note: requires `unshare -Urm` permissions for an unprivileged FUSE mount. # Resolve a working `stat -c %s` once: this host's unshare PATH can # front a non-GNU stat (aurora) that rejects -c. STAT_BIN="$(command -v stat)" if ! "${STAT_BIN}" -c %s /dev/null >/dev/null 2>&1; then STAT_BIN=/usr/bin/stat fi set -euo pipefail export LC_ALL=C # dd prints locale-formatted numbers; parse_mibs needs C REPO="$(cd "$(dirname "${BASH_SOURCE[0]}")/.." && pwd)" BIN="${REPO}/build/mergerfs" BASE_REPO="${BASE_REPO:-/tmp/mfs-base}" BASE_BIN="${BASE_REPO}/build/mergerfs" B=/dev/shm/bench-splice REPS="${REPS:-${1:-5}}" # --- OOM guard: pick SIZE_MIB so mergefs-blob fits comfortably into MemAvailable mem_available_kib() { awk '/^MemAvailable:/{print $2}' /proc/meminfo; } pick_size_mib() { local want=(256 128 64) local w for w in "${want[@]}"; do local need=$(( w * 2 * 1024 )) # blob on branch + mergefs page cache local have; have=$(mem_available_kib) if (( have >= need )); then echo "$w"; return; fi done echo "INSUFFICIENT-MEMORY" >&2 return 1 } SIZE_MIB="${SIZE_MIB:-$(pick_size_mib)}" # --- helpers ----------------------------------------------------------- die() { echo "bench-splice: $*" >&2; exit 1; } need_binary() { [[ -x "$1" ]] || die "missing binary: $1"; } need_splice_binary() { # --version comes from src/version.hpp, which incremental makes do not # refresh; assert the binary actually contains the code under test. # NB: consume strings fully - grep -q closes the pipe early, and with # pipefail the resulting SIGPIPE would fail the check spuriously. local marker="$2" local out out=$(strings "$1" | grep "${marker}" || true) [[ -n "${out}" ]] \ || die "binary $1 lacks marker '${marker}' (stale build? run make)" } ensure_unshared() { # Re-exec under unshare -Urm if we're not already root in a user ns # (mount requires CAP_SYS_ADMIN in this host's delegate). if (( EUID != 0 )) && [[ -z "${BENCH_INNS:-}" ]]; then BENCH_INNS=1 exec unshare -Urm -- "$0" "$@" fi } # --- mergefs process management ----------------------------------------- declare -g MNT="" BR1="" BR2="" MPID="" mfs_stop() { if [[ -n "${MPID}" ]]; then kill "${MPID}" 2>/dev/null || true wait "${MPID}" 2>/dev/null || true MPID= fi pkill -9 -f "$BIN .*${B}/mnt" 2>/dev/null || true pkill -9 -f "$BASE_BIN .*${B}/mnt" 2>/dev/null || true if [[ -n "${MNT}" ]] && grep -qs "${B}/mnt" /proc/mounts; then fusermount3 -u "${MNT}" >/dev/null 2>&1 || umount "${MNT}" 2>/dev/null || true fi } cleanup() { mfs_stop payload_src_fini rm -rf "${B}" } trap cleanup EXIT trap 'cleanup; exit 1' INT TERM mfs_start() { local binary="$1"; shift local opts="$1"; shift BR1="${B}/b1"; BR2="${B}/b2"; MNT="${B}/mnt" mkdir -p "${BR1}" "${BR2}" "${MNT}" "${binary}" -f "${BR1}:${BR2}" "${MNT}" -o "${opts}" >/dev/null 2>&1 & MPID=$! # wait for mount local t=0 while ! grep -qs " ${MNT} " /proc/mounts; do sleep 0.2 t=$(( t + 1 )) if (( t > 50 )); then die "mount did not come up for ${binary}" fi done sleep 0.5 # let it warm INIT } # --- one experiment ------------------------------------------------------ # run_4phases