1 From da0e5b7cee1cf8a699a9a6cf863ee97ab65f1fd4 Mon Sep 17 00:00:00 2001
2 From: Tony Jones <tonyj@suse.de>
3 Date: Sun, 20 Jan 2019 11:14:14 -0800
4 Subject: perf script: Fix crash when processing recorded stat data
6 [ Upstream commit 8bf8c6da53c2265aea365a1de6038f118f522113 ]
8 While updating perf to work with Python3 and Python2 I noticed that the
9 stat-cpi script was dumping core.
11 $ perf stat -e cycles,instructions record -o /tmp/perf.data /bin/false
13 Performance counter stats for '/bin/false':
17 604,622 instructions 802,148 cycles
20 0.001445842 seconds time elapsed
22 $ perf script -i /tmp/perf.data -s scripts/python/stat-cpi.py
23 Segmentation fault (core dumped)
26 rblist=rblist@entry=0xb2a200 <rt_stat>,
27 new_entry=new_entry@entry=0x7ffcb755c310) at util/rblist.c:33
28 ctx=<optimized out>, type=<optimized out>, create=<optimized out>,
29 cpu=<optimized out>, evsel=<optimized out>) at util/stat-shadow.c:118
30 ctx=<optimized out>, type=<optimized out>, st=<optimized out>)
31 at util/stat-shadow.c:196
32 count=count@entry=727442, cpu=cpu@entry=0, st=0xb2a200 <rt_stat>)
33 at util/stat-shadow.c:239
34 config=config@entry=0xafeb40 <stat_config>,
35 counter=counter@entry=0x133c6e0) at util/stat.c:372
39 The issue is that since 1fcd03946b52 perf_stat__update_shadow_stats now calls
40 update_runtime_stat passing rt_stat rather than calling update_stats but
41 perf_stat__init_shadow_stats has never been called to initialize rt_stat in
42 the script path processing recorded stat data.
44 Since I can't see any reason why perf_stat__init_shadow_stats() is presently
45 initialized like it is in builtin-script.c::perf_sample__fprint_metric()
46 [4bd1bef8bba2f] I'm proposing it instead be initialized once in __cmd_script
50 After applying the patch:
52 # perf script -i /tmp/perf.data -s tools/perf/scripts/python/stat-cpi.py
53 0.001970: cpu -1, thread -1 -> cpi 1.709079 (1075684/629394)
58 Signed-off-by: Tony Jones <tonyj@suse.de>
59 Reviewed-by: Jiri Olsa <jolsa@kernel.org>
60 Tested-by: Arnaldo Carvalho de Melo <acme@redhat.com>
61 Tested-by: Ravi Bangoria <ravi.bangoria@linux.ibm.com>
62 Cc: Andi Kleen <ak@linux.intel.com>
63 Cc: Jin Yao <yao.jin@linux.intel.com>
64 Fixes: 1fcd03946b52 ("perf stat: Update per-thread shadow stats")
65 Link: http://lkml.kernel.org/r/20190120191414.12925-1-tonyj@suse.de
66 Signed-off-by: Arnaldo Carvalho de Melo <acme@redhat.com>
67 Signed-off-by: Sasha Levin <sashal@kernel.org>
69 tools/perf/builtin-script.c | 7 ++-----
70 1 file changed, 2 insertions(+), 5 deletions(-)
72 diff --git a/tools/perf/builtin-script.c b/tools/perf/builtin-script.c
73 index 5c070a600233..d8791e0e5f75 100644
74 --- a/tools/perf/builtin-script.c
75 +++ b/tools/perf/builtin-script.c
76 @@ -1633,13 +1633,8 @@ static void perf_sample__fprint_metric(struct perf_script *script,
77 .force_header = false,
79 struct perf_evsel *ev2;
84 - perf_stat__init_shadow_stats();
88 perf_evlist__alloc_stats(script->session->evlist, false);
89 if (evsel_script(evsel->leader)->gnum++ == 0)
90 @@ -2305,6 +2300,8 @@ static int __cmd_script(struct perf_script *script)
92 signal(SIGINT, sig_handler);
94 + perf_stat__init_shadow_stats();
96 /* override event processing functions */
97 if (script->show_task_events) {
98 script->tool.comm = process_comm_event;