From 5ef1666053c621f9a4d6b7a0a03b498ad8024cc5 Mon Sep 17 00:00:00 2001 From: LGUG2Z Date: Wed, 28 Oct 2026 19:33:26 -0700 Subject: [PATCH] perf(e2e): key the saved state on the QEMU command line, not guest.py Any edit to guest.py made a new state (76 to 145 s). The state now follows the command line of the guest that is saved, with the paths as placeholders, so an edit to the rest of guest.py does not. --- e2e/platforms/qemu_windows/guest.py | 63 +++++++++++++++-------------- e2e/platforms/qemu_windows/state.py | 14 +++++-- 2 files changed, 44 insertions(+), 33 deletions(-) diff --git a/e2e/platforms/qemu_windows/guest.py b/e2e/platforms/qemu_windows/guest.py index 913ff41c..5676666e 100644 --- a/e2e/platforms/qemu_windows/guest.py +++ b/e2e/platforms/qemu_windows/guest.py @@ -102,36 +102,7 @@ class Guest: "--ctrl", f"type=unixio,path={run / 'tpm.sock'}"] ) ovmf = Path(os.environ["OVMF_FD"]) / "FV" - self._start( - ["qemu-system-x86_64", "-name", "e2e-windows", "-accel", "kvm", - "-machine", "q35,smm=off", "-cpu", "host", - "-smp", str(self._vcpus), "-m", self._mem, - *(["-rtc", f"base={self._rtc_base}"] if self._rtc_base else []), - "-drive", f"if=pflash,format=raw,readonly=on,unit=0," - f"file={ovmf / 'OVMF_CODE.fd'}", - "-drive", f"if=pflash,format=raw,unit=1,file={run / 'vars.fd'}", - "-chardev", f"socket,id=chrtpm,path={run / 'tpm.sock'}", - "-tpmdev", "emulator,id=tpm0,chardev=chrtpm", - "-device", "tpm-crb,tpmdev=tpm0", - "-drive", f"if=none,id=disk,file={overlay},format=qcow2", - *self._disk_device(), - "-netdev", f"user,id=net0,hostfwd=tcp:127.0.0.1:" - f"{self.agent_port}-:7878", - "-device", "e1000e,netdev=net0", - "-device", "qemu-xhci", "-device", "usb-tablet", - "-device", "usb-kbd", "-device", "VGA,vgamem_mb=64", - # hotplug slots for extra displays (see `WindowsPlatform`) - *[arg for n in range(1, HOTPLUG_PORTS + 1) for arg in - ("-device", f"pcie-root-port,id=rp{n},chassis={n},slot={n}")], - *self._extra, - # a QXL device wants a SPICE server behind it; without one QEMU - # aborts in handle_dev_oom when the device runs short of memory - "-spice", f"unix=on,addr={run / 'spice.sock'}," - "disable-ticketing=on", - "-display", "none", - "-qmp", f"unix:{run / 'qmp.sock'},server=on,wait=off", - *(["-incoming", "defer"] if self._state else [])] - ) + self._start(self.qemu_args(ovmf, run, overlay)) self.qmp = Qmp(run / "qmp.sock") if self._state: self._restore() @@ -139,6 +110,38 @@ class Guest: self.agent.connect(timeout=180) return self + def qemu_args(self, ovmf: Path, run: Path, overlay: Path) -> list[str]: + """The command line of QEMU. A saved state only fits a guest with the + same one apart from its paths, so `state.py` hashes it.""" + return ["qemu-system-x86_64", "-name", "e2e-windows", "-accel", "kvm", + "-machine", "q35,smm=off", "-cpu", "host", + "-smp", str(self._vcpus), "-m", self._mem, + *(["-rtc", f"base={self._rtc_base}"] if self._rtc_base else []), + "-drive", f"if=pflash,format=raw,readonly=on,unit=0," + f"file={ovmf / 'OVMF_CODE.fd'}", + "-drive", f"if=pflash,format=raw,unit=1,file={run / 'vars.fd'}", + "-chardev", f"socket,id=chrtpm,path={run / 'tpm.sock'}", + "-tpmdev", "emulator,id=tpm0,chardev=chrtpm", + "-device", "tpm-crb,tpmdev=tpm0", + "-drive", f"if=none,id=disk,file={overlay},format=qcow2", + *self._disk_device(), + "-netdev", f"user,id=net0,hostfwd=tcp:127.0.0.1:" + f"{self.agent_port}-:7878", + "-device", "e1000e,netdev=net0", + "-device", "qemu-xhci", "-device", "usb-tablet", + "-device", "usb-kbd", "-device", "VGA,vgamem_mb=64", + # hotplug slots for extra displays (see `WindowsPlatform`) + *[arg for n in range(1, HOTPLUG_PORTS + 1) for arg in + ("-device", f"pcie-root-port,id=rp{n},chassis={n},slot={n}")], + *self._extra, + # a QXL device wants a SPICE server behind it; without one QEMU + # aborts in handle_dev_oom when the device runs short of memory + "-spice", f"unix=on,addr={run / 'spice.sock'}," + "disable-ticketing=on", + "-display", "none", + "-qmp", f"unix:{run / 'qmp.sock'},server=on,wait=off", + *(["-incoming", "defer"] if self._state else [])] + def _disk_device(self) -> list[str]: if self._state is None and not self._ahci: return ["-device", "nvme,drive=disk,serial=e2e0,bootindex=1"] diff --git a/e2e/platforms/qemu_windows/state.py b/e2e/platforms/qemu_windows/state.py index 6bb485ee..bf4a848a 100644 --- a/e2e/platforms/qemu_windows/state.py +++ b/e2e/platforms/qemu_windows/state.py @@ -39,16 +39,24 @@ def _chain(disk: Path) -> list[str]: disk = (disk.parent / backing).resolve() +def _machine() -> list[str]: + """The command line of the guest that is saved, with the paths and the + port, which differ from run to run, as placeholders: a change to what the + guest is (its devices, memory, clock) makes a new state, and a change to + the rest of guest.py does not.""" + guest = Guest(Path("golden"), Path("run"), ahci=True) + guest.agent_port = 0 + return guest.qemu_args(Path("ovmf"), Path("run"), Path("run/overlay.qcow2")) + + def _key(golden: Path) -> str: qemu = subprocess.run( ["qemu-system-x86_64", "--version"], capture_output=True, text=True, check=True, ).stdout - # guest.py holds the command line: a change to it makes a new state - source = (Path(__file__).parent / "guest.py").read_bytes() parts = [ *_chain(golden / "disk.qcow2"), qemu, - os.environ.get("OVMF_FD", ""), hashlib.sha256(source).hexdigest(), + os.environ.get("OVMF_FD", ""), json.dumps(_machine()), ] return hashlib.sha256("\n".join(parts).encode()).hexdigest()