简体   繁体   English

如何在Linux用户空间应用程序中获得最精确的时钟?

[英]How to get the most exact clock in linux user space application?

I have an embedded system and I would like to share in a kernel module/driver the most accurate clock with an userspace application which is very sensitive to the clock, and should be the most accurate. 我有一个嵌入式系统,我想在内核模块/驱动程序中与对时钟非常敏感且应该最准确的用户空间应用程序共享最准确的时钟。

  1. These functions requires context switch and a huge overhead and the clock won't be accurate since the syscall. 这些功能需要上下文切换和巨大的开销,并且自从syscall以来,时钟将不准确。

  2. I thought to increase a shared integer every jiffy from the kernel module, assuming the userspace application can access it directly, the problem is that I can't share an integer/long which is not aligned to a page size and declare a whole new page for only one long variable is a huge overhead. 想让内核模块每增加一个共享整数,就假设用户空间应用程序可以直接访问它,问题是我不能共享一个不与页面大小对齐的integer/long ,并且不能声明一个新页面因为只有一个long变量是巨大的开销。

What should I do? 我该怎么办?

You want to use clock_gettime() for obtaining the current time (since startup) with either CLOCK_MONOTONIC (monotonic but not steady as it is influenced by NTP) or CLOCK_MONOTONIC_RAW (monotonic and steady, but Linux specific and requires a kernel >= 2.6.28). 你想使用clock_gettime()获取当前时间(自启动以来)有两种CLOCK_MONOTONIC (单调的,但不是稳定的,因为它是由NTP影响)或CLOCK_MONOTONIC_RAW (单调和稳定的,但是Linux特有的,并且需要一个内核> = 2.6.28 )。

For waking up at exact intervals, use clock_nanosleep() and specify TIMER_ABSTIME . 要以确切的间隔唤醒,请使用clock_nanosleep()并指定TIMER_ABSTIME Unfortunately clock_nanosleep() only supports CLOCK_MONOTONIC and not CLOCK_MONOTONIC_RAW , so you cannot pass a wakeup time obtained with CLOCK_MONOTONIC_RAW because these clocks may differ. 不幸的是clock_nanosleep()只支持CLOCK_MONOTONIC而不是CLOCK_MONOTONIC_RAW ,所以你不能传递与所获得的唤醒时间CLOCK_MONOTONIC_RAW因为这些时钟可能会有所不同。 Don't forget to check the return code for EINTR . 不要忘记检查EINTR的返回码。

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

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