[root@root test]# perf stat -a -e "r81d0","r82d0" -v ./a
r81d0: 71800964 1269047979 1269006431
r82d0: 26655201 1284214869 1284214869
这是详细选项的输出,如中所定义工具/perf/builtin-stat.c http://lxr.free-electrons.com/source/tools/perf/builtin-stat.c?v=3.18内核文件:
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 http://lxr.free-electrons.com/source/tools/perf/util/evsel.h?v=3.18#L12包含三个 uint64_t 值的数组,命名为val
, ena
, run
Three count
值由内核填充并从 fd 中读取,用以下命令打开perf_event_open()
系统调用。有相关部分man perf_event_open
: http://man7.org/linux/man-pages/man2/perf_event_open.2.html 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
启用所有 TIME 标志 http://lxr.free-electrons.com/source/tools/perf/builtin-stat.c?v=3.18#L294 if scale
是真的 -
298 if (scale)
299 attr->read_format = PERF_FORMAT_TOTAL_TIME_ENABLED |
300 PERF_FORMAT_TOTAL_TIME_RUNNING;
因此,第一个计数器是原始事件计数; Second 与收集该事件的时间成正比,last 与总运行时间成正比。当你询问时需要这个perf
统计大量事件,这些事件无法立即监控(硬件通常最多有 5-7 个性能监控器)。在这种情况下,内核中的 perf 将为执行的某些部分运行所需事件的子集;并且子集将被更改多次。和ena
and run
通过计数,perf 可以估计在多路复用情况下事件监控的不准确程度。
Performance counter stats for './a':
71,800,964 r81d0 [100.00%]
26,655,201 r82d0
在您的情况下,两个事件同时映射,无需多路复用;你的ena
and run
柜台很近。和print_aggr http://lxr.free-electrons.com/source/tools/perf/builtin-stat.c?v=3.18#L1117函数打印它们的比率:
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(最大值: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 比率较低的事件将不太准确。