Section 9.33

Worst-case execution time (WCET)

Definition

The worst-case execution time (WCET) of a piece of code is the longest time that code can take to run on a given processor, over every input and every starting state of the hardware that the analysis admits. It counts only time the processor spends running that code, not time spent waiting or interrupted. The true maximum is rarely known exactly, so an analysis produces a WCET bound: a number proven to be no smaller than any admitted execution time §9.32. The largest measured time is not such a bound.

The idea

A motor controller must compute a new output within 1 millisecond of every sensor reading. Its control routine was measured 10,000 times and never took longer than 40 microseconds. One millisecond is 1000 microseconds, so the deadline looks safe by a factor of 1000/40=251000 / 40 = 25. The measurements, however, show only the paths and hardware states that the tests happened to produce. A sensor value the tests never produced may take a longer branch, a loop may run its maximum number of times, the data may be missing from the cache (a small, fast memory near the processor that holds recently used data), or the processor may be running at a lower clock frequency. Any of these makes the routine slower than everything that was measured.

A WCET argument replaces “we never saw it take longer” with “it cannot take longer”. It lists every source of variation, bounds each one, and combines the bounds along the most expensive path through the code. The measured maximum §18.5.9 remains useful as a check. The true WCET is at least as large as any time actually observed, so the measured maximum is a lower bound on the WCET, and a claimed WCET bound smaller than some measurement is certainly wrong.

How it works

Execution time varies for four kinds of reasons, and an analysis must bound each:

There are three ways to obtain a bound.

  1. Static analysis §3.1 computes a bound from the program and a model of the processor, without running the code. Flow analysis builds the control-flow graph §3.13 (the straight-line pieces of code and the jumps between them), finds the loop bounds and rules out impossible paths. Processor-behavior analysis bounds the cycles of each basic block §3.14 (a straight-line instruction sequence entered only at its first instruction and left only at its last). It often uses abstract interpretation §3.4 (a method that computes facts true of every execution at once) to classify each memory access as always a cache hit, always a miss, or unknown. Path analysis then finds the most expensive path through the graph. The usual method, the implicit path enumeration technique (IPET), states this as an integer linear program, an optimization problem whose unknowns are whole numbers and whose constraints are linear. The unknowns are how many times each basic block runs; the program maximizes the total cycles, subject to the structure of the graph and the loop bounds.

  2. Measurement runs the code many times on the real hardware and records the largest time, the high-water mark, often with a safety margin added. It finds real costs that a wrong processor model would miss, but it proves nothing about inputs and states that the tests did not produce.

  3. Hybrid analysis measures the time of short code pieces on the hardware and combines those measurements with a static analysis of the paths.

Interrupts §6.11 and preemption are usually kept out of the WCET. The WCET assumes the code runs alone from start to finish, and the schedulability §9.37 analysis of the whole system adds the time taken by interrupt handlers §6.12 and higher-priority work. An interrupt still leaves a trace inside the WCET’s scope: the handler can evict the interrupted code’s data from the cache, so the code runs slower after it resumes. That extra cost, called cache-related preemption delay, must be counted on one side or the other.

Worked example

A routine has the following structure, with cycle costs taken from a hypothetical processor model. Setup code costs 20 cycles. A loop runs between 0 and 8 times at 15 cycles per iteration; the bound of 8 is proved from the code. A branch follows whose common side costs 30 cycles and whose rare side, taken for only one input value, costs 90 cycles. Finally the routine calls a helper whose own WCET bound is 50 cycles. The worst path takes the maximum at every point: 20+8×15+90+50=20+120+90+50=28020 + 8 \times 15 + 90 + 50 = 20 + 120 + 90 + 50 = 280 cycles.

The tests never produced the rare input and never ran the loop more than 5 times. The largest measured cost is therefore 20+5×15+30+50=20+75+30+50=17520 + 5 \times 15 + 30 + 50 = 20 + 75 + 30 + 50 = 175 cycles. The measured maximum falls 280−175=105280 - 175 = 105 cycles short of the bound, which is 105/280=37.5%105 / 280 = 37.5\% of the bound.

The processor may run at 64,000,000 or at 16,000,000 cycles per second. At the fast clock, 280 cycles take 280/64,000,000280 / 64{,}000{,}000 s =4.375= 4.375 microseconds. At the slow clock they take 280/16,000,000280 / 16{,}000{,}000 s =17.5= 17.5 microseconds. If the routine may run at either frequency and costs the same number of cycles at both, the time bound is 17.5 microseconds. On many microcontrollers §6.1 the cycle count is not the same: flash memory needs more wait states at the higher frequency. The cycle bound must then be computed separately for each permitted frequency and converted with that frequency.

Stated precisely

Let EE be the set of executions that the analysis admits. One execution is fixed by the input, the initial hardware state (cache contents, pipeline state), the clock frequency and the behavior of every called function. Let T(e)T(e) be the execution time of execution ee, counted under the stated convention, for example without interrupts. The true WCET is the largest of these times: WCET=maxe∈ET(e).\mathrm{WCET} = \max_{e \in E} T(e). A number BB is a safe WCET bound when T(e)≤BT(e) \le B for every ee in EE, that is, when B≥WCETB \ge \mathrm{WCET}. The bound is tight when BB is close to the WCET; the difference B−WCETB - \mathrm{WCET} is the pessimism of the analysis.

Let MM be the subset of EE that was measured. Then maxe∈MT(e)≤WCET,\max_{e \in M} T(e) \le \mathrm{WCET}, so a measured maximum is a lower bound on the WCET. It is an upper bound only if MM is proven to contain a worst execution.

If the cycle count does not depend on the frequency, let KK be a bound on the cycles of every admitted execution and fminf_{\min} the lowest admitted clock frequency in cycles per second; then K/fminK / f_{\min} is a bound in seconds. If the cycle count depends on the frequency, compute a cycle bound KfK_f for each admitted frequency ff and take the largest value of Kf/fK_f / f.

The bound holds only under these assumptions, each of which must be stated:

  1. every loop bound and recursion depth used is correct;

  2. the processor model is safe: in every hardware state the model admits, it never assigns an instruction fewer cycles than the real processor can take;

  3. the bound of every called function is safe for every argument that function can receive;

  4. the admitted inputs, frequencies and starting states include everything the real system can produce;

  5. the effects excluded by convention, such as interrupts and cache-related preemption delay, are accounted for elsewhere.

Limits and common mistakes

A WCET bound is not a bound on response time. It excludes queueing, preemption, interrupts and waits for locks or devices. The Schedulability entry adds those.

A wait with no limit makes the WCET infinite. Code that polls a device until the device reports ready, with no limit on the number of polls, has no finite WCET however short each poll is.

An enforcement limit is not a WCET. An instruction budget §2.32 that aborts code after a fixed number of instructions guarantees that the code stops, not that it completes its work; an aborted run did not do its job.

A cache miss is not always the worst case. On processors with complex pipelines, such as those that execute instructions out of order, a locally faster event such as a cache hit can change the order of later events so that the whole execution takes longer. Such timing anomalies mean that charging every unknown access as a miss is safe only on processors for which that has been shown.

On a multicore processor, another core can delay this core’s memory accesses through a shared bus or a shared cache. A bound computed for one core in isolation is not safe there.

A static bound can be far larger than any real execution time, because the analysis assumes the worst for everything it cannot prove. Pessimism wastes capacity but does not threaten safety; an optimistic bound does.

A bound belongs to one build and one configuration. A new compiler version, different optimization settings, a different memory layout, or a configuration that changes the clock frequency or the flash wait states requires a new analysis.

Questions answered

Does the analysis include helpers, cache state, interrupts, and clock changes?

A sound analysis must include each of the four, or exclude it explicitly and account for it somewhere else; a worst-case execution time (WCET) bound is a number proven to be at least the longest time some code can run, and every effect it silently leaves out can make it too small. Helpers, meaning functions supplied by the surrounding system that the code calls, are included by adding each helper’s own proven bound for the arguments it can receive; a helper that can wait without limit leaves no finite bound. Cache state is included by assuming an unknown cache at the start, or by proving which accesses hit, and by charging every access whose outcome is not proven at its slow cost, on processors where a miss is known to be the slow case.

Interrupts are normally excluded from the WCET itself, which assumes the code runs alone; the schedulability analysis of the whole system then adds the time of the interrupt handlers and the extra cache misses they cause in the interrupted code. Clock changes are included by converting cycles to time at the lowest clock frequency the system can use while the code runs, with a separate cycle bound for each frequency when flash wait states or other costs depend on it. A correct calculation over an incomplete set of cases proves a bound only for those cases.

Related entries: Termination versus time bound (§2.33), Microarchitecture (§4.39), Schedulability (§9.37), Response time versus execution time (§9.32), A measured maximum versus worst-case execution time (§18.5.9), A helper can invalidate an instruction-time bound (§17.4), Static analysis (§3.1).