Collecting the data you will need
So what is the latency of your system? Do you know? Why not? It's
appalling that so many of us build systems with a "if the stupid thing
works at all, ship it" philosophy. It seems to me there are certain
critical parameters we must understand in order to properly develop and
maintain a product. Like, is there any free ROM space? Is the system
20% loaded . . . or 99%? How bad is the maximum latency?
Latency is pretty easy to measure, sometimes those measurements will
yield surprising and scary results. Perhaps the easiest way to get a
feel for interrupt response is to instrument each ISR with an
instruction that toggles a parallel output bit high when the routine
starts. Drive it low just as it exits. Connect this bit to one input of
an oscilloscope, tying the other input to the interrupt signal itself.
The amount of information this simple setup gives is breathtaking.
Measure time from the assertion of the interrupt until the parallel bit
goes high. That's latency, minus a bit for the overhead of managing the
instrumentation bit. Twiddle the scope's time base to measure this to
any level of precision required.
The time the bit stays high is the ISR's total execution time. Tired
of guessing how fast your code runs? This is quantitative, cheap, and
accurate. In a real system, interrupts come often. Latency varies
depending on what other things are going on.
Use a digital scope in storage mode. After the assertion of the
interrupt input you'll see a clear space - that's the minimum system
latency to this input. Then there will be hash, a blur as the
instrumentation bit goes high at different times relative to the
interrupt input. These represent variations in latency. When the blur
resolves itself into a solid high, that's the maximum latency.
All this, for the mere cost of one unused parallel bit.
If you've got a spare timer channel, there's another approach that
requires neither extra bits nor a scope. Build an ISR just for
measurement purposes that services interrupts from the timer.
On initialization, start the timer counting up, programmed to
interrupt when the count overflows. Have it count as fast as possible.
Keep the ISR dead simple, with minimal overhead. This is a good thing
to write in assembly language to minimize unneeded code. Too many C
compilers push everything inside interrupt handlers.
The ISR itself reads the timer's count register and sums the number
into a long variable, perhaps called total_time. Also increment a
counter (iterations). Clean up and return.
The trick here is that, although the timer reads zero when it tosses
out the overflow interrupt, the timer register continues counting even
as the CPU is busy getting ready to invoke the ISR. If the system is
busy processing another interrupt, or perhaps stuck in an
interrupt-disabled state, the counter continues to increment.
An infinitely fast CPU with no latency would start the
instrumentation ISR with the counter register equal to zero. Real
processors with more usual latency issues will find the counter at some
positive nonzero value that indicates how long the system was off doing
other things.
Therefore, average latency is just the time accumulated into
total_time (normalized to microseconds) divided by the number of times
the ISR ran (iterations). It's easy to extend the idea to give even
more information. Possibly the most important thing we can know about
our interrupts is the longest latency. Add a few lines of code to
compare for and log the maximum time.
Is the method perfect? Of course not. The data is somewhat
statistical, so can miss single-point outlying events. Very speedy
processors may run so much faster than the timer tick rate that they
always log latencies of zero, although this may indicate that for all
practical purposes latencies are short enough to not be significant.
The point is that knowledge is power, once we understand the
magnitude of latency reasons for missed interrupts become glaringly
apparent.
Try running these experiments on purchased software components. One
embedded DOS, running on a 100-MHz 486, yielded latencies in the tens
of milliseconds!
Next in Part 5:
Using your
C-compiler to minimize code size.
To read Part 1 in this series, go to
Reentrancy, atomic variables and recursion.
To read Part 2 in this series, go to
Asynchronous Hardware/Firmware
To read Part 3, go to
Metastable
States
Jakob
Engblom (jakob@virtutech.com)
is technical marketing manager at
at Virtutech.
He has a MSc in computer science and a PhD in Computer Systems from
Uppsala University, and has
worked with programming tools and simulation tools for embedded and
real-time systems since 1997.
He was a contributor of
material to "The Firmware Handbook," edited
by Jack Ganssle, upon which this series of articles was based and
printed
with permission from Newnes, a division of Elsevier.
Copyright 2008. For
other publications by Jakob Engblom, see www.engbloms.se/jakob.html.