Section 18.1.6

Timing endpoints and inclusive versus exclusive windows

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 t0≤t1≤t2≤t3t_0 \le t_1 \le t_2 \le t_3. Three times follow from the four readings.

To read a figure that plots times, take these steps.

  1. 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.

  2. Decide whether one window lies inside another. If it does, the outer time is inclusive of the inner time.

  3. Compare a time only with a time that has the same two endpoints.

  4. When a condition changes an outer time, split the change in two: the change in HH equals the change in EE plus the change in XX.

Worked example

All values in this example are invented, and all are in microseconds. For a request that is allowed, the four timestamps t0t_0 to t3t_3 are 0, 1, 9 and 10.

For a request that is denied, the four timestamps are 0, 1, 4 and 15.

From ALLOW to DENY the evaluation time changes by 3−8=−53 - 8 = -5, the exclusive callback time changes by 12−2=+1012 - 2 = +10, and the inclusive callback time changes by 15−10=+515 - 10 = +5. The check is −5+10=+5-5 + 10 = +5. 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 t0≤t1≤t2≤t3t_0 \le t_1 \le t_2 \le t_3, H=(t1−t0)+E+(t3−t2).H = (t_1 - t_0) + E + (t_3 - t_2). 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 HH equals the mean of the time before, plus the mean of EE, 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 XX of 1, 2 and 9 and evaluation times EE of 9, 2 and 1. Their inclusive times H=X+EH = X + E are 10, 4 and 10. The median of XX is 2, the median of EE is 2, and the median of HH is 10, not 2+2=42 + 2 = 4. The largest values do not add either: the largest XX is 9, the largest EE is 9, and the largest HH is 10, not 9+9=189 + 9 = 18. The means do add: the mean of XX is 4, the mean of EE is 4, and the mean of HH is 8.

A duration is the later reading minus the earlier reading. A window from counter reading 3 to counter reading 7 lasts 7−3=47 - 3 = 4 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

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 −5+10=5-5 + 10 = 5. 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).