Definition
Measurement perturbation is the change that measuring causes in the system being measured. A measuring tool, such as a log statement, a timer reading, a trace probe or a debugger that halts the processor, uses processor time, memory or bus bandwidth, or pauses part of the system. The change may only add a cost to the measured quantity. Or it may alter what the system does, for example by letting a timer expire, so that the observed run takes a different course from an unobserved run.
The idea
A developer studies a Bluetooth Low Energy (BLE) §7.1 device that sometimes loses its connection to a phone. The device has one processor, which runs both the application and the BLE radio software. The developer sets a breakpoint §14.29, a marker at which a debugger halts the processor, in the radio software and inspects memory for 30 seconds. The phone does not stop. The link’s supervision timeout §7.8, the time a BLE device waits without hearing from its peer before declaring the link lost, is 4 seconds, so after 4 seconds the phone drops the link. When the processor resumes, the device soon finds itself disconnected, a state the uninterrupted run would not have reached then. The memory snapshot taken at the breakpoint is accurate; the run that continues after it is a different run.
Checking a tire’s pressure lets out a puff of air: a small, fixed loss that can be allowed for. Stopping a runner to take a pulse changes the race. Measurement perturbation comes in these two kinds, and only the first can be corrected by arithmetic. The second, a change in the course of events, can even hide what is being looked for. A race condition §9.2, an error that depends on the relative timing of two activities, can vanish when slow logging changes that timing; such a bug is nicknamed a Heisenbug. Other names for measurement perturbation are the observer effect and, for concurrent programs, the probe effect. The analogy stops being accurate in one respect: a tire gauge always removes air, but a software observer can make a run faster as well as slower, for example by changing what the processor’s caches hold.
How it works
An observer acts on the measured system through a few channels.
Processor time: code added to read a timer or write a log line runs on the processor being measured.
Memory and caches: log buffers take memory, and added code changes the program’s layout and the contents of the caches, the small fast memories that hold recently used data.
Input and output: a program that waits for a serial port to accept each log line stalls meanwhile.
Interrupts §6.11: a probe that briefly disables interrupts, the hardware signals that make the processor run urgent handlers, delays those handlers.
Halting: a debugger halt stops the processor core. Whether timers, radio hardware and the watchdog timer §6.16, a counter that resets the processor unless software restarts it regularly, also stop depends on the chip and its debug settings. Clocks outside the chip, such as a peer’s, never stop.
A measurement is checked for perturbation in four steps.
List every component that keeps time or changes state during the measurement: the processor, its timers and counters, peripherals such as the radio, the watchdog timer, other processor cores, and every peer outside the device.
For each component, write down what the observer does to it: adds work, delays it, pauses it, or lets it run while pausing something it interacts with.
Classify the perturbation. If the observer only adds a constant cost and changes neither which code runs nor which events occur, measure that cost separately and subtract it. If the observer can change which events occur or their order, such as a timer expiring, a buffer filling or a race ending the other way, subtraction is not valid. Then reduce the perturbation, for example by buffering log records in memory and sending them after the measured section, or by hardware tracing that streams events off the chip without stopping the processor; or compare runs with and without the observer on an outcome measured without it, such as the number of dropped connections counted by the phone.
Report the observer, its measured cost and what it may have changed, together with the result.
Worked example
A correctable cost. An operation takes 10 microseconds. The timing wrapper around it adds a cost that separate measurement shows to be a constant 2 microseconds, and the wrapper does not change what the operation does. The observed time is microseconds, and subtracting the cost gives microseconds, the quantity sought.
A blocking log line. A serial port runs at 115,200 baud, which for a serial port means 115,200 bits per second. Each byte travels as 10 bits: 1 start bit, 8 data bits and 1 stop bit. An 80-byte log line therefore takes s, about 6.9 ms, and a program that waits for the port stalls that long. If the code being studied must run every 5 ms, each log line makes it miss at least one period, since . The log then describes a system that misses its period, which the unobserved system may not do.
A halt. The debugger halts the processor for 30 s, and the supervision timeout is 4 s. Since , the phone declares the link lost 4 s into the halt, s before the processor resumes. After resuming, the device runs its disconnection and recovery code, which the run being studied never executed. Removing the 30 s from the timeline does not recreate the exchange that the halt prevented.
Stated precisely
Let be the measured quantity in an unobserved run, such as a duration, let be the same quantity in the observed run, and let be the observer’s cost. The additive model is and it justifies the correction only under three assumptions.
Known cost. is measured separately and is constant. If it varies between and , the correction gives only the interval .
Same path. The observer changes neither which instructions the measured code executes nor which events occur.
Same surroundings. The observer does not change the state of other components that the measured code depends on, such as caches, timers and peers.
When the second or third assumption fails, there is in general no function that turns the observed outcome into the unobserved one. The observed run answers a different question: what the system does while it is observed in this way. For a debugger halt, the exact statement is this. The halt preserves the registers and memory of the halted core as they were at the moment of the halt. It preserves the course of events only if nothing that the studied behavior depends on changes during the halt: every timer it depends on is frozen, no other core runs, and no peer is waiting with its own timer.
Limits and common mistakes
Fine resolution is not freedom from perturbation. A timestamp with nanosecond resolution can come from a run that the measuring itself altered.
Removing the observer is also a change: the system without logging can behave differently again. Comparing observed and unobserved runs needs an outcome measured without the observer, such as a count kept by a peer or by external equipment.
Sampling, which records the state at intervals instead of at every event, costs less than tracing, but it misses short events between samples, and its results are estimates.
Hardware observers perturb too. Hardware tracing can drop events when its buffer overflows §4.4. An oscilloscope probe adds capacitance to the signal it measures, and toggling a spare output pin to mark an event costs at least one instruction.
A cost measured in one setting does not carry over to another. The same probe can cost a different amount in another build, because the code layout and the cache contents differ. A result reported without a statement of how it was observed cannot be checked for any of this.
Questions answered
Does halting the CPU preserve the state evolution being studied?
No, not in general: halting the CPU stops only the halted processor core, while everything that keeps its own time continues. A debugger halt preserves the core’s registers and memory as they were at the moment of the halt, an accurate snapshot of one instant. It does not preserve how the state would have evolved. During the halt, peers outside the chip keep running their timers: a phone connected over Bluetooth Low Energy (BLE) drops the link once the supervision timeout, the time it waits without hearing from the device, has passed, for example 4 seconds into a 30-second halt. Peripherals that the debug settings do not freeze also keep running, such as timers, radio hardware and the watchdog timer, which resets the processor unless software restarts it regularly; so do other processor cores. After the halt, the run continues from a state the uninterrupted system would not have reached.
Halting preserves the state evolution only if nothing the studied behavior depends on changes during the halt: every timer and peripheral it depends on is frozen, no other core runs, and no peer has its own timer running. That condition must be checked for the chip, its debug settings and the test setup; it cannot be assumed. Otherwise, use the halt for what it supports, a snapshot of memory at one moment, and study the timing with a less intrusive observer, such as hardware tracing or log records buffered in memory.
Related entries: Runtime instrumentation (§14.24), Trace versus sampling (§14.30), Instrumentation overhead (§11.32), Counter quantization (§11.35), Hardware-in-the-loop testing (§10.36), Supervision timeout (§7.8), Watchdog timer (§6.16).