THINK FIRST·CODE LATER

← All labs

CPU limits and CFS throttling

Problem

Simulate CFS bandwidth control in 1 ms ticks.

Input: quota period threads work cores (quota and period in ms; each of threads threads needs work ms of CPU; the machine has cores cores). All threads start at t = 0.

In each 1 ms tick: the quota is refilled at the start of every period (t = 0, period, 2 × period, …); the number of threads that run is min(runnable threads, cores, remaining quota); running threads (lowest index first) each progress 1 ms and consume 1 ms of quota. A period is throttled if at some tick fewer threads ran than min(runnable, cores) because the quota was exhausted.

Print:

Limit: 0.20 CPUs (quota 20 ms per 100 ms)
Work: 1 thread x 50 ms on 4 cores
Finish time: 210 ms (without limit: 50 ms), throttled periods: 2

(limit as an integer if whole, else 2 decimals; "without limit" = ⌈threads × work / min(threads, cores)⌉.)

Write it here or in your IDE, then paste it. Compile and test it yourself before comparing. Your code stays in your browser — it is never sent to or stored on the server.