diff options
| author | Gabriel Schneider <[email protected]> | 2026-10-29 16:55:47 -0300 |
|---|---|---|
| committer | Gabriel Schneider <[email protected]> | 2026-10-29 19:54:26 -0300 |
| commit | 1c87b21d9255d9b0a7af102afd7defec25ac2c8a (patch) | |
| tree | 1bf647be56591971e0a6320f15742ceb4e547d08 /test/fork_stall.py | |
| parent | 6a6e1b2cb3e328d5abd4435c1c2659f4e4e31eb8 (diff) | |
| download | pardes-1c87b21d9255d9b0a7af102afd7defec25ac2c8a.tar.gz pardes-1c87b21d9255d9b0a7af102afd7defec25ac2c8a.zip | |
rollback-bench and fork_stall.py: what a replica's rollback copy is (a walk of ~100 KB once file texts are pinned) and what a fork stalls the authority for, which settle open points 3 and 5
`zig build rollback-bench -Dplatform=tty -Doptimize=ReleaseFast` (test/rollback_bench.zig) builds a typical session (a shell with 1 MB of scrollback, three 60 KB files and a 3 MB one, 40 edits) and a big one (14 panes, 18 MB, 4x3 MB and 8 small files, 8 MB of scrollback, 200 edits) in process, then measures the state the digest walk visits, the part of it that is a file's text and its undo snapshots (immutable once made: an edit puts a new buffer in place, a snapshot is a copy, so a copy pins them), and the walk again with those taken out, plus a memcpy of what is left. Typical: 36.3 MB walked, 80 KB to copy, about 0.02 ms; big: 282.5 MB, 96 KB, about 0.045 ms. Then a fork of the big session (431 MB RSS: 3.4 to 8.3 ms) and 64 MB written by the parent under the child (~28 ms of copy-on-write).
test/fork_stall.py does the fork alone, on any system with a Python (the FreeBSD box): N MB written, then fork() timed and 64 MB written under the child, with Linux's THP as set and with 4 KiB pages. 1 GB: 13.3 to 19.6 ms here, 20.4 ms on FreeBSD 15.1; the copy-on-write after it ~1.3 us a page on both.
Co-Authored-By: Claude Opus 5.5 <[email protected]>
Diffstat (limited to 'test/fork_stall.py')
| -rw-r--r-- | test/fork_stall.py | 76 |
1 files changed, 76 insertions, 0 deletions
diff --git a/test/fork_stall.py b/test/fork_stall.py new file mode 100644 index 00000000..c865b6bc --- /dev/null +++ b/test/fork_stall.py @@ -0,0 +1,76 @@ +#!/usr/bin/env python3 +"""What a fork stalls the authority for (docs/next-daemon.md, open point 5: +the join writer as a fork child): a process holding N MB of written +anonymous memory, as a session's heap and arenas are, forks a child that +waits and exits; timed are the fork() itself and the copy-on-write of the +authority's next 64 MB written while the child lives. + + python3 -B test/fork_stall.py [MB ...] (default 100 600 1024 4096) + +On Linux each size runs twice: with transparent huge pages as the system +has them, and with MADV_NOHUGEPAGE (4 KiB pages, as a fragmented heap). +""" +import mmap +import os +import statistics +import sys +import time + +REPS = 9 +COW = 64 << 20 + + +def fill(m, size): + block = b'\1' * (16 << 20) + for at in range(0, size, len(block)): + m[at:at + len(block)] = block[:size - at] + + +def one(size, huge): + m = mmap.mmap(-1, size, flags=mmap.MAP_PRIVATE | mmap.MAP_ANONYMOUS) + if not huge: + m.madvise(mmap.MADV_NOHUGEPAGE) + fill(m, size) + forks, cows = [], [] + for _ in range(REPS): + r, w = os.pipe() + t0 = time.perf_counter_ns() + pid = os.fork() + if pid == 0: + os.read(r, 1) + os._exit(0) + t1 = time.perf_counter_ns() + # What the authority writes next, the child holding its pages. + for at in range(0, min(COW, size), 4096): + m[at] = 2 + t2 = time.perf_counter_ns() + os.write(w, b'x') + os.waitpid(pid, 0) + os.close(r) + os.close(w) + forks.append((t1 - t0) / 1e6) + cows.append((t2 - t1) / 1e6) + # The same writes with no child: the loop's own cost, taken off. + t0 = time.perf_counter_ns() + for at in range(0, min(COW, size), 4096): + m[at] = 3 + loop = (time.perf_counter_ns() - t0) / 1e6 + m.close() + return statistics.median(forks), max(forks), statistics.median(cows) - loop + + +def main(): + sizes = [int(a) for a in sys.argv[1:]] or [100, 600, 1024, 4096] + modes = [True, False] if hasattr(mmap, 'MADV_NOHUGEPAGE') else [True] + print(f'{os.uname().sysname} {os.uname().release}: fork of a process with N MB written, ' + f'median and max of {REPS}; then {COW >> 20} MB written under the child') + for size in sizes: + for huge in modes: + fork_med, fork_max, cow = one(size << 20, huge) + pages = 'system pages' if huge else '4 KiB pages' + print(f'{size:>6} MB {pages:<12} fork {fork_med:8.3f} ms (max {fork_max:8.3f}) ' + f'cow {cow:8.3f} ms', flush=True) + + +if __name__ == '__main__': + main() |
