brewedbits.dev

Personal blog with guides and news on selfhosting, Linux and AI, from the Netherlands

nextDash nextdash --aboutYour bookmarks. Your terminal. checks your links knows your Docker host one container, no cloudLive demo →nextdash.cc ↗

Tip: strace -c counts the system calls of a Linux program to show where its time goes

Linux
~/tips/tip-linux-strace-c-syscall-summary

strace -c -f ./slow-script.sh

Get a summary table of syscalls, errors and time instead of thousands of trace lines.

A full strace run prints thousands of lines that are hard to read. When you only want to know what a slow program spends its time on, a summary is more useful.

strace -c -f ./slow-script.sh

The man page describes -c as counting time, calls and errors for every system call and printing a summary when the program exits, instead of the normal output. -f follows child processes, so commands the script starts are counted too. The table shows the share of time, the total time, the average per call, the number of calls and errors, and the name.

The errors column is often where the story is. Thousands of failed openat or stat calls mean a program searches many paths for a file or library. Lots of futex or poll time points at waiting rather than working.

By default the table measures system time, which leaves out time spent waiting. For a program that is slow because it waits on network or disk, add -w for wall clock time instead:

strace -c -w -f -p 1234

With -p you attach to a running process; stop with Ctrl-C to see the table. I’d sort it with -S calls when the number of calls matters more than their time.

strace slows the traced program down noticeably, so absolute times are inflated. Compare the shares, not the seconds. Attaching to another user’s process needs root, and many systems also restrict tracing through the ptrace_scope setting.

Further reading: strace(1) manual page

More Linux tips a2query -m rewrite renice -n 10 -p 4242 cat config.yml | xsel -ib xvfb-run -a your-program --some-flag dpkg --get-selections > packages.txt All Linux tips (261) All topics

Link