Skip to content

Commit 63b320a

Browse files
captain5050acmel
authored andcommitted
perf stat-shadow: In prepare_metric fix guard on reading NULL perf_stat_evsel
The aggr value is setup to always be non-null creating a redundant guard for reading from it. Switch to using the perf_stat_evsel (ps) and narrow the scope of aggr so that it is known valid when used. Fixes: 3d65f64 ("perf stat-shadow: Read tool events directly") Reported-by: Andres Freund <andres@anarazel.de> Signed-off-by: Ian Rogers <irogers@google.com> Cc: Adrian Hunter <adrian.hunter@intel.com> Cc: Alexander Shishkin <alexander.shishkin@linux.intel.com> Cc: Andi Kleen <ak@linux.intel.com> Cc: Dapeng Mi <dapeng1.mi@linux.intel.com> Cc: Dr. David Alan Gilbert <linux@treblig.org> Cc: Ian Rogers <irogers@google.com> Cc: Ingo Molnar <mingo@redhat.com> Cc: James Clark <james.clark@linaro.org> Cc: Jiri Olsa <jolsa@kernel.org> Cc: Namhyung Kim <namhyung@kernel.org> Cc: Peter Zijlstra <peterz@infradead.org> Cc: Thomas Falcon <thomas.falcon@intel.com> Cc: Thomas Richter <tmricht@linux.ibm.com> Cc: Yang Li <yang.lee@linux.alibaba.com> Signed-off-by: Arnaldo Carvalho de Melo <acme@redhat.com>
1 parent bc105a8 commit 63b320a

1 file changed

Lines changed: 16 additions & 8 deletions

File tree

tools/perf/util/stat-shadow.c

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,6 @@ static int prepare_metric(struct perf_stat_config *config,
5757
bool is_tool_time =
5858
tool_pmu__is_time_event(config, metric_events[i], &tool_aggr_idx);
5959
struct perf_stat_evsel *ps = metric_events[i]->stats;
60-
struct perf_stat_aggr *aggr;
6160
char *n;
6261
double val;
6362

@@ -82,8 +81,7 @@ static int prepare_metric(struct perf_stat_config *config,
8281
}
8382
}
8483
/* Time events are always on CPU0, the first aggregation index. */
85-
aggr = &ps->aggr[is_tool_time ? tool_aggr_idx : aggr_idx];
86-
if (!aggr || !metric_events[i]->supported || aggr->counts.run == 0) {
84+
if (!ps || !metric_events[i]->supported) {
8785
/*
8886
* Not supported events will have a count of 0, which
8987
* can be confusing in a metric. Explicitly set the
@@ -93,11 +91,21 @@ static int prepare_metric(struct perf_stat_config *config,
9391
val = NAN;
9492
source_count = 0;
9593
} else {
96-
val = aggr->counts.val;
97-
if (is_tool_time)
98-
val *= 1e-9; /* Convert time event nanoseconds to seconds. */
99-
if (!source_count)
100-
source_count = evsel__source_count(metric_events[i]);
94+
struct perf_stat_aggr *aggr =
95+
&ps->aggr[is_tool_time ? tool_aggr_idx : aggr_idx];
96+
97+
if (aggr->counts.run == 0) {
98+
val = NAN;
99+
source_count = 0;
100+
} else {
101+
val = aggr->counts.val;
102+
if (is_tool_time) {
103+
/* Convert time event nanoseconds to seconds. */
104+
val *= 1e-9;
105+
}
106+
if (!source_count)
107+
source_count = evsel__source_count(metric_events[i]);
108+
}
101109
}
102110
n = strdup(evsel__metric_id(metric_events[i]));
103111
if (!n)

0 commit comments

Comments
 (0)