在批处理模式下,每个命令间隔将输出重新格式化为一行

Reformat output to one line per interval of a command in batch mode

我正在使用 perf stat 来记录一些硬件计数器,它有一个批处理模式,它每 1 秒(或任何时间间隔)打印一次计数器值。我希望在一行而不是多行中输出一个间隔,如下所示:

#           time             counts events
     1.000650887        4015.442880 task-clock (msec)         [100.00%]
     1.000650887                214 context-switches          [100.00%]
     1.000650887                  2 cpu-migrations            [100.00%]
     1.000650887                 14 page-faults
     1.000650887         58,447,833 cycles                    [83.19%]
     1.000650887         50,476,562 stalled-cycles-frontend   [83.26%]
     1.000650887         18,469,093 stalled-cycles-backend    [66.85%]
     1.000650887         13,861,731 instructions              [83.56%]
     1.000650887          3,963,967 branches                  [83.60%]
     1.000650887            180,104 branch-misses             [83.21%]
     2.004854486        4003.706096 task-clock (msec)
     2.004854486                245 context-switches
     2.004854486                  0 cpu-migrations
     2.004854486                 30 page-faults
     2.004854486         60,750,234 cycles                    [83.27%]
     2.004854486         38,491,129 stalled-cycles-frontend   [83.26%]
     2.004854486         20,561,260 stalled-cycles-backend    [66.95%]
     2.004854486         15,651,369 instructions              [83.36%]
     2.004854486          3,826,936 branches                  [83.25%]
     2.004854486            183,319 branch-misses             [83.27%]

这样它就可以保存为一个 csv 文件,每个间隔的每个计数器值都作为一行。有没有简单的方法可以做到这一点?

类似于:

task-clock, context-switches, page-faults, cycles, instructions, branches
4105, 214, 14, 58447833, 13861, 3963967
4003, 245, 30, 60750234, 15651369, 3826936

我的性能版本是:3.13.11.10

我会通过

之类的方式传递 perf stat 的输出
awk '/task-clock/{printf("%d ", );} /page-faults/{printf("%d ", );} /branch-misses/{printf("%d\n", );} '

我没有在此处添加所有字段和 header。添加的内容非常明显