Questo contenuto non è disponibile nella lingua selezionata.
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
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
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
Example 20.1. Output of perf stat ran without root access
Desktop Documents Downloads Music Pictures Public Templates Videos Performance counter stats for 'ls': 1.28 msec task-clock:u # 0.165 CPUs utilized 0 context-switches:u # 0.000 M/sec 0 cpu-migrations:u # 0.000 K/sec 104 page-faults:u # 0.081 M/sec 1,054,302 cycles:u # 0.823 GHz 1,136,989 instructions:u # 1.08 insn per cycle 228,531 branches:u # 178.447 M/sec 11,331 branch-misses:u # 4.96% of all branches 0.007754312 seconds time elapsed 0.000000000 seconds user 0.007717000 seconds sys
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
Example 20.2. Output of perf stat ran with root access
Desktop Documents Downloads Music Pictures Public Templates Videos Performance counter stats for 'ls': 3.09 msec task-clock # 0.119 CPUs utilized 18 context-switches # 0.006 M/sec 3 cpu-migrations # 0.969 K/sec 108 page-faults # 0.035 M/sec 6,576,004 cycles # 2.125 GHz 5,694,223 instructions # 0.87 insn per cycle 1,092,372 branches # 352.960 M/sec 31,515 branch-misses # 2.89% of all branches 0.026020043 seconds time elapsed 0.000000000 seconds user 0.014061000 seconds sys
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
Additional resources
-
perf-stat(1)
man page on your system
20.3. Interpretation of perf stat output
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
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
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.
Additional resources
-
perf-stat(1)
man page on your system