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.
{ time sort -u words.txt | wc -l; } 2> total.txtKeep 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.
vmstat 1 5Step 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.
strace -c -o strace-summary.txt sort -u words.txt > /dev/nullRead 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.
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.