cosmopolitan/libc/proc
Justine Tunney cdfcee51ca
Properly serialize fork() operations
This change solves an issue where many threads attempting to spawn forks
at once would cause fork() performance to degrade with the thread count.
Things got real nasty on NetBSD, which slowed down the whole test fleet,
because there's no vfork() and we're forced to use fork() in our server.

   threads      count task
         1       1062 fork+exit+wait
         2        668 fork+exit+wait
         4         66 fork+exit+wait
         8         19 fork+exit+wait
        16         22 fork+exit+wait
        32         16 fork+exit+wait

Things are now much less bad on NetBSD, but not great, since it does not
have futexes; we rely on its semaphore file descriptors to do conditions

   threads      count task
         1       1085 fork+exit+wait
         2        842 fork+exit+wait
         4        532 fork+exit+wait
         8        400 fork+exit+wait
        16        276 fork+exit+wait
        32         66 fork+exit+wait

With OpenBSD which also lacks vfork(), things were just as bad as NetBSD

   threads      count task
         1        584 fork+exit+wait
         2        687 fork+exit+wait
         4        206 fork+exit+wait
         8         24 fork+exit+wait
        16         33 fork+exit+wait
        32         26 fork+exit+wait

But since OpenBSD has futexes fork() works terrifically thanks to *NSYNC

   threads      count task
         1        525 fork+exit+wait
         2        580 fork+exit+wait
         4        451 fork+exit+wait
         8        479 fork+exit+wait
        16        408 fork+exit+wait
        32        373 fork+exit+wait

This issue would most likely only manifest itself, when pthread_atfork()
callers manage to slip a spin lock into the outermost position of fork's
list of locks. Since fork() is very slow, a spin lock can be devastating

Needless to say vfork() rules and anyone who says differently is kidding
themselves. Look at what a FreeBSD 14.1 virtual machine with equal specs
can do over the course of three hundred milliseconds.

   threads      count task
         1       2559 vfork+exit+wait
         2       5389 vfork+exit+wait
         4      34933 vfork+exit+wait
         8      43273 vfork+exit+wait
        16      49648 vfork+exit+wait
        32      40247 vfork+exit+wait

So it's a shame that so few OSes support vfork(). It creates an unsavory
situation, where someone wanting to build a server that spawns processes
would be better served to not use threads and favor a multiprocess model
2024-07-27 08:23:44 -07:00
..
BUILD.mk Reduce code size of mandatory runtime 2024-07-04 02:50:20 -07:00
clock.c Implement proper time zone support 2024-05-04 23:06:37 -07:00
cocmd.c Fix ctype.h and wctype.h 2024-07-21 15:54:17 -07:00
daemon.c flip et / noet in modelines 2023-12-07 22:17:11 -05:00
describefds.c Get rid of .internal.h convention in LIBC_INTRIN 2024-07-19 19:38:00 -07:00
describefds.internal.h Get rid of .internal.h convention in LIBC_INTRIN 2024-07-19 19:38:00 -07:00
execl.c Apply clang-format update to repo (#1154) 2024-04-25 10:38:00 -07:00
execle.c Apply clang-format update to repo (#1154) 2024-04-25 10:38:00 -07:00
execlp.c Apply clang-format update to repo (#1154) 2024-04-25 10:38:00 -07:00
execv.c flip et / noet in modelines 2023-12-07 22:17:11 -05:00
execve-nt.greg.c Improve lock hierarchy 2024-07-24 16:05:48 -07:00
execve-sysv.c Get rid of .internal.h convention in LIBC_INTRIN 2024-07-19 19:38:00 -07:00
execve.c Get rid of .internal.h convention in LIBC_INTRIN 2024-07-19 19:38:00 -07:00
execve.internal.h Add dontthrow attribute to most libc functions 2024-01-09 01:26:03 -08:00
execvp.c flip et / noet in modelines 2023-12-07 22:17:11 -05:00
execvpe.c Delete ASAN 2024-06-22 05:45:49 -07:00
fexecve.c Get rid of .internal.h convention in LIBC_INTRIN 2024-07-19 19:38:00 -07:00
fork-nt.c Improve lock hierarchy 2024-07-24 16:05:48 -07:00
fork-sysv.c Introduce FreeBSD ARM64 support 2023-12-29 20:14:02 -08:00
fork.c Properly serialize fork() operations 2024-07-27 08:23:44 -07:00
getpriority-nt.c Get rid of .internal.h convention in LIBC_INTRIN 2024-07-19 19:38:00 -07:00
getpriority.c Get rid of .internal.h convention in LIBC_INTRIN 2024-07-19 19:38:00 -07:00
getrusage-nt.c flip et / noet in modelines 2023-12-07 22:17:11 -05:00
getrusage-sysv.c flip et / noet in modelines 2023-12-07 22:17:11 -05:00
getrusage.c Get rid of .internal.h convention in LIBC_INTRIN 2024-07-19 19:38:00 -07:00
handle.c flip et / noet in modelines 2023-12-07 22:17:11 -05:00
kill-nt.c Improve lock hierarchy 2024-07-24 16:05:48 -07:00
kill.c Get rid of .internal.h convention in LIBC_INTRIN 2024-07-19 19:38:00 -07:00
killpg.c Apply clang-format update to repo (#1154) 2024-04-25 10:38:00 -07:00
nice.c Apply clang-format update to repo (#1154) 2024-04-25 10:38:00 -07:00
ntspawn.h Support shebang on Windows 2024-05-20 22:11:42 -07:00
posix_spawn.c Introduce cosmocc flags -mdbg -mtiny -moptlinux 2024-07-26 05:10:25 -07:00
posix_spawn.h Add dontthrow attribute to most libc functions 2024-01-09 01:26:03 -08:00
posix_spawn.internal.h Reduce header complexity 2023-11-28 14:39:42 -08:00
posix_spawn_add_file_action.c Apply clang-format update to repo (#1154) 2024-04-25 10:38:00 -07:00
posix_spawn_file_actions_addchdir_np.c Apply clang-format update to repo (#1154) 2024-04-25 10:38:00 -07:00
posix_spawn_file_actions_addclose.c Apply clang-format update to repo (#1154) 2024-04-25 10:38:00 -07:00
posix_spawn_file_actions_adddup2.c Apply clang-format update to repo (#1154) 2024-04-25 10:38:00 -07:00
posix_spawn_file_actions_addfchdir_np.c Apply clang-format update to repo (#1154) 2024-04-25 10:38:00 -07:00
posix_spawn_file_actions_addopen.c Apply clang-format update to repo (#1154) 2024-04-25 10:38:00 -07:00
posix_spawn_file_actions_destroy.c flip et / noet in modelines 2023-12-07 22:17:11 -05:00
posix_spawn_file_actions_init.c flip et / noet in modelines 2023-12-07 22:17:11 -05:00
posix_spawnattr_destroy.c flip et / noet in modelines 2023-12-07 22:17:11 -05:00
posix_spawnattr_getflags.c flip et / noet in modelines 2023-12-07 22:17:11 -05:00
posix_spawnattr_getpgroup.c flip et / noet in modelines 2023-12-07 22:17:11 -05:00
posix_spawnattr_getrlimit.c flip et / noet in modelines 2023-12-07 22:17:11 -05:00
posix_spawnattr_getschedparam.c flip et / noet in modelines 2023-12-07 22:17:11 -05:00
posix_spawnattr_getschedpolicy.c flip et / noet in modelines 2023-12-07 22:17:11 -05:00
posix_spawnattr_getsigdefault.c flip et / noet in modelines 2023-12-07 22:17:11 -05:00
posix_spawnattr_getsigmask.c flip et / noet in modelines 2023-12-07 22:17:11 -05:00
posix_spawnattr_init.c flip et / noet in modelines 2023-12-07 22:17:11 -05:00
posix_spawnattr_setflags.c flip et / noet in modelines 2023-12-07 22:17:11 -05:00
posix_spawnattr_setpgroup.c flip et / noet in modelines 2023-12-07 22:17:11 -05:00
posix_spawnattr_setrlimit.c flip et / noet in modelines 2023-12-07 22:17:11 -05:00
posix_spawnattr_setschedparam.c flip et / noet in modelines 2023-12-07 22:17:11 -05:00
posix_spawnattr_setschedpolicy.c flip et / noet in modelines 2023-12-07 22:17:11 -05:00
posix_spawnattr_setsigdefault.c flip et / noet in modelines 2023-12-07 22:17:11 -05:00
posix_spawnattr_setsigmask.c flip et / noet in modelines 2023-12-07 22:17:11 -05:00
posix_spawnp.c Apply clang-format update to repo (#1154) 2024-04-25 10:38:00 -07:00
proc.c Improve lock hierarchy 2024-07-24 16:05:48 -07:00
proc.internal.h Improve lock hierarchy 2024-07-24 16:05:48 -07:00
sched_getaffinity.c Get rid of .internal.h convention in LIBC_INTRIN 2024-07-19 19:38:00 -07:00
sched_setaffinity.c Get rid of .internal.h convention in LIBC_INTRIN 2024-07-19 19:38:00 -07:00
setpriority-nt.c Get rid of .internal.h convention in LIBC_INTRIN 2024-07-19 19:38:00 -07:00
setpriority.c Get rid of .internal.h convention in LIBC_INTRIN 2024-07-19 19:38:00 -07:00
system.c Apply clang-format update to repo (#1154) 2024-04-25 10:38:00 -07:00
systemvpe.c flip et / noet in modelines 2023-12-07 22:17:11 -05:00
times.c Implement proper time zone support 2024-05-04 23:06:37 -07:00
verynice.c flip et / noet in modelines 2023-12-07 22:17:11 -05:00
vfork.S Get rid of .internal.h convention in LIBC_INTRIN 2024-07-19 19:38:00 -07:00
wait.c flip et / noet in modelines 2023-12-07 22:17:11 -05:00
wait3.c flip et / noet in modelines 2023-12-07 22:17:11 -05:00
wait4-nt.c Get rid of .internal.h convention in LIBC_INTRIN 2024-07-19 19:38:00 -07:00
wait4-sysv.c flip et / noet in modelines 2023-12-07 22:17:11 -05:00
wait4.c Get rid of .internal.h convention in LIBC_INTRIN 2024-07-19 19:38:00 -07:00
waitpid.c flip et / noet in modelines 2023-12-07 22:17:11 -05:00