1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
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()
|