[RFC] Reduce /etc/localtime stat calls when TZ is unset
Yair Lenga
yair.lenga@gmail.com
Sat Nov 7 16:45:27 GMT 2026
Currently, glibc calls 'stat' on '/etc/local/time' for every call to
localtime(), when TZ is unset.
I encountered a problem when a program (caching service/light calculation)
was able to handle >50K requests per second in a development environment,
and was able to handle <10K requests/second in production. Using strace, I
was able to identify the root cause for the performance differences - when
the TZ environment variable is not set, localtime() will run stat on
/etc/localtime on every call, and will reload the file, if it detects a
change to the file modification time.
Researching further, I found that in the development environment, the login
scripts populated a lot of environment variables (including TZ). In the
production environment (Docker) - there were no environment variables set).
I found that - when using other functions (localtime_r) - the timezone
information will be loaded on the first call, and will not get refreshed.
Similarly, when TZ is set to ":/etc/localtime" (or other file like
":/other/file") - the timezone information will be loaded only on the first
call.
I propose a change to the localtime function - instead of checking the
timestamp on every call, it will use a cheap clock check to reduce excess
probing - e.g., like every 60 seconds. While the call to stat is cheap, and
the information is likely to be cached in memory, and not result in actual
IO - it is time consuming. In my code - a server that made a call to the
localtime as part of the logging - the cost of the stat call was 1.5
microsecond per call.
The overall impact for adopting this proposal, for programs that make a lot
of calls to 'localtime':
1. If TZ is set - no impact
2. If TZ is not set, localtime will take approximately 0.1 microsecond
3. In the unlikely case that that /etc/localtime will be touched, the
program will need up to one extra minute to detect the change.
I proposed 1 minutes caching - as I believe it's a reasonable tradeoff. As
much as I know, most timezone files are practically fixed - especially when
running in a container/cloud environment. Update to time zone files is a
very infrequent event - happening when countries are changing the daylight
saving time rules, etc. Simple switch to/from daylight saving time is
already encoded in the timestamp file.
Reproduction is easy: compile the short program below, run with and without
TZ environment variable set.
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
int main(int argc, char **argv)
{
int n = argc > 1 ? atoi(argv[1]): 1000000 ;
for (int i=0 ; i<n ; i++ ) {
time_t now = time(NULL) ;
struct tm *now_tm = localtime(&now) ;
}
}
The fix itself is trivial - probably 10-15 lines in time/tzset.c
Looking for feedback, before submitting a formal ticket.
Yair
Side notes:
1. The following functions make calls to localtime - ctime, timelocal,
mktime
2. If one minutes detection time seems to much - even a 5 seconds TTL will
make significant difference.
3. Because many libraries make calls to localtime - it's not possible to
address for the localtime calls with a wrapping function. In my case, I was
using an external logging library - which I could not modify. Same is true
for any user that is using third-party libraries that call localtime
directly.
I published a short article in Medium (no firewall) - which provides more
details, historical context, and reference to other articles and options
for workaround.
https://medium.com/@yair.lenga/the-hidden-cost-of-time-functions-c01f62f29629
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://sourceware.org/pipermail/libc-alpha/attachments/20261107/967bbf19/attachment.htm>
More information about the Libc-alpha
mailing list