Section 17.4

A helper can invalidate an instruction-time bound

Question

Why does a finite instruction budget §2.32 not automatically bound evaluation latency?

Short answer

Because the budget counts instructions while latency is measured in time, and nothing in the count says how long one instruction takes. The budget stops a program after a fixed number of bytecode §2.4 instructions. One of those instructions can call a function outside the bytecode that copies a large buffer or waits for a device, with no time limit. Time the program spends waiting for the processor is not counted either. A latency bound needs a time limit for every instruction and every such function, and a limit on waiting.

Terms in the question

Full answer

  1. The budget bounds a count. With a budget of BB, at most BB instructions are carried out. This is a statement about a number of events, not about time.

  2. A count becomes a time only through a time per instruction. If every instruction takes at most cc seconds, all instructions together take at most B×cB \times c seconds. If one kind of instruction has no maximum time, no such cc exists, and the count says nothing about time.

  3. Ordinary instructions have a maximum time. An addition, a comparison or a jump is carried out by a short, fixed piece of interpreter code. For a given processor and clock speed, a maximum time for that piece of code can be established.

  4. A call instruction may have none. A call counts as one instruction, but the helper function behind the call is code of any length. The helper function may loop over a buffer whose length the caller chose. It may wait for a lock, for a device or for a reply from a network. While the helper function runs, the interpreter carries out no bytecode instruction, so it does not look at the counter and cannot stop the helper function.

  5. Running is only one part of latency. Even when every instruction and every helper function has a maximum time, the sum bounds only the time that the processor spends on this run, which is called execution time §9.32. Latency is the larger quantity called response time. It also contains the time the input waits in a queue before the run starts, the pauses while tasks of higher priority and interrupts §6.11 (hardware signals that make the processor switch to other code at once) use the processor, and the time spent waiting for locks.

A latency bound therefore needs four separate facts: a bounded number of instructions, a bounded time for each ordinary instruction, a bounded time for every helper function the program may call, and a bounded total wait. The budget supplies only the first fact.

Worked example

All figures here are invented. A program has a budget of 10 instructions and uses all of them: 9 ordinary instructions and 1 call to a helper function. Assume the following maximum times.

The processor time for the run is at most 10×1+9×1+2,000=2,019 microseconds.10 \times 1 + 9 \times 1 + 2{,}000 = 2{,}019 \text{ microseconds.} Each part is counted once: reading the call instruction is inside the first term, and the body of the helper function is the third term. The helper function accounts for 2,000/2,0192{,}000 / 2{,}019, about 99%, of the bound, although it used one tenth of the budget.

To bound latency, add the waiting. Assume that interrupts take the processor away for at most 500 microseconds during the run, and that the input waits at most 3,000 microseconds in a queue before the run starts. Latency is then at most 2,019+500+3,000=5,5192{,}019 + 500 + 3{,}000 = 5{,}519 microseconds.

Now remove one assumption. The helper function waits for a reply from another device and has no time limit. If the reply never arrives, the call never returns. The interpreter counted the call when the call began, and it cannot look at the counter again until the helper function returns. The budget is 10 instructions in both cases, but in the second case no latency bound exists.

Follow-up questions and limits

What is the budget good for, then? It guarantees that no loop in the bytecode runs forever, because the interpreter stops the run when the counter reaches zero. It also bounds the share of the time that the bytecode itself uses, once each ordinary instruction has a maximum time.

How is a latency bound obtained in practice? Offer only helper functions with a proven maximum time on every path, including the error paths: no waiting, and every loop limited by a constant. The largest processor time that any permitted run can need is called the worst-case execution time §9.33. Then bound the waiting by analyzing the interrupts and the scheduler, the part of the system that decides which task runs next. A timer that cuts the run off at a deadline is a different thing. The timer enforces a limit and gives no evidence that runs finish in time. The system then needs a defined result for a run that was cut off.

Is the largest latency measured in tests a bound? No. It covers only the inputs and timings that were tried. A bound must also hold for the cases that nobody tried.

Does the budget survive just-in-time compilation §2.14? Only if the compiled code also counts. Just-in-time compilation translates the bytecode into instructions for the real processor. The interpreter no longer runs, and its counter goes with it, unless the compiler adds counting code to the translation.

Fullest treatment: Termination versus time bound (§2.33).

Related entries: Instruction budget (fuel) (§2.32), Helper contract (§2.21), Response time versus execution time (§9.32), Worst-case execution time (WCET) (§9.33), Helper function (§2.19).