Linux, need accurate program timing. Scheduler wake up program

瘦欲@ 提交于 2019-12-06 19:39:27

问题


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.


回答1:


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.




回答2:


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.




回答3:


You won't be able to do what you want unless you run it on an actual "Real Time OS".




回答4:


If this is only Linux for x86 system I would choose HPET timer. I think all modern PCs has this hardware timer build in and it is very, very accurate. I allow you to define callback that will be called every millisecond and in this callback you can do your calculations (if they are simple) or just trigger other thread work using some synchronization object (conditional variable for example) Here is some example how to use this timer http://blog.fpmurphy.com/2009/07/linux-hpet-support.html




回答5:


Along with other advice such as setting the scheduling class to SCHED_FIFO, you will need to use a Linux kernel compiled with a high enough tick rate that it can meet your deadline.

For example, a kernel compiled with CONFIG_HZ of 100 or 250 Hz (timer interrupts per second) can never respond to timer events faster than that.

You must also set your timer to be just a little bit faster than you actually need, because timers are allowed to go beyond their requested time but never expire early, this will give you better results. If you need 1 ms, then I'd recommend asking for 999 us instead.



来源:https://stackoverflow.com/questions/6307266/linux-need-accurate-program-timing-scheduler-wake-up-program

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!