"perf stat" 输出是什么意思?

What does "perf stat" output mean?

我使用“perf stat”命令对一些事件进行统计:

[root@root test]# perf stat -a -e "r81d0","r82d0" -v ./a
r81d0: 71800964 1269047979 1269006431
r82d0: 26655201 1284214869 1284214869

 Performance counter stats for './a':

        71,800,964 r81d0                                                        [100.00%]
        26,655,201 r82d0

       0.036892349 seconds time elapsed

(1) 我知道71800964是“r81d0”的计数,但是12690479791269006431是什么意思?
(2)“[100.00%]”是什么意思?

我试过“perf stat --help”,但无法得到这些值的解释。

[root@root test]# perf stat -a -e "r81d0","r82d0" -v ./a
r81d0: 71800964 1269047979 1269006431
r82d0: 26655201 1284214869 1284214869

这是 verbose 选项的输出,定义在内核的 tools/perf/builtin-stat.c 文件中:

391 /*
392  * Read out the results of a single counter:
393  * aggregate counts across CPUs in system-wide mode
394  */
395 static int read_counter_aggr(struct perf_evsel *counter)

408         if (verbose) {
409                 fprintf(output, "%s: %" PRIu64 " %" PRIu64 " %" PRIu64 "\n",
410                         perf_evsel__name(counter), count[0], count[1], count[2]);
411         }

count 来自 struct perf_counts_values,定义为 http://lxr.free-electrons.com/source/tools/perf/util/evsel.h?v=3.18#L12,具有三个 uint64_t 值的数组,命名为 valenarun

三个 count 值由内核填充并从 fd 中读取,使用 perf_event_open() 系统调用打开。有man perf_event_open的相关部分:http://man7.org/linux/man-pages/man2/perf_event_open.2.html

   read_format
          This field specifies the format of the data returned by
          read(2) on a perf_event_open() file descriptor.

          PERF_FORMAT_TOTAL_TIME_ENABLED
                 Adds the 64-bit time_enabled field.  This can be used
                 to calculate estimated totals if the PMU is
                 overcommitted and multiplexing is happening.

          PERF_FORMAT_TOTAL_TIME_RUNNING
                 Adds the 64-bit time_running field.  This can be used
                 to calculate estimated totals if the PMU is
                 overcommitted and multiplexing is happening.  ...

perf stat enables all TIME flags 如果 scale 为真 -

298         if (scale)
299                 attr->read_format = PERF_FORMAT_TOTAL_TIME_ENABLED |
300                                     PERF_FORMAT_TOTAL_TIME_RUNNING;

所以,第一个计数器是原始事件计数; second 与收集此事件的时间成正比,last 与总 运行ning 时间成正比。当您要求 perf 对大量事件进行统计时,这是必需的,这些事件不能一次被监控(硬件通常有多达 5-7 个性能监视器)。在这种情况下,内核 perf 将 运行 执行某些部分所需事件的子集;和子集将被改变几次。使用 enarun 计数,perf 可以估计多路复用情况下事件监控的不准确程度。

 Performance counter stats for './a':

    71,800,964 r81d0                                            [100.00%]
    26,655,201 r82d0

在你的例子中,两个事件被同时映射而不需要多路复用;您的 enarun 计数器已关闭。 print_aggr 函数打印出它们的比率:

1137                                 val += counter->counts->cpu[cpu].val;
1138                                 ena += counter->counts->cpu[cpu].ena;
1139                                 run += counter->counts->cpu[cpu].run;

Print_noise 会在 -r N 选项的情况下输出到 re运行 任务 N 次以获取统计信息 (man: --repeat=<n> repeat command and打印平均值 + stddev (max: 100))

1176                                 print_noise(counter, 1.0);

还有[100.00%]台打印机:

1178                                 if (run != ena)
1179                                         fprintf(output, "  (%.2f%%)",
1180                                                 100.0 * run / ena);

如果 运行 和 ena 时间相等,并且您的 r82d0 事件也相等,则不会打印 100%。您的 r81d0 事件 运行 和 ena 略有不同,因此 100% 打印在一行中。

我知道 perf stat -d 可能不准确,因为它要求的事件太多;并且不会有 100% 的多路复用,而是大约 53%。意思是"this event was counted only in 53% of the program runtime in some random parts of it";如果你的程序有几个独立的计算阶段,低 run/ena 比率的事件将不太准确。