[linux-audio-dev] Linux kernel HZ, audio latency and how to measure?

Stephen Cameron smcameron at yahoo.com
Fri Aug 18 21:07:34 UTC 2006

--- Mulyadi Santosa <a_mulyadi at telkom.net> wrote:

> > High HZ will improve MIDI timing though.  MIDI is more likely to be
> > polled than interrupt driven.
> polled? Well, interesting. Do you mean MIDI playback? 

I know at least some MIDI sequencers (eg. the one I have written)
rely on simple gettimeofday() and sleep() and usleep() to
wake up when it's time (or nearly time) to send MIDI events
out to MIDI devices.  Mine uses gettimeofday to know what time
it is, consults a schedule of events it is supposed to send out, 
sleeps until just before it needs to do the next item on the
schedule, wakes up, then starts polling gettimeofday() for the 
imminent "go time" to arrive, and repeats for the next item 
on the schedule, etc.

sleep() relies on the internal timer.  With whatever the
default Hz setting is for 2.6.16 kernels or thereabouts, I seem
to remember getting notes out within 200 microseconds of the
scheduled time as long as there wasn't lots of contention.  I 
instrumented the code to check (via gettimeofday) if the 
deadline was missed by some adjustable interval and if it
was missed by more than that, print a message.  I then adjusted
the interval until only a few messages came out during times
when lots of notes were coming out at the same time.  That
turned out to be around 200 microseconds.  (Actually the code to
print a message is currently turned off, since if it executes,
it only makes a bad situation worse.)

Actually now that I think about it again, my algorithm, since 
it wakes up slightly before the "go time" then polls gettimeofday
may be mostly immune to HZ changes (kind of why I wrote it that
way, because I knew sleep() didn't make guarantees).

xruns are, I think, JACK's equivalent of my deadline-missed
message.  It calls such tardiness an "xrun".  JACK is the
Jack Audio Connection Kit, an app that is useful for sync'ing
and communicating audio data between various audio apps that
know how to talk to JACK.

-- steve

Do You Yahoo!?
Tired of spam?  Yahoo! Mail has the best spam protection around 

More information about the Linux-audio-dev mailing list