.
D 2023-06-06T13:09:42.725
J assignee nobody
J closer nobody
J cmimetype text/x-fossil-plain
J comment Management\ssummary:\sThe\susage\sof\sfork()\swithin\sTcl\sbecomes\sa\sproblem,\r\nwhen\sthe\smemory\sfootprint\sof\sthe\sprocess\sbecomes\slarge\sand\svirtual\smachines\r\nare\sused.\sThe\susage\sof\smore\smodern\ssystem\scalls\ssolves\sthis\s(patch\sand\r\nempirical\sdata\sincluded)\r\n\r\nWhen\sTcl\sforks()\son\sa\sUnix\ssystem,\sit\shas\sthe\sconsequence\sthat\sthe\sfull\r\nprocess\sis\sstopped\sfor\sthe\sduration\sof\sthe\sfork()\ssystem\scall.\sUnder\r\nnormal\sconditions,\sthis\sis\snot\sa\sbig\sissue,\sbut\sit\sis\svery\sbad\sin\r\nmultithreaded\sapplications.\sThe\sduration\sof\sthe\sfork()\ssystem\scall\r\ndepends\son\sthe\smemory\sfootprint\sof\sthe\sprocess.\sEven\sworse,\swhen\srunning\r\nin\sa\svirtual\smachine,\sthe\sduration\sof\sthe\sfork()\scall\sgrows\r\nsignificantly\scompared\sto\sa\sbare\smetal.\sIt\swas\sobserved\sthat\sa\sfork()\r\ntakes\sup\sto\se.g.\s6\sseconds\s[1].\sThis\sis\sespecially\sbad\swhen\srunning\sa\r\nmultithreaded\sweb\sserver\s(with,\se.g.,\s60\sconnection\sthreads),\ssince\sall\r\nthreads\sare\sstopped\sduring\sfork().\sWhen\sthe\sserver\shandles\se.g.\s1000\r\npages/second,\sa\ssubstantial\sbacklog\swill\sbe\sthe\sconsequence,\r\nvideo-streams\swill\shang\sshortly,\ssome\smutex\slocks\smight\stake\svery\slong\r\n(without\svisible\sevidence\swhen\slooking\sat\sthe\ssource\scode),\setc.\sThe\r\nservice\squality\sdegrades.\r\n\r\nSo,\sthe\smoral\sof\s[1]\sis\sto\stry\sto\savoid\ssuch\scases.\sIn\sgeneral,\sfork()\r\nis\san\sissue\sfor\s"exec"\sand\spipe-open,\sfor\sNaviServer/OpenACS,\sthis\sis\r\njust\san\sissue\sfor\sstarting\sa\sprocess\sin\sa\spipe,\ssince\sit\shas\sits\sown\r\nexec\simplementation\s(working\swith\sa\spool\sof\sseparate\sworker\sprocesses\r\nconnected\svia\sa\squeue).\sHowever,\swhen\se.g.\stcllib\sis\sused,\scalls\sof\s\r\npipe-open\scan't\sbe\savoided.\r\n\r\nHere\sare\ssome\sfigures\s(script\sis\sbelow)\sfrom\sLinux-based\sservers\r\n(server1,\sserver2,\stest-server)\srunning\sin\sa\svirtual\smachine,\susing\r\npipe-open\svia\sfork().\sThe\sspecified\ssizes\sare\sin\sGB.\sThe\sphysical\smemory\r\nsizes\sof\sall\sservers\sis\s>=128\sGB.\r\n\r\nserver1:\r\n\spipe\sopen\stook\s302ms\s(rss\s20.50\svsize\s22.95)\r\n\r\nserver2:\r\n\spipe\sopen\stook\s819ms\s(rss\s17.10\svsize\s19.62)\r\n\spipe\sopen\stook\s1176ms\s(rss\s21.07\svsize\s21.81)\r\n\r\nOn\sa\stest\smachine,\sone\scan\ssee\snicely\sthe\sdegradation\sof\sthe\r\nperformance,\swhen\sthe\smemory\sfootprint\sgrows:\r\n\r\ntest-server:\r\n\spipe\sopen\stook\s174ms\s(rss\s3.48\svsize\s4.19)\r\n\spipe\sopen\stook\s215ms\s(rss\s4.41\svsize\s5.12)\r\n\spipe\sopen\stook\s254ms\s(rss\s5.34\svsize\s6.06)\r\n\spipe\sopen\stook\s309ms\s(rss\s6.27\svsize\s6.99)\r\n\spipe\sopen\stook\s331ms\s(rss\s7.20\svsize\s7.92)\r\n\spipe\sopen\stook\s401ms\s(rss\s8.13\svsize\s8.85)\r\n\spipe\sopen\stook\s421ms\s(rss\s9.05\svsize\s9.78)\r\n\spipe\sopen\stook\s472ms\s(rss\s9.98\svsize\s10.72)\r\n\spipe\sopen\stook\s546ms\s(rss\s10.91\svsize\s11.65)\r\n\spipe\sopen\stook\s639ms\s(rss\s11.84\svsize\s12.58)\r\n\spipe\sopen\stook\s607ms\s(rss\s12.77\svsize\s13.51)\r\n\spipe\sopen\stook\s721ms\s(rss\s13.69\svsize\s14.45)\r\n\spipe\sopen\stook\s733ms\s(rss\s14.61\svsize\s15.38)\r\n\spipe\sopen\stook\s743ms\s(rss\s15.54\svsize\s16.31)\r\n\spipe\sopen\stook\s781ms\s(rss\s16.47\svsize\s17.24)\r\n\spipe\sopen\stook\s809ms\s(rss\s17.39\svsize\s18.17)\r\n\spipe\sopen\stook\s848ms\s(rss\s18.32\svsize\s19.10)\r\n\spipe\sopen\stook\s878ms\s(rss\s19.25\svsize\s20.03)\r\n\spipe\sopen\stook\s957ms\s(rss\s20.17\svsize\s20.97)\r\n\r\nWhen\sreplacing\sthe\sfork()\ssystem\scall\swith\sposix_spawn(),\sthe\r\nperformance\scan\sbe\ssubstantially\simproved\sin\severy\scase.\sFurthermore,\r\nwith\sposix_spawn(),\sthe\sduration\sis\sindependent\sof\sthe\smemory\sfootprint\r\nof\sthe\sprocess.\r\n\r\ntest-server:\r\n\spipe\sopen\stook\s5ms\s(rss\s2.87\svsize\s3.57)\r\n\spipe\sopen\stook\s6ms\s(rss\s3.68\svsize\s4.50)\r\n\spipe\sopen\stook\s5ms\s(rss\s4.61\svsize\s5.43)\r\n\spipe\sopen\stook\s5ms\s(rss\s5.54\svsize\s6.36)\r\n\spipe\sopen\stook\s4ms\s(rss\s6.47\svsize\s7.30)\r\n\spipe\sopen\stook\s5ms\s(rss\s7.40\svsize\s8.23)\r\n\spipe\sopen\stook\s5ms\s(rss\s8.33\svsize\s9.16)\r\n\spipe\sopen\stook\s5ms\s(rss\s9.26\svsize\s10.09)\r\n\spipe\sopen\stook\s5ms\s(rss\s10.20\svsize\s11.02)\r\n\spipe\sopen\stook\s4ms\s(rss\s11.13\svsize\s11.95)\r\n\spipe\sopen\stook\s6ms\s(rss\s12.06\svsize\s12.88)\r\n\spipe\sopen\stook\s5ms\s(rss\s12.99\svsize\s13.82)\r\n\spipe\sopen\stook\s10ms\s(rss\s13.92\svsize\s14.75)\r\n\spipe\sopen\stook\s6ms\s(rss\s14.85\svsize\s15.69)\r\n\spipe\sopen\stook\s5ms\s(rss\s15.79\svsize\s16.62)\r\n\spipe\sopen\stook\s5ms\s(rss\s16.72\svsize\s17.55)\r\n\spipe\sopen\stook\s5ms\s(rss\s17.65\svsize\s18.48)\r\n\spipe\sopen\stook\s5ms\s(rss\s18.58\svsize\s19.41)\r\n\spipe\sopen\stook\s4ms\s(rss\s19.51\svsize\s20.34)\r\n\r\nOne\scan\ssee,\sthat\sfor\se.g.\sa\sfootprint\sof\s20\sGB,\sthe\snew\sversion\sis\smore\r\nthan\s100\stimes\sfaster...\sbut\smore\simportant,\sthe\slong\slock\slatencies\sof\s\r\nabout\s1\ssecond\sare\sgone.\r\n\r\nThe\spatch\sbelow\sis\sagainst\stcl8.6.13.\sIt\sis\sintended\sto\sbe\sleast\r\ninvasive\sand\shas\sbeen\srunning\sfor\sa\swhile\son\se.g.\sthe\sserver\r\nopenacs.org.\sI\shave\stested\sit\swith\sUbuntu\s20.4,\sFC\s37,\sCentOS\s7\sand\r\nmacOS\s13.4\s(Ventura).\s\r\n\r\nActually,\sa\ssimilar\sbenefit\scould\sbe\sachieved\svia\svfork(),\swhich\sis\r\nintended\sto\sbe\sused\sunder\smacOS\s(check:\s"Darwin"\sconfigure.in\ssets\r\nUSE_VFORK,\sbut\smagically\sthis\sis\sunset\sby\sthe\sinclude\sfiles\sincluded\r\nfrom\stclInt.h\sin\stclUnixPipe.c).\sSo\ssetting\sUSE_VFORK\sfor\srecent\smacOS\sin\s\r\nconfigure.in\sis\suseless.\sWhen\scompiling\stclUnixPipe.c\swith\svfork(),\s\r\nrecent\sversions\sof\smacOS\s(starting\swith\smacOS\sMonterey)\scomplain\sthat\s\r\nvfork()\sis\sdeprecated,\s...\sand\sthe\scompiler\ssuggests\susing\seither\s\r\nposix_spawn()\sor\sfork().\s\r\n\r\nSo,\sposix_spawn()\sis\sthe\sbetter\schoice,\ssince\sit\salso\sworks\sfor\sother\s\r\nUnixes.\s\sWhen\sthis\sis\sto\sbe\sused,\sone\sshould\scheck\svia\sconfigure.in\s\r\nfor\sposix_spawn()\sin\slibc\sand\sclean\sup\stclUnixPipe.c\sa\slittle\smore.\r\n\r\nAll\sthe\sbest\r\n-g\r\n\r\n[1]\shttps://openacs.org/forums/message-view?message_id=5539060\r\n\r\n============================================================================test-pipe-open.tcl\r\nproc\smemsizes\s{pid}\s{\r\n\s\s#\r\n\s\s#\sReturn\sa\sdict\sof\smemory\ssizes\sof\spid\sin\sGB\r\n\s\s#\r\n\s\slassign\s{0\s0}\srss\svsize\r\n\s\sif\s{[file\sreadable\s/proc/$pid/statm]}\s{\r\n\s\s\s\s#\r\n\s\s\s\s#\sresult\sin\spages,\stypically\s4K\r\n\s\s\s\s#\r\n\s\s\s\sset\sF\s[open\s/proc/$pid/statm];\sset\sc\s[read\s$F];\sclose\s$F\r\n\s\s\s\slassign\s$c\svsize\srss\sshared\r\n\s\s\s\sset\srss\s\s\s[format\s%.2f\s[expr\s{$rss\s\s\s\s\s\s\s\s\s\s\s*\s4.0/(1024*1024)}]]\r\n\s\s\s\sset\svsize\s[format\s%.2f\s[expr\s{$vsize\s\s\s\s\s\s\s\s\s*\s4.0/(1024*1024)}]]\r\n\s\s}\r\n\s\sreturn\s[list\srss\s$rss\svsize\s$vsize]\r\n}\r\n\r\nfor\s{set\si\s0}\s{$i\s<\s19}\s{incr\si}\s{\r\n\s\s\sset\st\s[clock\smilliseconds]\r\n\s\s\sset\sf\s[open\s"|cat"\sw];\sputs\s$f\s"hi";\sclose\s$f\r\n\s\s\sputs\sstderr\s"pipe\sopen\stook\s[expr\s{[clock\smilliseconds]\s-\s$t}]ms\s([memsizes\s[pid]])"\s\r\n\s\s\sappend\s_$i\s[string\srepeat\s01234567890123456789012345678901234567890123456789\s20000000]\s;#\s1GB\r\n}\r\n============================================================================\r\n\r\n\r\n============================================================================tclUnixPipe.patch\r\n---\stclUnixPipe.c-orig\s\s2023-06-05\s18:00:14\r\n+++\stclUnixPipe.c\s\s\s\s\s\s\s2023-06-06\s12:04:25\r\n@@\s-13,6\s+13,12\s@@\r\n\s\r\n\s#include\s"tclInt.h"\r\n\s\r\n+#define\sUSE_POSIX_SPAWN\s1\r\n+\r\n+#ifdef\sUSE_POSIX_SPAWN\r\n+#\sinclude\s<spawn.h>\r\n+#endif\r\n+\r\n\s#ifdef\sUSE_VFORK\r\n\s#define\sfork\svfork\r\n\s#endif\r\n@@\s-439,7\s+445,7\s@@\r\n\s\s\s\s\s\s\s\snewArgv[i]\s=\sTcl_UtfToExternalDString(NULL,\sargv[i],\s-1,\s&dsArray[i]);\r\n\s\s\s\s\s}\r\n\s\r\n-#ifdef\sUSE_VFORK\r\n+#if\sdefined(USE_VFORK)\s||\sdefined(USE_POSIX_SPAWN)\r\n\s\s\s\s\s/*\r\n\s\s\s\s\s\s*\sAfter\svfork(),\sdo\snot\scall\scode\sin\sthe\schild\sthat\schanges\sglobal\sstate,\r\n\s\s\s\s\s\s*\sbecause\sit\sis\susing\sthe\sparent's\smemory\sspace\sat\sthat\spoint\sand\swrites\r\n@@\s-458,8\s+464,45\s@@\r\n\s\s\s\s\s\s\s\s\sTcl_GetStdChannel(TCL_STDERR);\r\n\s\s\s\s\s}\r\n\s#endif\r\n+#ifdef\sUSE_POSIX_SPAWN\r\n+\s\s\s\s{\r\n+\s\s\s\s\s\s\s\sposix_spawn_file_actions_t\sactions;\r\n+\s\s\s\s\s\s\s\sposix_spawnattr_t\sattr;\r\n\s\r\n+\s\s\s\s\s\s\s\sposix_spawn_file_actions_init(&actions);\r\n+\s\s\s\s\s\s\s\sposix_spawnattr_init(&attr);\r\n+\s\s\s\s\s\s\s\s\r\n+\s\s\s\s\s\s\s\sposix_spawnattr_setflags(&attr,\sPOSIX_SPAWN_SETSIGDEF|\r\n+#\sifdef\sPOSIX_SPAWN_USEVFORK\s\s\s\s\s\s\s\s\s\s\s\s\r\n+\s\s\s\s\s\s\s\s\s\s\s\s\s\s\s\s\s\s\s\s\s\s\s\s\s\s\s\s\s\s\s\s\sPOSIX_SPAWN_USEVFORK\r\n+#\selse\r\n+\s\s\s\s\s\s\s\s\s\s\s\s\s\s\s\s\s\s\s\s\s\s\s\s\s\s\s\s\s\s\s\s\s0\r\n+#\sendif\r\n+\s\s\s\s\s\s\s\s\s\s\s\s\s\s\s\s\s\s\s\s\s\s\s\s\s\s\s\s\s\s\s\s\s);\r\n+\s\s\s\s\s\s\s\sposix_spawn_file_actions_adddup2(&actions,\sGetFd(inputFile),\s0);\r\n+\s\s\s\s\s\s\s\sposix_spawn_file_actions_adddup2(&actions,\sGetFd(outputFile),\s1);\r\n+\s\s\s\s\s\s\s\sposix_spawn_file_actions_adddup2(&actions,\sGetFd(errorFile),\s2);\r\n+\r\n+\s\s\s\s\s\s\s\sstatus\s=\sposix_spawnp(&pid,\snewArgv[0],\s&actions,\s&attr,\snewArgv,\senviron);\r\n+\r\n+\s\s\s\s\s\s\s\sposix_spawn_file_actions_destroy(&actions);\r\n+\s\s\s\s\s\s\s\sposix_spawnattr_destroy(&attr);\r\n+\r\n+\s\s\s\s\s\s\s\s/*\r\n+\s\s\s\s\s\s\s\s\s*\sFork\ssemantics:\r\n+\s\s\s\s\s\s\s\s\s*\s\s-\spid\s==\s0:\schild\sprocess\r\n+\s\s\s\s\s\s\s\s\s*\s\s-\spid\s==\s-1:\serror\r\n+\s\s\s\s\s\s\s\s\s*\s\s-\spid\s>\s0:\sparent\sprocess\r\n+\s\s\s\s\s\s\s\s\s*\r\n+\s\s\s\s\s\s\s\s\s*\sMimic\sfork\ssemantics\sto\sminimize\schanges\sbelow\r\n+\s\s\s\s\s\s\s\s\s*/\r\n+\s\s\s\s\s\s\s\sif\s(status\s!=\s0)\s{\r\n+\s\s\s\s\s\s\s\s\s\s\s\spid\s=\s-1;\r\n+\s\s\s\s\s\s\s\s}\r\n+\s\s\s\s}\r\n+#else\s\s\r\n\s\s\s\s\spid\s=\sfork();\r\n+#endif\s\s\s\s\r\n\s\s\s\s\sif\s(pid\s==\s0)\s{\r\n\s\s\s\s\s\s\s\ssize_t\slen;\r\n\s\s\s\s\s\s\s\sint\sjoinThisError\s=\serrorFile\s&&\s(errorFile\s==\soutputFile);\r\n============================================================================
J foundin 8.6
J is_private 0
J login anonymous
J priority 5\sMedium
J private_contact ef1993a98c1daa778fe0b246a7af12b3076f2240
J resolution None
J severity Important
J status Open
J submitter anonymous
J subsystem 49.\sThreading
J title High\sTcl\slatencies\swith\sfork()\sin\slarger\ssystems
J type Bug
K c54e4a1aeb999fe8d878eae790243739ef05517a
U anonymous
Z 85502a7f41724622a2fd573780ef0f71