testsAndMisc/python_pkg/swap_bench/cli.py
Krzysztof kuhy Rudnicki 5e0d8727d6 A/B zswap vs zram: zram stays the default
Add setup_zswap.sh and make setup_zram.sh its one-command revert
(shared lib/swap_switch.sh: systemd-boot/GRUB cmdline, live switch or
exit 3 "reboot to apply"). Add swap_bench.sh + python_pkg/swap_bench, a
seeded memory-pressure benchmark. 3 runs x 2 setups in vmbox and on the
host: zram has lower median PSI full in both (VM 5996 vs 10360 ms, host
7427 vs 8973 ms), so the host keeps booting with zram. Results in
linux_configuration/fixes/DOCS-zswap-vs-zram.md.

Bug-class-check: none — new feature and measurement, not a bug fix

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-27 12:34:25 +01:00

85 lines
2.9 KiB
Python

"""``python -m python_pkg.swap_bench run|row|summary`` for swap_bench.sh."""
from __future__ import annotations
import argparse
from pathlib import Path
import sys
from python_pkg.swap_bench.stats import compute_row, format_row
from python_pkg.swap_bench.summary import summarize
from python_pkg.swap_bench.workload import Spec, run
def _snapshot(text: str) -> tuple[Path, Path]:
src, sep, dest = text.partition("=")
if not sep:
msg = f"--snapshot wants SRC=DEST, got {text!r}"
raise argparse.ArgumentTypeError(msg)
return Path(src), Path(dest)
def _parser() -> argparse.ArgumentParser:
parser = argparse.ArgumentParser(prog="swap_bench")
sub = parser.add_subparsers(dest="cmd", required=True)
p_run = sub.add_parser("run", help="run the seeded workload once")
p_run.add_argument("--size-mib", type=int, required=True)
p_run.add_argument("--seed", type=int, required=True)
p_run.add_argument("--rounds", type=int, default=3)
p_run.add_argument("--hot-share", type=float, default=0.25)
p_run.add_argument("--snapshot", type=_snapshot, action="append", default=[])
p_row = sub.add_parser("row", help="print one TSV row for a run directory")
p_row.add_argument("--dir", type=Path, required=True)
p_row.add_argument("--env", required=True)
p_row.add_argument("--setup", required=True)
p_row.add_argument("--seed", type=int, required=True)
p_row.add_argument("--size-mib", type=int, required=True)
p_row.add_argument("--date", required=True)
p_row.add_argument("--header", action="store_true", help="print the header first")
p_sum = sub.add_parser("summary", help="medians and the winner for a TSV")
p_sum.add_argument("tsv", type=Path)
return parser
def _cmd_run(args: argparse.Namespace) -> int:
try:
spec = Spec(
size_mib=args.size_mib,
seed=args.seed,
rounds=args.rounds,
hot_share=args.hot_share,
)
except ValueError as exc:
sys.stderr.write(f"swap_bench: {exc}\n")
return 2
result = run(spec, args.snapshot)
for field in ("pages", "fill_crc", "touch_crc", "majflt", "seconds"):
sys.stdout.write(f"{field}={getattr(result, field)}\n")
return 0
def _cmd_row(args: argparse.Namespace) -> int:
row = compute_row(
args.dir,
env=args.env,
setup=args.setup,
seed=args.seed,
size_mib=args.size_mib,
date=args.date,
)
if args.header:
sys.stdout.write(format_row(None) + "\n")
sys.stdout.write(format_row(row) + "\n")
return 0
def main(argv: list[str] | None = None) -> int:
"""Dispatch a subcommand; return its exit code."""
args = _parser().parse_args(argv)
if args.cmd == "run":
return _cmd_run(args)
if args.cmd == "row":
return _cmd_row(args)
sys.stdout.write(summarize(args.tsv))
return 0