Day 16: The final case
Goal: investigate a slow service with several hidden causes methodically, prove each cause with a measurement and fix it, measure again after every change and record the result in a short report · Duration: about 2.5 hours (60 minutes of it competition) · Prerequisite: Day 0–15
Where we start
You need the baseline from Day 0, wrk on last1 (Day 12),
perf (Day 5) and the bcc tools (Day 14).
# on perf1
cat ~/basislinie.txtExpected (your numbers):
Basislinie perf1, Tag 0
Netz perf1 -> last1 (iperf3, 10 s): 7.62 Gbit/s, 1 Retransmit
Zusatzplatte sdb (fio randread 4k, QD16): 24.9k IOPS, clat Mittel 618 us, p99 1.7 ms
Ping perf1 -> last1: RTT ca. 0.6 ms
# on last1
which wrkExpected:
/usr/bin/wrk
# on perf1
python3 --versionExpected:
Python 3.12.3
From Python 3.12 on, perf can name Python functions –
you need that today.
Introduction
For fifteen days you have practiced individual tools on individual resources. In reality a problem never arrives with a label “memory pressure” or “network”. It arrives as a sentence: “The shop is slow.” And often it doesn’t have one cause but several that hide each other. Fix the first, and the second takes the spotlight.
Today you work through such a case from start to finish: from the customer’s sentence through the USE method from Day 1 to a report that a colleague who wasn’t there can read.
Today you learn:
- to start a case with a problem statement and a measurement,
- to work through the USE method resource by resource instead of guessing,
- to prove every cause with a number and to measure again after every change,
- to recognize when a change fixes a real problem but does not improve throughput,
- to write a short, reliable report.
Background
The picture for today: at the family doctor’s
A patient comes in with “I don’t feel well”. A good doctor does not prescribe something right away. She always proceeds the same way:
- Medical history: What exactly, since when, how bad? She also measures temperature and pulse.
- Examination from head to toe: heart, lungs, abdomen – organ by organ, following a fixed list, including the organs she does not suspect.
- Finding: What stands out, and how far does it deviate from the normal values?
- Treatment: one measure after the other, not five at once.
- Follow-up: Did it work? Sometimes only now a second condition shows up that the first one was hiding.
- Doctor’s letter: short, with measurements, so the next doctor can continue.
The terms
| Term | Meaning | in the picture |
|---|---|---|
| Problem statement | Who notices what, since when, measured how? (Day 1) | medical history |
| Baseline | your numbers from the healthy system (Day 0) | normal values |
| USE | utilization, saturation, errors for every resource (Day 1) | examination organ by organ |
| Finding | a deviation with a number, e.g. “run queue 5 on 4 CPUs” | finding |
| Fix | exactly one change | treatment |
| Control measurement | the same measurement as at the start, after every fix | follow-up |
| Report | symptom, findings, fixes, effect, open points | doctor’s letter |
Remember: One fix after the other, and measure again after each. Otherwise you don’t know what helped – and you overlook the second condition.
Rules and scoring
- Time: 60 minutes from the first measuring command. Set a timer.
- Allowed: all tools from Day 0–15. Not allowed: rereading the source code or the unit files from the setup before a measurement points to that spot.
- Scoring: Every cause found and proven with a number gives 3 points, every cause fixed with a control measurement 2 more. The report gives up to 5 points. A finding without a measurement does not count.
- Compare yourself with the solution at the end.
Setup
A colleague set up the service. Type in the steps, but read them like assembly instructions, not like a hint: which part of it is a problem is something you are supposed to measure later.
Step 1 – The service.
# on perf1
mkdir -p ~/tag16# on perf1
nano ~/tag16/laden.py# laden.py – Bestelldienst für den Abschlussfall (Tag 16)
import os
import random
from http.server import BaseHTTPRequestHandler, ThreadingHTTPServer
PROTOKOLL = "/daten/laden.log"
def preise_berechnen():
preise = [random.random() for _ in range(40000)]
return sorted(preise)[len(preise) // 2]
def protokollieren(zeile):
with open(PROTOKOLL, "a") as f:
f.write(zeile)
f.flush()
os.fsync(f.fileno())
class Laden(BaseHTTPRequestHandler):
def do_GET(self):
preis = preise_berechnen()
protokollieren(f"{self.path} {preis:.4f}\n")
antwort = f"{preis:.4f}\n".encode()
self.send_response(200)
self.send_header("Content-Length", str(len(antwort)))
self.end_headers()
self.wfile.write(antwort)
def log_message(self, *args):
pass
ThreadingHTTPServer(("", 8090), Laden).serve_forever()Step 2 – The unit for the service.
# on perf1
sudo nano /etc/systemd/system/laden.service[Unit]
Description=Laden – Bestelldienst (Tag 16)
[Service]
ExecStart=/usr/bin/python3 /home/ubuntu/tag16/laden.py
User=ubuntu
[Install]
WantedBy=multi-user.targetStep 3 – A second service on the same machine.
# on perf1
sudo nano /etc/systemd/system/nachtlauf.service[Unit]
Description=Nachtlauf – Preisberechnung für morgen (Tag 16)
[Service]
ExecStart=/usr/bin/stress-ng --cpu 4 --cpu-method matrixprod
# damit der Nachtlauf bis morgens fertig ist
CPUWeight=900Step 4 – Start.
# on perf1
sudo systemctl daemon-reload# on perf1
sudo systemctl start laden# on perf1
sudo systemctl start nachtlaufStep 5 – A leftover from a test. Weeks ago the colleague simulated a slow line (Day 11) and forgot to undo it:
# on perf1
sudo tc qdisc add dev eth0 root netem delay 15msFrom here on you are the doctor. Forget what you just typed.
The case
The message from the business department: “Since this morning the shop has been terribly slow. Answers used to come instantly, now every order takes almost a second. It can’t handle more than a few orders per second.”
Your task: Find all causes, prove each with a number, fix them one after the other with a control measurement, and fill in this report:
# on perf1
nano ~/tag16/bericht.md# Bericht: Laden langsam
## Symptom
- gemeldet:
- gemessen (wrk, 8 Verbindungen, 30 s):
## Befunde und Maßnahmen
| Nr. | Befund (mit Zahl) | Maßnahme | Wirkung (Kontrollmessung) |
|---|---|---|---|
| 1 | | | |
## Ergebnis
- vorher:
- nachher:
## Geprüft, unauffällig
## Offene Punkte(The report template is in German, like everything in the lab: Symptom, Befunde und Maßnahmen = findings and fixes, Ergebnis = result, vorher/nachher = before/after, Geprüft, unauffällig = checked, unremarkable, Offene Punkte = open points.)
The measurement that everything starts with and that you use to check every fix:
# on last1
wrk -t2 -c8 -d30s --latency http://perf1:8090/-t2two threads,-c8eight concurrent connections,-d30s30 seconds,--latencywith percentiles.
From here the clock is running. The solution path follows – read it only after the 60 minutes.
Solution path
Round 1: Medical history
Step 1 – Measure the symptom.
# on last1
wrk -t2 -c8 -d30s --latency http://perf1:8090/Expected (excerpt):
Latency 921.08ms 239.03ms 1.74s 69.96%
50% 921.65ms
90% 1.23s
99% 1.59s
253 requests in 30.08s, 29.15KB read
Requests/sec: 8.41
The report is right: median 0.92 s, just over 8 requests per second. These are the numbers for the “Symptom” line in the report.
Remember: First translate the complaint into a number. Without that number you cannot show later that you improved anything.
Round 2: CPU
USE starts with the CPU because it is quickest to check. For this round, keep a long load running so you can measure at your leisure:
# on last1
wrk -t2 -c8 -d600s http://perf1:8090/That is ten minutes of load. You can stop it at any time with Ctrl+C.
Step 1 – The first look.
# on perf1
uptimeExpected:
22:31:45 up 9:30, 1 user, load average: 4.52, 1.77, 0.73
Load 4.5 on 4 CPUs and rising (the 1-minute number is the highest).
Step 2 – Utilization and queue.
# on perf1
vmstat 1 5Expected:
procs -----------memory---------- ---swap-- -----io---- -system-- -------cpu-------
r b swpd free buff cache si so bi bo in cs us sy id wa st gu
7 0 0 10920380 46200 1041428 77 77 2356 2549 844 1 2 1 96 1 0 0
5 0 0 10919932 46240 1041424 0 0 0 88 4686 1881 99 1 0 0 0 0
6 0 0 10923444 46288 1041448 0 0 0 96 4636 1587 99 1 0 0 0 0
5 0 0 10922212 46336 1041432 0 0 0 104 4795 2187 99 1 0 0 0 0
5 0 0 10921268 46376 1041448 0 0 0 88 4654 1454 100 0 0 0 0 0
| Column | Value | Meaning |
|---|---|---|
r |
5–7 | runnable processes – more than 4 CPUs: saturation |
us |
99–100 | utilization 100 %, almost everything in user space |
wa |
0 | nobody is waiting for the disk |
si/so |
0 | no swapping |
The first line is the average since boot and does not count (Day 2).
Step 3 – Who is computing?
# on perf1
top -b -n 1 -o %CPU-btext output,-n 1once,-o %CPUsorted by CPU.
Expected (excerpt – the top processes):
PID USER PR NI VIRT RES SHR S %CPU %MEM TIME+ COMMAND
23840 root 20 0 60796 3572 1432 R 100.0 0.0 1:05.33 stress-+
23842 root 20 0 60796 3572 1432 R 100.0 0.0 1:05.54 stress-+
23839 root 20 0 60796 3572 1432 R 90.9 0.0 1:05.41 stress-+
23841 root 20 0 60796 3572 1432 R 72.7 0.0 1:05.49 stress-+
21837 ubuntu 20 0 739236 27384 10620 S 18.2 0.2 0:54.77 python3
Four stress-ng workers take almost everything, the shop
(python3) gets 18 %.
Step 4 – Which service do they belong to?
# on perf1
systemd-cgtop -b -n 3 -d 4 --depth=2 -c system.sliceExpected (excerpt – the last round):
system.slice 60 399.3 695.7M 0B 28.8K
system.slice/nachtlauf.service 5 377.2 21.8M - -
system.slice/laden.service 6 22.0 15.2M 0B 27.8K
The night run (nachtlauf) gets 377 %, the shop
(laden) 22 %.
Step 5 – Does the shop suffer from it? The pressure in its cgroup (Day 15):
# on perf1
cat /sys/fs/cgroup/system.slice/laden.service/cpu.pressureExpected:
some avg10=26.11 avg60=16.38 avg300=5.75 total=20703324
full avg10=24.88 avg60=15.39 avg300=5.33 total=19048639
A quarter of the time the shop waits for a CPU. Why does it lose so clearly?
# on perf1
systemctl show -p CPUWeight laden nachtlaufExpected:
CPUWeight=[not set]
CPUWeight=900
The shop has the default weight 100, the night run 900 (Day 15, Drill 3).
Finding 1: CPU saturated (run queue 5–7 on 4 CPUs,
100 % us). The background job
nachtlauf.service has CPUWeight=900 and crowds
out the shop (22 % CPU, 25 % CPU pressure).
Step 6 – Treatment. A night run may compute, but only with what is left over. Stop the long load on last1 with Ctrl+C and change the unit:
# on perf1
sudo nano /etc/systemd/system/nachtlauf.service[Unit]
Description=Nachtlauf – Preisberechnung für morgen (Tag 16)
[Service]
ExecStart=/usr/bin/stress-ng --cpu 4 --cpu-method matrixprod
# Hintergrundjob: bekommt nur, was die Dienste übrig lassen
CPUWeight=10# on perf1
sudo systemctl daemon-reload# on perf1
sudo systemctl restart nachtlaufStep 7 – Follow-up.
# on last1
wrk -t2 -c8 -d30s --latency http://perf1:8090/Expected (excerpt):
50% 227.40ms
99% 393.82ms
Requests/sec: 32.30
From 8.4 to 32 requests per second, median from 922 to 227 ms. Better – but “answers used to come instantly”. Next.
Remember: 100 % CPU does not mean “buy more CPU”. First ask who gets it and why – in the cgroup, not just in the process.
Round 3: Network
The CPU is relieved. Next in USE comes a resource you have not checked yet: the network between last1 and perf1.
Step 1 – How long does a single request take without a crowd? Without parallel load you measure the pure response time:
# on last1
curl -s -o /dev/null -w "%{time_connect} %{time_total}\n" http://perf1:8090/-wprints values after the request:time_connect= until the TCP connection is established,time_total= until the response is complete, both in seconds.
Run the command three times. Expected (three runs):
0.016666 0.056194
0.020799 0.080909
0.016655 0.057281
The connection setup alone takes 17–21 ms. A TCP handshake needs one round trip (RTT). The baseline says 0.6 ms.
Step 2 – The RTT directly.
# on perf1
ping -c 5 last1Expected:
PING last1 (172.16.0.228) 56(84) bytes of data.
64 bytes from last1 (172.16.0.228): icmp_seq=1 ttl=64 time=16.7 ms
64 bytes from last1 (172.16.0.228): icmp_seq=2 ttl=64 time=15.3 ms
64 bytes from last1 (172.16.0.228): icmp_seq=3 ttl=64 time=15.4 ms
64 bytes from last1 (172.16.0.228): icmp_seq=4 ttl=64 time=15.5 ms
64 bytes from last1 (172.16.0.228): icmp_seq=5 ttl=64 time=15.8 ms
--- last1 ping statistics ---
5 packets transmitted, 5 received, 0% packet loss, time 4004ms
rtt min/avg/max/mdev = 15.299/15.738/16.663/0.488 ms
15.7 ms instead of 0.6 ms – 25 times the baseline. And very even:
mdev 0.5 ms. An overloaded line typically fluctuates a lot;
this smooth looks more like a fixed, artificial delay.
Step 3 – Where does the delay come from? The queue of the network card (Day 11):
# on perf1
tc qdisc show dev eth0Expected:
qdisc netem 8001: root refcnt 2 limit 1000 delay 15ms
A netem with 15 ms delay – a forgotten test.
Finding 2: RTT perf1 ↔︎ last1 15.7 ms instead of 0.6
ms (baseline), caused by netem delay 15ms on
eth0.
Step 4 – Treatment.
# on perf1
sudo tc qdisc del dev eth0 rootdel … rootremoves the queue at the root; the kernel puts the default back in place.
# on perf1
tc qdisc show dev eth0Expected:
qdisc fq_codel 0: root refcnt 2 limit 10240p flows 1024 quantum 1514 target 5ms interval 100ms memory_limit 32Mb ecn drop_batch 64
# on perf1
ping -c 3 last1Expected (last line):
rtt min/avg/max/mdev = 0.353/0.426/0.491/0.056 ms
Step 5 – Follow-up, twice. First the single request, again three times:
# on last1
curl -s -o /dev/null -w "%{time_connect} %{time_total}\n" http://perf1:8090/Expected (three runs):
0.002269 0.047601
0.001109 0.032112
0.001469 0.031191
Connection setup has dropped from 17 ms to 1–2 ms, a single request from 56–81 ms to 31–48 ms. Now the measurement under load:
# on last1
wrk -t2 -c8 -d30s --latency http://perf1:8090/Expected (excerpt):
50% 240.14ms
99% 404.57ms
Requests/sec: 32.20
No change – 32.2 instead of 32.3 requests per second. Was the fix wrong? No: the delay was a real fault, it cost every single request about 25 ms. But under load the requests wait 200 ms in the queue in front of the service. Next to that, 25 ms hardly show. The bottleneck is elsewhere.
Remember: A fix can be right and still not change throughput – then the bottleneck sits somewhere else. Measure response time and throughput.
Round 4: Where the service spends its time
The machine has CPU to spare (the night run now steps aside), the network is healthy, and still the shop manages only 32 requests per second. So you look into the service: a CPU profile as on Day 5.
Step 1 – Make Python readable for perf. Without
help, perf sees only the C functions of the Python
interpreter (_PyEval_EvalFrameDefault). From Python 3.12
on, the environment variable PYTHONPERFSUPPORT=1 switches
on a bridge through which perf sees the names of the Python
functions.
# on perf1
sudo nano /etc/systemd/system/laden.service[Unit]
Description=Laden – Bestelldienst (Tag 16)
[Service]
ExecStart=/usr/bin/python3 /home/ubuntu/tag16/laden.py
User=ubuntu
Environment=PYTHONPERFSUPPORT=1
[Install]
WantedBy=multi-user.target# on perf1
sudo systemctl daemon-reload# on perf1
sudo systemctl restart ladenStep 2 – The PID.
# on perf1
systemctl show -p MainPID --value ladenExpected: a number, 27748 in the lab.
Use yours below.
Step 3 – Profile under load. Start a one-minute load on last1:
# on last1
wrk -t2 -c8 -d60s http://perf1:8090/While it runs:
# on perf1
cd ~/tag16# on perf1
sudo perf record -F 99 -g -p 27748 -o laden.data -- sleep 10| Option | Meaning |
|---|---|
-F 99 |
99 samples per second |
-g |
with call chain |
-p 27748 |
only this process |
-o laden.data |
write to this file |
-- sleep 10 |
record for as long as sleep 10 runs |
Expected (last line):
[ perf record: Captured and wrote 0.390 MB laden.data (948 samples) ]
Step 4 – Evaluate, with inclusive time.
# on perf1
sudo perf report -i laden.data --children --stdio --sort symbol -g none--childrenshows the time per function including the functions it calls.--sort symbolone line per function,-g nonewithout call trees.
Expected (excerpt – the lines with
py::, cut off on the right):
# Children Self Symbol
97.15% 0.00% [.] py::Thread._bootstrap:/usr/lib/python3.12/threading.py
96.62% 0.00% [.] py::Laden.do_GET:/home/ubuntu/tag16/laden.py
92.72% 0.00% [.] py::preise_berechnen:/home/ubuntu/tag16/laden.py
3.13% 0.00% [.] py::protokollieren:/home/ubuntu/tag16/laden.py
93 % of the compute time is in
preise_berechnen (“calculate prices”).
protokollieren (“log”) needs only 3 % – but careful: a CPU
profile only shows where time is computed, not where it
is spent waiting (Day 5). You check that next.
Finding 3: 93 % of the service’s CPU time in
preise_berechnen(). Now – and only now – you may read the
source code: for every request, the function generates
40,000 random numbers and sorts them to derive a price. The price does
not depend on the request at all.
Step 5 – And the disk? protokollieren()
calls fsync on every request. How long the service
waits there is shown by syscount from Day
14 (the load from step 3 must still be running, otherwise restart
it):
# on perf1
sudo syscount-bpfcc -L -p 27748 -d 10-Lwith total duration per system call,-d 10ten seconds.
Expected (excerpt – after about 12 seconds of compiling):
SYSCALL COUNT TIME (us)
fsync 328 852621.750
madvise 659 51415.837
sendto 658 41118.341
write 330 33286.160
328 fsync in 10 seconds, 0.85 s in total – 2.6
ms per request. With a response time of 300 ms that is not the
cause. It goes into the report under “checked, unremarkable”.
Step 6 – Treatment. Calculate the price once at startup instead of on every request:
# on perf1
nano ~/tag16/laden.py# laden.py – Bestelldienst für den Abschlussfall (Tag 16)
import os
import random
from http.server import BaseHTTPRequestHandler, ThreadingHTTPServer
PROTOKOLL = "/daten/laden.log"
def preise_berechnen():
preise = [random.random() for _ in range(40000)]
return sorted(preise)[len(preise) // 2]
def protokollieren(zeile):
with open(PROTOKOLL, "a") as f:
f.write(zeile)
f.flush()
os.fsync(f.fileno())
PREIS = preise_berechnen()
class Laden(BaseHTTPRequestHandler):
def do_GET(self):
preis = PREIS
protokollieren(f"{self.path} {preis:.4f}\n")
antwort = f"{preis:.4f}\n".encode()
self.send_response(200)
self.send_header("Content-Length", str(len(antwort)))
self.end_headers()
self.wfile.write(antwort)
def log_message(self, *args):
pass
ThreadingHTTPServer(("", 8090), Laden).serve_forever()# on perf1
sudo systemctl restart ladenStep 7 – Follow-up.
# on last1
wrk -t2 -c8 -d30s --latency http://perf1:8090/Expected (excerpt):
Latency 10.35ms 9.99ms 220.97ms 98.84%
50% 9.32ms
99% 21.46ms
20516 requests in 30.06s, 2.31MB read
Requests/sec: 682.45
682 requests per second, median 9 ms. That is the “instantly” from before.
Remember: When the machine is healthy and the service is still slow, the profile belongs inside the service. A profiler shows compute time,
syscount/off-CPU tools show waiting time – you need both.
Round 5: The next bottleneck
After every treatment the bottleneck moves. Where is it now? Start a 40-second load once more:
# on last1
wrk -t2 -c8 -d40s http://perf1:8090/The PID has changed because of the restart:
# on perf1
systemctl show -p MainPID --value ladenExpected: the new PID, 29685 in the
lab.
# on perf1
sudo syscount-bpfcc -L -p 29685 -d 10Expected (excerpt):
SYSCALL COUNT TIME (us)
fsync 6684 12871113.122
poll 6684 899324.298
write 6685 814923.758
sendto 13366 398494.853
Now fsync is at the top: 6684 calls in 10 seconds, 12.9
s in total – more than the measuring time, because eight threads wait at
the same time. 1.9 ms per call.
# on perf1
pidstat -u -p 29685 5 1Expected (excerpt):
Average: 1000 29685 45.00 48.80 0.00 10.40 93.80 - python3
About 94 % CPU – for a Python process in which, because of the
Global Interpreter Lock, only one thread computes at a time,
that is close to the limit of one CPU. The service is now limited by
itself and by fsync – a normal, healthy bottleneck. Whether
you need the fsync per request is a business question (may
an order get lost?). That belongs in the report under “open points”, not
in a quick change at midnight.
Remember: A case is not done when everything is fast, but when you know where the next bottleneck is and whether it is acceptable.
The report
# on perf1
nano ~/tag16/bericht.md# Bericht: Laden langsam
## Symptom
- gemeldet: „jede Bestellung fast eine Sekunde, nur ein paar pro Sekunde“
- gemessen (wrk, 8 Verbindungen, 30 s): 8,4 Anfragen/s, Median 922 ms, p99 1,59 s
## Befunde und Maßnahmen
| Nr. | Befund (mit Zahl) | Maßnahme | Wirkung (Kontrollmessung) |
|---|---|---|---|
| 1 | CPU gesättigt (r 5–7 bei 4 CPUs); nachtlauf.service CPUWeight=900 nimmt 377 %, Laden 22 %, CPU-Druck Laden 25 % | nachtlauf.service CPUWeight=10 | 32,3 Anfragen/s, Median 227 ms |
| 2 | RTT perf1–last1 15,7 ms statt 0,6 ms; netem delay 15ms auf eth0 | tc qdisc del dev eth0 root | Einzelanfrage 31–48 ms statt 56–81 ms; Durchsatz unverändert 32,2/s |
| 3 | 93 % der CPU-Zeit in preise_berechnen() (perf), pro Anfrage neu berechnet | Preis einmal beim Start berechnen | 682 Anfragen/s, Median 9 ms, p99 21 ms |
## Ergebnis
- vorher: 8,4 Anfragen/s, Median 922 ms
- nachher: 682 Anfragen/s, Median 9 ms
## Geprüft, unauffällig
- Platte/fsync: 2,6 ms pro Anfrage bei 300 ms Antwortzeit; Speicher: kein Swapping (si/so 0)
## Offene Punkte
- jetzt begrenzt: ein Python-Prozess bei ~94 % CPU und fsync pro Anfrage (1,9 ms); fachlich klären, ob jede Bestellung sofort auf die Platte mussFive sections, every statement with a number, every fix with a follow-up. With this, a colleague can continue tomorrow morning without calling you.
Scoring
| Points | For |
|---|---|
| 3 each | cause found and proven with a number (3 causes: 9) |
| 2 each | cause fixed with a control measurement (3 causes: 6) |
| up to 5 | report: symptom with a number, table complete, “checked” and “open points” filled in |
| 20 | maximum score |
From 15 points on you have mastered the course. If you skipped the network because throughput did not change, you are missing 5 points – that is exactly the trap of this case.
Clean up
# on perf1
sudo systemctl stop laden# on perf1
sudo systemctl stop nachtlauf# on perf1
sudo rm /etc/systemd/system/laden.service /etc/systemd/system/nachtlauf.service# on perf1
sudo systemctl daemon-reload# on perf1
rm /daten/laden.log ~/tag16/laden.dataladen.data belongs to root (from
sudo perf record). If rm reports “Permission
denied”, use sudo rm.
Checkpoint
# on perf1
systemctl is-active laden nachtlaufExpected: inactive twice.
# on perf1
tc qdisc show dev eth0Expected: qdisc fq_codel 0: root … – no
netem.
# on perf1
ls ~/tag16Expected: bericht.md and laden.py. Keep the
report.
Test yourself
- Why do you measure with
wrkat the start, before you investigate anything? - The CPU is 100 % utilized. Which two questions do you ask before you demand “more CPU”?
- After removing the network delay, throughput stayed the same. Why was the fix still right, and how would you have measured that?
- A CPU profile shows 3 % in a function that calls
fsync. Can that function still be the cause? How do you check? - What belongs in a report so that a colleague can continue without asking back?
Answers:
- Without a starting number you cannot prove any effect – and “terribly slow” is not a number.
- Who is using it (process and cgroup), and is it allowed to (weight, quota)?
- The delay cost every single request ~25 ms; under load, the queue in
front of the service hid that. Measurable with a single request
(
curl -w) and withpingagainst the baseline. - Yes – a CPU profile shows no waiting time. Check with
syscount -L,offcputimeorbpftraceonfsync(Day 14). - Symptom with a number, every finding with a number, every fix with a control measurement, before/after, what was checked and unremarkable, open points.
Interview Note
“Tell me about a performance problem you solved.” – The best answer has the shape of this chapter: the symptom in numbers, a methodical approach (USE, not guessing), several findings in order with control measurements, one finding that was right but did not change throughput, and the bottleneck that was left at the end. Whoever tells “and then we ordered more CPUs” has lost.
Enterprise Note
- Postmortem instead of blame: The forgotten
netemtest and the weight 900 are typical causes in real incidents. The report names mechanisms, not people. - Traceable changes: Weights, quotas and
tcrules belong in configuration management. A rule that was set by hand on just one machine gets forgotten. - Monitoring: Run queue, CPU pressure per cgroup, RTT to important peers and the service’s response time (RED) as continuous measurements would have shown two of the three causes before the call came in.
Summary
- A case starts with a number: 8.4 requests per second, median 922 ms.
- CPU: saturated, but by a background job with
CPUWeight=900. Weight 10 → 32 requests/s. - Network: RTT 15.7 instead of 0.6 ms because of a
forgotten
netem. Fixed, single requests faster, throughput unchanged – the bottleneck was elsewhere. - Code: 93 % of the compute time in a function that needlessly computes on every request. Calculate once → 682 requests/s, median 9 ms.
- Checked:
fsync2.6 ms per request, unremarkable before, now the next bottleneck together with the single CPU of the Python process. - Report: symptom, findings with numbers, fixes with control measurements, what was checked, what is open.
Commands at a glance
| Command | What it does | Parameters |
|---|---|---|
wrk -t2 -c8 -d30s --latency URL |
generate load and measure response time | --latency percentiles |
curl -s -o /dev/null -w "%{time_connect} %{time_total}\n" URL |
one request, connect and total time | -w output format |
uptime, vmstat 1 5,
top -b -n 1 -o %CPU |
first look at the CPU | – |
systemd-cgtop -b -n 3 -d 4 --depth=2 -c system.slice |
CPU per service | -c by CPU |
cat /sys/fs/cgroup/system.slice/DIENST/cpu.pressure |
CPU pressure of a service | – |
systemctl show -p CPUWeight DIENST … |
weight of services | – |
ping -c 5 HOST |
round-trip time | -c count |
tc qdisc show dev eth0 /
sudo tc qdisc del dev eth0 root |
show / remove the queue | – |
Environment=PYTHONPERFSUPPORT=1 |
Python functions visible to perf | from Python 3.12 |
sudo perf record -F 99 -g -p PID -o DATEI -- sleep 10 |
profile of a running process | -g call chains |
sudo perf report -i DATEI --children --stdio --sort symbol -g none |
time per function including callees | --children |
sudo syscount-bpfcc -L -p PID -d 10 |
system calls with total duration | -L latency |