如何使用 perf stat 计算 MIPS

How to calculate MIPS using perf stat

以下建议:

perf stat ./my_program on Linux will use CPU performance counters to record how many instructions it ran, and how many core clock cycles it took. (And how much CPU time it used, and will calculate MIPS for you).


示例生成以下输出,其中不包含计算的 MIPS 信息。

 Performance counter stats for './hello.py':

       1452.607792 task-clock (msec)         #    0.997 CPUs utilized
               327 context-switches          #    0.225 K/sec
               147 cpu-migrations            #    0.101 K/sec
            35,548 page-faults               #    0.024 M/sec
     2,254,593,107 cycles                    #    1.552 GHz                     [26.64%]
   <not supported> stalled-cycles-frontend
   <not supported> stalled-cycles-backend
     1,652,281,933 instructions              #    0.73  insns per cycle         [38.87%]
       353,431,039 branches                  #  243.308 M/sec                   [37.95%]
        18,536,723 branch-misses             #    5.24% of all branches         [38.06%]
       612,338,241 L1-dcache-loads           #  421.544 M/sec                   [25.93%]
        41,746,028 L1-dcache-load-misses     #    6.82% of all L1-dcache hits   [25.71%]
        25,531,328 LLC-loads                 #   17.576 M/sec                   [26.39%]
         1,846,241 LLC-load-misses           #    7.23% of all LL-cache hits    [26.26%]

       1.456531157 seconds time elapsed

[Q] 我如何根据 perf stat 的输出正确计算出 MIPS?为了计算 MIPS,我应该根据从 perf stat?

获得的值遵循 instructions/seconds_time_elapsed

显然只是指令/秒。 (除以 100 万以缩放 Mega 度量前缀。)

使用总运行时间将为您提供整个程序的 MIPS,所有内核的总计,以及计算睡眠/等待所花费的任何时间。

Task-clock 将统计所有内核使用的总 CPU 时间,因此它将为您提供所有使用内核的 平均值 MIPS,不计算任何花费的时间睡眠。 (task-clock:u 只会计算用户 - space 时间,但 task-clock 也会计算在内核中花费的时间。)