Definition
Timing endpoints are the two events at which a timer is started and stopped. They decide exactly which work a measured time contains. The stretch of time between them is called the timing window. An inclusive window covers a piece of work together with every inner step it contains, such as the functions it calls. An exclusive window covers the same piece of work with a separately timed inner step taken out. The one difference that matters: an inclusive time contains the inner step, and an exclusive time does not.
The idea
Two numbers can both be called the time of one operation and still differ by a factor of ten, because their timers were started and stopped at different places. A figure that plots a time cannot be read until the two endpoints are known.
One situation shows the problem. A callback §14.22, a function that a system calls when a particular event happens, receives a request. The callback asks a policy check, called the evaluation, for a verdict. The verdict is ALLOW, meaning the request may proceed, or DENY, meaning the request is refused. The callback then acts on the verdict and returns. One timer is placed around the evaluation alone. A second timer is placed around the whole callback. The first timer reports that DENY is faster than ALLOW. The second timer reports that DENY is slower. Both reports are correct.
The picture is a small box inside a large box. The large box is the callback and the small box is the evaluation. The rest of the large box, the work before and after the evaluation, is free to grow while the small box shrinks. Profilers, the tools that measure where a program spends its time, use the same two words. The inclusive cost of a function includes the cost of everything the function calls. Its exclusive cost, also called self cost, is the cost of the function’s own code.
How it works
Record four timestamps with one clock during one call of the callback.
The order is . Three times follow from the four readings.
The evaluation time is .
The inclusive callback time is .
The exclusive callback time is . It equals the time before the evaluation, , plus the time after it, .
To read a figure that plots times, take these steps.
For every plotted time, find the event that starts the timer and the event that stops it. A name such as “callback time” does not settle this. The positions of the two timer readings in the code do.
Decide whether one window lies inside another. If it does, the outer time is inclusive of the inner time.
Compare a time only with a time that has the same two endpoints.
When a condition changes an outer time, split the change in two: the change in equals the change in plus the change in .
Worked example
All values in this example are invented, and all are in microseconds. For a request that is allowed, the four timestamps to are 0, 1, 9 and 10.
Evaluation: .
Inclusive callback: .
Exclusive callback: , made up of before the evaluation and after it.
For a request that is denied, the four timestamps are 0, 1, 4 and 15.
Evaluation: .
Inclusive callback: .
Exclusive callback: , made up of before the evaluation and after it.
From ALLOW to DENY the evaluation time changes by , the exclusive callback time changes by , and the inclusive callback time changes by . The check is . The evaluation became 5 microseconds shorter and the callback became 5 microseconds longer, with no contradiction.
One story fits these invented numbers: the evaluation stopped at the first rule that refused the request, and the callback then spent 10 more microseconds acting on the refusal, for example building a rejection message and recording the reason. The numbers show in which window the time went. They do not prove the story.
Stated precisely
For one call, with all four timestamps taken from the same clock and , The identity holds exactly for every single call, and therefore for sums and means taken over one and the same set of calls: the mean of equals the mean of the time before, plus the mean of , plus the mean of the time after.
The identity does not hold for medians §11.13 or maxima. A median is the middle value of a sorted set. Take three calls with exclusive times of 1, 2 and 9 and evaluation times of 9, 2 and 1. Their inclusive times are 10, 4 and 10. The median of is 2, the median of is 2, and the median of is 10, not . The largest values do not add either: the largest is 9, the largest is 9, and the largest is 10, not . The means do add: the mean of is 4, the mean of is 4, and the mean of is 8.
A duration is the later reading minus the earlier reading. A window from counter reading 3 to counter reading 7 lasts steps of the counter, not 5. The subtraction treats the window as a half-open interval §14.12, an interval that contains its first point and not its last: the step that begins at reading 3 is inside, and the step that begins at reading 7 is outside.
Limits and common mistakes
Work can lie outside both windows. If the callback only puts the refusal in a queue and another task carries it out later, neither timer sees that work.
The timer readings take time themselves. The two readings of the inner timer happen inside the outer window, so the outer time contains their cost.
The four timestamps must come from one clock. Readings from two clocks cannot be subtracted until the offset and the difference in rate between the two clocks are known.
With several evaluations in one callback, the exclusive time is minus the sum of all the evaluation times, and a figure must say whether it plots one evaluation or the sum.
The start of the outer window is a choice. A window that starts when the event arrives contains the time the event waited before the callback was entered. A window that starts at the first instruction of the callback does not.
The words inclusive and exclusive are also used for whether the first and the last element of a range belong to the range. In this entry they refer only to nested work.
Questions answered
How can DENY shorten an evaluation but lengthen the callback?
A DENY verdict, the decision to refuse a request, can shorten the evaluation and lengthen the callback because the evaluation time is only one part of the callback time, and a refusal can shorten that part while lengthening the rest. Picture a callback, a function that a system calls when a request arrives. The callback asks a policy check, the evaluation, for a verdict, which is DENY or ALLOW (the request may proceed), and then acts on the verdict. The evaluation time runs from the start to the end of the check. The callback time runs from the entry to the return of the whole function, so it contains the evaluation time plus the callback’s own work before and after the check.
A DENY verdict can be reached early, at the first rule that refuses the request, and that shortens the evaluation. Acting on a refusal can take more work than acting on an acceptance, and that lengthens the part after the evaluation. With invented timestamps in microseconds, an allowed request has an evaluation of 8 inside a callback of 10, and a denied request has an evaluation of 3 inside a callback of 15. The evaluation fell by 5, the callback’s own work rose from 2 to 12, and the callback rose by . The two results do not conflict. They describe different windows.
Related entries: Median (§11.13), Wall clock and clock synchronization (§9.36), Measurement perturbation (§13.30), Instrumentation perturbation and observer effects (§18.1.7), Wall time, CPU time, and instruction count (§18.1.5), Half-open interval (§14.12), Callback (§14.22), Clock domain (§13.29).