diff options
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() |
