简体   繁体   English

eglibc:以毫秒为单位的正常运行时间

[英]eglibc: Getting Uptime in Milliseconds

I want to write a log-output with the same format as the kernel-log: 我想编写一个与内核日志格式相同的日志输出:

[   11.947248] fsl-gianfar ffe24000.ethernet eth0: Link is Up

The timestamp should have the same time-reference as the kernel time. 时间戳记应与内核时间具有相同的时间参考。 This means, when a log-message is emitted at the same time as it is done in the kernel, the timestamps should have the same value. 这意味着,当在内核中同时发出日志消息时,时间戳记应具有相同的值。

The clock_gettime has a undefined clock starting-point - thus the timestamp of every program has a different value - even if they were created at the same time: clock_gettime具有未定义的时钟起始点-因此,每个程序的时间戳都有不同的值-即使它们是同时创建的:

clock_gettime(CLOCK_REALTIME, &ts);
clock_gettime(CLOCK_MONOTONIC, &ts);

When using gettimeofday you get the time since the Unix-epoch... which does not match by definition. 使用gettimeofday您会获得从Unix-epoch ...开始的时间,根据定义,该时间不匹配。

The function localtime() returns the uptime - but the granularity is in seconds - but I need more... 函数localtime()返回正常运行时间-但粒度以秒为单位-但我需要更多...


Update : It seems that clock_gettime(CLOCK_REALTIME, &ts) should do what I want... but eglibc seems to return CLOCK_MONOTONIC instead. 更新似乎 clock_gettime(CLOCK_REALTIME, &ts)应该做我想要的...但是eglibc似乎返回CLOCK_MONOTONIC

The idea is first to find the offset for your CLOCK_MONOTONIC. 首先想到的是找到您的CLOCK_MONOTONIC的偏移量。 Just read the /proc/uptime once for that. 为此,只需阅读一次/proc/uptime Then use the offset and CLOCK_MONOTONIC to print the time for every logged event. 然后使用偏移量和CLOCK_MONOTONIC来打印每个记录的事件的时间。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM