Section 18.1.7

Instrumentation perturbation and observer effects

Definition

Instrumentation perturbation is the change in the behavior of a program that is caused by the code added to measure it. That added code is called instrumentation §14.24: timer readings, counters and code that writes log messages. An observer effect is the general name for any case in which measuring a system changes the system. Instrumentation perturbation has two parts. Overhead adds time to what is measured. A changed path makes the program do something other than what it would have done unmeasured.

Where it fits

Main entry: Measurement perturbation (§13.30).

The entry on measurement perturbation covers every kind of observer, including a debugging tool that stops the processor. This entry covers one tactic for timing code: measure an empty timer pair, which is two timer readings with no work between them, and subtract it. The tactic rests on an additive model. Let MM be the measured time, TT the time the operation takes when it is not measured, and OO the overhead, the time that the timer readings add. The model says M=T+OM = T + O, with the same OO in every measurement.

The empty pair measures exactly this OO. A timer reading takes time, and the value it returns is captured at one instant during the reading. Every timed stretch of code, from one timer reading to the next, contains the last part of its first reading and the first part of its second reading. The empty pair contains those two parts and nothing else.

The model fails when the instrumentation changes the path. In an invented case, a device may transmit only at fixed instants 30,000 microseconds apart, and the next instant comes 400 microseconds after an operation starts. Unmeasured, the operation finishes after 395 microseconds and its result is transmitted at 400. With 10 microseconds of instrumentation the operation finishes at 405, misses the instant, and its result is transmitted at 30,400. Subtracting 10 microseconds gives 30,390, not 400.

Limits and common mistakes

Questions answered

Can we subtract an empty timer measurement?

An empty timer measurement can be subtracted, but only when the timer readings add the same cost to every measurement and change nothing else. An empty timer measurement is the time between two timer readings with no work between them. It captures the cost that the two readings add to any timed stretch. If an operation is measured at 10 microseconds and the empty measurement is 1 microsecond, the operation alone took 10−1=910 - 1 = 9 microseconds.

The subtraction gives a wrong answer when the code added for measuring changes what the program does. If the added work makes the program miss a transmission instant, lose its turn on the processor or take a different branch, the measured time contains a delay that no constant removes. Before subtracting, check that the empty measurement was taken at the same place and in the same state of the program as the real one, that it is stable when repeated, and that the operation is much longer than the empty measurement.

Related entries: Instrumentation overhead (§11.32), Runtime instrumentation (§14.24), Calibration (§11.42), Timing endpoints and inclusive versus exclusive windows (§18.1.6).