fork() intermittently takes 3x longer: dropping the child's MAP_SHARED clone at teardown stalls the parent
I maintainer di solito rispondono entro 2 giorni
Nessuno ha ancora preso questa issue.
Valutazione
- Difficoltà
- 4/5
- Tempo stimato
- 3-5 giorni
- Idoneità per principianti
- 58/100
- Tipo di issue
- Bug
- Chiarezza
- Specificata chiaramente
- Stato di attività
- Attiva
- Stack tecnologico
- c, macos
- Ambito
- operating-systems, performance
Direzione di ricerca
Start in src/runtime/forkipc.c at fork_child_main and trace how hdr.shm_is_clone selects the mapping mode passed to guest_init_from_shm. Read the existing clone and region-copy fork paths, then run the supplied serial and nested fork reproducer to compare latency and child behavior. Done means addressing the teardown stall while checking the documented nested-fork trade-off and reporting results for both modes.
Scritto dal modello di indicizzazione a partire dal testo della issue.
Descrizione
What happens
A guest that forks and execs short-lived children one after another (the
pattern of make in applications.make_ms, or any shell loop) sees fork go
back and forth between two speeds. Most forks take about 7 ms. In stretches,
forks take 20 ms or more, and a stretch lasts for several rounds of 50 forks.
The guest code does the same work at both speeds; the only difference is how
long elfuse takes before the child starts running.
On upstream main 1bfceb7 (Apple M4, macOS 15.4, on battery with Low Power
Mode off and no other heavy load), the reproducer below runs 60 rounds of 50
serial fork+exec. Each round took between 316 ms and 3112 ms:
round 0: 372.2 ms, 0/50 forks >= 12 ms, slowest 9.8 ms
...
round 29: 324.3 ms, 0/50 forks >= 12 ms, slowest 8.4 ms
round 30: 526.6 ms, 15/50 forks >= 12 ms, slowest 29.3 ms
...
round 40: 1174.4 ms, 49/50 forks >= 12 ms, slowest 61.3 ms
round 52: 1157.3 ms, 50/50 forks >= 12 ms, slowest 51.4 ms
round 55: 3111.8 ms, 41/50 forks >= 12 ms, slowest 915.5 ms
The same program built for macOS (fork + execv of itself) has a spread of
4% (60.2 to 62.4 ms per round).
This is a plausible cause of the two levels applications.make_ms jumps
between within a run (the reason it gets "no verdict" in #504). That has not
been checked against the real make benchmark.
Evidence
Measured with timestamps added to an experiment build. None of the
instrumentation below is upstream.
1. The child is waiting for the parent, not doing work. Splitting each
fork into phases (one run of 3000 forks; a fork is "slow" at >= 12 ms from
fork to the child's main):
| phase (median, ms) | fast fork | slow fork |
|---|---|---|
| parent: posix_spawn | 0.24 | 0.25 |
| host exec of elfuse to main() | 2.38 | 2.45 |
| parent: fclonefileat snapshot | 0.59 | 0.64 |
| parent: send header, registers, regions, process state | 2.68 | 2.67 |
| child: guest_init_from_shm | 0.42 | 0.41 |
| child: receive state | 0.38 | 0.38 |
| child: wait for the parent's admission byte | 0.25 | 17.04 |
| child: hv_vcpu_create | 0.35 | 0.43 |
| guest: 200 getppid syscalls | 0.007 | 0.007 |
| guest: first touch of 256 pages | 0.26 | 0.26 |
2. The parent is late because its registry writes are slow. Between
sending state and sending the admission byte, the parent runs
proc_register_child:
| step (median, ms) | fast fork | slow fork |
|---|---|---|
lifecycle_publish_child |
0.06 | 1.38 |
proc_registry_publish_self |
0.09 | 8.53 |
flock(LOCK_EX) wait inside them |
0.00 | 0.00 |
The registry files are 200 to 384 bytes. The lock is not contended; the
open/read/ftruncate/write calls themselves are slow.
3. They are slow while the previous child drops its clone. In
guest_destroy, one step changes between fast and slow forks:
| teardown step (median, ms) | fast | slow | slow p90 |
|---|---|---|---|
| hv_vm_unmap + hv_vm_destroy + munmap | 0.62 | 0.65 | — |
close(g->shm_fd) |
1.94 | 24.4 | 61.3 |
99% of the slow proc_registry_publish_self calls overlap the previous
child's close(g->shm_fd), with a median overlap of 12.7 ms out of 13.0 ms;
7% of the fast ones do. g->shm_fd is the child's only reference to the
unlinked APFS clone that fork_snapshot_shm_via_clonefile made for it.
4. The clone mapping is the trigger. Switches in the experiment build,
two runs of 2000 forks each:
| configuration | forks >= 12 ms | slowest / fastest round of 50 |
|---|---|---|
| main | 31% / 44% | 2393 / 368 ms, 2988 / 435 ms |
child leaves close(shm_fd) to process exit |
32% / 26% | 3521 / 417 ms, 1726 / 407 ms |
child maps the clone MAP_PRIVATE and closes it at once |
1.5% / 2.2% | 482 / 375 ms, 480 / 384 ms |
| no clonefile (live shm fd fallback) | 0.2% / 0.3% | 400 / 334 ms, 376 / 338 ms |
Deferring the close does not help, since the clone is still released at
process exit. What helps is not writing into the clone: with MAP_SHARED
(retain_shared, chosen when hdr.shm_is_clone), every guest store in the
child becomes a dirty page of the unlinked clone file.
Things ruled out:
- The P/E core split. With serial forks, every child starts on a P-core.
- PR #526 (the quiesce barrier).
thread_quiesce_siblingstakes 0.01 ms in
both fast and slow forks, and this test creates no CLONE_VM children. - Memory pressure. 67% of memory was free.
- The size of the clone. It stays at 0.3 MiB allocated.
- The clone delete alone. A single-process macOS program that does
clonefile,MAP_SHARED, dirties 4096 pages,hv_vm_map/ destroy and
thenclosedoes not reproduce it:closestays at 0.14 ms. So the stall
needs something more from the real setup, such as two processes with VMs or
the parent's VM writing the source file. What the kernel waits on was not
traced; that needs root.
Reproduce
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/wait.h>
#include <time.h>
#include <unistd.h>
static double now_ms(void)
{
struct timespec t;
clock_gettime(CLOCK_MONOTONIC, &t);
return t.tv_sec * 1e3 + t.tv_nsec / 1e6;
}
int main(int argc, char **argv)
{
if (argc > 1 && strcmp(argv[1], "child") == 0)
return 0;
if (argc > 1 && strcmp(argv[1], "nested") == 0) { /* like sh running sed */
pid_t pid = fork();
if (pid == 0) {
execl("/proc/self/exe", argv[0], "child", (char *) NULL);
_exit(127);
}
waitpid(pid, NULL, 0);
return 0;
}
int rounds = argc > 1 ? atoi(argv[1]) : 60;
const char *mode = argc > 2 ? argv[2] : "child"; /* "nested": two levels */
for (int r = 0; r < rounds; r++) {
int slow = 0;
double t0 = now_ms(), worst = 0;
for (int i = 0; i < 50; i++) { /* like make running 50 rules serially */
double a = now_ms();
pid_t pid = fork();
if (pid == 0) {
execl("/proc/self/exe", argv[0], mode, (char *) NULL);
_exit(127);
}
waitpid(pid, NULL, 0);
double d = now_ms() - a;
slow += d >= 12;
if (d > worst)
worst = d;
}
printf("round %2d: %7.1f ms, %2d/50 forks >= 12 ms, slowest %5.1f ms\n",
r, now_ms() - t0, slow, worst);
fflush(stdout);
}
return 0;
}
aarch64-linux-musl-gcc -O2 -static fork-bimodal.c -o fork-bimodal
build/elfuse ./fork-bimodal 60
The slow stretches usually start after a few hundred forks and come sooner
on later runs. Run it two or three times in a row if the first run stays
fast.
Possible fix
Map the clone MAP_PRIVATE in the fork child even when it is a private clone:
--- a/src/runtime/forkipc.c
+++ b/src/runtime/forkipc.c
@@ -449,8 +449,14 @@ int fork_child_main(int ipc_fd,
close(ipc_fd);
return 1;
}
+ /* Map the snapshot MAP_PRIVATE even when it is a private clone. A
+ * MAP_SHARED mapping turns every guest store into a dirty page of the
+ * unlinked clone, and dropping the last reference to that file at
+ * teardown can stall for tens of milliseconds while blocking other
+ * filesystem calls on the volume (the parent's registry updates).
+ */
if (guest_init_from_shm(&g, shm_fd, hdr.guest_size, hdr.ipa_bits,
- hdr.shm_is_clone != 0) < 0) {
+ false) < 0) {
log_error("fork-child: guest_init_from_shm failed");
close(ipc_fd);
return 1;
Isolation is unchanged. The clone is still an independent snapshot, and the
child's writes go to private anonymous CoW pages instead of the clone file.
The trade-off: retain_shared exists so that a fork child can fclonefileat
its own memory when it forks again. With this change the child holds no shm
fd, so a nested fork (make → sh → sed) takes the region-copy path instead.
Measured with only this diff on top of 1bfceb7, using the reproducer
(child = 50 serial forks per round, nested = each child forks and execs a
grandchild). Two runs each, alternating main and fix:
| mode | build | round median (ms) | fastest / slowest round (ms) | slowest / fastest |
|---|---|---|---|---|
| child, 60 rounds | main | 360 / 352 | 305 / 2183, 269 / 2445 | 7.2x / 9.1x |
| child, 60 rounds | fix | 333 / 322 | 320 / 375, 302 / 391 | 1.2x / 1.3x |
| nested, 30 rounds | main | 1515 / 1585 | 962 / 3019, 1058 / 3862 | 3.1x / 3.7x |
| nested, 30 rounds | fix | 1505 / 1638 | 1454 / 2696, 1370 / 2611 | 1.9x / 1.9x |
For single-level forks, the fix removes the slow mode and lowers the median.
For nested forks, the median is about the same, the worst case drops, and the
best case gets worse (962 → 1454 ms, 1058 → 1370 ms) because grandchildren
now take the region-copy path. Nested forks still vary by 1.9x, so the
region-copy path or something else in nested forks has its own variance; that
was not investigated.
Alternatives, if nested forks need to keep the clone path:
- Keep
MAP_SHARED, but have the child switch to an anonymous copy before it
writes heavily (for example at its first execve, which resets guest memory
anyway). The clone file then stays clean, and the child can create a new shm
for its own children. - Move the parent's per-fork registry updates off the filesystem (shared
memory instead of files in$TMPDIR), so a stalled clone release no longer
delays admission. This hides the stall from fork latency but not from
guests that do their own file I/O at the same time, so it treats the
symptom.
- Lingua principale
- C
- Stelle
- 287
- Fork
- 33
- Merge medio
- 2g 17h
- PR unite (30g)
- 18
Preparare l'ambiente
- Nessun Dockerfile né file Docker Compose
- Nessun modello di pull request
- Leggi la guida per i contributori
Come iniziare
- Leggi tutta la issue e poi la guida ai contributi del progetto.
- Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
- Fai un fork del repository e lavora su un branch.
- Apri una pull request che faccia riferimento al numero della issue.
Altre issue di sysprog21/elfuse
-
Difficoltà 2/5 1-3 ore Idoneità per principianti 64/100
I maintainer di solito rispondono entro 2 giorni
-
Difficoltà 1/5 Meno di un'ora Idoneità per principianti 80/100
I maintainer di solito rispondono entro 2 giorni
-
bug
Difficoltà 2/5 1-3 ore Idoneità per principianti 86/100
I maintainer di solito rispondono entro 2 giorni
-
bug
Difficoltà 2/5 1-3 ore Idoneità per principianti 72/100
I maintainer di solito rispondono entro 2 giorni
-
bug
Difficoltà 2/5 1-3 ore Idoneità per principianti 75/100
I maintainer di solito rispondono entro 2 giorni
Tutte le issue di sysprog21/elfuse
Issue simili
-
Difficoltà 2/5 1-3 ore Idoneità per principianti 70/100
libreswan/libreswan#3185 · 1 commento ·
I maintainer di solito rispondono entro 1 giorno
-
Difficoltà 2/5 1-3 ore Idoneità per principianti 70/100
-
[SDL3pp] update to 0.12.0Apertacategory:port-update
Difficoltà 2/5 1-3 ore Idoneità per principianti 72/100
I maintainer di solito rispondono entro 2 giorni
-
[InstCombine] 23.x folds 0.0 * (0.0 / x) to 0.0 when x may be zero (fixed on main by #202482)Apertallvm:instcombine
Difficoltà 2/5 1-3 ore Idoneità per principianti 64/100
llvm/llvm-project#246503 · 1 commento ·
I maintainer di solito rispondono entro 1 giorno
-
Difficoltà 2/5 1-3 ore Idoneità per principianti 85/100
I maintainer di solito rispondono entro 1 giorno