Live data from Hacker News

How setting the TZ environment variable avoids thousands of system calls

blog.packagecloud.io

11–20 of 148 posts

Re: How setting the TZ environment variable avoids thousands of system calls

#11
post #6

does anyone know if this impacts docker images as well ?

Yes, at least the Docker images that use glibc as their libc. (eg, most Debian/Ubuntu images) It looks like musl, which is used on Alpine Linux images for example, will only read it once, and then cache it: https://github.com/esmil/musl/blob/master/src/time/__tz.c#L1... It has a mutex/lock around the use of the TZ info, but avoids re-stat'ing the localtime file.

This is the best part of HN. Not only did you answer GP's question in 6 minutes, but you link to the exact line of the source code.

Re: How setting the TZ environment variable avoids thousands of system calls

#12
post #7

Side note - why do some sites completely hide information about who is behind them? I couldn't find a single thing about that on their blog or main site.

Maybe it's just that I use packagecloud and follow people in their circle on Twitter, but Joe Damato is the CEO and founder: https://twitter.com/joedamato I'm under the impression that he may also write a lot these himself? Not entirely certain, though.

Pretty sure he writes all of the linux internals posts. He also has a bunch of great ones on his blog http://timetobleed.com/

Re: How setting the TZ environment variable avoids thousands of system calls

#13

If this has a real-world/measurable/etc. impact why isn't this set by default? Are there potential side-effects? Is it set in some distros but not others?

Probably because while there may be tens of thousands of additional syscalls, the total amount of added latency and resources consumed are more likely to be on a scale of micro/nano/milli seconds.

Re: How setting the TZ environment variable avoids thousands of system calls

#14

If this has a real-world/measurable/etc. impact why isn't this set by default? Are there potential side-effects? Is it set in some distros but not others?

Probably because while there may be tens of thousands of additional syscalls, the total amount of added latency and resources consumed are more likely to be on a scale of micro/nano/milli seconds.

In the trace you can see that the syscall takes less than a tenth of a millisecond. I don't think this is a big penalty to check if I have changed my timezone or not, as unlikely as that is during normal operation.

Re: How setting the TZ environment variable avoids thousands of system calls

#18
System calls in Linux are really fast. So saving "thousands" of system calls when /etc/localtime is in cache doesn't actually save that much actual CPU time.

I ran an experiment where I timed the runtime of the sample program provided in the OP, except I changed the number of calls to localtime() from ten times to a million. I then timed the difference with and without export TZ=:/etc/localhost. The net savings was .6 seconds. So for a single call to localtime(3), the net savings is 0.6 microseconds.

That's non-zero, but it's likely in the noise compared to everything else that your program might be doing.

Re: How setting the TZ environment variable avoids thousands of system calls

#19

Is there a reason why the path to the timezone file is prefixed with a colon? TZ=:/etc/localtime I've set TZ sometimes without the colon and it seem to work. I did a quick online search and didn't find anything relevant.

: means "read it from the " file. See the last part of the relevant glibc documentation: https://www.gnu.org/savannah-checkouts/gnu/libc/manual/html_...

However the reason it works without : is that the implementation is being lazy and just ignores the : delimiter and falls back to parsing out a filename either way:

https://sourceware.org/git/?p=glibc.git;a=blob;f=time/tzset....

Re: How setting the TZ environment variable avoids thousands of system calls

#20
post #7

Side note - why do some sites completely hide information about who is behind them? I couldn't find a single thing about that on their blog or main site.

It's not in any way hidden, just run a whois query:

    $ whois packagecloud.io|grep Owner
    Owner Name    : JOSEPH DAMATO
    Owner OrgName : COMPUTOLOGY, LLC
    Owner Addr    : 359 FILLMORE ST!12
    Owner Addr    : SAN FRANCISCO
    Owner Addr    : CA
    Owner Addr    : US
Post reply on HN