C1239 of 55
0 XP
  1. Hook
  2. Play
  3. Predict
  4. Name it
  5. Check
  6. Explain

C12 · Lesson 39 · Latency, jitter and deadlines

Doing things on time, every time

"Real time" doesn't mean fast. It means on time: a motor controller that updates every millisecond, give or take a microsecond, is real-time; one that's usually quicker but sometimes a millisecond late is not. Three numbers describe it. Latency is how long a response takes to start after its trigger. Jitter is how much that time varies. A deadline is the latest a job may finish and still be correct. Designing for real time means knowing the worst case of each, not the average.

  • A control task must run every 1 ms; each run toggles P0 and records the gap since the last run. Meanwhile, background work runs in chunks of 0 to 0.8 ms. After 200 runs the program prints the jitter: the longest gap minus the shortest, in microseconds.
  • Polled from the loop (USE_IRQ 0), the task runs whenever the background chunk ends: it prints about 1215 µs. Zoom in on P0 and see the uneven edges.
  • With USE_IRQ 1 the timer interrupt runs it the instant the period is up, whatever the loop is doing: it prints 0. (On a real chip, a cycle or two.)

Predict

A superloop checks a flag once per pass, and a pass takes anywhere from 0.1 ms to 0.9 ms. What's the worst-case latency for responding to the flag?

Name it

The three numbers

  • Latency: from the trigger (an edge, a timer, a byte) to the start of the response.
  • Jitter: the spread in latency, or in the interval between periodic events: maximum minus minimum.
  • Deadline: the latest a job may finish. A job meets it if, in the worst case, latency plus execution time (C, its worst-case execution time) fits: Lmax+C≤D.

Hard real time means a missed deadline is a failure (an airbag, a motor commutation); soft real time means lateness only degrades quality (an audio glitch, a sluggish display).

Where jitter comes from

  • polling: the response waits for the loop to come round;
  • other tasks' turns in a cooperative scheduler;
  • sections with interrupts disabled, and other interrupts' handlers;
  • code whose running time varies with its data.

The cures, roughly in order: move the job to an interrupt; keep ISRs and critical sections short; give urgent sources higher priority; and for the strictest timing, let hardware act directly (a timer driving a PWM pin, a DAC fed by DMA) so software isn't in the path at all.

Why it matters

A sampled control loop or a signal converter assumes evenly spaced samples. Sampling a signal of frequency f and amplitude A at a time off by Δt reads it wrong by up to 2πfAΔt, the same jitter limit as an ADC's clock (C11).

Measuring

Toggle a spare pin at the start and end of the job and look at it on a logic analyzer, as the play does: the edges show latency and execution time directly, and their spread over many runs shows the jitter.

Worked example

A 50 Hz signal of amplitude 1 V is sampled with 30 µs of timing jitter. How large can the error in a sample be?

Show the solution
  1. Steepest slope times the jitter. Δv=2πfAΔt=2π×50×1×30×10−6V=9.4mV
  2. Compare with the amplitude. That's about 1% of the amplitude: comparable to a 7-bit converter's resolution, wasting a 12-bit ADC.

Answer up to 9.4 mV, about 1% of the amplitude.

Worked example

A job must finish within 200 µs of its trigger. It takes up to 120 µs to run, and interrupts may be disabled for up to 50 µs elsewhere. Does it meet its deadline?

Show the solution
  1. Wait. In the worst case the job waits 50 µs, plus the interrupt's few cycles.
  2. Run. Then it runs for 120 µs: 50+120=170μs about 170 µs in all, within 200 µs.
  3. Another interrupt first. If another, higher-priority interrupt can also take 40 µs first, it becomes 210 µs, and it doesn't.

Answer yes, at about 170 µs in the worst case; if a higher-priority interrupt can take 40 µs first, it becomes 210 µs and misses.

Worked example

Three periodic tasks use 25% of the processor in all. Does the rate-monotonic utilization bound guarantee they meet their deadlines?

Show the solution
  1. The Liu and Layland bound. For three tasks it is 3(21/3−1)=0.780
  2. Compare. 25% is well under 78%, so with rate-monotonic priorities (shorter period, higher priority) every deadline is met.
  3. Above the bound. A more exact response-time analysis is needed.

Answer yes: 25% is well under the three-task bound of 78%.

Check

A loop polls a flag once per pass; passes take between 0.1 and 0.6 ms. What's the worst-case latency?

Write units the way you normally would: 1.5 ms, 1500 µs and 0.0015 s are the same answer.

Check

A periodic job's measured intervals range from 995 µs to 1010 µs. What's its jitter?

Write units the way you normally would: 1.5 µs, 1500 ns and 0.0015 ms are the same answer.

Check

A job's worst-case latency is 40 µs, its worst-case execution time 50 µs, and its deadline 200 µs. How much margin is left (negative if it misses)?

Write units the way you normally would: 1.5 µs, 1500 ns and 0.0015 ms are the same answer.

Check

A 1000 Hz signal of amplitude 1 V is sampled with 100 µs of timing jitter. How large can a sample's error be?

Write units the way you normally would: 1.5 mV, 1500 µV and 0.0015 V are the same answer.

Check

An ISR runs every 100 µs and takes 60 µs. What share of the time does it occupy, and so how long can it hold off everything below it?

Check

A PWM output for a motor must switch at exactly 20 kHz, with no jitter. What's the best way?

Check

Make the play's control task jitter-free: run it from the timer interrupt instead of polling (everything else stays the same).

C on the course's RISC-V board (10 MHz, no floating point). #include "board.h" for the registers; print(x) shows a number. Tab indents; press Esc and then Tab to move on.

Check

Toggle P0 every 10 ms, steadily, while the main loop does work of varying length (the starter's work). Use a machine.Timer.

MicroPython on the course's board: machine (Pin, PWM, ADC, DAC, UART, I2C, SPI, Timer, WDT) and time. Each call into the hardware takes about 40 µs. Tab indents; press Esc and then Tab to move on.

Check

A 50 Hz sine of amplitude 1 V is sampled with 30 µs of timing jitter.

(a)

How large can the error be, at most?

Write units the way you normally would: 1.5 mV, 1500 µV and 0.0015 V are the same answer.

(b)

How many 12-bit ADC codes (0.806 mV each) is that?

Check

3 periodic tasks are scheduled rate-monotonically.

(a)

What is the Liu and Layland utilization bound?

(b)

What does it approach for many tasks?

Explain why

Why do real-time designs care about the worst case instead of the average?

Practice

Keep practicing

10 kinds of problem on this topic, dealt one at a time with new numbers every time, for as long as you like.