Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Linux' hrtimer - microsecond precision?

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.

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).

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).

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.

like image 490
user1034081 Avatar asked Oct 21 '22 17:10

user1034081


1 Answers

You can do what you want from user space

  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

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.

like image 131
amdn Avatar answered Oct 27 '22 18:10

amdn