Skip to content

Lesson 3 of 9 · 23m

CPU Scheduling and Latency: Why Fast Machines Still Feel Slow

Runqueues, scheduler latency, stealing and throttling, and how to distinguish CPU saturation from CPU contention.

A machine can have 64 cores and still serve requests slowly. Scheduling is about who gets a core next, not how powerful the cores are. When latency degrades while utilization looks 'only' 60%, the missing part is usually queueing, contention, throttling, or scheduler delay — not raw compute capacity.

Utilization is not saturation

$ sar -u 1 5
Linux 6.8.0 (web-04)  11/29/2026  _x86_64_  (16 CPU)

11:20:01 AM  CPU  %user  %system  %iowait  %steal  %idle
11:20:02 AM  all   62.10    12.40     0.30    0.10   25.10

62% user time does not mean 38% headroom for latency-sensitive work. Cores are discrete: a process cannot use half of core 3 and half of core 7 simultaneously. If too many runnable threads land on the same cores at the same moment, they queue even while other cores are idle.

The r column in vmstat 1 is runqueue depth: runnable or waiting-to-run threads. A sustained r greater than CPU count is a strong saturation signal. Short spikes are normal; sustained queueing is requests waiting behind other work.

nice is weaker than people think

nice adjusts CPU priority within the normal scheduler, but it does not reserve CPU and it does not fix unrelated latency. Raising a batch job with nice 10 helps only when the workload is CPU-bound and the scheduler is the actual bottleneck. It does nothing for lock contention, disk waits, network latency, or memory reclaim.

For stronger control, use cgroups CPU bandwidth or weights. On systemd systems:

[Service]
CPUWeight=50
CPUQuota=180%

CPUQuota is a hard throttle; CPUWeight shapes contention only when CPU is scarce. Confusing those two causes real incidents: a latency-sensitive service under a quota can be throttled while the machine appears idle overall.

steal time and noisy neighbors

On virtual machines, %steal means the guest wanted CPU but the hypervisor gave it to someone else. Your application did nothing wrong, and changing code will not fix the noisy neighbor. Persistent steal above a few percent during incidents is evidence for provider escalation or placement changes.

Inside containers, cpu.stat tells the analogous story:

cat /sys/fs/cgroup/cpu.stat
# nr_periods, nr_throttled, throttled_usec

If nr_throttled rises during latency spikes, the service is being deliberately paused by quota. CPU utilization graphs can look deceptively low because the process was not allowed to run.

scheduler latency is a first-class metric

Linux exposes per-thread scheduling delay through /proc/<pid>/schedstat and aggregate pressure through PSI:

cat /proc/pressure/cpu
# some avg10=2.31 avg60=1.04 avg300=0.62 total=...

CPU pressure means tasks were runnable but could not run. That is exactly the quantity application latency cares about. Pair it with runqueue length, utilization, steal, and cgroup throttling before declaring 'CPU-bound.'

Field notes

  • CPU-bound means runnable work is waiting for cores. I/O-bound means threads are asleep in the kernel. These need opposite fixes.
  • Hyperthread siblings share execution resources; '32 logical CPUs' are not 32 independent physical lanes.
  • CPU affinity can help cache locality but can also concentrate contention. Do not pin casually.
  • Autoscaling on average CPU is often too late for latency-sensitive services. Queueing metrics react sooner.

Was this lesson helpful?

Sign in to rate