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.
Caps.
cpu.max, set by systemd’sCPUQuota=, holds a quota and a period , both in microseconds: the group’s threads may run for at most of processor time in each period , added up over all processors. When the quota is used up, every thread of the group is throttled, meaning it is not run, until the next period begins.CPUQuota=50%becomes a quota of 50 ms in the default period of 100 ms.memory.max(MemoryMax=) caps memory use: at the limit the kernel takes memory back from the group, and if it cannot, its out-of-memory killer ends a process of the group.pids.max(TasksMax=) caps the number of processes and threads.Shares.
cpu.weight(CPUWeight=), from 1 to 10000 with default 100, divides processor time in proportion to the weights of the groups that want to run at that moment. Time that nobody else wants goes to whoever wants it. A share is a lower bound only relative to the current competitors, so it is neither a cap nor a reservation.Placement.
cpuset.cpus(AllowedCPUs=) restricts which processors the group may use; it does not keep other groups off those processors. A cpuset partition with exclusive processors does keep other groups’ processes off them, which comes closest to reserving a core, although work that the kernel ties to each processor, such as some interrupt §6.11 handling, can still run there.Protection.
memory.min(MemoryMin=) protects memory that the group already uses, up to the given amount, from being taken back. It sets no memory aside in advance.
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 in every period , 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 ms in each period ms. A job in the group needs 40 ms of processor time, its execution time §9.32.
Idle machine. 40 ms is below the 50 ms quota, so the job runs without pause and finishes after 40 ms.
Busy machine. A program in another group runs without pause, with
cpu.weight900 against the job’s 100. The job receives about of the processor, about 10 ms in each 100 ms, and needs about 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.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 ms. It finishes at ms, although the machine was idle.
Four processors, four threads. The quota is shared by all the group’s threads. Four threads running together use it up after ms, then all four wait ms. “Half a processor” arrives as a burst on four processors followed by a long pause.
Compare a reservation of ms in every 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 be the processor time that a group’s threads use, added over all processors, in the -th period, the interval from to . The cap cpu.max with quota and period promises for every , 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 of processor time and starts at the beginning of a period on an otherwise idle processor, and let . Let , where is rounded up to a whole number. The earliest finishing time is For ms, ms and ms, and ms. The cap gives no latest finishing time: that depends on the competitors, which the cap does not restrict.
A reservation of runtime in every period promises the opposite direction: in every period throughout which the job is ready to run, it receives at least of processor time. A job that needs and is ready from the start of a period therefore finishes by For ms, ms and ms, ms. This promise rests on three assumptions.
Admission: the sum of the shares of all admitted reservations fits the capacity.
No blocking: the job does not wait for a lock, a device or input.
Bounded work: 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 , 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].