Skip to content

armgit / Cap

Public Imported from GitHub
0

Open source Loom alternative. Beautiful, shareable screen recordings.

fix: measure complete desktop process groups accurately

Richie McIlroy099c3f86511d
diff --git a/scripts/instant-mode-performance-macos.py b/scripts/instant-mode-performance-macos.py
index 309550823..ce85872d4 100755
--- a/scripts/instant-mode-performance-macos.py
+++ b/scripts/instant-mode-performance-macos.py
@@ -4,6 +4,7 @@ import atexit
import argparse
import csv
import ctypes
+import hashlib
import json
import platform
import re
@@ -448,6 +449,20 @@ def process_executable(pid):
return executable
+def executable_identity(executable):
+ digest = hashlib.sha256()
+ with executable.open("rb") as handle:
+ for block in iter(lambda: handle.read(1024 * 1024), b""):
+ digest.update(block)
+ metadata = executable.stat()
+ return {
+ "path": str(executable),
+ "sha256": digest.hexdigest(),
+ "size_bytes": metadata.st_size,
+ "modified_ns": metadata.st_mtime_ns,
+ }
+
+
def associated_pids(app_pid):
result = run(["lsappinfo", "list"])
blocks = re.split(r"(?=^\s*\d+\) \")", result.stdout, flags=re.MULTILINE)
@@ -483,6 +498,25 @@ def associated_pids(app_pid):
role = f"webkit-{role_match.group(1).lower()}" if role_match else "webkit"
pids.append(pid)
processes.append({"pid": pid, "role": role, "command": command})
+ process_rows = [
+ line.split(None, 2)
+ for line in run(["ps", "-axo", "pid=,ppid=,comm="]).stdout.splitlines()
+ ]
+ known_pids = set(pids)
+ while True:
+ children = [
+ (int(row[0]), row[2])
+ for row in process_rows
+ if len(row) == 3
+ and int(row[1]) in known_pids
+ and int(row[0]) not in known_pids
+ ]
+ if not children:
+ break
+ for pid, command in children:
+ known_pids.add(pid)
+ pids.append(pid)
+ processes.append({"pid": pid, "role": "child", "command": command})
if len(pids) > 64:
raise RuntimeError(f"Too many associated processes to sample: {len(pids)}")
return pids, processes
@@ -661,7 +695,17 @@ def system_snapshot():
}
-def summarize_process_csv(csv_path, app_pid):
+def sampled_cpu_seconds(rows):
+ previous_elapsed = 0.0
+ cpu_seconds = 0.0
+ for row in rows:
+ interval = row["elapsed_s"] - previous_elapsed
+ cpu_seconds += row["cpu_pct"] * interval / 100
+ previous_elapsed = row["elapsed_s"]
+ return cpu_seconds
+
+
+def summarize_process_csv(csv_path, app_pid, expected_pids=None):
rows = []
with csv_path.open(newline="") as handle:
for row in csv.DictReader(handle):
@@ -677,6 +721,12 @@ def summarize_process_csv(csv_path, app_pid):
by_sample = {}
for row in rows:
by_sample.setdefault(row["sample"], []).append(row)
+ expected_pids = set(expected_pids or (row["pid"] for row in rows))
+ complete_samples = list(by_sample) == list(range(1, len(by_sample) + 1)) and all(
+ len(sample_rows) == len(expected_pids)
+ and {row["pid"] for row in sample_rows} == expected_pids
+ for sample_rows in by_sample.values()
+ )
group_samples = []
for sample_rows in by_sample.values():
group_samples.append(
@@ -707,13 +757,7 @@ def summarize_process_csv(csv_path, app_pid):
row["cpu_pct"] for row in pid_rows
),
"cpu_average_pct": (
- (
- last_by_pid[pid]["user_ns"]
- + last_by_pid[pid]["system_ns"]
- - first_by_pid[pid]["user_ns"]
- - first_by_pid[pid]["system_ns"]
- )
- / 1_000_000_000
+ sampled_cpu_seconds(pid_rows)
/ max(pid_rows[-1]["elapsed_s"], 0.001)
* 100
),
@@ -731,22 +775,18 @@ def summarize_process_csv(csv_path, app_pid):
for pid in last_by_pid
)
elapsed = max(item["elapsed_s"] for item in group_samples)
- cpu_time_seconds = sum(
- max(
- 0,
- (
- last_by_pid[pid]["user_ns"]
- + last_by_pid[pid]["system_ns"]
- - first_by_pid[pid]["user_ns"]
- - first_by_pid[pid]["system_ns"]
- ),
- )
- for pid in last_by_pid
- ) / 1_000_000_000
+ counter_elapsed = elapsed - group_samples[0]["elapsed_s"]
+ cpu_time_seconds = sampled_cpu_seconds(group_samples)
+
+ def counter_rate(column, divisor=1):
+ return deltas[column] / divisor / counter_elapsed if counter_elapsed > 0 else None
+
return {
"sample_count": len(group_samples),
"process_count": len(last_by_pid),
+ "complete_samples": complete_samples,
"elapsed_seconds": elapsed,
+ "counter_elapsed_seconds": counter_elapsed,
"cpu_median_pct": statistics.median(
item["cpu_pct"] for item in group_samples
),
@@ -777,19 +817,19 @@ def summarize_process_csv(csv_path, app_pid):
"thread_median": statistics.median(
item["threads"] for item in group_samples
),
- "disk_read_bytes_per_second": deltas["disk_read_bytes"] / elapsed,
- "disk_write_bytes_per_second": deltas["disk_write_bytes"] / elapsed,
- "energy_millijoules_per_second": deltas["energy_nj"] / 1_000_000 / elapsed,
- "instructions_per_second": deltas["instructions"] / elapsed,
- "cycles_per_second": deltas["cycles"] / elapsed,
- "faults_per_second": deltas["faults"] / elapsed,
+ "disk_read_bytes_per_second": counter_rate("disk_read_bytes"),
+ "disk_write_bytes_per_second": counter_rate("disk_write_bytes"),
+ "energy_millijoules_per_second": counter_rate("energy_nj", 1_000_000),
+ "instructions_per_second": counter_rate("instructions"),
+ "cycles_per_second": counter_rate("cycles"),
+ "faults_per_second": counter_rate("faults"),
"wakeups_per_second": (
- deltas["idle_wakeups"] + deltas["interrupt_wakeups"]
- )
- / elapsed,
- "context_switches_per_second": deltas["context_switches"] / elapsed,
- "mach_syscalls_per_second": deltas["mach_syscalls"] / elapsed,
- "unix_syscalls_per_second": deltas["unix_syscalls"] / elapsed,
+ (deltas["idle_wakeups"] + deltas["interrupt_wakeups"]) / counter_elapsed
+ if counter_elapsed > 0 else None
+ ),
+ "context_switches_per_second": counter_rate("context_switches"),
+ "mach_syscalls_per_second": counter_rate("mach_syscalls"),
+ "unix_syscalls_per_second": counter_rate("unix_syscalls"),
"deltas": deltas,
}
@@ -857,11 +897,16 @@ def measure_phase(
network_after["raw"]
)
system_after = system_snapshot()
- process_summary = summarize_process_csv(csv_path, app_pid)
+ after_pids, after_processes = associated_pids(app_pid)
+ process_summary = summarize_process_csv(csv_path, app_pid, pids)
+ processes_stable = set(pids) == set(after_pids)
return {
"name": name,
"duration_seconds": elapsed,
"processes": processes,
+ "processes_after": after_processes,
+ "processes_stable": processes_stable,
+ "valid_for_comparison": processes_stable and process_summary["complete_samples"],
"process_metrics": process_summary,
"network": network_delta(network_before, network_after, elapsed),
"gpu": summarize_gpu(gpu_samples),
@@ -1118,6 +1163,7 @@ def aggregate_runs(runs):
for run_result in runs
for phase in run_result["phases"]
if phase["name"] == phase_name
+ and phase.get("valid_for_comparison", True)
and phase[section].get(metric) is not None
]
if values:
@@ -1150,6 +1196,9 @@ def print_summary(summary):
"phase cpu median footprint disk write preview IPC other net"
)
for name, phase in summary["aggregate"].items():
+ if not phase:
+ print(f"{name:<14} incomplete process coverage; inspect raw phase results")
+ continue
cpu = phase.get("cpu_median_pct", {}).get("median", 0)
memory = (
phase.get("phys_footprint_median_bytes", {}).get("median", 0)
@@ -1260,6 +1309,9 @@ def main():
"created_at": datetime.now().astimezone().isoformat(),
"repository_root": str(REPO_ROOT),
"git_commit": run(["git", "rev-parse", "HEAD"]).stdout.strip(),
+ "git_status": run(["git", "status", "--porcelain"]).stdout.strip(),
+ "app_binary": executable_identity(app_executable),
+ "cap_binary": executable_identity(args.cap_binary),
"configuration": configuration,
"targets": targets,
"artifacts_dir": str(artifacts_dir),
diff --git a/scripts/instant-mode-performance.md b/scripts/instant-mode-performance.md
index 24cd592fc..ff97cc459 100644
--- a/scripts/instant-mode-performance.md
+++ b/scripts/instant-mode-performance.md
@@ -11,11 +11,11 @@ or executed by its production runtime.
## Files
-| File | Purpose |
-|---|---|
+| File | Purpose |
+| ----------------------------------- | ---------------------------------------------------------------------------------- |
| `instant-mode-performance-macos.py` | Orchestrates profiling runs, records phase metrics, and validates recording output |
-| `instant-mode-process-sampler.c` | Samples CPU, memory, energy, I/O, wakeups, syscalls, and related process counters |
-| `instant-mode-action-macos.swift` | Sends debug actions to the running app and reads its macOS window state |
+| `instant-mode-process-sampler.c` | Samples CPU, memory, energy, I/O, wakeups, syscalls, and related process counters |
+| `instant-mode-action-macos.swift` | Sends debug actions to the running app and reads its macOS window state |
The Python harness compiles the C and Swift helpers into the selected artifact
directory. Compiled helper binaries are not written to the repository.
@@ -83,8 +83,34 @@ location. The directory contains:
- `run-*/result.json` with media and lifecycle validation
- `summary.json` with medians and ranges across repetitions
+`prepared.json` includes executable SHA-256 hashes and checkout status. The
+checkout commit alone does not establish the running binary's source revision;
+verify the build identity before comparing runs and keep builds idle while sampling.
+
Compare builds with identical targets, phase durations, preview settings, and
repetition counts. Use at least three warmed repetitions and compare the
`recording` aggregates in `summary.json`. Confirm output resolution, frame rate,
audio, frame drops, upload completion, and validation errors before accepting a
performance change.
+
+Process totals include Cap, its associated WebKit processes, and descendants such
+as the separate muxer. Shared macOS camera and encoder services are not attributed
+to Cap by this sampler. CPU percentages use one fully occupied core as 100%.
+Physical footprint totals sum per-process values and can include shared pages.
+
+CPU averages include the first sampled interval and weight intervals by elapsed
+time. Cumulative counter rates, including disk writes and wakeups, use the time
+between the first and last rows; `counter_elapsed_seconds` records that window.
+A single row cannot establish a cumulative counter rate.
+
+Phases with missing process samples or changed process membership are marked
+`valid_for_comparison: false` and excluded from comparison aggregates. Inspect
+their raw samples when studying startup or shutdown, where processes can appear
+or exit during a phase. A stable process list does not prove that a workload or
+foreground capture target stayed unchanged.
+
+Run the profiler's measurement regression tests with:
+
+```bash
+python3 -B scripts/test-instant-mode-performance.py
+```
diff --git a/scripts/test-instant-mode-performance.py b/scripts/test-instant-mode-performance.py
new file mode 100644
index 000000000..f547e7e87
--- /dev/null
+++ b/scripts/test-instant-mode-performance.py
@@ -0,0 +1,97 @@
+import csv
+import importlib.util
+import subprocess
+import tempfile
+import unittest
+from pathlib import Path
+from unittest.mock import patch
+
+
+SPEC = importlib.util.spec_from_file_location(
+ "instant_performance", Path(__file__).with_name("instant-mode-performance-macos.py")
+)
+PERF = importlib.util.module_from_spec(SPEC)
+SPEC.loader.exec_module(PERF)
+
+
+class ProcessMetricsTests(unittest.TestCase):
+ def summarize(self, samples, expected_pids=None):
+ with tempfile.TemporaryDirectory() as directory:
+ path = Path(directory) / "processes.csv"
+ with path.open("w", newline="") as handle:
+ writer = csv.DictWriter(handle, fieldnames=[
+ "sample", "elapsed_s", "pid", "name", "cpu_pct", *sorted(PERF.NUMERIC_COLUMNS)
+ ])
+ writer.writeheader()
+ for sample, elapsed, pid, cpu in samples:
+ writer.writerow({
+ **dict.fromkeys(PERF.NUMERIC_COLUMNS, 0),
+ "sample": sample,
+ "elapsed_s": elapsed,
+ "pid": pid,
+ "name": f"process-{pid}",
+ "cpu_pct": cpu,
+ "user_ns": int(elapsed * cpu * 10_000_000),
+ "disk_write_bytes": int(elapsed * 200),
+ })
+ return PERF.summarize_process_csv(path, 10, expected_pids)
+
+ def test_cpu_includes_the_first_measured_interval(self):
+ result = self.summarize([(1, 1, 10, 100), (2, 2, 10, 100)])
+ self.assertEqual(result["cpu_average_pct"], 100)
+ self.assertEqual(result["cpu_time_seconds"], 2)
+ self.assertEqual(result["process_breakdown"][0]["cpu_average_pct"], 100)
+ self.assertEqual(result["disk_write_bytes_per_second"], 200)
+ self.assertEqual(result["counter_elapsed_seconds"], 1)
+ self.assertTrue(result["complete_samples"])
+
+ def test_cpu_weights_unequal_intervals_and_sums_processes(self):
+ result = self.summarize([
+ (1, 1, 10, 100), (1, 1, 11, 50),
+ (2, 3, 10, 25), (2, 3, 11, 50),
+ ])
+ self.assertEqual(result["cpu_average_pct"], 100)
+ self.assertEqual(result["cpu_time_seconds"], 3)
+
+ def test_single_sample_has_cpu_but_no_counter_rate(self):
+ result = self.summarize([(1, 1, 10, 75)])
+ self.assertEqual(result["cpu_average_pct"], 75)
+ self.assertIsNone(result["disk_write_bytes_per_second"])
+
+ def test_missing_process_or_sample_cannot_be_complete(self):
+ for samples, expected in [
+ ([(1, 1, 10, 10), (2, 2, 10, 10)], [10, 11]),
+ ([(1, 1, 10, 10), (3, 3, 10, 10)], [10]),
+ ([(1, 1, 10, 10), (1, 1, 10, 10)], [10]),
+ ]:
+ with self.subTest(samples=samples):
+ self.assertFalse(self.summarize(samples, expected)["complete_samples"])
+
+ def test_incomplete_phases_do_not_enter_comparison_aggregates(self):
+ result = PERF.aggregate_runs([{"phases": [
+ {"name": "recording", "valid_for_comparison": False,
+ "process_metrics": {"cpu_average_pct": 1}, "network": {}},
+ {"name": "recording", "valid_for_comparison": True,
+ "process_metrics": {"cpu_average_pct": 50}, "network": {}},
+ ]}])
+ self.assertEqual(result["recording"]["cpu_average_pct"]["median"], 50)
+
+ def test_encoder_descendants_are_counted_once(self):
+ apps = '\n'.join([
+ '1) "cap-desktop" ASN:0x1:', ' pid = 10',
+ '2) "cap-desktop Graphics and Media" ASN:0x2:',
+ ' bundleID="com.apple.WebKit.GPU"', ' pid = 11',
+ '3) "Other App" ASN:0x3:', ' pid = 99',
+ ])
+ processes = '10 1 cap-desktop\n11 10 WebKit\n12 10 cap-muxer\n13 12 helper\n99 1 Other App\n'
+ with patch.object(PERF, "run", side_effect=[
+ subprocess.CompletedProcess([], 0, stdout=apps),
+ subprocess.CompletedProcess([], 0, stdout=processes),
+ ]):
+ pids, roles = PERF.associated_pids(10)
+ self.assertEqual(pids, [10, 11, 12, 13])
+ self.assertEqual([role["role"] for role in roles], ["app", "webkit-gpu", "child", "child"])
+
+
+if __name__ == "__main__":
+ unittest.main()