Definition
Bounds for sums and maxima are upper limits on a combined quantity, such as the total time of an operation made of phases, derived from upper limits on its parts. If every execution that can occur has phase times and , and the total is exactly , then , even if the phases depend on each other. If two parts run at the same time and the total waits for both, . A part whose limit holds only with a stated probability passes that uncertainty on to the total.
The idea
An operation runs in three phases: it parses a message, checks it and acts on it. A test of 1,000 runs records the largest time of each phase, 40, 25 and 60 microseconds, which add up to 125 microseconds. It is tempting to call that the worst-case execution time §9.33 (WCET), the longest time the operation can take. Two questions decide whether that is right. Are the three numbers limits that no execution can exceed? And is the total exactly the sum of the phases, with no waiting between them and no time counted twice?
A journey is a fair picture. If the walk to the station takes at most 10 minutes, the train at most 30 and the walk at the far end at most 8, the journey takes at most minutes, however the legs relate to each other. The wait on the platform is a fourth leg that the sum must include, and the longest walk recorded last month is not the longest walk possible. The picture has no counterpart for parts that run at the same time.
Adding true limits is safe but can be pessimistic, because the worst cases of the parts may never happen in the same execution; a long parse might always be followed by a short check. The worst total that can actually occur then lies between the largest total observed and the sum of the limits.
How it works
Combining limits takes four steps.
Write the timing model. Express the total as a sum, or a maximum, of named parts, and list every other contribution: waiting for a shared resource, interrupts §6.11 (hardware signals that suspend the running code), the time between the end of one phase and the start of the next, and the time spent reading the timer. A contribution left out makes a sum too small. A contribution counted twice, such as two overlapping phases both added in full, makes a sum too large, which is safe for an upper limit but wasteful.
Classify each number. A part’s number is a proven bound, which no execution that can occur exceeds; a probabilistic limit, which an execution exceeds with at most a stated probability; an observed maximum, the largest value in a sample; or a summary such as a mean or a median §11.13.
Combine by kind. Proven bounds add to a proven bound. Probabilistic limits add to a limit exceeded with at most the sum of their probabilities, by the union bound §15.42: the probability that at least one of several events happens is at most the sum of their probabilities. Means add exactly, whatever the dependence. Observed maxima, medians and percentiles §11.16 do not add to a bound, a median or a percentile of the total.
State the kind of the result and of each number that went into it.
A part can also be uncertain because it is measured. If each of two phase durations is known only to within 1 microsecond, their sum is known only to within 2 microseconds; a guaranteed limit adds the largest possible errors. The measurement uncertainty §11.33 entry combines standard uncertainties, the typical sizes of independent errors, as the square root of the sum of their squares. That rule describes typical error, so it does not belong in a guaranteed limit.
A limit estimated from data holds only with a confidence level, the long-run fraction of experiments in which the estimating rule gives a valid limit. Two limits estimated at 97.5% confidence each fail in at most 2.5% of experiments, so by the union bound both are valid together in at least . Only then does their sum carry 95% confidence.
Worked example
Three runs of a two-phase operation give phase times of , and , in milliseconds (ms). The totals are 4, 6 and 4 ms. The observed maxima, 3 ms for and 5 ms for , sum to 8 ms, while the largest observed total is 6 ms. A sum of observed maxima is never below the largest observed total, because each run’s total is at most . That makes 8 ms look safe, but it is not a limit: a fourth run with totals 9 ms, because 3 ms was never a limit on .
Suppose instead that an analysis proves ms and ms for every execution that can occur, and that exactly. Then ms, and the fourth run’s 9 ms obeys it. The worst total that can occur lies between 9 ms, the largest observed, and 10 ms, the proven limit.
Suppose next that is known to exceed 3 ms in at most 1% of executions and to exceed 5 ms in at most 1%. A total above ms needs or , since otherwise . By the union bound, exceeds 8 ms in at most of executions, whatever the dependence between and . To hold the total to 1%, use the limits that each phase exceeds in at most 0.5% of executions.
Percentiles do not add. Let each phase take 1 ms normally and 11 ms in 0.9% of executions, independently of the other. The 99th percentile of a phase, the smallest value that at least 99% of executions do not exceed, is 1 ms, because only 0.9% exceed 1 ms; the two percentiles sum to 2 ms. The total exceeds 2 ms whenever either phase is slow, with probability , about 1.79%. That is more than 1%, so the total’s 99th percentile is 12 ms, not 2 ms. The union bound still holds: 1.79% is at most .
Stated precisely
Let be the component times of one execution and its total time. Assume a complete timing model: for every execution that can occur, with equality when the components follow one another without gaps or overlaps.
Proven bounds. If for every execution that can occur and every , then . If instead , then . Neither statement assumes independence.
Probabilistic limits. Let denote probability over an execution drawn at random from a stated population of executions. If for each , then , because can exceed the sum only if some exceeds , and the union bound limits the probability of that.
Estimated limits. If each comes from a rule that, over repetitions of the experiment, fails to give the property of item 1 or 2 with probability at most , all limits hold together with probability at least , and so does the conclusion for .
Means. If , then , where denotes the long-run mean, whatever the dependence.
For observed values only: in any sample of runs, , where and are the phase times of run . This compares numbers inside the sample and says nothing about runs outside it.
Limits and common mistakes
A sum of observed maxima is neither an upper nor a lower limit on the worst total that can occur. It falls below that worst total when an unobserved execution is longer, as the run showed. It exceeds it when the phase maxima never coincide: if only the runs , and ms could occur, the worst total would be 6 ms, not 8 ms.
Proven bounds add only if they were proven for the same processor, the same settings and the same treatment of interference. A phase bound that assumes interrupts are disabled does not add to a bound for a phase during which interrupts may arrive.
A sum of proven bounds can lie far above the worst total that can occur. A tighter limit needs an analysis of the whole operation that knows which phase behaviors can happen together.
Gaps make a sum too small and overlaps make it too large. An overlap is safe but wasteful in an upper limit and an error in an estimate of the typical total.
A confidence interval §11.18 for the mean time of a phase locates the long-run mean. It is not a limit on single executions, many of which can lie above its upper end.
Medians and percentiles of the parts do not add to the median or percentile of the total, in either direction: the sum of the parts’ 99th percentiles can lie below the total’s 99th percentile, as in the example, or above it.
Questions answered
Can I sum the largest measured phase times to obtain WCET?
No. Summing the largest measured phase times does not give a worst-case execution time (WCET) bound, an upper limit on the total time valid for every execution that can occur. Each largest measured time is only the longest that phase took in the runs made; a later run can take longer, so it is a lower limit on that phase’s worst case, not an upper one. Adding numbers that are not limits does not produce a limit. In runs with phase times , and milliseconds (ms) the phase maxima sum to 8 ms, yet a run with ms totals 9 ms. The same sum can also overstate the worst total, when the phase maxima never occur in the same execution.
A sum of phase numbers is a valid WCET bound under three conditions. Each phase number is a proven bound for every execution that can occur, for example from a static analysis §3.1. All the bounds rest on the same processor, settings and treatment of interference. And the phases account for the whole time, including waiting, interrupts and the time between phases; overlapping phases make the sum larger than needed but not wrong. If the phase limits are probabilistic instead, each exceeded in at most a stated fraction of executions, their sum is exceeded in at most the sum of those fractions, by the union bound: a probabilistic limit, not a WCET bound.
Related entries: Worst-case execution time (WCET) (§9.33), Peak versus retained memory (§11.37), Measurement uncertainty (§11.33), Union bound (§15.42), A measured maximum versus worst-case execution time (§18.5.9), Quantile and percentile (§11.16).