Definition
Wall time, CPU time and instruction count are three measures of what one run of a program costs. Wall time is the real time that passes between the start and the end of the run, as a clock on the wall would show it. CPU time is the time during which a processor was executing the run. Instruction count is the number of instructions the run executed; it measures work and has no unit of time. Time during which no processor executes the run adds to wall time only.
The idea
A user who presses a button cares how long the answer takes. An engineer who pays for processors cares how much processor time the answer used. A reviewer who must rule out a program that runs forever cares how much work the program may do. Mixing the three measures produces a false argument: the program may execute at most 100 instructions, therefore it answers quickly.
The argument comes up with a virtual machine §2.3 (VM), a machine that exists only as software and runs another program, called the guest program, by carrying out that program’s instructions. A virtual machine that carries them out one at a time can count them, and can stop a guest program that exceeds a fixed number of instructions. That limit is called an instruction budget §2.32. It guarantees that the guest program stops. It does not say when.
A clerk with a checklist keeps the three measures apart. The instruction count is the number of items on the checklist. CPU time is the time the clerk spends working through the items. Wall time runs from the handing over of the checklist to the return of the result, and it includes every minute the clerk spent serving someone more urgent or waiting for a colleague. The picture is inaccurate in one respect: checklist items look alike, but instructions differ widely in cost.
How it works
Each measure is read differently.
Wall time, also called wall-clock §9.36 time or elapsed real time, is the difference between two readings of a clock that keeps running whatever the processor does, taken at the start and at the end of the run.
CPU time is kept by the operating system, which adds up the periods during which a processor was assigned to the run. Periods in which the run waits for its turn, sleeps or waits for input are not added.
Instruction count is kept by whatever carries out the instructions: a virtual machine, for example, can advance a counter once for each instruction of the guest program.
Two relations connect the three measures.
For a run that uses one processor at a time, wall time equals CPU time plus the time during which the run was not executing. The run is not executing in three situations: the scheduler, the part of the operating system that chooses which task the processor runs next, has given the processor to another task; the processor is serving an interrupt §6.11, an urgent event that takes over the processor; or the run itself is waiting for something.
CPU time equals the sum of the times of the executed instructions, and those times are not equal. On an Arm Cortex-M4 §6.3 processor, for example, an addition takes 1 cycle of the processor’s clock and a division takes between 2 and 12 cycles, depending on the numbers divided. An instruction that calls a helper function §2.19, a routine of the surrounding system that the guest program is allowed to call, takes as long as that routine takes.
There is no fixed exchange rate between the three measures.
The programming language Python §12.23 offers both clocks. Its documentation says that time.perf_counter(), which measures wall time, “does include time elapsed during sleep”, and that time.process_time(), which measures CPU time, “does not include time elapsed during sleep”. A program that sleeps for 1 second measures about 1 second with the first clock and almost nothing with the second.
Worked example
All numbers here are invented. A virtual machine runs a guest program under an instruction budget of instructions. One run executes 80 instructions, and two of the 80 call helper functions.
Instruction count: 80, within the budget of 100.
Each instruction takes 1 microsecond of processor time, not counting time inside a helper function: microseconds. Each helper call takes 10 microseconds: microseconds. CPU time: microseconds.
During the run an interrupt takes the processor for 25 microseconds. Wall time: microseconds.
Repeat the same run, with no interrupt, while the scheduler gives the processor to a more urgent task for 2 milliseconds, which is 2000 microseconds, in the middle of the run. The instruction count is still 80 and the CPU time is still 100 microseconds. The wall time is microseconds. The amount of work did not change. The wall time became times as long.
Stated precisely
Let be the wall time of one run, its CPU time, and the total time between the start and the end of the run during which no processor was executing it. For a run that executes on one processor at a time,
A limit on the instruction count becomes a limit on wall time only together with three assumptions.
Bounded instruction time: every instruction takes at most of processor time, not counting time inside a helper function.
Bounded helper work: one run makes at most helper calls, and each call takes at most of processor time.
Bounded time away: is at most . This covers other tasks, interrupts and every wait inside the run, including a wait inside a helper function.
If all three hold, then Both bounds assume that the run spends processor time only on instructions and inside helper functions. Processor time that the virtual machine spends before the first instruction or after the last needs its own bound, added to both right-hand sides.
A helper call is counted once in , for the instruction that makes the call, and once in , for the work of the routine. Each of , , and must hold for every run the system permits, not only for the runs that were observed. If one of them has no finite value, gives no limit on wall time.
Limits and common mistakes
A measured maximum §18.5.9 is not a limit. If the longest of 10,000 observed runs took 150 microseconds, that number describes those 10,000 runs. The worst-case execution time §9.33 is the longest processor time that any permitted input and starting state of the machine can cause. A limit guaranteed to lie at or above it needs an argument that covers the runs nobody observed.
CPU time can be larger than wall time. A program that keeps 4 processors busy for 1 second uses 4 seconds of CPU time in 1 second of wall time.
The work inside a helper function is not made of instructions of the guest program. The instruction counter does not see that work unless the virtual machine is built to charge for it.
A CPU time that an operating system reports follows the accounting rules of that system. The Linux operating system, for example, can add time spent serving an interrupt to the CPU time of the task that the interrupt displaced.
Questions answered
Does a limit of VM instructions bound latency?
A limit of instructions on a program that a virtual machine (VM) runs does not by itself bound latency. It bounds only the number of instructions that one run executes. Latency is wall time: the real time from the event that starts the run to the result. Three things separate an instruction count from a latency, and the limit controls none of them: how long each instruction takes; how long the helper functions take, which are routines of the surrounding system that the program calls; and how long the run is kept from executing by other tasks, by interrupts or by its own waiting. A program can stay inside its instruction limit while one helper call waits without end, or while the operating system keeps the processor on other work.
The limit does give a latency bound once those three things are bounded too. If every instruction takes at most , one run makes at most helper calls of at most each, and the run is kept from executing for at most in total, then the latency is at most . With the invented values , microsecond, , microseconds and microseconds, that is microseconds. A largest value seen in testing does not establish , , or .
Related entries: Resource cap versus resource reservation (§13.22), Prospective exclusion rule (§13.15), Confirmation window (§8.24), Instruction budget (fuel) (§2.32), Termination versus time bound (§2.33), Response time versus execution time (§9.32), Worst-case execution time (WCET) (§9.33), Timing endpoints and inclusive versus exclusive windows (§18.1.6), A measured maximum versus worst-case execution time (§18.5.9). Further reading: [5, 33].