David Chen
David Chen

Reputation: 1825

How to parse number of instructions and elapsed time from output of perf stat

TL;DR

Is there any way to make perf stat output time elapsed when -x is supplied?

Background

I executed perf stat -e instructions:u make run, and the output is pasted below.

 Performance counter stats for 'make run':

    53,781,961,288      instructions:u

       2.802749955 seconds time elapsed

      21.453244000 seconds user
       0.249223000 seconds sys

I would like to parse number of instructions and time elapsed, so I add the -x option to make the output separated by comma, and the corresponding output is pasted below.

53781782267,,instructions:u,20694056846,100.00,,

I noticed that all the time measurements weren't displayed, so I checked the output of perf help stat. In the CSV FORMAT section, I found that run time of counter, optional metric value, and optional unit of metric may have something to do with my needs, but I couldn't figure out how exactly.

Upvotes: 4

Views: 681

Answers (1)

osgx
osgx

Reputation: 94225

"time elapsed" is printed from print_footer function of tools/perf/util/stat-display.c file if (config->ru_display) is true.

But the print_footer is not called from perf_evlist__print_counters when there is "-x ," option which sets csv_output.

if (!interval && !config->csv_output)
    print_footer(config);

"user" / "sys" times are in config->ru_data.ru_utime and ru_data.ru_stime and "seconds time elapsed" is average of config->walltime_nsecs_stats.

There is no other code in stat-display.c to display ru_data.ru_utime, ru_data.ru_stime or walltime_nsecs_stats, so at Linux kernel version 4.20 it is not possible to output time from the perf stat in CSV mode.

You can patch the stat-display.c file to output anything you need and compile the perf (cd tools/perf; make). perf tool from other kernel version will work with any linux kernel.

Upvotes: 1

Related Questions