Linux的hrtimer-微秒精度? [英] Linux' hrtimer - microsecond precision?

查看:567
本文介绍了Linux的hrtimer-微秒精度?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否可以在Linux主机上以微秒精度执行任务?即,我想在特定时间执行任务.我知道,Linux不是实时系统,但我正在寻找Linux上的最佳解决方案.

Is it possible to execute tasks on a Linux host with microsecond precision? I.e., I'd like to execute a task at a specific instant of time. I know, Linux is no real-time system but I'm searching for the best solution on Linux.

到目前为止,我已经创建了一个内核模块,设置了hrtimer并测量了输入回调函数时的抖动(我不太在乎实际的延迟,它是很重要的抖动)-大约是20- 50us.这并没有比在用户空间中使用timerfd更好(也尝试在进程中使用实时优先级,但这并没有真正改变任何东西).

So far, I've created a kernel module, setup hrtimer and measured the jitter when the callback function is entered (I don't really care too much about the actual delay, it's jitter that counts) - it's about 20-50us. That's not significantly better than using timerfd in userspace (also tried using real-time priority for the process but that did not really change anything).

我正在运行Linux 3.5.0(仅作为示例,尝试了从2.6.35到3.7的不同内核),/proc/timer_list显示hrtimer_interrupt,我未在故障保护模式下运行,该模式禁用了hrtimer功能.在不同的CPU(Intel Atom至Core i7)上试用过.

I'm running Linux 3.5.0 (just an example, tried different kernels from 2.6.35 to 3.7), /proc/timer_list shows hrtimer_interrupt, I'm not running in failsafe mode which disables hrtimer functionality. Tried on different CPUs (Intel Atom to Core i7).

到目前为止,我最好的主意是将hrtimer与ndelay/udelay结合使用.这真的是最好的方法吗?我不敢相信以微秒的精度触发任务是不可能的.将代码作为模块运行在内核空间中是可以接受的,但是如果代码没有被其他任务打断,那将是很好的选择.我不太在乎系统的其余部分,该任务每秒仅执行几次,因此每次使用mdelay/ndelay每次应执行任务将CPU燃烧几微秒都没有关系.好的,我希望有一个更优雅的解决方案.

My best idea so far would be using hrtimer in combination with ndelay/udelay. Is this really the best way to do it? I can't believe it's not possible to trigger a task with microsecond precision. Running the code in kernel space as module is acceptable, would be great if the code was not interrupted by other tasks though. I dont' really care too much about the rest of the system, the task will be executed only very few times a second so using mdelay/ndelay for burning the CPU for some microseconds every time the task should be executed would not really matter. Altough, I'd prefer a more elegent solution.

我希望这个问题很清楚,找到了很多有关计时器精度的主题,但是没有真正的答案.

I hope the question is clear, found a lot of topics concerning timer precision but no real answer to that problem.

推荐答案

您可以在用户空间中完成所需的操作

You can do what you want from user space

  1. CLOCK_REALTIME一起使用 clock_gettime() 来获取时间具有nano秒的分辨率
  2. 使用 nanosleep() 产生CPU,直到接近时间为止需要执行任务(分辨率至少为milli秒).
  3. clock_gettime()一起使用旋转环,直到达到所需的时间
  4. 执行任务
  1. use clock_gettime() with CLOCK_REALTIME to get the time-of-day with nano-second resolution
  2. use nanosleep() to yield the CPU until you are close to the time you need to execute your task (it is at least milli-second resolution).
  3. use a spin loop with clock_gettime() until you reach the desired time
  4. execute your task

clock_gettime()函数在最近的内核和现代x86中被实现为 VDSO 处理器-需要20-30 nano秒才能获得具有nano秒分辨率的日期时间-您应该能够每micro秒调用30次以上的clock_gettime().使用此方法,您的任务应在预期时间的micro-秒的1/30内分派.

The clock_gettime() function is implemented as a VDSO in recent kernels and modern x86 processors - it takes 20-30 nanoseconds to get the time-of-day with nano-second resolution - you should be able to call clock_gettime() over 30 times per micro-second. Using this method your task should dispatch within 1/30th of a micro-second of the intended time.

这篇关于Linux的hrtimer-微秒精度?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆