[Libav-user] Best way to create a fractional ms timer?
Nicolas George
nicolas.george at normalesup.org
Mon Apr 1 17:27:56 CEST 2013
Le duodi 12 germinal, an CCXXI, "René J.V. Bertin" a écrit :
> In any case we seem to agree that it all depends on how the timer is
> implemented, and of course that goes both ways.
Of course, but there are standard kernel designs for that.
> (What's a NOHZ kernel?)
The traditional way of implementing timing in an operating system kernel is
to have a periodic timer interrupt: each time the interrupt is triggered,
the kernel checks whether something needs to be done. This includes userland
timers and sleep functions, but also timed operations at the kernel level,
such as forcing disk cache flush or TCP retransmits.
Problem: an interrupt into kernel space is expensive, several hundreds or
thousands of CPU cycles; you do not want them too frequent. On the other
hand, all timed operations will have the accuracy of the timer interrupt:
this is a balance between efficiency and cost. IIRC, the timer interrupt for
Minix (late eighties) ran at 60 Hz; the Linux kernel used to run at 100 Hz,
but as hardware grew faster, options to make it 250 Hz or 1000 Hz were
added.
There is a newer design, where the kernel computes the exact time for the
next task, and sets the interrupt controller to trigger an interrupt at
exactly that time. In Linux, this design is called NOHZ. This design is
slightly more complex to program, but much more efficient, since it reduces
the interrupt overhead to its exact minimum while allowing a much better
accuracy. For tightly embedded systems, just waking the CPU 100 times per
second for an interrupt and a few trivial computations would have a
significant impact on the power consumption, that the NOHZ design
eliminates.
I am not sure whether the NOHZ design is practical, or even possible, on
older hardware, especially the old i8253 used in the PC architecture: it
requires an interrupt timer that can be re-programmed with a very low
overhead.
All this is completely orthogonal to the time slice issue. A time slice is
the amount of time dedicated to a process in a preemptive multitasking
environment: if the process has run for that amount of time, the kernel will
preempt it and see if someone else wants to run. In real world situations,
this is of course made more complex by conditions about process priority and
such.
Preempting a process is very expensive: it has all the costs of an
interrupt, and more, since all CPU caches become useless. For that reason,
time slices can be quite large.
There is no link between timer interrupts and time slices except that
preempting is a timed operation like any other, and therefore is subject to
the accuracy of the timer interrupt.
I suspect reading a good book on kernel design would be a good idea for
anyone who intends to do advanced programming. The standard reference is of
course Tanenbaum & Woodhull.
Regards,
--
Nicolas George
-------------- next part --------------
A non-text attachment was scrubbed...
Name: not available
Type: application/pgp-signature
Size: 198 bytes
Desc: Digital signature
URL: <http://ffmpeg.org/pipermail/libav-user/attachments/20130401/c4c6405f/attachment.asc>
More information about the Libav-user
mailing list