Clock_getres 和内核 2.6

发布于 2024-11-18 10:23:07 字数 451 浏览 2 评论 0原文

我现在使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

烟花易冷人易散 2024-11-25 10:23:07

根据内核源代码中的“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

对于低分辨率定时器(对于 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

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

低分辨率计时器的值可能会舍入到如此低的精度(实际上它们就像 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

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

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

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

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.

三月梨花 2024-11-25 10:23:07

尝试从 procfs 获取它。

猫 /proc/timer_list

Try to get it from procfs.

cat /proc/timer_list

天赋异禀 2024-11-25 10:23:07

为什么你认为这是不正确的?

例如,在现代 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文