请教关于os.date在指定时间戳时是否会引发比较慢的系统调用

177 views
Skip to first unread message

Lance

unread,
Mar 7, 2014, 12:34:37 PM3/7/14
to open...@googlegroups.com
请教 agentzh,

lua 的 os.date 当指定后面的 time 参数时,还会引起比较慢的系统调用吗?

我现在的需求是按照一定的格式生成日期字符串,比如 mmddyyyy 这样,用 os.date(format, time)能够实现,但担心会引发系统调用。而 ngx_lua 里的 ngx.now 之类的函数,第一不支持自定义格式,第二也不支持输入时间戳,所以并不合用,唯一支持输入时间戳的 ngx.http_time 输出格式也是固定的。

谢谢

Lance

Yichun Zhang (agentzh)

unread,
Mar 7, 2014, 4:58:09 PM3/7/14
to openresty
Hello!

2014-03-07 9:34 GMT-08:00 Lance:
> lua 的 os.date 当指定后面的 time 参数时,还会引起比较慢的系统调用吗?
>

这取决于具体的 OS 和 libc 实现。

在我的比较新的 Linux 系统上(较新的 glibc),使用 os.date("...", ngx.time())
并不会产生任何系统调用。我测试了下面这个小例子:

location /t {
content_by_lua '
print("hi")
for i = 1, 3 do
io.stderr:write(os.date("today is %A, in %B (%x)\\n",
ngx.time()))
end
ngx.say("hi")
';
}

在我的机器上用 strace 追踪对应的 nginx worker 进程显示一个典型的到 /t 的请求并没有触发多余的系统调用:

https://gist.github.com/agentzh/9421063

建议使用 strace 工具在你的系统上面进行确认 :)

Best regards,
-agentzh

Lance

unread,
Mar 8, 2014, 10:28:45 AM3/8/14
to open...@googlegroups.com
好的。明白了,谢谢

Lance



--
--
邮件来自列表“openresty”,专用于技术讨论!
订阅: 请发空白邮件到 openresty...@googlegroups.com
发言: 请发邮件到 open...@googlegroups.com
退订: 请发邮件至 openresty+...@googlegroups.com
归档: http://groups.google.com/group/openresty
官网: http://openresty.org/
仓库: https://github.com/agentzh/ngx_openresty
教程: http://openresty.org/download/agentzh-nginx-tutorials-zhcn.html

Reply all
Reply to author
Forward
0 new messages