Definition
Instrumentation overhead is the cost that code added for measurement imposes on the measured system: the processor time, memory and other resources used by timer readings, counters, log records and their buffers. Part of the added work may fall between the two timer readings and lengthen the measured interval directly. Added work anywhere can also change the conditions under which the measured code runs, such as the contents of caches (small fast memories), code addresses and the timing of other activity, and so change that code’s own duration.
The idea
A developer wants to know how long a function takes on a small device. The developer reads a cycle counter, a hardware register that counts processor clock cycles, before and after the call, and stores the difference in a log. Reading the counter takes cycles, storing the record takes cycles, and sending the log out over a serial line takes many more, some of them while the measured function runs. The number in the log is the function’s time plus part of this work, measured on a device that is busier than it would be without the measurement.
The simplest hope is that the overhead is a constant, measured once and subtracted. Sometimes it is. But the added cost can also vary with the workload, for example with contention, the competition of several tasks for one shared log. Worse, the measurement code can change the measured code’s own duration, for example by evicting its data from a cache or by moving its instructions to different addresses. These three situations, a constant cost, an additive cost that depends on the workload, and an interaction, need different treatment.
A cold thermometer put into a small cup of hot water is a fair picture: it cools the water it measures. The picture stops being accurate in one respect: the thermometer always errs in one direction, whereas instrumentation §14.24 can make code faster as well as slower, for example when moved code lands on a more favorable address.
How it works
Map the measurement work against the timing endpoints §18.1.6, the two events at which the timer is read. Note which work lies inside the interval (the end of the first reading, the start of the second, any record preparation between them) and which runs outside it but nearby, such as formatting a log message or the interrupts §6.11 (signals that make the processor pause its current work to run a handler) that drive a serial transmission.
Calibrate the direct cost in the same build and at the same place. Time an empty interval, two timer readings with nothing between them, many times, at the same point in the code and in the same state. Record the distribution of the results, not one number.
Test whether the cost is constant. Repeat the calibration §11.42 under each workload condition of interest, such as idle and loaded, small and large inputs, cold and warm caches. If the distributions agree, a constant model fits. If they differ, the overhead depends on the workload and must be calibrated for each condition.
Test whether the cost is additive. Add one, then two, dummy measurement points to the measured path, and check that each adds the calibrated cost. Growth in equal steps of that size supports an additive model; any other growth shows that the measurement points affect each other or the measured code.
Check against an uninstrumented reference. Time a whole batch of operations with one pair of readings around the batch, once with the per-operation measurement points active and once with them disabled at run time but still present, so that every instruction stays at the same address (a disabled point still costs one test of a flag). The difference between the two batch times, divided by , is the total overhead per operation, interaction included. If it matches the calibrated direct cost, subtraction is justified for the mean.
Compare within one build. Code placement changes times by itself (next paragraph), so differences between paths measured in the same build are more trustworthy than absolute times compared across builds.
Report the calibration, the model it supports and any remaining disagreement.
Code placement matters because a processor’s speed on the same instructions depends on where they lie in memory. Instructions are fetched from memory in lines of fixed size, such as 16 or 64 bytes. A loop that fits in one line can need fewer fetches per iteration than the same loop split across two lines, so the alignment §4.22 of the loop, whether its start lies at a multiple of the line size, can change its speed. Caches, and the branch predictors that guess which way each branch will go, choose storage slots from bits of the address, so moved code can begin to collide with other code in them. Adding measurement code anywhere in a program can shift the addresses of everything placed after it, so a function whose instructions are unchanged can become faster or slower. A 2009 study (Mytkowicz, Diwan, Hauswirth and Sweeney, “Producing Wrong Data Without Doing Anything Obviously Wrong!”) showed that changing only the order in which the linker §12.4 combined the object files, or the total size of the environment variables, the named text settings an operating system passes to every program, changed measured run times enough to change whether a compiler optimization §3.39 appeared to speed programs up or slow them down [479].
Worked example
Constant, then interacting. A function takes 100 cycles without instrumentation, and a pair of timer readings adds 20 cycles inside the interval, so the measured time is cycles. Subtracting the calibrated 20 gives cycles, the true value. Now the same function runs while other code logs heavily. The measurement record goes through a shared buffer protected by a lock, so the direct cost rises to 80 cycles, and the logging evicts the function’s data from the cache, which adds 30 cycles to the function itself. The measured time is cycles. Subtracting the idle calibration gives cycles, an overstatement by cycles. A calibration made under the same load removes the 80 but not the 30, giving cycles. An empty interval contains no function whose data could be evicted, so no empty-interval calibration can see those 30 cycles of interaction.
Code placement, in an invented and simplified model. A processor fetches instructions from flash memory §6.6 in 16-byte lines and keeps only the most recently fetched line in a buffer; each line fetch costs 1 extra cycle. A loop whose body fits in one line takes 6 cycles per iteration once that line is in the buffer, because the loop never leaves the line. In a second build, measurement code added to an earlier function shifts the loop by 8 bytes, so its body crosses a line boundary. Every iteration now fetches both lines, one evicting the other, and costs cycles. Ignoring the first fetch, 1000 iterations take cycles in the first build and cycles in the second, which is slower with identical instructions.
Suppose two paths share this loop and differ only by a check that costs 300 cycles. In the first build they take and 6000 cycles; in the second, and 8000 cycles. The absolute times changed between the builds, but the difference between the paths, 300 cycles, did not. A difference measured within one build stays comparable across builds as long as the code that moved is shared by both paths.
Stated precisely
Let be the duration of the measured code without instrumentation, and the duration measured with instrumentation. Let describe the workload and the system’s state during the measurement, such as the input size, the load and the cache contents. Write where is the direct cost, the measurement work that lies between the timer readings, and is the interaction, the change in the measured code’s own duration caused by the instrumentation, which can be negative. The three cases are:
constant: for every , and ;
additive, dependent on the workload: , but varies with ;
interacting: for some .
Subtracting a calibrated value estimates the mean of without bias for the workloads of interest exactly when for those workloads, where denotes the expected value (the probability-weighted average). Subtraction shifts every percentile §11.16 correctly only in the constant case; when varies, the spread of includes the spread of . An empty-interval calibration measures only and cannot see . Measurements made with the instrumentation in place cannot separate from ; that needs a reference taken without the per-operation measurement points, as in step 5 of How it works, with the code layout held fixed.
Limits and common mistakes
An empty-interval calibration measures only the part of the measurement work that lies between the readings. It misses work outside the interval and every interaction.
Instrumentation can change what the program does, not only how long it takes. Measurement code can make an operation miss a deadline, change the order in which threads run, or hide a race condition §9.2, a bug whose effect depends on the relative timing of concurrent activities. The instrumentation perturbation §18.1.7 entry treats such changes of path.
Compiler optimization interacts with measurement code. The compiler may move work across a timer reading unless the code prevents it, and measurement code can stop the compiler from optimizing the measured code as it otherwise would, for example by forcing a value to be stored in memory.
Absolute times compared across builds mix the change under study with changes in code placement. To measure the size of placement effects themselves, produce several layouts of one program, for example by changing the link order, and compare them.
A corrected time near zero, or below it, shows that the overhead varies or that the operation is too short for this timer.
Overhead is not only time. Buffers use memory, logs use bandwidth on the link that carries them, and all of it uses energy. A system that is short of memory can behave differently because of the buffer alone.
Questions answered
Is the overhead additive, constant, or interacting with the workload?
Instrumentation overhead can be constant, additive but dependent on the workload, or interacting with the workload, and which of these holds must be tested, not assumed. Constant: the measurement code adds the same cost to every measurement, so one calibrated number can be subtracted. Additive but dependent on the workload: the added cost changes with input size, contention or the number of measurement points, but the measured code’s own duration does not change, so subtraction needs a calibration for each workload. Interacting: the measurement code changes the measured code’s own duration, for example by evicting cached data, moving code to different addresses or delaying it past a deadline, and no subtraction removes that.
Three tests tell the cases apart. Repeat the empty-interval calibration, two timer readings with nothing between them, under each workload: equal results indicate a constant direct cost. Add dummy measurement points to the measured path: if each adds the calibrated cost, the overhead is additive. Time a batch of operations with the measurement points active and with them disabled at run time, keeping every code address unchanged: if the difference per operation equals the calibrated cost, there is no measurable interaction. Report which model the tests support; subtracting one constant is justified only when the overhead is constant.
Related entries: Measurement uncertainty (§11.33), Calibration (§11.42), Runtime instrumentation (§14.24), Trace versus sampling (§14.30), Instrumentation perturbation and observer effects (§18.1.7), Measurement perturbation (§13.30). Further reading: [479].