top shows an almost idle machine, yet every request takes almost 400 ms. The cause is not in utilization but in the service's cgroup: it may only use part of a CPU and is stopped in every 100 ms period.
Symptom
wrk -t2 -c8 -d20s --latency http://perf1:8082/
Latency 366.43ms 93.11ms 788.93ms 74.28%
50% 384.70ms
99% 685.52ms
Requests/sec: 19.00
mpstat 5 1
Average: all 6.21 0.00 0.65 0.00 0.00 0.99 1.09 0.00 0.00 91.05
Only 19 requests per second while the machine is 91 % idle.
Diagnosis
Look at the process and the service's cgroup.
pidstat -u -p 18259 5 1 Average: 1000 18259 27.00 3.00 0.00 24.20 30.00 - python3 cat /sys/fs/cgroup/system.slice/rechner.service/cpu.stat nr_periods 90 nr_throttled 81 throttled_usec 8866129
Causes
CPUQuota=30%A quota in the unit
Someone limited the service to 30 % of a CPU "to be safe". systemd translates that into cpu.max of the cgroup.
systemctl show -p CPUQuotaPerSecUSec rechner CPUQuotaPerSecUSec=300ms
%waitThrottled processes count as waiting
A throttled process is runnable but not allowed to run. pidstat shows that as %wait – on a machine whose CPUs are free.
100 ms periodThe quota applies per period
Once the quota of a 100 ms period is used up, the service stands still until the next one. Several threads use it up particularly fast.
Fix
sudo systemctl set-property --runtime rechner CPUQuota=
cat /sys/fs/cgroup/system.slice/rechner.service/cpu.max
max 100000
wrk -t2 -c8 -d20s --latency http://perf1:8082/
Latency 88.18ms 23.13ms 320.47ms 75.59%
50% 86.24ms
99% 156.35ms
Requests/sec: 73.57
Don't: Suspect the code before you have looked at the cgroup. And forget runtime changes – systemctl status shows them as a drop-in, systemctl revert removes them.
Linux system performance hands-on, day 15: cgroups v2 with quotas, weights and I/O throttling – and how to recognize a limit.
Go to day 15 →
FAQ
How do I spot CPU throttling in Kubernetes?
limits.cpu becomes cpu.max. The metric container_cpu_cfs_throttled_periods_total relative to the periods shows throttled containers.
Does removing the quota help?
In the course requests went from 19 to 74 per second and the median dropped from 385 to 86 ms. After that the single CPU of the Python process was the limit.