Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Linux, need accurate program timing. Scheduler wake up program

I have a thread running on a Linux system which i need to execute in as accurate intervals as possbile. E.g. execute once every ms.

Currently this is done by creating a timer with

 timerfd_create(CLOCK_MONOTONIC, 0)

, and then passing the desired sleep time in a struct with

 timerfd_settime (fd, 0, &itval, NULL);

A blocking read call is performed on this timer which halts thread execution and reports lost wakeup calls.

The problem is that at higher frequencies, the system starts loosing deadlines, even though CPU usage is below 10%. I think this is due to the scheduler not waking the thread often enough to check the blocking call. Is there a command i can use to tell the scheduler to wake the thread at certain intervals as far as it is possble? Busy-waiting is a bad option since the system handles many other tasks.

Thank you.

like image 775
K_scheduler Avatar asked Jun 10 '11 13:06

K_scheduler


People also ask

How does Linux scheduler work?

The Linux scheduling algorithm works by dividing the CPU time into epochs . In a single epoch, every process has a specified time quantum whose duration is computed when the epoch begins. In general, different processes have different time quantum durations.

What Scheduler does Linux use?

The LINUX Kernel used the O(n) scheduler between version 2.4 and 2.6. n is the number of runnable processes in the system. O(n) scheduler divides the processor's time into a unit called epochs. Each task is allowed to use at max 1 epoch.

What is a scheduling kernel?

The task scheduler, sometimes called process scheduler, is the part of the kernel that decides which task to run next. It is responsible for best using system resources to guarantee that multiple tasks are being executed simultaneously. This makes it a core component of any multitasking operating system.


2 Answers

You need to get RT linux*, and then increase the RT priority of the process that you want to wake up at regular intervals.

Other then that, I do not see problems in your code, and if your process is not getting blocked, it should work fine.

(*) RT linux - an os with some real time scheduling patches applied.

like image 67
BЈовић Avatar answered Sep 26 '22 07:09

BЈовић


One way to reduce scheduler latency is to run your process using the realtime scheduler such as SCHED_FIFO. See sched_setscheduler .

This will generally improve latency a lot, but still theres little guarantee, to further reduce latency spikes, you'll need to move to the realtime brance of linux, or a realtime OS such as VxWorks, RTEMS or QNX.

like image 30
nos Avatar answered Sep 24 '22 07:09

nos