Section 13.22

Resource cap versus resource reservation

Definition

A resource cap is an upper limit on how much of a resource, such as processor time or memory, a consumer may use; it protects everyone else from that consumer. A resource reservation is a lower bound: a promise that a stated amount will be available to the consumer, whatever others do; it protects the consumer from everyone else. The difference that matters is the direction: a cap can make work slower but never faster, and only a reservation supports a promise that work finishes by a deadline.

The idea

An experimenter runs a benchmark job with a processor quota of 50%, meaning that the job may use at most half of one processor’s time, and expects the job to receive half a processor. On a busy machine the job takes ten times longer than expected, and on an idle machine a longer job pauses between bursts of work. Neither is a fault: the quota promised the other processes that this job would never take more than half a processor, and promised the job nothing.

A speed limit and a reserved lane are a fair picture. A speed limit, the cap, tells a driver how fast they may go; it does not clear the road. A reserved bus lane, the reservation, keeps the road clear for the bus, so the bus can count on moving. Caps protect the others from one consumer; reservations protect one consumer from the others. The picture stops being accurate in one respect: a processor reservation is a budget per period, not a lane that is always open, so even reserved work can wait within each period until its budget is served.

How it works

Linux sets most resource controls through cgroups §13.20, kernel groups of processes, and systemd offers the same controls as unit settings. Sorted by direction, they are these.

A reservation needs two parts that caps lack. The first is admission control §19.4.5: the system refuses a new reservation unless the sum of all reservations fits the capacity, because promises that add up to more than the machine has cannot all be kept. The second is a scheduler that serves each admitted reservation its budget, a runtime QQ in every period PP, ahead of work without a reservation. Some real-time operating systems offer such reservations, and Linux offers them through the scheduling policy SCHED_DEADLINE. A memory reservation, likewise, is memory set aside before it is needed, such as a buffer allocated at start-up.

Worked example

Take one processor and a group with cpu.max set to 50000 100000: a quota q=50q = 50 ms in each period p=100p = 100 ms. A job in the group needs 40 ms of processor time, its execution time §9.32.

  1. Idle machine. 40 ms is below the 50 ms quota, so the job runs without pause and finishes after 40 ms.

  2. Busy machine. A program in another group runs without pause, with cpu.weight 900 against the job’s 100. The job receives about 100/(100+900)=0.1100 / (100 + 900) = 0.1 of the processor, about 10 ms in each 100 ms, and needs about 40/0.1=40040 / 0.1 = 400 ms to finish. It uses only about 10 ms of its 50 ms quota in each period, so the cap plays no part; the competitor decides the completion time.

  3. Idle machine, longer job. The job needs 120 ms. In the first period it runs 50 ms and is throttled for the remaining 50 ms; the second period is the same, bringing it to 100 ms; in the third it runs the last 120−100=20120 - 100 = 20 ms. It finishes at 100+100+20=220100 + 100 + 20 = 220 ms, although the machine was idle.

  4. Four processors, four threads. The quota is shared by all the group’s threads. Four threads running together use it up after 50/4=12.550 / 4 = 12.5 ms, then all four wait 100−12.5=87.5100 - 12.5 = 87.5 ms. “Half a processor” arrives as a burst on four processors followed by a long pause.

Compare a reservation of Q=50Q = 50 ms in every P=100P = 100 ms. In case 2 the job would receive at least 50 ms in the first period, whatever the other program’s weight, and its 40 ms of work would be done within 100 ms.

Stated precisely

Let uku_k be the processor time that a group’s threads use, added over all processors, in the kk-th period, the interval from kpkp to (k+1)p(k+1)p. The cap cpu.max with quota qq and period pp promises uk≤qu_k \le q for every kk, as long as no burst allowance is set; a burst allowance, cpu.max.burst, lets unused quota carry over into later periods up to a stated amount. A cap is an upper bound on service, so it gives a lower bound on completion time. Take one thread that needs CC of processor time and starts at the beginning of a period on an otherwise idle processor, and let q≤pq \le p. Let n=⌈C/q⌉n = \lceil C / q \rceil, where ⌈x⌉\lceil x \rceil is xx rounded up to a whole number. The earliest finishing time is Tmin=(n−1)p+(C−(n−1)q).T_{\min} = (n - 1)\,p + \bigl(C - (n - 1)\,q\bigr). For C=120C = 120 ms, q=50q = 50 ms and p=100p = 100 ms, n=3n = 3 and Tmin=200+20=220T_{\min} = 200 + 20 = 220 ms. The cap gives no latest finishing time: that depends on the competitors, which the cap does not restrict.

A reservation of runtime QQ in every period PP promises the opposite direction: in every period throughout which the job is ready to run, it receives at least QQ of processor time. A job that needs CC and is ready from the start of a period therefore finishes by Tmax=⌈C/Q⌉⋅P.T_{\max} = \lceil C / Q \rceil \cdot P. For Q=50Q = 50 ms, P=100P = 100 ms and C=120C = 120 ms, Tmax=3×100=300T_{\max} = 3 \times 100 = 300 ms. This promise rests on three assumptions.

  1. Admission: the sum of the shares Qi/PiQ_i / P_i of all admitted reservations fits the capacity.

  2. No blocking: the job does not wait for a lock, a device or input.

  3. Bounded work: CC is a true upper bound, such as a worst-case execution time §9.33.

Whether a whole set of tasks meets its deadlines under such promises is the question of schedulability §9.37.

Limits and common mistakes

A quota does not give a core. Under a quota, the job’s threads share processors, caches and memory bandwidth with every other process, and run on whichever processors the scheduler picks. A weight is not a reservation either: the share it yields shrinks as competitors arrive.

A cap changes what it limits. Throttling adds pauses even on an idle machine, as case 3 shows, and those pauses change measured latency, a form of measurement perturbation §13.30. A measurement taken under a cap describes the job under that cap.

Memory limits fail loudly. At memory.max the out-of-memory killer can end a process of the group. Protection by memory.min keeps memory already in use; it does not make a new allocation succeed when the machine has no free memory.

Controls apply to the group’s members only. Work moved out of the group, or done by other services on its behalf, is neither capped nor counted.

A reservation promises processor time, not completion. A job that needs more than its bound CC, or that waits for a device, can still be late.

Most reservation schedulers also cap: a thread that has used its budget waits for the next period, which keeps one reservation from eating into the others.

Questions answered

Does a CPU quota reserve an uncontended core for this task?

No. A CPU quota, such as cpu.max in a Linux cgroup (a kernel group of processes) or CPUQuota= in systemd, is a cap: an upper limit on the processor time that the task’s group may use in each period, for example 50 ms in every 100 ms. It reserves nothing and keeps no other process off any processor. The task still competes for processor time and shares caches and memory bandwidth, so it can finish much later than its quota suggests, and the quota can even add pauses on an idle machine.

An uncontended core needs exclusive placement, such as a cpuset partition with exclusive processors, plus moving other work and interrupt handling away from that core and checking afterwards that nothing else ran there. A guaranteed amount of processor time needs a reservation with admission control, such as Linux’s SCHED_DEADLINE policy, and a claim that work meets a deadline needs, in addition, a bound on the work. Without these, report completion times as observations, not as guarantees.

Related entries: cgroup (§13.20), Schedulability (§9.37), Response time versus execution time (§9.32), Worst-case execution time (WCET) (§9.33). Further reading: [32, 45].