Linux CLOCK_MONOTONIC 的起点
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/14726401/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Starting point for CLOCK_MONOTONIC
提问by rimas
As I understand on Linux starting point for CLOCK_MONOTONIC
is boot time. In my current work I prefer to use monotonic clock instead of CLOCK_REALTIME
(for calculation) but in same time I need to provide human friendly timestamps (with year/month/day) in reporting. They can be not very precise so I was thinking to join monotonic counter with boot time.
据我了解,Linux 的起点CLOCK_MONOTONIC
是启动时间。在我目前的工作中,我更喜欢使用单调时钟而不是CLOCK_REALTIME
(用于计算),但同时我需要在报告中提供人类友好的时间戳(带有年/月/日)。它们可能不是很精确,所以我想在启动时间加入单调计数器。
From where I can get this time on linux system using api calls?
我可以从哪里使用 api 调用在 linux 系统上获得这个时间?
回答by NPE
CLOCK_MONOTONIC
is generally not affected by any adjustments to system time. For example, if the system clock is adjusted via NTP, CLOCK_MONOTONIC
has no way of knowing (nor does it need to).
CLOCK_MONOTONIC
通常不受任何系统时间调整的影响。例如,如果系统时钟是通过 NTP 调整的,CLOCK_MONOTONIC
则无法知道(也不需要)。
For this reason, don't use CLOCK_MONOTONIC
if you need human-readable timestamps.
因此,CLOCK_MONOTONIC
如果您需要人类可读的时间戳,请不要使用。
See Difference between CLOCK_REALTIME and CLOCK_MONOTONIC?for a discussion.
回答by Maxim Egorushkin
http://www.kernel.org/doc/man-pages/online/pages/man2/clock_getres.2.html:
http://www.kernel.org/doc/man-pages/online/pages/man2/clock_getres.2.html:
CLOCK_MONOTONIC Clock that cannot be set and represents monotonic time since some unspecified starting point.
CLOCK_MONOTONIC Clock that cannot be set and represents monotonic time since some unspecified starting point.
Means that you can use CLOCK_MONOTONIC
for interval calculations and other things but you can't really convert it to a human readable representation.
意味着您可以CLOCK_MONOTONIC
用于间隔计算和其他事情,但您无法真正将其转换为人类可读的表示形式。
Moreover, you prabably want CLOCK_MONOTONIC_RAW
instead of CLOCK_MONOTONIC
:
此外,您可能想要CLOCK_MONOTONIC_RAW
而不是CLOCK_MONOTONIC
:
CLOCK_MONOTONIC_RAW (since Linux 2.6.28; Linux-specific) Similar to CLOCK_MONOTONIC, but provides access to a raw hard‐ ware-based time that is not subject to NTP adjustments.
CLOCK_MONOTONIC_RAW (since Linux 2.6.28; Linux-specific) Similar to CLOCK_MONOTONIC, but provides access to a raw hard‐ ware-based time that is not subject to NTP adjustments.
Keep using CLOCK_REALTIME
for human-readable times.
继续使用CLOCK_REALTIME
人类可读的时间。
回答by Vilhelm Gray
Assuming the Linux kernel starts the uptimecounter at the same time as it starts keeping track of the monotonic clock, you can derive the boot time (relative to the Epoch) by subtracting uptimefrom the current time.
假设 Linux 内核在开始跟踪单调时钟的同时启动正常运行时间计数器,您可以通过从当前时间中减去正常运行时间来推导出启动时间(相对于纪元)。
Linux offers the system uptimein seconds via the sysinfo
structure; the current timein seconds since the Epoch can be acquired on POSIX compliant libraries via the time
function.
Linux通过结构提供以秒为单位的系统正常运行时间sysinfo
;可以通过该函数在符合 POSIX 的库上获取自 Epoch 以来的当前时间(以秒为单位)time
。
#include <stddef.h>
#include <stdio.h>
#include <time.h>
#include <sys/sysinfo.h>
int main(void) {
/* get uptime in seconds */
struct sysinfo info;
sysinfo(&info);
/* calculate boot time in seconds since the Epoch */
const time_t boottime = time(NULL) - info.uptime;
/* get monotonic clock time */
struct timespec monotime;
clock_gettime(CLOCK_MONOTONIC, &monotime);
/* calculate current time in seconds since the Epoch */
time_t curtime = boottime + monotime.tv_sec;
/* get realtime clock time for comparison */
struct timespec realtime;
clock_gettime(CLOCK_REALTIME, &realtime);
printf("Boot time = %s", ctime(&boottime));
printf("Current time = %s", ctime(&curtime));
printf("Real Time = %s", ctime(&realtime.tv_sec));
return 0;
}
Unfortunately, the monotonic clock may not match up relative to boot time exactly. When I tested out the above code on my machine, the monotonic clock was a second off from the system uptime. However, you can still use the monotonic clock as long as you take the respective offset into account.
不幸的是,相对于启动时间,单调时钟可能不完全匹配。当我在我的机器上测试上述代码时,单调时钟与系统正常运行时间相差一秒。但是,只要考虑到相应的偏移量,您仍然可以使用单调时钟。
Portability note: although Linux may return current monotonic time relative to boot time, POSIX machines in general are permitted to return current monotonic time from any arbitrary -- yet consistent -- point in time (often the Epoch).
可移植性注意事项:尽管 Linux 可能会返回相对于启动时间的当前单调时间,但 POSIX 机器通常可以从任何任意 - 但一致 - 时间点(通常是 Epoch)返回当前单调时间。
As a side note, you may not need to derive boot time as I did. I suspect there is a way to get the boot time via the Linux API, as there are many Linux utilities which display the boot time in a human-readable format. For example:
附带说明一下,您可能不需要像我一样推导出启动时间。我怀疑有一种方法可以通过 Linux API 获取启动时间,因为有许多 Linux 实用程序以人类可读的格式显示启动时间。例如:
$ who -b
system boot 2013-06-21 12:56
I wasn't able to find such a call, but inspection of the source code for some of these common utilities may reveal how they determine the human-readable boot time.
我找不到这样的调用,但检查其中一些常用实用程序的源代码可能会揭示它们如何确定人类可读的启动时间。
In the case of the who
utility, I suspect it utilizes the utmp
file to acquire the system boot time.
对于该who
实用程序,我怀疑它利用该utmp
文件来获取系统启动时间。