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

nos*_*nos 22 c linux

我离开了我的一个记录处理程序,在strace下运行了几分钟.

这表明在那几分钟内超过2亿次呼叫stat("/etc/localtime",..)听起来有点过分且不需要.

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, "\224q\1\207\0\0\202\1\4\203\1\4\204\1\1\205\1\1\206\1\7\207\1\6\211\1\22\212\1\22\213\1"..., 16384) = 16384
Run Code Online (Sandbox Code Playgroud)

从本质上讲,每次处理的记录都是1 stat()调用,罪魁祸首证明是这个非常普通的代码行

strftime(call->date_time,DATELEN,"%Y%m%d %H%M%S",&tm_buf);
Run Code Online (Sandbox Code Playgroud)

那么 - 我怎样才能避免strftime()在每次调用时调用stat(/ etc/localtime)?

Lin*_*een 22

它可能是这样做的,因为你的时区没有设置.strftime查询/etc/localtime以找到它.

尝试设置TZ环境变量.

这是该行为的链接.

  • @awang显然不是.如果设置了TZ变量,glibc将使用TZ变量,它将从匹配TZ变量中设置的文件读取时区(或直到调用tzset()) - 在需要该程序的程序中的第一次调用时信息.如果未设置TZ env变量,glibc将每次查看/ etc/localtime文件(或者更确切地说,它将检查自上次读取以来/ etc/localtime是否已更改 - 这是stat()打电话吗) (3认同)