Search code examples
bashperf

How do I pass a loop to the command perf?


The following commands are valid.

perf stat -I 500 -e cycles sleep 5
while true; do : ; done

However, this command results in a syntax error.

perf stat -I 500 -e cycles  while true; do : ; done
bash: syntax error near unexpected token `do'

I tried correcting this by escaping the ;, but it just results in a different error.

$ perf stat -I 500 -e cycles  while true\; do : \; done                                                                                                                                                                                                                                    
#           time             counts unit events
     0.005386453      <not counted>      cycles                   
     0.005678719      <not counted>      cycles                   
Workload failed: No such file or directory

What is the correct syntax to pass my bash loop to perf?


Solution

  • perf can only run executables with parameters (execve semantics). It does not invoke a shell by itself, so won't run bash commands.

    You can manually invoke a shell and have it interpret your command:

    perf stat -I 500 -e cycles bash -c 'while true; do true; done'