[root@root test]# perf stat -a -e "r81d0","r82d0" -v ./a
r81d0: 71800964 1269047979 1269006431
r82d0: 26655201 1284214869 1284214869
这是详细选项的输出,定义在内核的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 }
计数来自struct perf_counts_values,定义为http://lxr.free-electrons.com/source/tools/perf/util/evsel.h?v=3.18#L12,具有三个uint64_t值的数组,命名为val、ena、run
三个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 与总运行时间成正比。当您要求perf 统计大量事件时需要这样做,而这些事件无法一次监控(硬件通常有多达 5-7 个性能监视器)。在这种情况下,内核性能将为某些执行部分运行所需事件的子集;并且子集将被多次更改。通过 ena 和 run 计数,perf 可以估计在多路复用情况下事件监控的不准确程度。
Performance counter stats for './a':
71,800,964 r81d0 [100.00%]
26,655,201 r82d0
在您的情况下,两个事件是同时映射的,不需要多路复用;您的 ena 和 run 计数器已关闭。而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 选项重新运行任务 N 次以获取统计信息的情况下输出(man: --repeat=<n> 重复命令并打印平均值 + stddev (max: 100))
1176 print_noise(counter, 1.0);
还有[100.00%]打印机:
1178 if (run != ena)
1179 fprintf(output, " (%.2f%%)",
1180 100.0 * run / ena);
如果 run 和 ena 时间相等,并且您的 r82d0 事件相等,它将不会打印 100%。您的 r81d0 事件的 run 和 ena 略有不同,因此 100% 打印在一行中。
我知道perf stat -d 可能不准确,因为它要求的事件太多;并且不会有 100% 的多路复用,而是 53% 之类的。这意味着“这个事件在它的某些随机部分中仅占程序运行时间的 53%”;如果你的程序有几个独立的计算阶段,那么运行/ena 比率低的事件将不太准确。