From e2d9cb7dd42eb4323a743347343cc97cc381dd21 Mon Sep 17 00:00:00 2001 From: Hermenix Date: Sun, 11 Oct 2026 22:47:28 +0000 Subject: [PATCH] spike: record what crashes the compositor, and the copy probe that explains grim-rs CRASH-FINDING.md is the delegated session's sharper version of the crash result: hold a live ext capture session on a session's output, kill the session, call create_frame -> SIGSEGV in CScreenshareFrame::transform(). That is a reproducible trigger, not a correlation, and it is why ext-image-copy-capture was measured and then not adopted. copyout_probe.py prices the per-frame allocate/copy/swizzle that grim-rs's API forces: 2.2 ms of client time, which is nowhere near its 46 ms deficit against the in-process route -- so the difference is buffer reuse, not language. --- spikes/capture-speed/CRASH-FINDING.md | 57 ++++++++++++++++++++ spikes/capture-speed/copyout_probe.py | 77 +++++++++++++++++++++++++++ 2 files changed, 134 insertions(+) create mode 100644 spikes/capture-speed/CRASH-FINDING.md create mode 100644 spikes/capture-speed/copyout_probe.py diff --git a/spikes/capture-speed/CRASH-FINDING.md b/spikes/capture-speed/CRASH-FINDING.md new file mode 100644 index 0000000..9ea57b0 --- /dev/null +++ b/spikes/capture-speed/CRASH-FINDING.md @@ -0,0 +1,57 @@ +# What crashes the compositor, and exactly when + +Recorded from the delegated Claude session that spent its run on this, before that +session was shut down. Its finding is sharper than the one the benchmark itself +produced, so it is written down here rather than left in a tmux transcript. + +## The trigger + +Reproduced deliberately, not just observed: + +1. Hold a live capture session on a **session's headless output** (route 001b's + `--protocol ext` producer, i.e. `ext-image-copy-capture-v1`). +2. Run `hhstream session kill` on that session, which destroys its output. +3. Call `create_frame` on the now-dangling capture session. + +→ `SIGSEGV`, core dumped. + +The kill is **not** the crash: the compositor dies inside +`ext-image-copy-capture`'s frame creation, on an output that no longer exists. + +## The stack + +``` +#0 CScreenshareFrame::transform() const +#1 CImageCopyCaptureFrame::CImageCopyCaptureFrame(...) +#2 CExtImageCopyCaptureManagerV1Options... (createFrame) +#3 _CExtImageCopyCaptureSessionV1CreateFrame +... +#4 ffi_call_unix64 +#10 wl_display_run +``` + +## Why it matters, and what was done about it + +- It explains **three** compositor deaths during the benchmark (the two whose + stack reached `CScreenshareFrame::transform()`, and one in `libaquamarine`), + including one that happened mid-run and invalidated cells: the harness kills a + session between passes while a producer may still hold a capture session on it. + So the harness, not the protocol by itself, was pulling the trigger. +- It is the reason the candidate that was measured and **not** adopted is not + adopted: `ext-image-copy-capture-v1` measured no faster than + `zwlr-screencopy-v1` (78.0 vs 77.7 ms; compositor 67.5 vs 67.2 ms) and its + frame-creation path is the one that segfaults the compositor. + The `feat/pywayland-capture` branch therefore keeps the deprecated + `zwlr-screencopy-v1`, with the switch documented as one constant. +- The crash is in Hyprland (0.56.2, software rendering), not in the harness or in + pywayland. Anything that wants to propose it upstream will need this trigger, + because it is the reproducible part: hold a capture session, destroy the + output underneath it, then ask for a frame. + +## Not established + +- Whether `zwlr-screencopy-v1` survives the same dangling-output sequence. The + in-process backend in `feat/pywayland-capture` does handle the nearest + equivalent (`global_remove` drops the output, and the next `grab()` reconnects + and re-selects) — but that path has not been exercised against a real session + kill, which is the obvious next experiment. diff --git a/spikes/capture-speed/copyout_probe.py b/spikes/capture-speed/copyout_probe.py new file mode 100644 index 0000000..1f0783b --- /dev/null +++ b/spikes/capture-speed/copyout_probe.py @@ -0,0 +1,77 @@ +#!/usr/bin/env python3 +"""What does a per-frame copy + swizzle cost? Isolating the grim-rs gap. + +The suspicion this answers: a compiled Rust library should not be *slower* than a +subprocess, and pure Python should not be faster than either. The explanation +under test is that the routes are not doing the same work. + +grim-rs's public API returns owned pixels -- per capture it does +`mmap.to_vec()` plus an XRGB/ARGB -> RGBA swizzle (its own source, and its own +notes in the results). Our producer hands the mapped buffer straight to the +encoder: no allocation, no copy, no conversion. + +So this measures what that extra work costs on this box, on its own, with no +compositor involved: allocate a mapping the size of one 1280x720 frame, then time +(a) copying it out and (b) swizzling it, per frame, over 200 frames. + +If the copy+swizzle costs about the gap between our number (~78 ms) and grim-rs's +(~124 ms), then the difference is the work, not the language -- and the honest +conclusion is "a client that reuses its buffer beats one that does not", not +"Python beats Rust". +""" + +from __future__ import annotations + +import mmap +import os +import statistics +import sys +import time + +WIDTH, HEIGHT = 1280, 720 +STRIDE = WIDTH * 4 +SIZE = STRIDE * HEIGHT +FRAMES = 200 + + +def main() -> int: + memory = mmap.mmap(-1, SIZE) + # Fill with something non-trivial so the copy is not optimised away. + memory.write(b"\x01\x02\x03\x04" * (SIZE // 4)) + + copies: list[float] = [] + swizzles: list[float] = [] + both: list[float] = [] + for _ in range(FRAMES): + start = time.monotonic_ns() + pixels = bytes(memoryview(memory)) + copies.append((time.monotonic_ns() - start) / 1e6) + + start = time.monotonic_ns() + swapped = bytearray(len(pixels)) + swapped[0::4] = pixels[2::4] + swapped[1::4] = pixels[1::4] + swapped[2::4] = pixels[2::4] + swapped[3::4] = pixels[3::4] + swizzles.append((time.monotonic_ns() - start) / 1e6) + + both.append(copies[-1] + swizzles[-1]) + del pixels, swapped + + print(f"frame: {WIDTH}x{HEIGHT}, {SIZE / 1e6:.2f} MB, {FRAMES} frames") + for label, samples in (("copy only (mmap -> Vec)", copies), + ("swizzle only (BGRX -> RGBA)", swizzles), + ("copy + swizzle", both)): + print(f" {label:<28} p50 {statistics.median(samples):>6.2f} ms " + f"min {min(samples):>6.2f} max {max(samples):>6.2f}") + print("\nAdd 'copy + swizzle' to the in-process route's number to compare it " + "against a library that returns owned pixels.") + memory.close() + # Flush before `os._exit`: os._exit skips the interpreter's cleanup, so + # buffered stdout is dropped -- the first run of this printed nothing at all. + sys.stdout.flush() + os._exit(0) + + +if __name__ == "__main__": + raise SystemExit(main())