Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>Try altering it as follows, this should pretty much garuntee that it'll never miss a wakeup, but be careful with it since running realtime priority can lock your machine hard if it doesn't sleep, also you may need to set things up so that your user has the ability to run stuff at realtime priority (see <code>/etc/security/limits.conf</code>)</p> <pre><code>#include &lt;sys/timerfd.h&gt; #include &lt;time.h&gt; #include &lt;string.h&gt; #include &lt;stdint.h&gt; #include &lt;stdio.h&gt; #include &lt;sched.h&gt; int main(int argc, char *argv[]) { int timerfd = timerfd_create(CLOCK_MONOTONIC,0); int milliseconds = atoi(argv[1]); struct itimerspec timspec; struct sched_param schedparm; memset(&amp;schedparm, 0, sizeof(schedparm)); schedparm.sched_priority = 1; // lowest rt priority sched_setscheduler(0, SCHED_FIFO, &amp;schedparm); bzero(&amp;timspec, sizeof(timspec)); timspec.it_interval.tv_sec = 0; timspec.it_interval.tv_nsec = milliseconds * 1000000; timspec.it_value.tv_sec = 0; timspec.it_value.tv_nsec = 1; int res = timerfd_settime(timerfd, 0, &amp;timspec, 0); if(res &lt; 0){ perror("timerfd_settime:"); } uint64_t expirations = 0; int iterations = 0; while( res = read(timerfd, &amp;expirations, sizeof(expirations))){ if(res &lt; 0){ perror("read:"); continue; } if(expirations &gt; 1){ printf("%ld expirations, %d iterations\n", expirations, iterations); break; } iterations++; } } </code></pre> <p>If you are using threads you should use <code>pthread_setschedparam</code> instead of <code>sched_setscheduler</code>.</p> <p>Realtime also isn't about low latency, it's about guarantees, RT means that if you want to wake up exactly once every second on the second, you WILL, the normal scheduling does not give you this, it might decide to wake you up 100ms later, because it had some other work to do at that time anyway. If you want to wake up every 10ms and you REALLY do need to, then you should set yourself to run as a realtime task then the kernel will wake you up every 10ms without fail. Unless a higher priority realtime task is busy doing stuff. </p> <p>If you need to guarantee that your wakeup interval is exactly some time it doesn't matter if it's 1ms or 1 second, you won't get it unless you run as a realtime task. There are good reasons the kernel will do this to you (saving power is one of them, higher throughput is another, there are others), but it's well within it's rights to do so since you never told it you need better guarantees. Most stuff doesn't actually need to be this accurate, or need to never miss so you should think hard about whether or not you really do need it. </p> <p>quoting from <a href="http://www.ganssle.com/articles/realtime.htm" rel="nofollow noreferrer">http://www.ganssle.com/articles/realtime.htm</a></p> <blockquote> <p>A hard real time task or system is one where an activity simply must be completed - always - by a specified deadline. The deadline may be a particular time or time interval, or may be the arrival of some event. Hard real time tasks fail, by definition, if they miss such a deadline.</p> <p>Notice this definition makes no assumptions about the frequency or period of the tasks. A microsecond or a week - if missing the deadline induces failure, then the task has hard real time requirements.</p> </blockquote> <p>Soft realtime is pretty much the same, except that missing a deadline, while undesirable, is not the end of the world (for example video and audio playback are soft realtime tasks, you don't want to miss displaying a frame, or run out of buffer, but if you do it's just a momentary hiccough, and you simply continue). If what you are trying to do is 'soft' realtime I wouldn't bother with running at realtime priority since you should generally get your wakeups in time (or at least close to it). </p> <p>EDIT:</p> <p>If you aren't running realtime the kernel will by default give any timers you make some 'slack' so that it can merge your request to wake up with other events that happen at times close to the one you asked for (that is if the other event is within your 'slack' time it will not wake you at the time you asked, but a little earlier or later, at the same time it was already going to do something else, this saves power).</p> <p>For a little more info see <a href="http://lwn.net/Articles/296578/" rel="nofollow noreferrer">High- (but not too high-) resolution timeouts</a> and <a href="http://lwn.net/Articles/369549/" rel="nofollow noreferrer">Timer slack</a> (note I'm not sure if either of those things is exactly what's really in the kernel since both those articles are about lkml mailing list discussions, but something like the first one really is in the kernel.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload