perf script: Add script per-event-dump support
v5 cover perf script: Add script per-event-dump support
Introduce a new option to dump trace output to files named by the monitored events and update perf-script documentation accordingly.
Shown below is output of perf script command with the newly introduced option.
$perf record -e cycles -e cs -ag -- sleep 1
$perf script --per-event-dump
[ perf script: Wrote 0.051 MB perf.data-script-dump-cycles.txt (76 samples) ] [ perf script: Wrote 0.012 MB perf.data-script-dump-cs.txt (69 samples) ] $ls
perf.data-script-dump-cycles.txt perf.data-script-dump-cs.txt
Without per-event-dump support, drawing flamegraphs for different events would be a very troublesome thing. You can monitor only one event at a time if you want to get flamegraphs for different events. Using this option, you can get the trace output files named by the monitored events, and could draw flamegraphs according to the event's name.
yuzhoujian (4):
Patch 1: Add new elements for per-event-dump option
Patch 2: Add the fp_selection_helper to set the fp for print functions
Changes since v4:
- Patch 1: None
- Patch 2: The fp_selection_helper will be invoked by process_event not process_sample_event Replace some printf with fprintf in process_event
Changes since v3:
- Patch 1: Remove three elements for perf_evsel struct and create the perf_script_evsel struct to save them.
- Patch 2: None
- Patch 3: None
- Patch 4: Free the evsel->priv by zfree().
Changes since v2:
- Patch 1: Add the last_evsel_name for per_tool struct and three elements for perf_evsel struct.
- Patch 2: None
- Patch 3: None
- Patch 4: Remove the file_name and per_event_dump_file variables.
Add the fp_selection_helper function to select the fp and open the dump file
for all print functions.
Close the dump file for all the evsels and calculate the dump file's size at
the end of the perf script.
Solve the segmentation fault generated by perf script --per-event-dump --show-mmap-events
Changes since v1:
- Patch 1: Remove the set of script.tool.per_event_dump variable.
- Patch 2: Add the __maybe_unused attribute for the fp argument in the second patch.
- Patch 3: remove the fp_selection_helper function for setting the fp argument.
- Patch 2: split the original second patch(Makes all those related functions receive the FILE pointer) to two patches.
- Patch 4: modify the file name of per-event-dump to
<ORIGINAL PERF DATA FILE NAME>-script-dump-<EVENT NAME>.txt
tools/perf/builtin-script.c | 110 ++++++++++++++++++++++++++++++++++++-------- tools/perf/util/evsel.h | 11 +++++ tools/perf/util/session.c | 19 ++++++++ tools/perf/util/tool.h | 2 + 4 files changed, 122 insertions(+), 20 deletions(-)
Signature · 1 line
1.8.3.1
PATCH v5 1/2 Add new elements for per-event-dump option
This patch will add two elements for perf_tool struct: per_event_dump is used to mark the per-event-dump option, last_evsel_name is used to save last evsel's name. Add a new struct perf_script_evsel to save evsel's specific data. There are three elements in this new struct:dump_evsel_fp is used to save the file pointer of the dump_event file, filename is used to save the file name of the dump_event file, samples is used to save the number of samples for each evsel. The perf_script_evsel struct will be saved in the evsel->priv. Add the OPT_BOOLEAN for per-event-dump in the perf_data_file struct.
Changes since v4:
- none.
Changes since v3:
- remove three elements for perf_evsel struct and create a new struct:
perf_script_evsel to save them.
Changes since v2:
- add the last_evsel_name for per_tool struct to save last evsel's name.
- add three elements for perf_evsel struct:dump_event_fp is used to save the file pointer of the dump_event file, filename is used to save the file name of the dump_event file, samples is used to save the number of samples for each evsel.
Changes since v1:
- remove the set for script.tool.per_event_dump variable,since the OPT_BOOLEAN will do the same thing.
--- tools/perf/builtin-script.c | 3 +++ tools/perf/util/evsel.h | 11 +++++++++++ tools/perf/util/tool.h | 2 ++ 3 files changed, 16 insertions(+)
tools/perf/builtin-script.c +3 -0
@@ -2758,6 +2758,7 @@ int cmd_script(int argc, const char **argv) .cpu_map = process_cpu_map_event, .ordered_events = true, .ordering_requires_timestamps = true, + .per_event_dump = false, }, }; struct perf_data_file file = {
@@ -2828,6 +2829,8 @@ int cmd_script(int argc, const char **argv) "Show context switch events (if recorded)"),OPT_BOOLEAN ('\0' ,"show-namespace-events" , &script.show_namespace_events,"Show namespace events (if recorded)" ), +OPT_BOOLEAN ('\0' ,"per-event-dump" , &script.tool.per_event_dump, +"Dump trace output to files named by the monitored events" ),OPT_BOOLEAN ('f' ,"force" , &symbol_conf.force,"don't complain, do it" ),OPT_INTEGER (0 ,"max-blocks" , &max_blocks, "Maximum number of code blocks to dump with brstackinsn"),
tools/perf/util/evsel.h +11 -0
@@ -5,6 +5,7 @@#include <stdbool.h> #include <stddef.h> #include <linux/perf_event.h> +#include <stdio.h> #include <linux/types.h> #include "xyarray.h" #include "symbol.h"
@@ -51,6 +52,16 @@ enum { PERF_EVSEL__CONFIG_TERM_MAX, }; +/* +* The struct perf_script_evsel is used to save the dump file's name, +* dump file's fp and the total number of samples for each evsel. +*/ +struct perf_script_evsel { +char *filename; +FILE *dump_evsel_fp; +unsigned long samples; +}; +struct perf_evsel_config_term {struct list_head list;int type;
tools/perf/util/tool.h +2 -0
@@ -75,6 +75,8 @@ struct perf_tool {bool ordered_events;bool ordering_requires_timestamps;bool namespace_events; +bool per_event_dump; +const char *last_evsel_name;enum show_feature_header show_feat_hdr; };
Signature · 1 line
1.8.3.1
Arnaldo Carvalho de Melo
maintainerRe: [PATCH v5 1/2] Add new elements for per-event-dump option
Em Wed, Oct 25, 2017 at 04:53:18PM +0800, yuzhoujian escreveu:
Quoted 40 lines
This patch will add two elements for
perf_toolstruct:per_event_dumpis used to mark the per-event-dump option,last_evsel_nameis used to save last evsel's name. Add a new structperf_script_evselto save evsel's specific data. There are three elements in this new struct:dump_evsel_fpis used to save the file pointer of thedump_eventfile, filename is used to save the file name of thedump_eventfile, samples is used to save the number of samples for each evsel. Theperf_script_evselstruct will be saved in theevsel->priv. Add theOPT_BOOLEANfor per-event-dump in theperf_data_filestruct.Changes since v4:
- none.Changes since v3:
- remove three elements forperf_evselstruct and create a new struct:
perf_script_evselto save them.Changes since v2:
- add thelast_evsel_nameforper_toolstruct to save last evsel's name.
- add three elements forperf_evselstruct:dump_event_fp is used to save the file pointer of thedump_eventfile, filename is used to save the file name of thedump_eventfile, samples is used to save the number of samples for each evsel.Changes since v1:
- remove the set for script.tool.per_event_dump variable,since theOPT_BOOLEANwill do the same thing.Signed-off-by yuzhoujian <[email protected]>--- tools/perf/builtin-script.c | 3 +++ tools/perf/util/evsel.h | 11 +++++++++++ tools/perf/util/tool.h | 2 ++ 3 files changed, 16 insertions(+)tools/perf/builtin-script.c +0
-0
tools/perf/builtin-script.c int cmd_script(int argc, const char **argv) · line 2761
Quoted 4 lines
tools/perf/builtin-script.c +1
-0@@ -2758,6 +2758,7 @@ int cmd_script(int argc, const char **argv) .cpu_map = process_cpu_map_event, .ordered_events = true, .ordering_requires_timestamps = true, + .per_event_dump = false,
No need for this, if you simply don't init it, it will be set to false.
tools/perf/builtin-script.c int cmd_script(int argc, const char **argv) · line 2833
Quoted 9 lines
tools/perf/builtin-script.c +2
-0}, };struct perf_data_file file = {@@ -2828,6 +2829,8 @@ int cmd_script(int argc, const char **argv) "Show context switch events (if recorded)"),OPT_BOOLEAN ('\0' ,"show-namespace-events" , &script.show_namespace_events,"Show namespace events (if recorded)" ), +OPT_BOOLEAN ('\0' ,"per-event-dump" , &script.tool.per_event_dump, + "Dump trace output to files named by the monitored events"),
this becomes:
+ OPT_BOOLEAN('\0', "per-event-dump", &script.per_event_dump,
+ "Dump trace output to files named by the monitored events"),
Quoted 19 lines
tools/perf/builtin-script.c +0
-0OPT_BOOLEAN ('f' ,"force" , &symbol_conf.force,"don't complain, do it" ),OPT_INTEGER (0 ,"max-blocks" , &max_blocks, "Maximum number of code blocks to dump with brstackinsn"),tools/perf/util/evsel.h +1
-0@@ -5,6 +5,7 @@#include <stdbool.h> #include <stddef.h> #include <linux/perf_event.h> +#include <stdio.h> #include <linux/types.h> #include "xyarray.h" #include "symbol.h" @@ -51,6 +52,16 @@ enum { PERF_EVSEL__CONFIG_TERM_MAX, };
tools/perf/util/evsel.h enum { · line 64
Quoted 10 lines
tools/perf/util/evsel.h +10
-0+/* +* The struct perf_script_evsel is used to save the dump file's name, +* dump file's fp and the total number of samples for each evsel. +*/ +struct perf_script_evsel { +char *filename; +FILE *dump_evsel_fp; +unsigned long samples; +}; +
Don't pollute evsel.h with things that specific to some tool, this is not the place to put this.
Quoted 8 lines
tools/perf/util/evsel.h +0
-0struct perf_evsel_config_term {struct list_head list;int type;tools/perf/util/tool.h +0
-0
tools/perf/util/tool.h struct perf_tool { · line 80
Quoted 6 lines
tools/perf/util/tool.h +2
-0@@ -75,6 +75,8 @@ struct perf_tool {bool ordered_events;bool ordering_requires_timestamps;bool namespace_events; +bool per_event_dump; +const char *last_evsel_name;enum show_feature_header show_feat_hdr;
Ditto, this should be in struct perf_script.
I'm cooking a patch...
Quoted 4 lines
tools/perf/util/tool.h +0
-0};Signature · 1 line
1.8.3.1
Arnaldo Carvalho de Melo
maintainerRe: [PATCH v5 1/2] Add new elements for per-event-dump option
Em Thu, Oct 26, 2017 at 10:01:43AM -0300, Arnaldo Carvalho de Melo escreveu:
Quoted 8 lines
Em Wed, Oct 25, 2017 at 04:53:18PM +0800, yuzhoujian escreveu:
tools/perf/util/tool.h struct perf_tool { · line 80
Quoted 3 lines
tools/perf/util/tool.h +2
-0+bool per_event_dump; +const char *last_evsel_name;enum show_feature_header show_feat_hdr;Ditto, this should be in struct
perf_script.I'm cooking a patch...
So, please take a look at my perf/core branch, I've reimplemented it the way I had suggested, should work as you intended, and I added the missing entry to tools/perf/Documentation/perf-script.txt, its man page:
[root@jouet ~]# mkdir tt
[root@jouet ~]# cd tt
[root@jouet tt]# perf sched record sleep 5
[ perf record: Woken up 1 times to write data ]
[ perf record: Captured and wrote 4.450 MB perf.data (16177 samples) ] [root@jouet tt]# perf evlist
sched:sched_switch
sched:sched_stat_wait
sched:sched_stat_sleep
sched:sched_stat_iowait
sched:sched_stat_runtime
sched:sched_process_fork
sched:sched_wakeup
sched:sched_wakeup_new
sched:sched_migrate_task
# Tip: use 'perf evlist --trace-fields' to show fields for tracepoint events [root@jouet tt]# perf script | head
perf 24359 [003] 203964.311770: sched:sched_wakeup: perf:24360 [120] success=1 CPU:001
swapper 0 [001] 203964.311778: sched:sched_switch: swapper/1:0 [120] R ==> perf:24360 [120]
perf 24359 [003] 203964.311804: sched:sched_stat_runtime: comm=perf pid=24359 runtime=716900 [ns] vruntime=9190193330 [ns]
perf 24359 [003] 203964.311806: sched:sched_switch: perf:24359 [120] S ==> swapper/3:0 [120]
swapper 0 [000] 203964.312036: sched:sched_wakeup: chrome:20356 [120] success=1 CPU:000
swapper 0 [000] 203964.312041: sched:sched_switch: swapper/0:0 [120] R ==> chrome:20356 [120]
chrome 20356 [000] 203964.312079: sched:sched_stat_runtime: comm=chrome pid=20356 runtime=39381 [ns] vruntime=7598655260580 [ns]
perf 24360 [001] 203964.312079: sched:sched_stat_runtime: comm=perf pid=24360 runtime=304174 [ns] vruntime=917028583 [ns]
chrome 20356 [000] 203964.312086: sched:sched_wakeup: kworker/0:23:12157 [120] success=1 CPU:000
chrome 20356 [000] 203964.312096: sched:sched_stat_runtime: comm=chrome pid=20356 runtime=3531 [ns] vruntime=7598655264111 [ns][root@jouet tt]# ls
perf.data
[root@jouet tt]# perf script --per-event-dump
[root@jouet tt]# ls
perf.data perf.data.sched:sched_stat_runtime.dump perf.data.sched:sched_wakeup.dump
perf.data.sched:sched_migrate_task.dump perf.data.sched:sched_stat_sleep.dump perf.data.sched:sched_wakeup_new.dump
perf.data.sched:sched_process_fork.dump perf.data.sched:sched_stat_wait.dump
perf.data.sched:sched_stat_iowait.dump perf.data.sched:sched_switch.dump
[root@jouet tt]# head -5 perf.data.sched:sched_switch.dump
swapper 0 [001] 203964.311778: sched:sched_switch: swapper/1:0 [120] R ==> perf:24360 [120]
perf 24359 [003] 203964.311806: sched:sched_switch: perf:24359 [120] S ==> swapper/3:0 [120]
swapper 0 [000] 203964.312041: sched:sched_switch: swapper/0:0 [120] R ==> chrome:20356 [120]
chrome 20356 [000] 203964.312097: sched:sched_switch: chrome:20356 [120] R ==> kworker/0:23:12157 [120]
kworker/0:23 12157 [000] 203964.312110: sched:sched_switch: kworker/0:23:12157 [120] t ==> chrome:20356 [120]
[root@jouet tt]#
[root@jouet tt]# head -5 perf.data.sched:sched_stat_runtime.dump
perf 24359 [003] 203964.311804: sched:sched_stat_runtime: comm=perf pid=24359 runtime=716900 [ns] vruntime=9190193330 [ns]
chrome 20356 [000] 203964.312079: sched:sched_stat_runtime: comm=chrome pid=20356 runtime=39381 [ns] vruntime=7598655260580 [ns]
perf 24360 [001] 203964.312079: sched:sched_stat_runtime: comm=perf pid=24360 runtime=304174 [ns] vruntime=917028583 [ns]
chrome 20356 [000] 203964.312096: sched:sched_stat_runtime: comm=chrome pid=20356 runtime=3531 [ns] vruntime=7598655264111 [ns]
kworker/0:23 12157 [000] 203964.312108: sched:sched_stat_runtime: comm=kworker/0:23 pid=12157 runtime=7327 [ns] vruntime=110992086009578 [ns][root@jouet tt]#
- Arnaldo
PATCH v5 2/2 Add the fp_selection_helper to set the fp for print functions
In reply to yuzhoujianThis patch will make all print functions receive the fp, add the fp_selection_helper function to select the fp(stdout or the dump_event fp) and open the dump file for all print functions. When the perf script is over, closes the dump_event file and calculates its size.
Changes since v4:
- the fp_selection_helper will be invoked by process_event not process_sample_event
- replace some printf with fprintf in process_event
Changes since v3:
- free the evsel->priv by zfree()
Changes since v2:
- remove the file_name variable and get the data file name from struct perf_session
- remove the per_event_dump_file variable and get the dump_event fp from struct perf_evsel
- add the fp_selection_helper function to select the fp(stdout or the dump_event fp) and open the dump file for all print functions if evname and last evsel name is not the same.
- close the dump file for all the evsels and calculate the dump file's size at the end of the perf script.
- solve the segmentation fault generated by perf script --per-event-dump --show-mmap-events
Changes since v1:
- modify the dump file name to <ORIGINAL PERF DATA FILE NAME>-script-dump-<EVENT NAME>.txt ect. perf.data-script-dump-cycles.txt, perf.data-script-dump-cs.txt
- split the original patch(Make all those related functions receive the file pointer) to two patches, and this is the second part of the original one.
--- tools/perf/builtin-script.c | 107 +++++++++++++++++++++++++++++++++++--------- tools/perf/util/session.c | 19 ++++++++ 2 files changed, 106 insertions(+), 20 deletions(-)
tools/perf/builtin-script.c +87 -20
@@ -1431,6 +1431,50 @@ static int data_src__fprintf(u64 data_src, FILE *fp)return fprintf (fp,"%-*s" , maxlen, out); } +static FILE *fp_selection_helper (struct perf_script *script,struct perf_evsel *evsel) +{ +const char *evname; +char *filename; +FILE *fp; +struct perf_tool *tool = &script->tool; +struct perf_script_evsel *ps; + + evname =perf_evsel__name (evsel); +if (tool->per_event_dump) { + ps = (struct perf_script_evsel *)evsel->priv; + +/* if evname and last evsel name is not the same, opens another dump file. */ +if (evname != tool->last_evsel_name) { +if (asprintf (&filename,"%s%s%s%s" , script->session->file->path, +"-script-dump-" , evname,".txt" ) <0 ) +BUG_ON ("fail to add the suffix for dump file's name!\n" ); + + fp =fopen (filename,"a+" ); +if (!fp) +BUG_ON ("fail to open the dump file!\n" ); + +if (!ps) { + ps =zalloc (sizeof (struct perf_script_evsel )); +if (!ps) +BUG_ON ("fail to create a perf_script_evsel struct!\n" ); + ps->samples =0 ; + } + ps->filename = filename; + ps->dump_evsel_fp = fp; + tool->last_evsel_name = evname; + evsel->priv = ps; + }else { +if (ps->dump_evsel_fp) + fp = ps->dump_evsel_fp; +else +BUG_ON ("fail to get the dump file's fp!\n" ); + } + }else { + fp = stdout; + } +return fp; +} + static void process_event(struct perf_script *script, struct perf_sample *sample, struct perf_evsel *evsel, struct addr_location *al,
@@ -1439,15 +1483,25 @@ static void process_event(struct perf_script *script,struct thread *thread = al->thread;struct perf_event_attr *attr = &evsel->attr;unsigned int type =output_type (attr->type); -FILE *fp = stdout; +struct perf_script_evsel *ps; +struct perf_tool *tool = &script->tool; +FILE *fp; + + fp =fp_selection_helper (script, evsel);if (output[type].fields ==0 )return ; +if (tool->per_event_dump) { + ps = (struct perf_script_evsel *)evsel->priv; + ps->samples++; + evsel->priv = ps; + } +perf_sample__fprintf_start (sample, thread, evsel, fp);if (PRINT_FIELD (PERIOD )) -printf ("%10" PRIu64" " , sample->period); +fprintf (fp,"%10" PRIu64" " , sample->period);if (PRINT_FIELD (EVNAME )) {const char *evname =perf_evsel__name (evsel);
@@ -1455,7 +1509,7 @@ static void process_event(struct perf_script *script,if (!script->name_width) script->name_width =perf_evlist__max_name_len (script->session->evlist); -printf ("%*s: " , script->name_width, +fprintf (fp,"%*s: " , script->name_width, evname ? evname :"[unknown]" ); }
@@ -1513,8 +1567,8 @@ static void process_event(struct perf_script *script,perf_sample__fprintf_insn (sample, attr, thread, machine, fp);if (PRINT_FIELD (PHYS_ADDR )) -printf ("%16" PRIx64, sample->phys_addr); -printf ("\n" ); +fprintf (fp,"%16" PRIx64, sample->phys_addr); +fprintf (fp,"\n" ); }static struct scripting_ops *scripting_ops;
@@ -1671,6 +1725,7 @@ static int process_comm_event(struct perf_tool *tool, struct perf_script *script = container_of(tool, struct perf_script, tool);struct perf_session *session = script->session;struct perf_evsel *evsel =perf_evlist__id2evsel (session->evlist, sample->id); +FILE *fp;int ret =-1 ; thread =machine__findnew_thread (machine, event->comm.pid, event->comm.tid);
@@ -1688,8 +1743,9 @@ static int process_comm_event(struct perf_tool *tool, sample->tid = event->comm.tid; sample->pid = event->comm.pid; } -perf_sample__fprintf_start (sample, thread, evsel, stdout); -perf_event__fprintf (event, stdout); + fp =fp_selection_helper (script, evsel); +perf_sample__fprintf_start (sample, thread, evsel, fp); +perf_event__fprintf (event, fp); ret =0 ; out:thread__put (thread);
@@ -1705,6 +1761,7 @@ static int process_namespaces_event(struct perf_tool *tool, struct perf_script *script = container_of(tool, struct perf_script, tool);struct perf_session *session = script->session;struct perf_evsel *evsel =perf_evlist__id2evsel (session->evlist, sample->id); +FILE *fp;int ret =-1 ; thread = machine__findnew_thread(machine, event->namespaces.pid,
@@ -1723,8 +1780,9 @@ static int process_namespaces_event(struct perf_tool *tool, sample->tid = event->namespaces.tid; sample->pid = event->namespaces.pid; } -perf_sample__fprintf_start (sample, thread, evsel, stdout); -perf_event__fprintf (event, stdout); + fp =fp_selection_helper (script, evsel); +perf_sample__fprintf_start (sample, thread, evsel, fp); +perf_event__fprintf (event, fp); ret =0 ; out:thread__put (thread);
@@ -1740,6 +1798,7 @@ static int process_fork_event(struct perf_tool *tool, struct perf_script *script = container_of(tool, struct perf_script, tool);struct perf_session *session = script->session;struct perf_evsel *evsel =perf_evlist__id2evsel (session->evlist, sample->id); +FILE *fp;if (perf_event__process_fork (tool, event, sample, machine) <0 )return -1 ;
@@ -1756,8 +1815,9 @@ static int process_fork_event(struct perf_tool *tool, sample->tid = event->fork.tid; sample->pid = event->fork.pid; } -perf_sample__fprintf_start (sample, thread, evsel, stdout); -perf_event__fprintf (event, stdout); + fp =fp_selection_helper (script, evsel); +perf_sample__fprintf_start (sample, thread, evsel, fp); +perf_event__fprintf (event, fp);thread__put (thread);return 0 ;
@@ -1772,6 +1832,7 @@ static int process_exit_event(struct perf_tool *tool, struct perf_script *script = container_of(tool, struct perf_script, tool);struct perf_session *session = script->session;struct perf_evsel *evsel =perf_evlist__id2evsel (session->evlist, sample->id); +FILE *fp; thread =machine__findnew_thread (machine, event->fork.pid, event->fork.tid);if (thread ==NULL ) {
@@ -1785,8 +1846,9 @@ static int process_exit_event(struct perf_tool *tool, sample->tid = event->fork.tid; sample->pid = event->fork.pid; } -perf_sample__fprintf_start (sample, thread, evsel, stdout); -perf_event__fprintf (event, stdout); + fp =fp_selection_helper (script, evsel); +perf_sample__fprintf_start (sample, thread, evsel, fp); +perf_event__fprintf (event, fp);if (perf_event__process_exit (tool, event, sample, machine) <0 ) err =-1 ;
@@ -1804,6 +1866,7 @@ static int process_mmap_event(struct perf_tool *tool, struct perf_script *script = container_of(tool, struct perf_script, tool);struct perf_session *session = script->session;struct perf_evsel *evsel =perf_evlist__id2evsel (session->evlist, sample->id); +FILE *fp;if (perf_event__process_mmap (tool, event, sample, machine) <0 )return -1 ;
@@ -1820,8 +1883,9 @@ static int process_mmap_event(struct perf_tool *tool, sample->tid = event->mmap.tid; sample->pid = event->mmap.pid; } -perf_sample__fprintf_start (sample, thread, evsel, stdout); -perf_event__fprintf (event, stdout); + fp =fp_selection_helper (script, evsel); +perf_sample__fprintf_start (sample, thread, evsel, fp); +perf_event__fprintf (event, fp);thread__put (thread);return 0 ; }
@@ -1835,6 +1899,7 @@ static int process_mmap2_event(struct perf_tool *tool, struct perf_script *script = container_of(tool, struct perf_script, tool);struct perf_session *session = script->session;struct perf_evsel *evsel =perf_evlist__id2evsel (session->evlist, sample->id); +FILE *fp;if (perf_event__process_mmap2 (tool, event, sample, machine) <0 )return -1 ;
@@ -1851,8 +1916,9 @@ static int process_mmap2_event(struct perf_tool *tool, sample->tid = event->mmap2.tid; sample->pid = event->mmap2.pid; } -perf_sample__fprintf_start (sample, thread, evsel, stdout); -perf_event__fprintf (event, stdout); + fp =fp_selection_helper (script, evsel); +perf_sample__fprintf_start (sample, thread, evsel, fp); +perf_event__fprintf (event, fp);thread__put (thread);return 0 ; }
@@ -1866,6 +1932,7 @@ static int process_switch_event(struct perf_tool *tool, struct perf_script *script = container_of(tool, struct perf_script, tool);struct perf_session *session = script->session;struct perf_evsel *evsel =perf_evlist__id2evsel (session->evlist, sample->id); +FILE *fp;if (perf_event__process_switch (tool, event, sample, machine) <0 )return -1 ;
@@ -1876,9 +1943,9 @@ static int process_switch_event(struct perf_tool *tool,pr_debug ("problem processing SWITCH event, skipping it.\n" );return -1 ; } - -perf_sample__fprintf_start (sample, thread, evsel, stdout); -perf_event__fprintf (event, stdout); + fp =fp_selection_helper (script, evsel); +perf_sample__fprintf_start (sample, thread, evsel, fp); +perf_event__fprintf (event, fp);thread__put (thread);return 0 ; }
tools/perf/util/session.c +19 -0
@@ -8,6 +8,8 @@#include <unistd.h> #include <sys/types.h> #include <sys/mman.h> +#include <sys/stat.h> +#include <stdio.h> #include "evlist.h" #include "evsel.h"
@@ -1828,6 +1830,9 @@ static int __perf_session__process_events(struct perf_session *session, {struct ordered_events *oe = &session->ordered_events;struct perf_tool *tool = session->tool; +struct perf_evlist *evlist = session->evlist; +struct perf_evsel *evsel; +struct perf_script_evsel *ps;int fd =perf_data_file__fd (session->file);u64 head, page_offset, file_offset, file_pos, size;int err, mmap_prot, mmap_flags, map_idx =0 ;
@@ -1835,6 +1840,7 @@ static int __perf_session__process_events(struct perf_session *session,char *buf, *mmaps[NUM_MMAPS ];union perf_event *event;struct ui_progress prog; +struct stat statbuf;s64 skip;perf_tool__fill_defaults (tool);
@@ -1940,6 +1946,19 @@ static int __perf_session__process_events(struct perf_session *session,ordered_events__reinit (&session->ordered_events);auxtrace__free_events (session); session->one_mmap = false; +/* Closes the dump_event file and calculates its size at last. */ +if (tool->per_event_dump) { +evlist__for_each_entry (evlist, evsel) { + ps = (struct perf_script_evsel *)evsel->priv; +if (!ps) +BUG_ON ("fail to get the perf_script_evsel\n" ); +stat (ps->filename, &statbuf); +fprintf (stderr,"[ perf script: Wrote %.3f MB %s (%" PRIu64" samples) ]\n" , + statbuf.st_size /1024.0 /1024.0 , ps->filename, ps->samples); +fclose (ps->dump_evsel_fp); +zfree (&evsel->priv); + } + }return err; }
Signature · 1 line
1.8.3.1
tip-bot for Arnaldo Carvalho de Melo
merge bot[tip:perf/core] perf script: Allow creating per-event dump files
perf script: Allow creating per-event dump files
Commit-ID: a14390fde64e862c4fe8b92ecb293ee4b12d5bfe Gitweb: https://git.kernel.org/tip/a14390fde64e862c4fe8b92ecb293ee4b12d5bfe Author: Arnaldo Carvalho de Melo <[email protected]> AuthorDate: Thu, 26 Oct 2017 10:30:20 -0300 Committer: Arnaldo Carvalho de Melo <[email protected]> CommitDate: Fri, 27 Oct 2017 09:10:10 -0300
perf script: Allow creating per-event dump files
Introduce a new option to dump trace output to files named by the monitored events and update perf-script documentation accordingly.
Shown below is output of perf script command with the newly introduced option.
$ perf record -e cycles -e cs -ag -- sleep 1
$ perf script --per-event-dump
$ ls
perf.data.cycles.dump perf.data.cs.dumpWithout per-event-dump support, drawing flamegraphs for different events would require post processing to separate events. You can monitor only one event at a time if you want to get flamegraphs for different events. Using this option, you can get the trace output files named by the monitored events, and could draw flamegraphs according to the event's name.
--- tools/perf/Documentation/perf-script.txt | 4 +++ tools/perf/builtin-script.c | 59 +++++++++++++++++++++++++++++++- 2 files changed, 62 insertions(+), 1 deletion(-)
tools/perf/Documentation/perf-script.txt +4 -0
@@ -325,6 +325,10 @@ include::itrace.txt[] Set the maximum number of program blocks to print with brstackasm for each sample. +--per-event-dump:: + Create per event files with a "perf.data.EVENT.dump" name instead of + printing to stdout, useful, for instance, for generating flamegraphs. + --inline:: If a callgraph address belongs to an inlined function, the inline stack will be printed. Each entry has function name and file/line. Enabled by
tools/perf/builtin-script.c +58 -1
91 lines not on this page · raw message
tip-bot for Arnaldo Carvalho de Melo
merge bot[tip:perf/core] perf script: Print information about per-event-dump files
In reply to yuzhoujianperf script: Print information about per-event-dump files
Commit-ID: 642ee1c6df4c8571d8a6846a2623fb54f925ef92 Gitweb: https://git.kernel.org/tip/642ee1c6df4c8571d8a6846a2623fb54f925ef92 Author: Arnaldo Carvalho de Melo <[email protected]> AuthorDate: Mon, 30 Oct 2017 13:11:15 -0300 Committer: Arnaldo Carvalho de Melo <[email protected]> CommitDate: Mon, 30 Oct 2017 13:11:15 -0300
perf script: Print information about per-event-dump files
For a file generated by "perf sched record sleep 50":
# perf script --per-event-dump [ perf script: Wrote 23.121 MB perf.data.sched:sched_switch.dump (206015 samples) ] [ perf script: Wrote 0.000 MB perf.data.sched:sched_stat_wait.dump (0 samples) ] [ perf script: Wrote 0.000 MB perf.data.sched:sched_stat_sleep.dump (0 samples) ] [ perf script: Wrote 0.000 MB perf.data.sched:sched_stat_iowait.dump (0 samples) ] [ perf script: Wrote 17.680 MB perf.data.sched:sched_stat_runtime.dump (129342 samples) ] [ perf script: Wrote 0.000 MB perf.data.sched:sched_process_fork.dump (24 samples) ] [ perf script: Wrote 11.328 MB perf.data.sched:sched_wakeup.dump (106770 samples) ] [ perf script: Wrote 0.000 MB perf.data.sched:sched_wakeup_new.dump (24 samples) ] [ perf script: Wrote 2.477 MB perf.data.sched:sched_migrate_task.dump (20434 samples) ] #
Similar to what is generated by 'perf record'.
--- tools/perf/builtin-script.c | 77 ++++++++++++++++++++++++++++++++++++++++----- 1 file changed, 69 insertions(+), 8 deletions(-)
tools/perf/builtin-script.c +69 -8
@@ -210,6 +210,51 @@ static struct { }, }; +struct perf_evsel_script { +char *filename; +FILE *fp; +u64 samples; +}; + +static struct perf_evsel_script *perf_evsel_script__new (struct perf_evsel *evsel, +struct perf_data_file *file) +{ +struct perf_evsel_script *es =malloc (sizeof (*es)); + +if (es !=NULL ) { +if (asprintf (&es->filename,"%s.%s.dump" , file->path,perf_evsel__name (evsel)) <0 ) + goto out_free; + es->fp =fopen (es->filename,"w" ); +if (es->fp ==NULL ) + goto out_free_filename; + es->samples =0 ; + } + +return es; +out_free_filename: +zfree (&es->filename); +out_free: +free (es); +return NULL ; +} + +static void perf_evsel_script__delete (struct perf_evsel_script *es) +{ +zfree (&es->filename); +fclose (es->fp); + es->fp =NULL ; +free (es); +} + +static int perf_evsel_script__fprintf (struct perf_evsel_script *es,FILE *fp) +{ +struct stat st; + +fstat (fileno (es->fp), &st); +return fprintf (fp,"[ perf script: Wrote %.3f MB %s (%" PRIu64" samples) ]\n" , + st.st_size /1024.0 /1024.0 , es->filename, es->samples); +} +static inline int output_type (unsigned int type) {switch (type) {
@@ -1439,11 +1484,14 @@ static void process_event(struct perf_script *script,struct thread *thread = al->thread;struct perf_event_attr *attr = &evsel->attr;unsigned int type =output_type (attr->type); -FILE *fp = evsel->priv; +struct perf_evsel_script *es = evsel->priv; +FILE *fp = es->fp;if (output[type].fields ==0 )return ; + ++es->samples; +perf_sample__fprintf_start (sample, thread, evsel, fp);if (PRINT_FIELD (PERIOD ))
@@ -1896,7 +1944,7 @@ static void perf_script__fclose_per_event_dump(struct perf_script *script)evlist__for_each_entry (evlist, evsel) {if (!evsel->priv)break ; -fclose (evsel->priv); +perf_evsel_script__delete (evsel->priv); evsel->priv =NULL ; } }
@@ -1906,10 +1954,7 @@ static int perf_script__fopen_per_event_dump(struct perf_script *script)struct perf_evsel *evsel;evlist__for_each_entry (script->session->evlist, evsel) { -char filename[PATH_MAX ]; -snprintf (filename,sizeof (filename),"%s.%s.dump" , - script->session->file->path,perf_evsel__name (evsel)); - evsel->priv =fopen (filename,"w" ); + evsel->priv =perf_evsel_script__new (evsel, script->session->file);if (evsel->priv ==NULL ) goto out_err_fclose; }
@@ -1924,16 +1969,32 @@ out_err_fclose:static int perf_script__setup_per_event_dump (struct perf_script *script) {struct perf_evsel *evsel; +static struct perf_evsel_script es_stdout;if (script->per_event_dump)return perf_script__fopen_per_event_dump (script); + es_stdout.fp = stdout; +evlist__for_each_entry (script->session->evlist, evsel) - evsel->priv = stdout; + evsel->priv = &es_stdout;return 0 ; } +static void perf_script__exit_per_event_dump_stats (struct perf_script *script) +{ +struct perf_evsel *evsel; + +evlist__for_each_entry (script->session->evlist, evsel) { +struct perf_evsel_script *es = evsel->priv; + +perf_evsel_script__fprintf (es, stdout); +perf_evsel_script__delete (es); + evsel->priv =NULL ; + } +} +static int __cmd_script (struct perf_script *script) {int ret;
@@ -1963,7 +2024,7 @@ static int __cmd_script(struct perf_script *script) ret =perf_session__process_events (script->session);if (script->per_event_dump) -perf_script__fclose_per_event_dump (script); +perf_script__exit_per_event_dump_stats (script);if (debug_mode)pr_err ("Misordered timestamps: %" PRIu64"\n" , nr_unordered);