如何避免 linux 上 strftime() 中过多的 stat(/etc/localtime) 调用?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4554271/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-05 02:19:10  来源:igfitidea点击:

How to avoid excessive stat(/etc/localtime) calls in strftime() on linux?

clinux

提问by nos

I left a record processing program of mine running for a few minutes under strace.

我让我的一个记录处理程序在 strace 下运行了几分钟。

This showed in those minutes over 200 000 000 calls to stat("/etc/localtime",..)which sounds a bit excessive and unneeded.

这表明在那几分钟内有超过 200 000 000 个电话,stat("/etc/localtime",..)这听起来有点过分和不必要。

The strace output looks like this:

strace 输出如下所示:

write(1, "C137015 393393093052629137110 47"..., 16384) = 16384
stat("/etc/localtime", {st_mode=S_IFREG|0644, st_size=2225, ...}) = 0
stat("/etc/localtime", {st_mode=S_IFREG|0644, st_size=2225, ...}) = 0
stat("/etc/localtime", {st_mode=S_IFREG|0644, st_size=2225, ...}) = 0
stat("/etc/localtime", {st_mode=S_IFREG|0644, st_size=2225, ...}) = 0
stat("/etc/localtime", {st_mode=S_IFREG|0644, st_size=2225, ...}) = 0
stat("/etc/localtime", {st_mode=S_IFREG|0644, st_size=2225, ...}) = 0
stat("/etc/localtime", {st_mode=S_IFREG|0644, st_size=2225, ...}) = 0
stat("/etc/localtime", {st_mode=S_IFREG|0644, st_size=2225, ...}) = 0
stat("/etc/localtime", {st_mode=S_IFREG|0644, st_size=2225, ...}) = 0
stat("/etc/localtime", {st_mode=S_IFREG|0644, st_size=2225, ...}) = 0
stat("/etc/localtime", {st_mode=S_IFREG|0644, st_size=2225, ...}) = 0
stat("/etc/localtime", {st_mode=S_IFREG|0644, st_size=2225, ...}) = 0
stat("/etc/localtime", {st_mode=S_IFREG|0644, st_size=2225, ...}) = 0
stat("/etc/localtime", {st_mode=S_IFREG|0644, st_size=2225, ...}) = 0
stat("/etc/localtime", {st_mode=S_IFREG|0644, st_size=2225, ...}) = 0
stat("/etc/localtime", {st_mode=S_IFREG|0644, st_size=2225, ...}) = 0
stat("/etc/localtime", {st_mode=S_IFREG|0644, st_size=2225, ...}) = 0
stat("/etc/localtime", {st_mode=S_IFREG|0644, st_size=2225, ...}) = 0
stat("/etc/localtime", {st_mode=S_IFREG|0644, st_size=2225, ...}) = 0
stat("/etc/localtime", {st_mode=S_IFREG|0644, st_size=2225, ...}) = 0
stat("/etc/localtime", {st_mode=S_IFREG|0644, st_size=2225, ...}) = 0
stat("/etc/localtime", {st_mode=S_IFREG|0644, st_size=2225, ...}) = 0
stat("/etc/localtime", {st_mode=S_IFREG|0644, st_size=2225, ...}) = 0
stat("/etc/localtime", {st_mode=S_IFREG|0644, st_size=2225, ...}) = 0
stat("/etc/localtime", {st_mode=S_IFREG|0644, st_size=2225, ...}) = 0
stat("/etc/localtime", {st_mode=S_IFREG|0644, st_size=2225, ...}) = 0
stat("/etc/localtime", {st_mode=S_IFREG|0644, st_size=2225, ...}) = 0
read(0, "4q7
strftime(call->date_time,DATELEN,"%Y%m%d %H%M%S",&tm_buf);
##代码##234567123"..., 16384) = 16384

Essentially it turned out to be 1 stat() call for every record processed and the culprit turned out to be this quite ordinary line of code

本质上,结果是对处理的每条记录进行 1 次 stat() 调用,而罪魁祸首是这行非常普通的代码

##代码##

So - how can I avoid strftime() calling stat(/etc/localtime) at every call?

那么 - 如何避免 strftime() 在每次调用时调用 stat(/etc/localtime) ?

采纳答案by Linus Kleen

It might be doing that because your timezone isn't set. strftimequeries /etc/localtimeto find it.

这样做可能是因为您的时区未设置。strftime查询/etc/localtime以找到它。

Try setting the TZenvironment variable.

尝试设置TZ环境变量。

Here's a link for that behavior.

这是该行为的链接