Este contenido no está disponible en el idioma seleccionado.
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
perfuser space tool installed as described in Installing perf.
20.1. The purpose of perf stat Copiar enlaceEnlace copiado en el portapapeles!
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 Copiar enlaceEnlace copiado en el portapapeles!
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
perfuser space tool installed as described in Installing perf.
Procedure
Count the events.
Running the
perf statcommand without root access will only count events occurring in the user space:perf stat ls
$ perf stat lsCopy 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 statruns 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 lsCopy 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 statoperates in per-thread mode. To change to CPU-wide event counting, pass the-aoption toperf stat. To count CPU-wide events, you need root access:perf stat -a ls
# perf stat -a lsCopy to Clipboard Copied! Toggle word wrap Toggle overflow
20.3. Interpretation of perf stat output Copiar enlaceEnlace copiado en el portapapeles!
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 statcounts 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 Copiar enlaceEnlace copiado en el portapapeles!
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
perfuser space tool installed as described in Installing perf.
Procedure
Attach
perf statto a running process:perf stat -p ID1,ID2 sleep seconds
$ perf stat -p ID1,ID2 sleep secondsCopy to Clipboard Copied! Toggle word wrap Toggle overflow The previous example counts events in the processes with the IDs of
ID1andID2for a time period ofsecondsseconds as dictated by using thesleepcommand.