Sunday, October 02, 2022

Measuring PREEMPT_RT Latency with an Oscilloscope

A ready-to-use bootable SD card image for BeagleBone Black/Green with a fully preemptible Linux kernel (PREEMPT_RT) — includes a gpio-test program that mirrors an input signal to a GPIO output pin for measuring scheduler latency with an oscilloscope.

Oscilloscope showing input and output waveforms with scheduler latency

Oscilloscope view — yellow is the input signal, blue is the GPIO output. The phase difference indicates scheduler latency.

Thanks to Maxime Chevallier of Bootlin for providing the gpio-test demo utility during PREEMPT_RT training.

Setup diagram

Setup diagram — signal generator, BeagleBone, and oscilloscope

Setup diagram — signal generator feeds the BeagleBone, oscilloscope measures input vs output.

Items needed

Instructions

  1. Prepare the setup as shown in the diagram (set signal generator to 100 Hz, 50% duty cycle)
  2. Download sdcard-beaglebone-preemptrt-demo.img.xz (~25 MB)
  3. Write the image to a micro SD card using Balena Etcher
  4. Insert the SD card into the BeagleBone and power on
  5. After boot, the oscilloscope shows two square waves with a phase difference — yellow (input) and blue (output from BeagleBone)
  6. The phase difference indicates the latency introduced by the scheduler while running the preemptrt-gpiotest program

On startup, /etc/init.d/S99PreemptrtGpioTester invokes:

chrt -r 99 /usr/sbin/preemptrt-gpiotest gpiochip0 28 gpiochip0 17 h p

This starts the gpio-test utility at real-time priority 99.

Measuring latency under load

The gpio-test program mirrors a GPIO input to an output — the phase difference between the two signals shows how fast the scheduler delivers context to your application. Jitter on the output signal (measured in oscilloscope persist mode) indicates the best and worst case scheduler latency under different load conditions (network traffic, interrupts, etc.).

Oscilloscope persist mode showing best and worst case latency

Persist mode — showing best and worst case scheduler latency under load.

For further details on PREEMPT_RT, see Bootlin's PREEMPT_RT training material.

Building from source

Detailed build instructions are available in the git repository. Cross-compile instructions for modifying and running a customized gpio-test utility are also available.

SOURCE CODE

github.com/hackboxguy/preemptrt-gpiotest — GPIO latency test program

github.com/hackboxguy/br-wrapper — Buildroot config and build instructions

sdcard-beaglebone-preemptrt-demo.img.xz — pre-built SD card image (~25 MB)

2 comments:

Anonymous said...

Great article! Is the response time just 3 kHz? What speed runs the cpu on?

Maxime Chevallier said...

Hello Albert,

That's a very neat article ! Thanks for the credit :)

You made my day :)

Maxime