Overview
| Artifact ID: | 82d6b8be2ff80a717f9cb0c31b6f45d0b05126bda582dcd31bcdd70679491c51 |
|---|---|
| Ticket: | c54e4a1aeb999fe8d878eae790243739ef05517a
High Tcl latencies with fork() in larger systems |
| User & Date: | anonymous on 2023-06-06 13:09:42 |
Changes
- assignee changed to: "nobody"
- closer changed to: "nobody"
- cmimetype changed to: "text/x-fossil-plain"
- comment changed to:
Management summary: The usage of fork() within Tcl becomes a problem, when the memory footprint of the process becomes large and virtual machines are used. The usage of more modern system calls solves this (patch and empirical data included) When Tcl forks() on a Unix system, it has the consequence that the full process is stopped for the duration of the fork() system call. Under normal conditions, this is not a big issue, but it is very bad in multithreaded applications. The duration of the fork() system call depends on the memory footprint of the process. Even worse, when running in a virtual machine, the duration of the fork() call grows significantly compared to a bare metal. It was observed that a fork() takes up to e.g. 6 seconds [1]. This is especially bad when running a multithreaded web server (with, e.g., 60 connection threads), since all threads are stopped during fork(). When the server handles e.g. 1000 pages/second, a substantial backlog will be the consequence, video-streams will hang shortly, some mutex locks might take very long (without visible evidence when looking at the source code), etc. The service quality degrades. So, the moral of [1] is to try to avoid such cases. In general, fork() is an issue for "exec" and pipe-open, for NaviServer/OpenACS, this is just an issue for starting a process in a pipe, since it has its own exec implementation (working with a pool of separate worker processes connected via a queue). However, when e.g. tcllib is used, calls of pipe-open can't be avoided. Here are some figures (script is below) from Linux-based servers (server1, server2, test-server) running in a virtual machine, using pipe-open via fork(). The specified sizes are in GB. The physical memory sizes of all servers is >=128 GB. server1: pipe open took 302ms (rss 20.50 vsize 22.95) server2: pipe open took 819ms (rss 17.10 vsize 19.62) pipe open took 1176ms (rss 21.07 vsize 21.81) On a test machine, one can see nicely the degradation of the performance, when the memory footprint grows: test-server: pipe open took 174ms (rss 3.48 vsize 4.19) pipe open took 215ms (rss 4.41 vsize 5.12) pipe open took 254ms (rss 5.34 vsize 6.06) pipe open took 309ms (rss 6.27 vsize 6.99) pipe open took 331ms (rss 7.20 vsize 7.92) pipe open took 401ms (rss 8.13 vsize 8.85) pipe open took 421ms (rss 9.05 vsize 9.78) pipe open took 472ms (rss 9.98 vsize 10.72) pipe open took 546ms (rss 10.91 vsize 11.65) pipe open took 639ms (rss 11.84 vsize 12.58) pipe open took 607ms (rss 12.77 vsize 13.51) pipe open took 721ms (rss 13.69 vsize 14.45) pipe open took 733ms (rss 14.61 vsize 15.38) pipe open took 743ms (rss 15.54 vsize 16.31) pipe open took 781ms (rss 16.47 vsize 17.24) pipe open took 809ms (rss 17.39 vsize 18.17) pipe open took 848ms (rss 18.32 vsize 19.10) pipe open took 878ms (rss 19.25 vsize 20.03) pipe open took 957ms (rss 20.17 vsize 20.97) When replacing the fork() system call with posix_spawn(), the performance can be substantially improved in every case. Furthermore, with posix_spawn(), the duration is independent of the memory footprint of the process. test-server: pipe open took 5ms (rss 2.87 vsize 3.57) pipe open took 6ms (rss 3.68 vsize 4.50) pipe open took 5ms (rss 4.61 vsize 5.43) pipe open took 5ms (rss 5.54 vsize 6.36) pipe open took 4ms (rss 6.47 vsize 7.30) pipe open took 5ms (rss 7.40 vsize 8.23) pipe open took 5ms (rss 8.33 vsize 9.16) pipe open took 5ms (rss 9.26 vsize 10.09) pipe open took 5ms (rss 10.20 vsize 11.02) pipe open took 4ms (rss 11.13 vsize 11.95) pipe open took 6ms (rss 12.06 vsize 12.88) pipe open took 5ms (rss 12.99 vsize 13.82) pipe open took 10ms (rss 13.92 vsize 14.75) pipe open took 6ms (rss 14.85 vsize 15.69) pipe open took 5ms (rss 15.79 vsize 16.62) pipe open took 5ms (rss 16.72 vsize 17.55) pipe open took 5ms (rss 17.65 vsize 18.48) pipe open took 5ms (rss 18.58 vsize 19.41) pipe open took 4ms (rss 19.51 vsize 20.34) One can see, that for e.g. a footprint of 20 GB, the new version is more than 100 times faster... but more important, the long lock latencies of about 1 second are gone. The patch below is against tcl8.6.13. It is intended to be least invasive and has been running for a while on e.g. the server openacs.org. I have tested it with Ubuntu 20.4, FC 37, CentOS 7 and macOS 13.4 (Ventura). Actually, a similar benefit could be achieved via vfork(), which is intended to be used under macOS (check: "Darwin" configure.in sets USE_VFORK, but magically this is unset by the include files included from tclInt.h in tclUnixPipe.c). So setting USE_VFORK for recent macOS in configure.in is useless. When compiling tclUnixPipe.c with vfork(), recent versions of macOS (starting with macOS Monterey) complain that vfork() is deprecated, ... and the compiler suggests using either posix_spawn() or fork(). So, posix_spawn() is the better choice, since it also works for other Unixes. When this is to be used, one should check via configure.in for posix_spawn() in libc and clean up tclUnixPipe.c a little more. All the best -g [1] https://openacs.org/forums/message-view?message_id=5539060 ============================================================================test-pipe-open.tcl proc memsizes {pid} { # # Return a dict of memory sizes of pid in GB # lassign {0 0} rss vsize if {[file readable /proc/$pid/statm]} { # # result in pages, typically 4K # set F [open /proc/$pid/statm]; set c [read $F]; close $F lassign $c vsize rss shared set rss [format %.2f [expr {$rss * 4.0/(1024*1024)}]] set vsize [format %.2f [expr {$vsize * 4.0/(1024*1024)}]] } return [list rss $rss vsize $vsize] } for {set i 0} {$i < 19} {incr i} { set t [clock milliseconds] set f [open "|cat" w]; puts $f "hi"; close $f puts stderr "pipe open took [expr {[clock milliseconds] - $t}]ms ([memsizes [pid]])" append _$i [string repeat 01234567890123456789012345678901234567890123456789 20000000] ;# 1GB } ============================================================================ ============================================================================tclUnixPipe.patch --- tclUnixPipe.c-orig 2023-06-05 18:00:14 +++ tclUnixPipe.c 2023-06-06 12:04:25 @@ -13,6 +13,12 @@ #include "tclInt.h" +#define USE_POSIX_SPAWN 1 + +#ifdef USE_POSIX_SPAWN +# include <spawn.h> +#endif + #ifdef USE_VFORK #define fork vfork #endif @@ -439,7 +445,7 @@ newArgv[i] = Tcl_UtfToExternalDString(NULL, argv[i], -1, &dsArray[i]); } -#ifdef USE_VFORK +#if defined(USE_VFORK) || defined(USE_POSIX_SPAWN) /* * After vfork(), do not call code in the child that changes global state, * because it is using the parent's memory space at that point and writes @@ -458,8 +464,45 @@ Tcl_GetStdChannel(TCL_STDERR); } #endif +#ifdef USE_POSIX_SPAWN + { + posix_spawn_file_actions_t actions; + posix_spawnattr_t attr; + posix_spawn_file_actions_init(&actions); + posix_spawnattr_init(&attr); + + posix_spawnattr_setflags(&attr, POSIX_SPAWN_SETSIGDEF| +# ifdef POSIX_SPAWN_USEVFORK + POSIX_SPAWN_USEVFORK +# else + 0 +# endif + ); + posix_spawn_file_actions_adddup2(&actions, GetFd(inputFile), 0); + posix_spawn_file_actions_adddup2(&actions, GetFd(outputFile), 1); + posix_spawn_file_actions_adddup2(&actions, GetFd(errorFile), 2); + + status = posix_spawnp(&pid, newArgv[0], &actions, &attr, newArgv, environ); + + posix_spawn_file_actions_destroy(&actions); + posix_spawnattr_destroy(&attr); + + /* + * Fork semantics: + * - pid == 0: child process + * - pid == -1: error + * - pid > 0: parent process + * + * Mimic fork semantics to minimize changes below + */ + if (status != 0) { + pid = -1; + } + } +#else pid = fork(); +#endif if (pid == 0) { size_t len; int joinThisError = errorFile && (errorFile == outputFile); ============================================================================ - foundin changed to: "8.6"
- is_private changed to: "0"
- login: "anonymous"
- priority changed to: "5 Medium"
- private_contact changed to: "ef1993a98c1daa778fe0b246a7af12b3076f2240"
- resolution changed to: "None"
- severity changed to: "Important"
- status changed to: "Open"
- submitter changed to: "anonymous"
- subsystem changed to: "49. Threading"
- title changed to: "High Tcl latencies with fork() in larger systems"
- type changed to: "Bug"