Your phone buzzes at 9 pm. The shop’s checkout is slow, customers are giving up, and you are the one on call. You open a terminal on the server. What do you type first?
This is the capstone of the course. Over three nights, the same server is slow for three different reasons, and each reason comes from a module you have studied. Nobody will tell you which one it is. You will find out the way engineers do: run a command, read what the machine says, decide what to look at next, and finally decide what to change.
How the cases work
Each case below is a simulator with a terminal. When it asks you a question, the side panel shows your choices: commands to run, and at the end, changes to make. Pick one and the terminal shows that command’s output. The bar above the terminal lists the commands you have run so far.
You cannot break anything, and there is more than one road to the answer. A command that does not show the problem still teaches you something: what a healthy number looks like. To try a different choice, click an earlier step in the step list (or press ← to go back) and pick again. ↻ starts the case over.
The output is made up, but it is what these commands really print, and the numbers agree with each other: the memory in top is the memory in free, and the disk’s reads in iostat are the bi column of vmstat.
Your toolkit
Every command answers one question. Knowing which question you are asking is most of the skill.
| Command | The question it answers |
|---|---|
top |
Which processes use the CPU and memory, and how loaded is the machine? |
vmstat 1 |
Every second: how many threads are runnable (r) or blocked (b), is the machine swapping (si, so), and where does CPU time go (us, sy, id, wa)? |
free -m |
How much memory is used, and how much is still available, in MiB? |
iostat |
How busy is each disk (%util), and how long does a request wait (await)? |
perf top |
Which functions are the CPUs running right now? |
strace -c |
Which system calls is a process making, and how long does it spend in each? |
ps |
What state is each process or thread in, and where in the kernel is it waiting? |
dmesg |
What has the kernel said lately, for example that it killed a process? |
One number needs a word before you start. top begins with the load average: three numbers, the average number of threads that wanted to run over the last 1, 5 and 15 minutes. Compare it with the number of CPUs. A load of 4 on a 4-CPU machine means every CPU was busy; a load of 8 means that, on average, as many threads again were waiting in a run queue (Lesson 3.5). Linux has a twist: its load also counts threads in state D, waiting for a disk (Lesson 2.2). Keep that in mind on Thursday.
Case 1: Tuesday
The checkout API runs on one server with 4 CPUs. Tonight nothing was deployed, and the slowness started a few minutes after 9 pm.
Case 1: Tuesday, 21:04
-
The pager goes off
It is Tuesday, just after 9 pm. A checkout usually takes 180 ms; now the slowest ones take 2400 ms. Nothing was deployed today. You are logged in to the server.
- Time
- Tue 21:04
- Checkout p99
- 2400 ms (usually 180)
- Last command
- none yet
Ask the server: what do you run first?
- top
- vmstat 1
- free -m
- iostat
Whichever road you took, the clue was the same: a load of 8.84 on 4 CPUs, with 10 runnable threads and no time spent waiting for anything else. That is CPU contention, the subject of Module 3. The fair scheduler did exactly what it is built to do: it shared the CPUs between runnable threads by weight. The report simply had more threads that always wanted to run.
The fix that worked did not make anything faster. It changed the weights. With report-gen at nice 19, the load hardly moved (8.97 a few minutes later), but the p99 dropped back to 210 ms. A busy machine is fine; a machine where the important work waits behind the unimportant work is not.
Case 2: Wednesday
The report now runs at 3 am. But on Wednesday the checkout is slow again around 9 pm, and this time some requests fail completely. This afternoon, version 2.14 of checkout-api was deployed. It added an in-memory cache of the product catalogue to make checkouts faster.
Case 2: Wednesday, 21:06
-
Slow, and failing
Wednesday, 9 pm again. The report runs at 3 am now, so it is not that. Version 2.14 of checkout-api went out this afternoon, with its new product cache. Checkouts take up to 3900 ms, and some give up completely.
- Time
- Wed 21:06
- Checkout p99
- 3900 ms (usually 180)
- Last command
- none yet
Ask the server: what do you run first?
- top
- vmstat 1
- iostat
- free -m
Here the clue was si and so: 1840 KiB a second coming back from swap and 2216 going out, with the CPUs mostly idle. That is the picture of thrashing from Module 6. One process held 82.4% of memory, and its working set had outgrown the machine. Every major fault turned a memory access of nanoseconds into a disk read of milliseconds.
Notice what did not help. More swap only gave the problem more room. Waiting let the OOM killer end it in the bluntest way possible, by killing the very service you were trying to save. The real fixes were to shrink the working set (roll back, then cap the cache) and to contain the damage next time with a memory limit, the same cgroup idea you met with containers.
Case 3: Thursday
No deploy today. The report runs at 3 am, and checkout-api now has a memory limit. At 9 pm, it is slow again.
Case 3: Thursday, 21:07
-
Slow again
Thursday, 9 pm, slow again. Nothing was deployed, the report runs at 3 am, and checkout-api's memory has a limit since yesterday. Checkouts take up to 4200 ms. Something new is going on.
- Time
- Thu 21:07
- Checkout p99
- 4200 ms (usually 180)
- Last command
- none yet
Ask the server: what do you run first?
- top
- vmstat 1
- iostat
- perf top
The first two cases were about a resource running out: CPU, then memory. This one had plenty of CPU (61.3% of the time spent waiting for I/O, and a load of only 4.12 on 4 CPUs) and plenty of memory. The disk was full, at 100% busy, but that alone does not explain the checkout, which barely used the disk.
The explanation needed two modules at once. From Module 7: fsync waits until the journal has committed, so on a saturated disk each call is slow. From Module 4: a lock turns many threads into a queue of one. Put an fsync inside a lock, and in ten seconds only 452 checkouts could pass, about 9.94 seconds of fsync back to back. With a pool of 256 threads, the count was still 455: the lock, not the thread count, sets the pace.
In the real world
Engineers who do this often keep a short checklist of commands for the first minute on a slow machine, very like the toolkit above: load, vmstat, iostat, free, then the busiest processes. A common way to organise it is to go through each resource (CPUs, memory, disks, network, and locks inside programs) and ask three questions: how busy is it, is work queueing for it, and is it reporting errors? A checklist stops you from guessing, and from fixing the resource you happen to know best.
Two habits from these cases carry over to any system. First, measure before you change anything: two of the wrong choices above (more memory, more CPUs) cost money and changed nothing. Second, after a fix, look at the numbers again to confirm that the change did what you expected.
What the three cases have in common
Look back at the three clues:
- Tuesday: a long run queue and CPUs at 100%. Threads were waiting for a CPU, and the fix changed who gets it first (Module 3).
- Wednesday: swap-ins, swap-outs and major faults, with idle CPUs. Threads were waiting for memory that had been pushed out to disk, and the fix made the working set fit (Module 6).
- Thursday: high iowait, a saturated disk, threads in
Dand infutex. Threads were waiting for a lock, which waited for the disk, and the fix took the disk out of the lock (Modules 4 and 7).
In every case the program was mostly waiting, not computing, and the work was to find out what it was waiting for. Each command you ran shows one kind of waiting: the run queue, the swap columns, the disk queue, the lock. That is the whole course in one sentence: an operating system decides who waits for what, and most performance problems are a queue in the wrong place.
Quick check
Quick check
-
Linux counts uninterruptible threads in the load as well as runnable ones. A high load with idle CPUs points at I/O, not at the CPU.
-
The job's threads still want to run, so they still count in the load. Nice changes the weights, so the service's threads win when both want a CPU.
-
Swap traffic with idle CPUs is thrashing. The working set does not fit in memory, so the fix is to make it fit, not to give it more disk to thrash on.
-
The lock lets one thread through at a time, so the disk's speed sets the pace for everyone. Moving the slow wait out of the lock, and grouping writes, removes the queue without giving up durability.
Review: the whole course
Coming back to an idea after a break is what makes it stick. These questions come from earlier modules, starting with any you missed. Answer from memory before you look anything up.
Spaced review
This review picks questions for you, so it needs JavaScript.
Course recap
You started with the operating system as a referee: a kernel running in a privileged mode, reached through system calls, that turns one machine into a safe, shared one. You followed a program as it became a process, was copied by fork and replaced by exec, and split into threads. You watched schedulers decide who runs next, from FCFS to MLFQ and Linux’s fair scheduler. You lost updates to race conditions, fixed them with locks, semaphores and condition variables, and saw the four conditions behind deadlock. You translated addresses through page tables and the TLB, took page faults, chose victims for replacement, and watched a machine thrash. You drove devices with interrupts and DMA, scheduled a disk, looked inside a file system, and saw how a journal survives a crash. Then you saw whole systems share one machine as virtual machines and containers. In this capstone you used all of it to find three different queues on a slow server.
Where to go next
You now know what an operating system does and why. To go deeper:
- Operating Systems: Three Easy Pieces by Remzi and Andrea Arpaci-Dusseau, a free online book that covers this course’s topics in more depth, with exercises: https://pages.cs.wisc.edu/~remzi/OSTEP/
- xv6, a small teaching operating system from MIT, short enough to read in full. Reading how it implements
fork, the scheduler and the file system makes the ideas very concrete: https://github.com/mit-pdos/xv6-riscv - The Linux kernel documentation, for how a real kernel does it, including the scheduler, memory management and the tools for measuring them: https://docs.kernel.org/
The best next step is to try the commands from this capstone on your own machine. Run top, vmstat 1 and free -m while it is idle, then while it compiles something big, and learn what your own normal looks like.
Join the discussion