Commit 63b320aa authored by Ian Rogers's avatar Ian Rogers Committed by Arnaldo Carvalho de Melo
Browse files

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: 3d65f644 ("perf stat-shadow: Read tool events directly")
Reported-by: default avatarAndres Freund <andres@anarazel.de>
Signed-off-by: default avatarIan 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: default avatarArnaldo Carvalho de Melo <acme@redhat.com>
parent bc105a89
Loading
Loading
Loading
Loading
+16 −8
Original line number Diff line number Diff line
@@ -57,7 +57,6 @@ static int prepare_metric(struct perf_stat_config *config,
		bool is_tool_time =
			tool_pmu__is_time_event(config, metric_events[i], &tool_aggr_idx);
		struct perf_stat_evsel *ps = metric_events[i]->stats;
		struct perf_stat_aggr *aggr;
		char *n;
		double val;

@@ -82,8 +81,7 @@ static int prepare_metric(struct perf_stat_config *config,
			}
		}
		/* Time events are always on CPU0, the first aggregation index. */
		aggr = &ps->aggr[is_tool_time ? tool_aggr_idx : aggr_idx];
		if (!aggr || !metric_events[i]->supported || aggr->counts.run == 0) {
		if (!ps || !metric_events[i]->supported) {
			/*
			 * Not supported events will have a count of 0, which
			 * can be confusing in a metric. Explicitly set the
@@ -92,13 +90,23 @@ static int prepare_metric(struct perf_stat_config *config,
			 */
			val = NAN;
			source_count = 0;
		} else {
			struct perf_stat_aggr *aggr =
				&ps->aggr[is_tool_time ? tool_aggr_idx : aggr_idx];

			if (aggr->counts.run == 0) {
				val = NAN;
				source_count = 0;
			} else {
				val = aggr->counts.val;
			if (is_tool_time)
				val *= 1e-9; /* Convert time event nanoseconds to seconds. */
				if (is_tool_time) {
					/* Convert time event nanoseconds to seconds. */
					val *= 1e-9;
				}
				if (!source_count)
					source_count = evsel__source_count(metric_events[i]);
			}
		}
		n = strdup(evsel__metric_id(metric_events[i]));
		if (!n)
			return -ENOMEM;