Clock_getres 和内核 2.6
我现在使用 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 中时钟分辨率的正确值应该是多少
谢谢
i'm using ubuntu 11.04 now and using v2lin to port my program from vxWorks tolinux. I have problem with clock_getres().
with this code:
struct timespec res;
clock_getres(CLOCK_REALTIME, &res);
i have res.tv_nsec = 1 , which is somehow not correct.
Like this guy showed: http://forum.kernelnewbies.org/read.php?6,377,423 , there is difference between kernel 2.4 and 2.6.
So what should be the correct value for the clock resolution in kernel 2.6
Thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
根据内核源代码中的“include/linux/hrtimer.h”文件,对于高分辨率定时器,
clock_getres()
将始终返回 1ns(一纳秒)(如果系统中存在此类定时器)。该值是硬编码的,它意味着:“定时器的值将四舍五入到它”http://www.cs.fsu.edu/~baker/devices/lxr/http/source/linux/include/linux/hrtimer.h
对于低分辨率定时器(对于 MONOTONIC 和 REALTIME 时钟,如果没有 hrtimer 硬件),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
低分辨率计时器的值可能会舍入到如此低的精度(实际上它们就像
jiffles
,Linux 内核“滴答”)。PS:这篇文章http://forum.kernelnewbies.org/read.php?6,377,423据我所知,将没有启用(实现) hrtimers 的 2.4 linux 与具有可用 hrtimers 的 2.6 内核进行比较。所以所有的值都是正确的。
According to "include/linux/hrtimer.h" file from kernel sources,
clock_getres()
will always return 1ns (one nanosecond) for high-resolution timers (if there are such timers in the system). This value is hardcoded and it means: "Timer's value will be rounded to it"http://www.cs.fsu.edu/~baker/devices/lxr/http/source/linux/include/linux/hrtimer.h
For low-resolution timers (and for MONOTONIC and REALTIME clocks if there is no hrtimer hardware), linux will return 1/HZ (typical HZ is from 100 to 1000; so value will be from 1 to 10 ms):
http://www.cs.fsu.edu/~baker/devices/lxr/http/source/linux/include/linux/ktime.h#L321
Values from low-resolution timers may be rounded to such low precision (effectively they are like
jiffles
, the linux kernel "ticks").PS: This post http://forum.kernelnewbies.org/read.php?6,377,423 as I can understand, compares 2.4 linux without hrtimers enabled (implemented) with 2.6 kernel with hrtimers available. So all values are correct.
尝试从 procfs 获取它。
猫 /proc/timer_list
Try to get it from procfs.
cat /proc/timer_list
为什么你认为这是不正确的?
例如,在现代 x86 CPU 上,内核使用 TSC 来提供高分辨率时钟 - 任何运行速度高于 1Ghz 的 CPU 都有一个 TSC,其滴答速度快于每纳秒滴答一次,因此纳秒分辨率相当常见。
Why do you think it is incorrect?
For example, on modern x86 CPUs the kernel uses the TSC to provide high resolution clocks - any CPU running at higher than 1Ghz has a TSC that ticks over faster than a tick per nanosecond, so nanosecond resolution is quite common.