Section 18.1.5

Wall time, CPU time, and instruction count

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.

Two relations connect the three measures.

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

  2. 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 S=100S = 100 instructions. One run executes 80 instructions, and two of the 80 call helper functions.

  1. Instruction count: 80, within the budget of 100.

  2. Each instruction takes 1 microsecond of processor time, not counting time inside a helper function: 80×1=8080 \times 1 = 80 microseconds. Each helper call takes 10 microseconds: 2×10=202 \times 10 = 20 microseconds. CPU time: 80+20=10080 + 20 = 100 microseconds.

  3. During the run an interrupt takes the processor for 25 microseconds. Wall time: 100+25=125100 + 25 = 125 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 100+2000=2100100 + 2000 = 2100 microseconds. The amount of work did not change. The wall time became 2100/125=16.82100 / 125 = 16.8 times as long.

Stated precisely

Let TwallT_{\mathrm{wall}} be the wall time of one run, TcpuT_{\mathrm{cpu}} its CPU time, and TawayT_{\mathrm{away}} 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, Twall=Tcpu+Taway.T_{\mathrm{wall}} = T_{\mathrm{cpu}} + T_{\mathrm{away}}.

A limit SS on the instruction count becomes a limit on wall time only together with three assumptions.

  1. Bounded instruction time: every instruction takes at most cc of processor time, not counting time inside a helper function.

  2. Bounded helper work: one run makes at most hh helper calls, and each call takes at most dd of processor time.

  3. Bounded time away: TawayT_{\mathrm{away}} is at most II. This covers other tasks, interrupts and every wait inside the run, including a wait inside a helper function.

If all three hold, then Tcpu≤Sc+hdandTwall≤Sc+hd+I.T_{\mathrm{cpu}} \le S c + h d \qquad \text{and} \qquad T_{\mathrm{wall}} \le S c + h d + I. 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 SS, for the instruction that makes the call, and once in hh, for the work of the routine. Each of cc, hh, dd and II must hold for every run the system permits, not only for the runs that were observed. If one of them has no finite value, SS gives no limit on wall time.

Limits and common mistakes

Questions answered

Does a limit of SS VM instructions bound latency?

A limit of SS 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 cc, one run makes at most hh helper calls of at most dd each, and the run is kept from executing for at most II in total, then the latency is at most Sc+hd+IS c + h d + I. With the invented values S=100S = 100, c=1c = 1 microsecond, h=2h = 2, d=10d = 10 microseconds and I=30I = 30 microseconds, that is 100+20+30=150100 + 20 + 30 = 150 microseconds. A largest value seen in testing does not establish cc, hh, dd or II.

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