Real Systems, Performance & Frontiers

OS performance tuning

OS performance tuning is the practical craft of measuring why a system is slower than it could be and then changing something to make it faster. Picture a city's traffic engineer: before adding a lane, they count cars at every junction to find where the jam actually is. The cardinal rule is the same — measure first, then change. Guessing where the slowdown is wastes effort, because the real bottleneck is often not where intuition says.

The method centres on finding the bottleneck: the single resource (CPU, memory, disk, or network) that is saturated and limiting everything else, since speeding up anything but the bottleneck changes nothing. You benchmark to get repeatable numbers, then profile a running system to see where time is actually going. Common operating-system costs to watch for are surprisingly expensive: a system call crosses from user mode into kernel mode and back, which costs far more than an ordinary function call; a context switch saves and restores process state and pollutes the CPU caches; a page fault or a trip to disk costs orders of magnitude more than a memory access; and lock contention can leave many cores idling while they wait for one another. Tuning then means reducing these — batching system calls, cutting unnecessary copies, improving locality of reference so the caches hit, choosing a better scheduler or I/O policy, or simply giving a workload more memory so it stops thrashing.

Two honest cautions. First, Amdahl's law: if a part you optimize is only a small fraction of the total time, even making it infinitely fast barely helps — always attack the biggest cost. Second, beware optimizing without measuring on the real workload; a change that helps a benchmark can hurt the actual application, and a fix that helps one machine can hurt another. Performance work is empirical: measure, change one thing, measure again.

A program reads a file one byte at a time with a million separate read calls and feels slow. Profiling shows almost all the time is spent crossing into the kernel. The fix is not a faster disk but fewer system calls: read in 64 KB chunks instead, turning a million kernel crossings into a few hundred — often a tenfold speedup with no new hardware.

Find the bottleneck first; fewer system calls beat a faster disk here.

The classic trap is optimizing by guesswork. Always measure first: the slowest part is frequently not where you expect, and by Amdahl's law speeding up anything but the true bottleneck gains almost nothing.

Also called
performance optimization效能優化效能調整