【问题标题】:How to avoid excessive stat(/etc/localtime) calls in strftime() on linux?如何避免在 linux 上的 strftime() 中调用过多的 stat(/etc/localtime)?
【发布时间】:2022-03-03 15:31:13
【问题描述】:

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

这表明在那几分钟内有超过 200 000 000 次调用 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

实际上,每条处理的记录都需要 1 次 stat() 调用,而罪魁祸首竟然是这行非常普通的代码

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

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

【问题讨论】:

  • 您是否有任何证据表明这实际上导致了可衡量的问题(即分析器测量的显着运行时成本)?您不应该尝试根据 感觉 进行优化(例如“听起来有点过分”)。获取测量结果并使用事实。
  • +1 询问有关深入了解 glibc 膨胀的问题。 :-)
  • 在我的 shell 脚本中添加 export TZ=:/etc/localtime 为我消除了重复的系统调用。
  • 虽然这个问题很老: AndrewMedico:是的,当然。这被测量为具有大约 10% 的性能影响。 @R .. 似乎这也是必需的,例如捕捉时区变化,并且 posix 要求某些函数的行为就像调用 tzset() 一样
  • @nos:tzset 需要响应对 TZ 变量的更改。变量没有变化时,不需要检查磁盘上的文件是否发生变化。

标签: c linux


【解决方案1】:

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

尝试设置TZ 环境变量。

这里是a link for that behavior。

【讨论】:

  • setting TZ=":/etc/localtime" 确实“修复”了这个问题。但是该页面还指出“您通常不需要设置 TZ”
  • @nos 在 GNU C 库中,默认时区类似于规范 'TZ=:/etc/localtime' [...]。这是重复的stat 调用的来源。
  • @awang 显然不是。如果设置了 TZ 变量,glibc 将使用 TZ 变量,并且它将从与 TZ 变量中设置的内容匹配的文件中读取时区一次(或直到调用 tzset() )——在需要的程序中的第一次调用时信息 。如果未设置 TZ 环境变量,glibc 将每次查看 /etc/localtime 文件(或者更确切地说,它会检查 /etc/localtime 自上次读取以来是否已更改 - 这就是 stat()电话确实)
  • 这种情况在六年里没有改变,尽管最近有一些公开的调查证实了这一点:blog.packagecloud.io/eng/2017/02/21/…我不知道目前有任何发行版设置了 TZ。
  • 对于那些感兴趣的人,我刚刚在我正在编写的 C 程序的开头尝试了setenv("TZ", ":/etc/localtime", 0);,它似乎可以工作。欢迎评论;我隐约记得关于从程序内部为了exec() 以外的目的戳环境的手波概念是bad,但这似乎有效,现在我不需要包装脚本了二进制。
【解决方案2】:

使用 GNU libc 库,当 /etc/localtime 未配置时,这里有另一个提示来解决这个问题(在微型嵌入式系统上经常出现这种情况)。这包括将TZ 环境变量设置为空字符串。这使库默认为 UTC。

这是一个示例 strace 输出在某些嵌入式系统上调用日志记录函数,当 /etc/localtime 和 TZ 均未设置时,该函数调用 localtime() 和 strftime()。在 glibc 2.23 中,这会触发对“openat(/etc/localtime)”的多次失败调用:

[...]
[pid 200113] gettid() = 200113
[pid 200113] openat(AT_FDCWD, "/etc/localtime", O_RDONLY|O_CLOEXEC) = -1 ENOENT (No such file or directory)
[pid 200113] openat(AT_FDCWD, "/etc/localtime", O_RDONLY|O_CLOEXEC) = -1 ENOENT (No such file or directory)
[pid 200113] gettid() = 200113
[pid 200113] openat(AT_FDCWD, "/etc/localtime", O_RDONLY|O_CLOEXEC) = -1 ENOENT (No such file or directory)
[pid 200113] openat(AT_FDCWD, "/etc/localtime", O_RDONLY|O_CLOEXEC) = -1 ENOENT (No such file or directory)
[pid 200113] getpid() = 200113
[pid 200113] writev(4, [{iov_base="DUH\1\1\0\0\0", iov_len=8}, {iov_base="=\2\0\261ECU1\0\3\r\261\r\32\265\3431\2SWMCSWMC", iov_len=26}, {iov_base="\0\2\0\0'\0Tue Mar 1 13:49:20 2022 _"..., iov_len=151}], 3) = 185
[pid 200113] gettid() = 200113
[pid 200113] openat(AT_FDCWD, "/etc/localtime", O_RDONLY|O_CLOEXEC) = -1 ENOENT (No such file or directory)
[pid 200113] openat(AT_FDCWD, "/etc/localtime", O_RDONLY|O_CLOEXEC) = -1 ENOENT (No such file or directory)
[pid 200113] gettid() = 200113
[...]

当TZ 设置为空字符串时,这是与strace 相同的程序输出:

$ export TZ=
[...]
[pid 201837] gettid() = 201837
[pid 201837] gettid() = 201837
[pid 201837] getpid() = 201837
[pid 201837] writev(4, [{iov_base="DUH\1\1\0\0\0", iov_len=8}, {iov_base="=\2\0\261ECU1\0\3\24m\r7\253\3171\2SWMCSWMC", iov_len=26}, {iov_base="\0\2\0\0'\0Tue Mar 1 13:52:30 2022 _"..., iov_len=151}], 3) = 185
[pid 201837] gettid() = 201837
[pid 201857] <... read resumed>"DUH\1\f\0\0\0\0DUH\1\6\0\0\0\5\0\0\0\0\0DUH\1\6\0\0\0\5"..., 10024) = 37
[pid 201837] gettid( <unfinished ...>
[pid 201857] read(3, <unfinished ...>
[pid 201837] <... gettid resumed>) = 201837
[pid 201837] getpid() = 201837
[pid 201837] writev(4, [{iov_base="DUH\1\1\0\0\0", iov_len=8}, {iov_base="=\3\0\257ECU1\0\3\24m\r7\253\364A\2SWMCSWMC", iov_len=26}, {iov_base="\0\2\0\0'\0Tue Mar 1 13:52:30 2022 _"..., iov_len=149}], 3) = 183
[...]

注意:这是 time/tzset.c 中对应的 glibc 的源代码 sn-p,它在 TZ 为空时设置 UTC:

/* Interpret the TZ envariable. */
static void
internal_function
tzset_internal (int always, int explicit)
{
 static int is_initialized;
 const char *tz;
if (is_initialized && !always)
 return;
 is_initialized = 1;
/* Examine the TZ environment variable. */
 tz = getenv ("TZ");
 if (tz == NULL && !explicit)
 /* Use the site-wide default. This is a file name which means we
 would not see changes to the file if we compare only the file
 name for change. We want to notice file changes if tzset() has
 been called explicitly. Leave TZ as NULL in this case. */
 tz = TZDEFAULT;
 if (tz && *tz == '\0')
 /* User specified the empty string; use UTC explicitly. */
 tz = "Universal";
[...]

【讨论】:

    猜你喜欢
    • 2011-11-06
    • 1970-01-01
    • 2022-11-10
    • 1970-01-01
    • 1970-01-01
    • 2020-07-12
    • 1970-01-01
    • 2015-08-02
    • 1970-01-01
    相关资源
    最近更新 更多