Este conteúdo não está disponível no idioma selecionado.

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

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

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 perf user 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 stat command without root access will only count events occurring in the user space:

      $ perf stat ls
      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

      In the previous example, 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, enter:

      # perf stat ls
      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 to perf stat. To count CPU-wide events, you need root access:

    # perf stat -a ls

12.3. Interpretation of perf stat output

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 stat counts 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

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 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 and ID2 for a time period of seconds seconds as dictated by using the sleep command.

Red Hat logoGithubredditYoutubeTwitter

Aprender

Experimente, compre e venda

Comunidades

Sobre a documentação da Red Hat

Ajudamos os usuários da Red Hat a inovar e atingir seus objetivos com nossos produtos e serviços com conteúdo em que podem confiar. Explore nossas atualizações recentes.

Tornando o open source mais inclusivo

A Red Hat está comprometida em substituir a linguagem problemática em nosso código, documentação e propriedades da web. Para mais detalhes veja o Blog da Red Hat.

Sobre a Red Hat

Fornecemos soluções robustas que facilitam o trabalho das empresas em plataformas e ambientes, desde o data center principal até a borda da rede.

Theme

© 2026 Red Hat
Voltar ao topo