linux - 使用 linux perf 实用程序每秒报告计数器,如 vmstat

标签 linux performancecounter perf

Linux 中有 perf command-linux 实用程序来访问硬件性能监控计数器,它使用 perf_events 内核子系统工作。

perf 本身基本上有两种模式:perf record/perf top 记录采样配置文件(例如,每 100000 个 cpu 时钟采样一次)周期或执行的命令),以及 perf stat 模式来报告应用程序(或整个系统)的周期/执行命令的总数。

是否有 perf 模式可以每秒(每 3、5、10 秒)打印系统范围或每个 CPU 的总计数摘要,就像在 vmstat< 中打印的那样 和 systat 系列工具(iostatmpstatsar -n DEV...如 http://techblog.netflix.com/2015/11/linux-performance-analysis-in-60s.html 中列出的)?例如,通过周期和指令计数器,我将获得系统(或每个 CPU)每秒的平均 IPC。

是否有任何非 perf 工具(在 https://perf.wiki.kernel.org/index.php/Tutorialhttp://www.brendangregg.com/perf.html 中)可以通过 perf_events 内核子系统获取此类统计信息?系统范围内的每进程 IPC 计算(以秒为单位)怎么样?

最佳答案

-I Nperf stat 选项“interval-print”,其中 N 是每 N 毫秒进行间隔计数器打印的毫秒间隔 (N>=10): http://man7.org/linux/man-pages/man1/perf-stat.1.html

  -I msecs, --interval-print msecs
       Print count deltas every N milliseconds (minimum: 10ms) The
       overhead percentage could be high in some cases, for instance
       with small, sub 100ms intervals. Use with caution. example: perf
       stat -I 1000 -e cycles -a sleep 5

  For best results it is usually a good idea to use it with interval
   mode like -I 1000, as the bottleneck of workloads can change often.

还可以以机器可读的形式导入结果,并且使用 -I 第一个字段是日期时间:

With -x, perf stat is able to output a not-quite-CSV format output ... optional usec time stamp in fractions of second (with -I xxx)

vmstat、systat系列工具iostatmpstat等定期打印性能为-I 1000 stat(每秒),例如系统范围(将 -A 添加到单独的 cpu 计数器):

  perf stat -a -I 1000

该选项在builtin-stat.c http://lxr.free-electrons.com/source/tools/perf/builtin-stat.c?v=4.8中实现__run_perf_stat函数

531 static int __run_perf_stat(int argc, const char **argv)
532 {
533         int interval = stat_config.interval;

对于具有某些程序参数 (forks=1) 的 perf stat -I 1000,例如 perf stat -I 1000 sleep 10是间隔循环(ts 是转换为 struct timespec 的毫秒间隔):

639                 enable_counters();
641                 if (interval) {
642                         while (!waitpid(child_pid, &status, WNOHANG)) {
643                                 nanosleep(&ts, NULL);
644                                 process_interval();
645                         }
646                 }
666         disable_counters();

对于系统范围硬件性能监视器计数和 forks=0 的变体,还有其他间隔循环

658                 enable_counters();
659                 while (!done) {
660                         nanosleep(&ts, NULL);
661                         if (interval)
662                                 process_interval();
663                 }
666         disable_counters();

process_interval() http://lxr.free-electrons.com/source/tools/perf/builtin-stat.c?v=4.8#L347来自同一文件的使用 read_counters();它循环事件列表并调用 read_counter()其中loops over所有已知线程和所有CPU并启动实际读取功能:

306         for (thread = 0; thread < nthreads; thread++) {
307                 for (cpu = 0; cpu < ncpus; cpu++) {
...
310                         count = perf_counts(counter->counts, cpu, thread);
311                         if (perf_evsel__read(counter, cpu, thread, count))
312                                 return -1;

perf_evsel__read是程序仍在运行时读取的真实计数器:

1207 int perf_evsel__read(struct perf_evsel *evsel, int cpu, int thread,
1208                      struct perf_counts_values *count)
1209 {
1210         memset(count, 0, sizeof(*count));
1211 
1212         if (FD(evsel, cpu, thread) < 0)
1213                 return -EINVAL;
1214 
1215         if (readn(FD(evsel, cpu, thread), count, sizeof(*count)) < 0)
1216                 return -errno;
1217 
1218         return 0;
1219 }

关于linux - 使用 linux perf 实用程序每秒报告计数器,如 vmstat,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40521226/

相关文章:

linux - 在 Linux 上将 hwclock 设置为 UTC

linux - 在任何 linux 文件系统上是否有类似备用数据流的东西?

linux - 我需要在 shell 中用双引号括起一个替代变量

c# - 如何对硬件性能计数器进行编程

linux - LBR vs DWARF vs fp 的性能记录选择有什么作用?

linux - Linux 中的线程与进程

c# - 为什么我的应用程序随着时间的推移变得响应速度变慢?

c# - PerformanceCounter 报告的 CPU 使用率高于观察到的

linux - 在 Kcachegrind 中打开 perf.data

linux - perf stat 周期数和频率缩放