bench/floor-cost/measure.sh
#!/bin/sh
# SPDX-License-Identifier: AGPL-3.0-only
# Heimdall is a defensive sandbox. The per-call cost of the static floor, the
# floor with three answers, and a brokered call's round trip to the supervisor
# (docs/v2/graph-filter.md §12 step 10, without the follower, which is not
# built), on two floors:
# abcdf nornir's `A { B C D }* F` fixture, probed here and its
# profile generated (heimdall profile probe + gen)
# heimdall-wasm-host the wasm host worker's checked-in profile
# Each floor is measured by crates/heimdall-broker/examples/floor_cost.rs: a
# fresh child per row, pinned to one CPU, each call timed on its own.
#
# Usage, from the repo root on a Linux host with podman (privileged, rootless
# is enough; no seccomp filter of podman's own, so only Heimdall's is measured):
# bench/floor-cost/measure.sh <out dir>
# Environment:
# HEIMDALL_TEST_IMAGE toolchain image (default localhost/nordisk-cargo-toolchain:heim-ci)
# CARGO_TARGET_DIR host target dir (default ./target)
# CARGO_HOME host cargo home to mount (optional)
# FLOOR_COST_CPU the CPU the measured child runs on (default 2)
# FLOOR_COST_SUP_CPU the CPU the supervisor thread runs on (default 3)
# FLOOR_COST_ARGS more floor_cost arguments (e.g. --iters 200000 --runs 3)
set -eu
cd "$(dirname "$0")/../.."
if [ "${FLOOR_COST_INNER:-}" != 1 ]; then
out=${1:?usage: bench/floor-cost/measure.sh <out dir>}
mkdir -p "$out"
out=$(cd "$out" && pwd)
img=${HEIMDALL_TEST_IMAGE:-localhost/nordisk-cargo-toolchain:heim-ci}
tgt=${CARGO_TARGET_DIR:-$PWD/target}
mkdir -p "$tgt"
extra=""
if [ -n "${CARGO_HOME:-}" ]; then extra="-v $CARGO_HOME:/floor-cost-cargo-home -e CARGO_HOME=/floor-cost-cargo-home"; fi
# shellcheck disable=SC2086
exec podman run --rm --privileged --security-opt seccomp=unconfined \
-v "$PWD":/src -v "$tgt":/tgt -v "$out":/out -e CARGO_TARGET_DIR=/tgt $extra \
-e FLOOR_COST_INNER=1 -e CARGO_BUILD_JOBS="${CARGO_BUILD_JOBS:-8}" \
-e FLOOR_COST_CPU="${FLOOR_COST_CPU:-2}" -e FLOOR_COST_SUP_CPU="${FLOOR_COST_SUP_CPU:-3}" \
-e FLOOR_COST_ARGS="${FLOOR_COST_ARGS:-}" \
-w /src "$img" bench/floor-cost/measure.sh
fi
echo "kernel $(uname -r); $(grep -m1 'model name' /proc/cpuinfo); load $(cat /proc/loadavg)"
grep Seccomp /proc/self/status
cargo build --release --locked -p nordisk-heimdall-cli
cargo build --release --locked -p nordisk-heimdall-broker --example floor_cost
heimdall=$CARGO_TARGET_DIR/release/heimdall
fc=$CARGO_TARGET_DIR/release/examples/floor_cost
# abcdf: build the fixture, probe it, generate its profile.
CARGO_TARGET_DIR=/out/abcdf-target cargo build --release --quiet \
--manifest-path crates/heimdall-probe/tests/abcdf/Cargo.toml
w=/out/profiles/abcdf
mkdir -p "$w"
cat > "$w/workload.toml" <<EOF
workload = "abcdf"
timeout_secs = 60
repeat = 3
[binary]
glibc = "/out/abcdf-target/release/abcdf"
[[run]]
name = "abcdf"
args = []
expect_exit = 0
EOF
"$heimdall" profile probe abcdf --libc glibc --profiles /out/profiles
"$heimdall" profile gen abcdf --libc glibc --profiles /out/profiles
cpu="--cpu $FLOOR_COST_CPU --sup-cpu $FLOOR_COST_SUP_CPU"
# shellcheck disable=SC2086
"$fc" --profile "$w/x86_64-glibc.json" --label abcdf \
--calls getppid,getegid,getuid,getgid,geteuid,mprotect,openat $cpu $FLOOR_COST_ARGS \
--json /out/abcdf.json | tee /out/abcdf.txt
# shellcheck disable=SC2086
"$fc" --profile profiles/heimdall-wasm-host/x86_64-glibc.json --label heimdall-wasm-host \
--calls gettid,sched_yield,prctl,mprotect,openat $cpu $FLOOR_COST_ARGS \
--json /out/wasm-host.json | tee /out/wasm-host.txt
echo "load at the end: $(cat /proc/loadavg)"