time - clock_getres和内核2.6

标签 time kernel resolution clock

我现在正在使用ubuntu 11.04,并使用v2lin将我的程序从vxWorks移植到linux。我的clock_getres()有问题。

使用此代码:

struct timespec res;
clock_getres(CLOCK_REALTIME, &res);


我有res.tv_nsec = 1,这是不正确的。

就像这个人显示的那样:http://forum.kernelnewbies.org/read.php?6,377,423,内核2.4和2.6之间是有区别的。

那么内核2.6中时钟分辨率的正确值应该是多少

谢谢

最佳答案

根据内核来源的“ include / linux / hrtimer.h”文件,clock_getres()对于高分辨率计时器(如果系统中存在此类计时器)将始终返回1ns(一纳秒)。此值是硬编码的,表示:“计时器的值将四舍五入到它”

http://www.cs.fsu.edu/~baker/devices/lxr/http/source/linux/include/linux/hrtimer.h

269 /*
270  * The resolution of the clocks. The resolution value is returned in
271  * the clock_getres() system call to give application programmers an
272  * idea of the (in)accuracy of timers. Timer values are rounded up to
273  * this resolution values.
274  */
275 # define HIGH_RES_NSEC          1
276 # define KTIME_HIGH_RES         (ktime_t) { .tv64 = HIGH_RES_NSEC }
277 # define MONOTONIC_RES_NSEC     HIGH_RES_NSEC
278 # define KTIME_MONOTONIC_RES    KTIME_HIGH_RES


对于低分辨率计时器(如果没有hrtimer硬件,则用于MONOTONIC和REALTIME时钟),Linux将返回1 / HZ(典型的HZ为100到1000;因此该值为1到10 ms):

http://www.cs.fsu.edu/~baker/devices/lxr/http/source/linux/include/linux/ktime.h#L321

321 #define LOW_RES_NSEC            TICK_NSEC
322 #define KTIME_LOW_RES           (ktime_t){ .tv64 = LOW_RES_NSEC }


低分辨率计时器的值可能会四舍五入到如此低的精度(实际上,它们就像jiffles,Linux内核“ ticks”)。

PS:据我了解,这篇文章http://forum.kernelnewbies.org/read.php?6,377,423比较了未启用(已实现)hrtimers的2.4 linux和具有hrtimers的2.6内核。因此,所有值都是正确的。

关于time - clock_getres和内核2.6,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6574044/

相关文章:

android - Android 应用程序的安装时间?

android - 如何在 Android 中格式化日期和时间?

linux - 为 Linux 内核启动时间优化计时

linux - 我如何知道发行版使用哪个 Linux 内核版本?

algorithm - 粗细算法

JavaScript 日期 getSeconds 返回 0

php - MySQL w/PHP 中的查询时间结果

客户端服务器管道-c

ruby - 如何在 ruby​​ 中获取屏幕分辨率

html - 使用静态分辨率缩放 Canvas 元素