如何避免 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
How to avoid excessive stat(/etc/localtime) calls in strftime() on linux?
提问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, "4q7strftime(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. strftime
queries /etc/localtime
to find it.
这样做可能是因为您的时区未设置。strftime
查询/etc/localtime
以找到它。
Try setting the TZ
environment variable.
尝试设置TZ
环境变量。
Here's a link for that behavior.
这是该行为的链接。