[英]Any difference between clock_gettime( CLOCK_REALTIME … ) and time()?
A simple question: do time(...)
and clock_gettime( CLOCK_REALTIME, ... )
produce the same time theoretically (in respect to seconds only)? 一个简单的问题:做
time(...)
和clock_gettime( CLOCK_REALTIME, ... )
理论上产生相同的时间(仅限于秒)?
Here's what I mean: 这就是我的意思:
time_t epoch;
time( &epoch );
and 和
struct timespec spec;
clock_gettime( CLOCK_REALTIME, &spec );
Are these two supposed to return exactly the same result (in respect to seconds)? 这两个应该返回完全相同的结果(相对于秒)?
I "tested" this with changing time and time zones and epoch
and spec.tv_sec
always show the same result, but the documentation of CLOCK_REATIME
confuses me a bit and I'm not sure, that they will always be the same. 我通过更改时间和时区以及
epoch
“测试”了这一点, spec.tv_sec
总是显示相同的结果,但CLOCK_REATIME
的文档让我感到困惑,我不确定,它们将永远是相同的。
Real world situation: I have a piece of code, which uses time
. 现实世界的情况:我有一段代码,它使用
time
。 Now I want to have the time in milliseconds (which can be taken from spec.tv_nsec
, multiplied by 1000000). 现在我想要以毫秒为单位的时间(可以从
spec.tv_nsec
,乘以1000000)。 So I think about removing time
and using directly clock_gettime
, but I'm not sure if this will remain the same in all situations. 所以我考虑删除
time
并直接使用clock_gettime
,但我不确定在所有情况下这是否会保持不变。
The question is somehow related to Measure time in Linux - time vs clock vs getrusage vs clock_gettime vs gettimeofday vs timespec_get? 这个问题在某种程度上与Linux中的测量时间有关- 时间与时间对比getrusage vs clock_gettime vs gettimeofday vs timespec_get? but the information there was not enough for me.. I think.
但那里的信息对我来说还不够......我想。
[Note: I used the git master branch and v4.7 for the reference links below, x86 only, as I'm lazy.] [注意:我使用git master分支和v4.7作为下面的参考链接,仅限x86,因为我很懒。]
time()
is in fact an alias for the equally named syscall, which calls get_seconds
, happens at kernel/time/time.c
. time()
实际上是同名的syscall的别名,它调用get_seconds
,发生在kernel/time/time.c
That syscall uses the get_seconds
function to return the UNIX timestamp, which is read from the core timekeeping struct, more precisely from the "Current CLOCK_REALTIME time in seconds" field ( xtime_sec
). 该syscall使用
get_seconds
函数返回UNIX时间戳,该时间戳从核心计时结构中读取,更准确地说是从“当前CLOCK_REALTIME时间(以秒为单位)”字段( xtime_sec
)中xtime_sec
。
clock_gettime()
is a glibc function in sysdeps\\unix\\clock_gettime.c
, which simply calls gettimeofday
if the supplied clock ID is CLOCK_REALTIME
, which is again backed by the equally named syscall (source is in the same time.c
file, above). clock_gettime()
是sysdeps\\unix\\clock_gettime.c
一个glibc函数,如果提供的时钟ID是CLOCK_REALTIME
, CLOCK_REALTIME
调用gettimeofday
,它同样由同名的syscall支持(source在同一个time.c
文件中,上面) 。 This one calls do_gettimeofday
, which eventually ends up calling __getnstimeofday64
, that queries... the very same xtime_sec
field from the same struct as above. 这个调用
do_gettimeofday
,最终调用__getnstimeofday64
,查询来自上面相同结构的xtime_sec
字段。
Update: 更新:
As @MaximEgorushkin cleverly pointed out, a new vDSO mechanism hijacks (a good sign it is present, if your binary depends on linux-vdso.so.*
) the clock_gettime
call and redirects it to __vdso_clock_gettime
. 正如@MaximEgorushkin巧妙地指出的那样,一个新的vDSO机制劫持(如果你的二进制文件依赖于
linux-vdso.so.*
,这是一个好的迹象) clock_gettime
调用并将其重定向到__vdso_clock_gettime
。 This one uses a new clock source management framework (gtod - Generic Time Of Day ). 这个使用新的时钟源管理框架(gtod - Generic Time Of Day )。 A call to
do_realtime
, and it reads from a structure, struct vsyscall_gtod_data
's wall_time_sec
field. 的呼叫到
do_realtime
,它从一个结构,读取struct vsyscall_gtod_data
的wall_time_sec
字段。 This structure is maintained by update_vsyscall
, from the same timekeeper struct as the above. 这个结构由
update_vsyscall
维护,来自与上面相同的update_vsyscall
结构。
The answer is: yes , they get the time from the same clock source. 答案是: 是的 ,他们从相同的时钟源获得时间。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.