$_ CLI In Depth
Learning progress 0%
case performance tmux vim git

System Deep-Dive: Profiling a Slow Pipeline

5 min read Module 18: Performance, Debugging & Power Tools Updated Dec 5, 2026
Cheat Sheet
0:00 / 3:09

The report

A colleague says a daily report takes twenty minutes, and the pipeline reads a large log, filters it, counts events, and sorts the counts. The goal is to find out which stage is slow and why, and to make a change that the numbers support. The pipeline is a shell line of several commands joined by pipes, and each stage is a separate process that runs at the same time as the others.

Step 1: time the whole

Start with the total. Wrap the pipeline in the time keyword and write the report to a file. The real time is the number the user cares about, and the user and system times tell you whether the work is computation or kernel activity. A pipeline with a total user time close to its real time is busy on the CPU. A pipeline whose real time is much larger than the sum of user and system is waiting for something.

BASH1 lines
{ time sort -u words.txt | wc -l; } 2> total.txt

Keep this report. It is the baseline for every later comparison, and a change that does not reduce it is not a fix.

Step 2: time each stage

Pipelines are concurrent, so the stage times do not add up to the total. To find the slow stage, run each part separately with its input redirected from a file. A stage that takes most of the time alone is the one to change. A stage that is fast alone but slow in the pipeline may be waiting for its input, which points at the stage before it.

The bash variable PIPESTATUS records the exit status of each stage after a pipeline finishes. It does not record time, but it does confirm that every stage ran, which matters when a stage fails early and the others finish quickly. A pipeline that looks fast may simply have stopped at the first error.

Step 3: watch the system while it runs

Start the pipeline in the background, and in another terminal or a second command run vmstat with an interval. The r column shows the number of runnable processes, and the wa column shows CPU time waiting for storage. If r stays near the core count and wa is low, the stages are CPU bound. If wa rises and bi is high, the pipeline reads storage faster than it can process the data, and the fix is to read less or read it in larger chunks.

top in batch mode shows which process uses the most CPU at one moment. Run it once during the slow part, and the top row names the stage.

BASH1 lines
vmstat 1 5

Step 4: trace the slow stage

When the stage is known, strace shows what it does with the kernel. The summary mode prints the number of calls and their time for each system call. If the stage makes millions of tiny read calls, the buffering is poor, and a larger read or a tool that buffers by default is the fix. If the time sits in one call, such as a wait on a socket, the problem is outside the stage.

BASH1 lines
strace -c -o strace-summary.txt sort -u words.txt > /dev/null

Read the summary from the top. The call with the largest time share is where the stage spends its life, and the calls next to it explain why.

Step 5: change one thing and measure again

Make one change, such as sorting with a larger buffer, reading the input once instead of twice, or replacing a loop with a single awk program. Measure the total again with the same input. A change that cuts the real time without raising the system time is a real improvement. A change that moves work from one stage to another is not finished until the total falls.

Tip

Write down each change next to its measurements. A profile that shows which change helped is worth more than a faster script that nobody can explain.

What the case teaches

Profiling is a sequence of narrow questions: how long, which stage, what the system is doing, which call dominates, and did the change help. The tools in this module answer one question each. The time keyword gives the clocks, free, vmstat, and top give the pressure, and strace gives the calls. Used in that order, they turn a vague complaint into a specific fix.

Writing down the answer

The result of a profile is a short statement that another person can check: the slow stage, the measured share of time, the system evidence, the change, and the new total. A statement like that is more useful than a faster script, because it tells the next person where to look when the data grows. Keep the raw files of the measurements next to the statement, so the numbers can be read again.

Real, user and sys: where the time went

Visualizer

time reports three clocks. Reading them tells you whether a command was waiting, computing, or making system calls.

1 / 3
The three numbers `time` prints
ClockCountsFor sleep 1
realwall clock time, start to finishabout 1 s
userCPU time spent in the program’s own codeabout 0 s
sysCPU time spent in the kernel for its system callsabout 0 s

time prints three numbers once the command ends. real is the wall clock. user and sys add up the CPU time the process actually used.

time sleep 1
Check yourself Question 1 of 4
Concept Check The strace summary option recall

Which strace option switches to a summary table of call counts and time?

Recap & Key Takeaways

Time the whole pipeline first, because the real time is the baseline and the user and system times show what kind of work it does.
Time each stage alone with its input from a file, since stages in a pipeline run at the same time and their times do not add up.
vmstat with an interval shows whether the slow stage is CPU bound or waiting on storage while it runs.
strace -c shows the call counts and time for the slow stage, which points at buffering or waiting.
Was this lesson helpful? Spot an issue or have a suggestion? Let us know on GitHub.
Report an issue