如何将循环传递给命令 perf?

How do I pass a loop to the command perf?

以下命令有效。

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

但是,此命令会导致语法错误。

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

我尝试通过转义 ; 来更正此问题,但它只会导致不同的错误。

$ 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

将我的 bash 循环传递给 perf 的正确语法是什么?

perf 只能 运行 带参数的可执行文件(execve 语义)。它本身不会调用 shell,因此不会调用 运行 bash 命令。

您可以手动调用 shell 并让它解释您的命令:

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