Chapter 20. Counting events during process execution with perf stat
You can use the perf stat
command to count hardware and software events during process execution.
Prerequisites
-
You have the
perf
user space tool installed as described in Installing perf.
20.1. The purpose of perf stat Copy linkLink copied to clipboard!
The perf stat
command executes a specified command, keeps a running count of hardware and software event occurrences during the commands execution, and generates statistics of these counts. If you do not specify any events, then perf stat
counts a set of common hardware and software events.
20.2. Counting events with perf stat Copy linkLink copied to clipboard!
You can use perf stat
to count hardware and software event occurrences during command execution and generate statistics of these counts. By default, perf stat
operates in per-thread mode.
Prerequisites
-
You have the
perf
user space tool installed as described in Installing perf.
Procedure
Count the events.
Running the
perf stat
command without root access will only count events occurring in the user space:perf stat ls
$ perf stat ls
Copy to Clipboard Copied! Toggle word wrap Toggle overflow Example 20.1. Output of perf stat ran without root access
Copy to Clipboard Copied! Toggle word wrap Toggle overflow As you can see in the previous example, when
perf stat
runs without root access the event names are followed by:u
, indicating that these events were counted only in the user-space.To count both user-space and kernel-space events, you must have root access when running
perf stat
:perf stat ls
# perf stat ls
Copy to Clipboard Copied! Toggle word wrap Toggle overflow Example 20.2. Output of perf stat ran with root access
Copy to Clipboard Copied! Toggle word wrap Toggle overflow By default,
perf stat
operates in per-thread mode. To change to CPU-wide event counting, pass the-a
option toperf stat
. To count CPU-wide events, you need root access:perf stat -a ls
# perf stat -a ls
Copy to Clipboard Copied! Toggle word wrap Toggle overflow
20.3. Interpretation of perf stat output Copy linkLink copied to clipboard!
perf stat
executes a specified command and counts event occurrences during the commands execution and displays statistics of these counts in three columns:
- The number of occurrences counted for a given event
- The name of the event that was counted
When related metrics are available, a ratio or percentage is displayed after the hash sign (
#
) in the right-most column.For example, when running in default mode,
perf stat
counts both cycles and instructions and, therefore, calculates and displays instructions per cycle in the right-most column. You can see similar behavior with regard to branch-misses as a percent of all branches since both events are counted by default.
20.4. Attaching perf stat to a running process Copy linkLink copied to clipboard!
You can attach perf stat
to a running process. This will instruct perf stat
to count event occurrences only in the specified processes during the execution of a command.
Prerequisites
-
You have the
perf
user space tool installed as described in Installing perf.
Procedure
Attach
perf stat
to a running process:perf stat -p ID1,ID2 sleep seconds
$ perf stat -p ID1,ID2 sleep seconds
Copy to Clipboard Copied! Toggle word wrap Toggle overflow The previous example counts events in the processes with the IDs of
ID1
andID2
for a time period ofseconds
seconds as dictated by using thesleep
command.