]> granicus.if.org Git - procps-ng/commitdiff
Set TZ to avoid repeated stat("/etc/localtime")
authorStephen Brennan <stephen.s.brennan@oracle.com>
Fri, 20 Nov 2020 00:03:58 +0000 (16:03 -0800)
committerCraig Small <csmall@dropbear.xyz>
Tue, 22 Dec 2020 00:09:44 +0000 (11:09 +1100)
With glibc, each time the strftime() function is used (twice per process
in a typical ps -fe run), a stat("/etc/localtime") system call is used
to determine the timezone. Not only does this add extra system call
overhead, but when multiple ps processes are trying to access this
file (or multiple glibc programs using strftime) in parallel, this can
trigger significant lock contention within the OS kernel.

Since ps is not intended to run for long periods of time as a
daemon (during which the system timezone could be altered and PS might
reasonably be expected to adapt its output), there is no benefit to
repeatedly doing this stat(). To stop this behavior, explicitly set the
TZ variable to its default value (:/etc/localtime) whenever it is unset.
glibc will then cache the stat() result.

ps/display.c

index bec232313e4d57a4c1fca0394490513065c4c496..1fddbbe1b7f75754c7b9960717f264b00fbf205a 100644 (file)
@@ -614,6 +614,7 @@ int main(int argc, char *argv[]){
   setlocale (LC_ALL, "");
   bindtextdomain(PACKAGE, LOCALEDIR);
   textdomain(PACKAGE);
+  setenv("TZ", ":/etc/localtime", 0);
 
 #ifdef DEBUG
   init_stack_trace(argv[0]);