I am trying to understand the --cpu option of perf-stat. In the man pages it says:

Count only on the list of CPUs provided. Multiple CPUs can be provided as a comma-separated list with no space: 0,1. Ranges of CPUs are specified with -: 0-2. In per-thread mode, this option is ignored. The -a option is still necessary to activate system-wide monitoring. Default is to count on all CPUs.

Can we control with this option on which CPU the program will be executed, ie. if I put --cpu 0, will the program be executed only on CPU 0? If not, what is the value of this option (since the results are probably going to be different each time we run perf stat with this option)?

1 Answer 1


The value of the --cpu is the CPU for which it should return statistics. It does not mean that the binary itself will be executed on that same CPU, although it might do that as well.

If you want for a single CPU:

perf-stat --cpu 0

will return statistics for CPU 0.

For more than one CPU (as an example - two CPUs):

perf-stat --cpu 1,2

will return statistics for CPUs 1 and 2.

And as a range:

perf-stat --cpu 0-4

will return statistics for CPUs from 0 to 4 (0, 1, 2, 3, 4).

Can we control with this option on which CPU the program will be executed, ie. if I put --cpu 0, will the program be executed only on CPU 0?

If you use --cpu 0 the program will return the statistics only for CPU 0, it does not guarantee that it will be executed only on CPU 0, nowhere in the docs does it say that it runs on that same CPU, however, it might still do that.


From testing, I can see that perf stat does actually execute the command on that certain CPU you define with --cpu.

  • Sorry, but I still don't understand the value of it. If the numbers will probably be different in each run, then I don't get anything useful. Hypothetically, If I am testing the performance of the program, I can not do some changes in my program and run the statistics again, to see if something is better. Also, if I am creating a processor, and I'm interested in CPU0, different outputs each time will not tell me anything. Or am I missing something?
    – adamm
    May 16, 2019 at 8:51
  • @adamm you can't get exact same statistics for CPU with each run.. that's impossible. The only statistic that can be almost the same with each run is the frequency (GHz). What the output means and why is it different every time is a whole different question. The value of it is just the CPU you want to gather statistics for.
    – Fanatique
    May 16, 2019 at 9:06
  • Regarding your last point, perf doesn’t limit the process’ execution to one of the selected CPUs. Run e.g. perf stat ls to get a baseline, then perf stat --cpu 0 ls — you should see that the latter often shows much smaller counter values than the baseline, which means that ls ran on another CPU. May 16, 2019 at 9:12
  • @StephenKitt exactly, that is because perf stat ls defaults to all CPUs, it's not because it ran on another CPU, it is because it ran on one CPU.
    – Fanatique
    May 16, 2019 at 11:12
  • By “ls ran on another CPU”, I meant that with perf stat --cpu 0 ls, ls ran on another CPU than the one being measured. May 16, 2019 at 11:42

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .