CSE Roadmap

Core tier · about 3 hours · 7 parts

Part 5 of 7 · 2 min read

Counting

Think of a bank statement that totals each kind of payment: rent, food, travel. You see where the money went without reading every line. The analogy stops in one place: a statement lists money, and the summary of calls lists counts and times.

In plain words: strace -c runs the program and prints a table at the end: for each call, the share of time, total seconds, time per call, the count and the number of errors. Sort by count with -S calls. It shows what a program mostly does: many small read calls, many failed stat calls, or a loop of mmap.

A worked example. strace -c -S calls ls /tmp printed, among others:

% time     seconds  usecs/call     calls    errors syscall
 15.67    0.000168           9        17           mmap
  6.62    0.000071           8         8           close
  5.22    0.000056           8         7           fstat
  5.50    0.000059           9         6           openat
------ ----------- ----------- --------- --------- ----------------
100.00    0.001072          15        71         6 total

The command ls /tmp made 71 system calls in all, with 6 errors, and 17 of them were mmap: again the cost of loading libraries. The total time inside the kernel was 1.072 ms. Compare with the hello program: 34 calls for a program that does nothing. A command that calls the system thousands of times for a simple job (for example read of one byte at a time) shows at once in the count column.

The key lines. Counting turns a long trace into a short question: which call is called most, and does it fail? The time column is influenced by tracing, as the next section shows.

System call summary

A system call summary (strace -c) is a table that counts, for each system call, how many times the program made it, how many failed and how much time the kernel spent in it, printed when the program ends.

Example: ls /tmp made 71 calls, 6 of them failing.

Check yourself: 1. What does the errors column count? 2. Which flag sorts the table by count? 3. What can a very high count of tiny read calls tell you?