Search code examples
unixmonitoringperf

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


I am using perf stat to log some hardware counters, it has a batch mode in which it prints the counter values every 1s (or whatever the interval). I want the output of one interval in one single line instead of multiple lines as shown below:

#           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%]

So that it can be saved as a csv file with each counter value for one interval as a single row. Is there a simple way to do this?

something like:

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

my perf version is: 3.13.11.10


Solution

  • I would pass the output of perf stat through something like

    awk '/task-clock/{printf("%d ", $2);} /page-faults/{printf("%d ", $2);} /branch-misses/{printf("%d\n", $2);} '
    

    I did not add here all the fields and the header. The additions are pretty obvious