Definition
Termination and a time bound are two guarantees about the runs of a program. Termination means that every run stops after finitely many steps, with no limit on how many or how long. A time bound states a maximum duration, such as 940 microseconds, that no run exceeds on a stated machine under stated conditions. An instruction budget §2.32 gives termination and a maximum number of steps. Turning that step limit into a time bound needs a maximum duration for every step and for everything else that can delay the run.
The idea
A device must answer every incoming request within 2 milliseconds, because the sender gives up after that. To decide each answer, the device runs a guest program, code supplied from outside and run inside a virtual machine §2.3 (VM), with a budget of 1,000 instructions, so every run stops. One of those instructions calls a host function that reads a sensor over a slow bus, and the sensor takes 30 milliseconds to reply. The run still stops after at most 1,000 instructions: it terminates. It also answers 28 milliseconds too late. Counting instructions settled whether the run ends, not when.
The two guarantees are different kinds of property. Termination is a liveness property §1.12: something good eventually happens, and no finite observation refutes it, because a run that has not stopped yet may still stop. A time bound is a safety property §1.11: something bad never happens, here a run still going at the deadline, and one observed run past the bound refutes it. A step limit is like the length of a trip: it guarantees arrival if the vehicle keeps moving, but an arrival time also needs a minimum speed and a limit on stops. The comparison stops at one point: a trip’s length is fixed, while a program’s number of steps depends on its input, so a step limit is a maximum, not the actual count.
How it works
A time bound is built by bounding every source of time in a run separately and adding the bounds.
Guest steps. Bound the number of guest instructions a run can execute. An instruction budget of units, charged once per instruction, gives at most instructions.
Time per step. Bound the time the VM needs for its slowest instruction on this processor at this clock frequency. The bound includes the VM’s own work for each instruction, such as decoding it, charging the budget and checking memory bounds, and the slowest behavior of the hardware, such as waiting for slow memory.
Helper calls. A helper function §2.19 is a host function that the guest calls. Bound how many helper calls one run can make and how long the slowest helper takes, including any time it waits for hardware or for another task.
Host work around the run. Bound the host’s work before the guest starts, such as preparing the input, and after it stops, such as turning the result into a decision.
Interference. Bound the time that other work takes while the run is in progress: interrupts §6.11, hardware signals that make the processor run a handler at once; tasks of higher priority, which a preemptive scheduler runs first; and waits for locks that other tasks hold.
Add. The time bound is the number of steps times the time per step, plus the bounds from items 3, 4 and 5.
Each bound must be a proven maximum, not a measurement. A mean, or the longest of 10,000 test runs, describes those runs; the next run can take longer. The longest time a piece of code can take on a given processor is its worst-case execution time §9.33 (WCET); timing analysis computes a safe upper bound on it, and that bound is what the sum needs. The parts must also be kept apart: the dispatch of a call instruction belongs to the time per step and the helper’s body to the helper bound, so that no work is counted twice and none is left out.
Items 1 to 4 give the run’s execution time §9.32, the processor time spent on it. Adding interference gives its response time, the wall time §18.1.5 from start to finish. A requirement such as 2 milliseconds concerns the response time. If the run can also wait in a queue before it starts, that wait comes on top.
A host can also limit time directly: it reads a monotonic clock §9.35, one that never runs backward, at regular points in the run, or sets a timer interrupt, and stops the run when the time is used up. This needs no bound on each step, but it has assumptions of its own. The check must happen often enough, which fails inside a long helper that the VM cannot interrupt; stopping must be safe where the run is stopped, which fails while a helper holds a lock; and the run then ends with a timeout §9.34 instead of an answer, which needs a decision of its own. Such a limit is a timeout, a rule for when to give up, not a proof that the work finishes in time.
Worked example
Take a hypothetical processor running at 64 MHz (megahertz, millions of cycles per second). One cycle lasts microseconds, so 64 cycles take 1 microsecond. Assume the following bounds, each established by analysis of the code and the hardware.
Budget: one unit per instruction and , so at most guest instructions.
Slowest guest instruction, including the VM’s own work for it: 48 cycles, which is microseconds.
Helper calls: at most per run, each taking at most 1,600 cycles, which is microseconds.
Host work before and after the run: at most 640 cycles, which is microseconds.
Interference: one interrupt source, whose handler takes at most 40 microseconds and which fires at most once in any 500 microseconds; nothing else can delay the run.
Without interference a run takes at most microseconds. Interrupts make the run longer, and a longer run admits more interrupts, so the bound is found by repeating the calculation until it stops changing. A window of 860 microseconds contains at most interrupts, where means rounded up to a whole number. They add microseconds, which gives . A window of 940 microseconds also contains at most interrupts, so the value no longer changes. The response time is at most 940 microseconds, inside the 2-millisecond requirement.
Now let one helper wait for a sensor’s reply with no limit on the wait. The counts are unchanged, and every run still stops if the sensor eventually replies, but that helper’s 25-microsecond bound is gone, and with it the 940-microsecond bound. If the helper instead gives up after at most 1,000 microseconds, overhead included, and a run calls it at most once, the bound before interference is microseconds. That window holds interrupts, adding for 1,995, and a window of 1,995 also holds . The bound is 1,995 microseconds, inside the requirement by 5 microseconds, provided the run treats a read that gave up as a defined result.
Finally, return to the original helpers and let the processor run at 16 MHz to save power. Every part counted in cycles takes times as long, so the 860 microseconds before interference become microseconds, beyond 2 milliseconds before a single interrupt. The step count did not change; the assumption about time per step did.
Stated precisely
Let be the maximum number of guest instructions in one run, and the maximum time of one guest instruction, including the VM’s work to fetch, decode, charge and check it. Let be the maximum number of helper calls in one run, and the maximum time of one helper’s work after the VM has dispatched its call instruction. Let be the maximum time of the host’s work before and after the run, and the maximum delay that other work causes while the run is in progress. With all times in one unit, the response time of one run satisfies
When the interference depends on the length of the run, as with an interrupt that fires at most once every time units and takes at most each time, compute and then until . The final value bounds . If the values grow past the deadline, the run is not shown to meet it. This is the iteration of fixed-priority response-time analysis §19.4.7.
The inequality holds only if each symbol is a true maximum: covers every path, which holds when the instruction budget charges every path that can repeat; holds for this processor, this memory and this clock frequency; every helper returns within , any waiting included, and no run makes more than calls; covers all of the host’s work around the guest; every source of delay during the run has a known maximum rate and duration, all counted in ; and no work is counted in two terms or in none. Termination needs much less: a finite , and helpers that each return eventually, however long they take.
Limits and common mistakes
A step limit, however small, says nothing about time on its own. A budget of 10 instructions with one helper that can wait forever gives no time bound.
A time bound belongs to one machine. Moving the same program to another processor, to another clock frequency, or from the interpreter §2.13 to a just-in-time (JIT §2.14) compiler, which translates bytecode §2.4 into machine code before running it, changes and invalidates the number.
Testing cannot establish a bound. A test can show that a bound is violated; it cannot show that the bound holds.
A bound per run does not bound the total load. A run of at most 940 microseconds started 2,000 times per second needs up to microseconds of processor time per second, more than the 1,000,000 microseconds that a second contains.
In a preemptive system a low-priority run waits for as long as higher-priority work keeps the processor busy. Bounding that wait needs limits on the higher-priority work, not on the guest program.
Questions answered
What additional assumptions turn a step limit into a wall-clock §9.36 bound?
A step limit becomes a wall-clock bound only when every other source of time in the run also has a proven maximum. A step limit, such as an instruction budget, guarantees that a run of a guest program executes at most a fixed number of instructions; a wall-clock bound promises that the run finishes within a fixed time as measured by a real clock. The additional assumptions are these.
The step limit covers every path, including jumps back, calls and callbacks §14.22.
Each step has a maximum duration on the actual processor, memory and clock frequency, including the virtual machine’s own work for that step.
The number of helper calls, calls from the guest into host functions, is bounded, and each helper finishes within a maximum time that includes any waiting for hardware, locks or other tasks.
The host’s work before and after the run is bounded.
Interference is bounded: interrupts and higher-priority tasks have a known maximum rate and duration.
The terms are added without counting any work twice or leaving any out.
The bound is then the number of steps times the time per step, plus the number of helper calls times the time per helper, plus the host’s work, plus interference. For example, 1,000 instructions of at most 0.75 microseconds each, 4 helper calls of at most 25 microseconds each and 10 microseconds of host work give 860 microseconds; an interrupt handler §6.12 of at most 40 microseconds that fires at most once per 500 microseconds adds 80, for 940 microseconds. Measured averages and the longest observed run do not supply these maxima, and one helper that can wait without limit removes the bound.
Related entries: Instruction budget (fuel) (§2.32), Liveness property (§1.12), Safety property (§1.11), Response time versus execution time (§9.32), Worst-case execution time (WCET) (§9.33), Deadline versus timeout (§9.34), Fixed-priority response-time analysis (§19.4.7).