Ce contenu n'est pas disponible dans la langue sélectionnée.
Chapter 12. Counting events during process execution with perf stat
You can use the perf stat command to count hardware and software events during process execution.
12.1. The purpose of perf stat Copier lienLien copié sur presse-papiers!
The perf stat command generates counts for hardware and software performance events. If you do not specify any events, then perf stat counts a set of common hardware and software events.
12.2. Counting events with perf stat Copier lienLien copié sur presse-papiers!
You can use perf stat to count hardware and software event occurrences while a command is running 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. - You have root access to count both user-space and kernel-space events.
Procedure
Count the events.
Running the
perf statcommand without root access will only count events occurring in the user space:$ perf stat lsDesktop 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 sysIn the previous example,
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, enter:
# perf stat lsDesktop 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 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
12.3. Interpretation of perf stat output Copier lienLien copié sur presse-papiers!
perf stat executes a specified command and counts event occurrences during the command execution. It 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, in default mode,perf statcounts cycles and instructions to calculate and display instructions per cycle in the right-most column. Branch misses as a percentage of total branches show similar behavior in default counts.
12.4. Attaching perf stat to a running process Copier lienLien copié sur presse-papiers!
You can attach perf stat to a running process. This instructs 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 secondsThe previous example counts events in the processes with the IDs of
ID1andID2for a time period ofsecondsseconds as dictated by using thesleepcommand.