The question is which capture route to ship for live streaming and agent eyes. Speed alone does not decide it, so every route now also reports producer CPU and RSS, machine CPU from /proc/stat, the compositor's own CPU, and a per-frame buffer footprint -- all sampled around the measured loop, with an idle control sample so an animating page's cost is not attributed to the capture. The baseline forks a process per frame and spends 95% of each frame waiting, so the interesting finding is not which route is faster but which one leaves the compositor able to do its job: in-process acquisition roughly halves the compositor's per-frame CPU even though the machine saturates either way.
143 lines
5.1 KiB
Python
143 lines
5.1 KiB
Python
#!/usr/bin/env python3
|
|
"""Resource measurement for the capture routes: RAM, and CPU from three angles.
|
|
|
|
Speed is not the whole answer: a route that reaches 15 fps by burning a core is
|
|
not one this machine can ship while it is also rendering windows. So every route
|
|
reports
|
|
|
|
- **its own** CPU and peak RSS (`getrusage`, `RUSAGE_CHILDREN` for the
|
|
subprocess-per-frame baseline, whose cost lives in children);
|
|
- **the machine's** CPU from `/proc/stat`, which is the number that catches a
|
|
route that is only fast because somebody else did more work;
|
|
- **the compositor's** CPU from `/proc/<hyprland>/stat`, which separates "the
|
|
capture is expensive" from "the capture made the compositor expensive".
|
|
|
|
Everything is sampled around the *measured* loop, never the warm-up, so the
|
|
numbers line up with the per-frame times in the result.
|
|
"""
|
|
|
|
from __future__ import annotations
|
|
|
|
import os
|
|
import resource
|
|
|
|
CLK_TCK = os.sysconf("SC_CLK_TCK")
|
|
|
|
|
|
def _hyprland_pid() -> int | None:
|
|
"""The compositor's pid, read from /proc rather than by shelling out.
|
|
|
|
`systemctl show -p MainPID` would be a CLI call, and a benchmark that shells
|
|
out to ask its own question is measuring something other than the route.
|
|
"""
|
|
try:
|
|
for entry in os.listdir("/proc"):
|
|
if not entry.isdigit():
|
|
continue
|
|
try:
|
|
with open(f"/proc/{entry}/comm") as handle:
|
|
comm = handle.read().strip()
|
|
except OSError:
|
|
continue
|
|
if "Hyprland" in comm:
|
|
return int(entry)
|
|
except OSError:
|
|
return None
|
|
return None
|
|
|
|
|
|
def _proc_cpu_ticks(pid: int | None) -> float | None:
|
|
"""utime+stime of one process, in seconds."""
|
|
if pid is None:
|
|
return None
|
|
try:
|
|
with open(f"/proc/{pid}/stat") as handle:
|
|
fields = handle.read().rsplit(")", 1)[1].split()
|
|
# After the comm field: state is field 3, so utime/stime (14/15 in the
|
|
# full line) land at 11 and 12 here.
|
|
return (int(fields[11]) + int(fields[12])) / CLK_TCK
|
|
except (OSError, IndexError, ValueError):
|
|
return None
|
|
|
|
|
|
def _system_cpu_seconds() -> float | None:
|
|
"""Total CPU consumed by everything, all cores, in seconds."""
|
|
try:
|
|
with open("/proc/stat") as handle:
|
|
line = handle.readline()
|
|
parts = [int(value) for value in line.split()[1:]]
|
|
# user nice system idle iowait irq softirq steal guest guest_nice
|
|
busy = parts[0] + parts[1] + parts[2] + parts[5] + parts[6] + parts[7]
|
|
return busy / CLK_TCK
|
|
except (OSError, ValueError, IndexError):
|
|
return None
|
|
|
|
|
|
class MachineSample:
|
|
"""One instant of the three CPU counters plus the RSS source."""
|
|
|
|
def __init__(self, children: bool) -> None:
|
|
self.children = children
|
|
self.pid = _hyprland_pid()
|
|
self.process_cpu = self._own_cpu_seconds()
|
|
self.system_cpu = _system_cpu_seconds()
|
|
self.compositor_cpu = _proc_cpu_ticks(self.pid)
|
|
|
|
def _own_cpu_seconds(self) -> float:
|
|
usage = resource.getrusage(
|
|
resource.RUSAGE_CHILDREN if self.children else resource.RUSAGE_SELF
|
|
)
|
|
return usage.ru_utime + usage.ru_stime
|
|
|
|
@property
|
|
def rss_peak_kb(self) -> int:
|
|
usage = resource.getrusage(
|
|
resource.RUSAGE_CHILDREN if self.children else resource.RUSAGE_SELF
|
|
)
|
|
# ru_maxrss is KiB on Linux, despite the name.
|
|
return int(usage.ru_maxrss)
|
|
|
|
|
|
def resource_fields(
|
|
before: MachineSample,
|
|
after: MachineSample,
|
|
frames: int,
|
|
bytes_allocated: int | None,
|
|
window_ms: float,
|
|
cores: int | None = None,
|
|
) -> dict:
|
|
"""The contract's resource block, from two samples around the measured loop."""
|
|
if frames <= 0:
|
|
frames = 1
|
|
notes: list[str] = []
|
|
|
|
system_delta_ms = None
|
|
if before.system_cpu is not None and after.system_cpu is not None:
|
|
system_delta_ms = (after.system_cpu - before.system_cpu) * 1000.0
|
|
|
|
compositor_ms = None
|
|
if before.compositor_cpu is not None and after.compositor_cpu is not None:
|
|
compositor_ms = (after.compositor_cpu - before.compositor_cpu) * 1000.0
|
|
|
|
own_ms = (after.process_cpu - before.process_cpu) * 1000.0
|
|
|
|
if compositor_ms is None:
|
|
notes.append("compositor CPU unreadable (/proc/<Hyprland>/stat)")
|
|
|
|
cores = cores or os.cpu_count() or 1
|
|
machine_pct = None
|
|
if system_delta_ms is not None and window_ms > 0:
|
|
machine_pct = system_delta_ms / window_ms * 100.0
|
|
|
|
return {
|
|
"cpu_ms_per_frame": round(own_ms / frames, 3),
|
|
"rss_peak_mb": round(after.rss_peak_kb / 1024, 2),
|
|
"rss_peak_kb_raw": after.rss_peak_kb,
|
|
"system_cpu_ms_per_frame": None if system_delta_ms is None else round(system_delta_ms / frames, 3),
|
|
"compositor_cpu_ms_per_frame": None if compositor_ms is None else round(compositor_ms / frames, 3),
|
|
"machine_cpu_pct": None if machine_pct is None else round(machine_pct, 1),
|
|
"machine_cpu_pct_of_all_cores": None if machine_pct is None else round(machine_pct / cores, 1),
|
|
"frames_bytes_allocated_p50": bytes_allocated,
|
|
"resource_notes": "; ".join(notes),
|
|
}
|