【发布时间】: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变量的更改。变量没有变化时,不需要检查磁盘上的文件是否发生变化。