xref: /linux/tools/perf/util/stat-shadow.c (revision a77ecea7ced2fef7cc0a8ad0323542f781ad9788)
1 // SPDX-License-Identifier: GPL-2.0
2 #include <errno.h>
3 #include <math.h>
4 #include <stdio.h>
5 #include "evsel.h"
6 #include "stat.h"
7 #include "color.h"
8 #include "debug.h"
9 #include "pmu.h"
10 #include "rblist.h"
11 #include "evlist.h"
12 #include "expr.h"
13 #include "metricgroup.h"
14 #include "cgroup.h"
15 #include "units.h"
16 #include "iostat.h"
17 #include "util/hashmap.h"
18 #include "tool_pmu.h"
19 
20 static bool tool_pmu__is_time_event(const struct perf_stat_config *config,
21 				   const struct evsel *evsel, int *tool_aggr_idx)
22 {
23 	enum tool_pmu_event event = evsel__tool_event(evsel);
24 	int aggr_idx;
25 
26 	if (event != TOOL_PMU__EVENT_DURATION_TIME &&
27 	    event != TOOL_PMU__EVENT_USER_TIME &&
28 	    event != TOOL_PMU__EVENT_SYSTEM_TIME)
29 		return false;
30 
31 	if (config) {
32 		cpu_aggr_map__for_each_idx(aggr_idx, config->aggr_map) {
33 			if (config->aggr_map->map[aggr_idx].cpu.cpu == 0) {
34 				*tool_aggr_idx = aggr_idx;
35 				return true;
36 			}
37 		}
38 		pr_debug("Unexpected CPU0 missing in aggregation for tool event.\n");
39 	}
40 	*tool_aggr_idx = 0; /* Assume the first aggregation index works. */
41 	return true;
42 }
43 
44 static int prepare_metric(struct perf_stat_config *config,
45 			  const struct metric_expr *mexp,
46 			  const struct evsel *evsel,
47 			  struct expr_parse_ctx *pctx,
48 			  int aggr_idx)
49 {
50 	struct evsel * const *metric_events = mexp->metric_events;
51 	struct metric_ref *metric_refs = mexp->metric_refs;
52 	int i;
53 
54 	for (i = 0; metric_events[i]; i++) {
55 		int source_count = 0, tool_aggr_idx;
56 		int aggr_nr = 1;
57 		bool is_tool_time =
58 			tool_pmu__is_time_event(config, metric_events[i], &tool_aggr_idx);
59 		struct perf_stat_evsel *ps = metric_events[i]->stats;
60 		char *n;
61 		double val;
62 
63 		/*
64 		 * If there are multiple uncore PMUs and we're not reading the
65 		 * leader's stats, determine the stats for the appropriate
66 		 * uncore PMU.
67 		 */
68 		if (evsel && evsel->metric_leader &&
69 		    evsel->pmu != evsel->metric_leader->pmu &&
70 		    mexp->metric_events[i]->pmu == evsel->metric_leader->pmu) {
71 			struct evsel *pos;
72 
73 			evlist__for_each_entry(evsel->evlist, pos) {
74 				if (pos->pmu != evsel->pmu)
75 					continue;
76 				if (pos->metric_leader != mexp->metric_events[i])
77 					continue;
78 				ps = pos->stats;
79 				source_count = 1;
80 				break;
81 			}
82 		}
83 		/* Time events are always on CPU0, the first aggregation index. */
84 		if (!ps || !metric_events[i]->supported) {
85 			/*
86 			 * Not supported events will have a count of 0, which
87 			 * can be confusing in a metric. Explicitly set the
88 			 * value to NAN. Not counted events (enable time of 0)
89 			 * are read as 0.
90 			 */
91 			val = NAN;
92 			source_count = 0;
93 			aggr_nr = 0;
94 		} else {
95 			struct perf_stat_aggr *aggr =
96 				&ps->aggr[is_tool_time ? tool_aggr_idx : aggr_idx];
97 
98 			if (aggr->counts.run == 0) {
99 				val = NAN;
100 				source_count = 0;
101 				aggr_nr = 0;
102 			} else {
103 				val = aggr->counts.val;
104 				if (is_tool_time) {
105 					/* Convert time event nanoseconds to seconds. */
106 					val *= 1e-9;
107 				}
108 				if (!source_count)
109 					source_count = evsel__source_count(metric_events[i]);
110 				aggr_nr = aggr->nr ?: 1;
111 			}
112 		}
113 		n = strdup(evsel__metric_id(metric_events[i]));
114 		if (!n)
115 			return -ENOMEM;
116 
117 		expr__add_id_val_source_count_aggr_nr(pctx, n, val, source_count, aggr_nr);
118 	}
119 
120 	for (int j = 0; metric_refs && metric_refs[j].metric_name; j++) {
121 		int ret = expr__add_ref(pctx, &metric_refs[j]);
122 
123 		if (ret)
124 			return ret;
125 	}
126 
127 	return i;
128 }
129 
130 static void generic_metric(struct perf_stat_config *config,
131 			   struct metric_expr *mexp,
132 			   struct evsel *evsel,
133 			   int aggr_idx,
134 			   struct perf_stat_output_ctx *out)
135 {
136 	print_metric_t print_metric = out->print_metric;
137 	const char *metric_name = mexp->metric_name;
138 	const char *metric_expr = mexp->metric_expr;
139 	const char *metric_threshold = mexp->metric_threshold;
140 	const char *metric_unit = mexp->metric_unit;
141 	struct evsel * const *metric_events = mexp->metric_events;
142 	int runtime = mexp->runtime;
143 	struct expr_parse_ctx *pctx;
144 	double ratio, scale, threshold;
145 	int i;
146 	void *ctxp = out->ctx;
147 	enum metric_threshold_classify thresh = METRIC_THRESHOLD_UNKNOWN;
148 
149 	pctx = expr__ctx_new();
150 	if (!pctx)
151 		return;
152 
153 	if (config->user_requested_cpu_list)
154 		pctx->sctx.user_requested_cpu_list = strdup(config->user_requested_cpu_list);
155 	pctx->sctx.runtime = runtime;
156 	pctx->sctx.system_wide = config->system_wide;
157 	i = prepare_metric(config, mexp, evsel, pctx, aggr_idx);
158 	if (i < 0) {
159 		expr__ctx_free(pctx);
160 		return;
161 	}
162 	if (!metric_events[i]) {
163 		if (expr__parse(&ratio, pctx, metric_expr) == 0) {
164 			char *unit;
165 			char metric_bf[128];
166 
167 			if (metric_threshold &&
168 			    expr__parse(&threshold, pctx, metric_threshold) == 0 &&
169 			    !isnan(threshold)) {
170 				thresh = fpclassify(threshold) == FP_ZERO
171 					? METRIC_THRESHOLD_GOOD : METRIC_THRESHOLD_BAD;
172 			}
173 
174 			if (metric_unit && metric_name) {
175 				if (perf_pmu__convert_scale(metric_unit,
176 					&unit, &scale) >= 0) {
177 					ratio *= scale;
178 				}
179 				if (strstr(metric_expr, "?"))
180 					scnprintf(metric_bf, sizeof(metric_bf),
181 					  "%s  %s_%d", unit, metric_name, runtime);
182 				else
183 					scnprintf(metric_bf, sizeof(metric_bf),
184 					  "%s  %s", unit, metric_name);
185 
186 				print_metric(config, ctxp, thresh, "%8.1f",
187 					     metric_bf, ratio);
188 			} else {
189 				print_metric(config, ctxp, thresh, "%8.2f",
190 					metric_name ?
191 					metric_name :
192 					out->force_header ?  evsel->name : "",
193 					ratio);
194 			}
195 		} else {
196 			print_metric(config, ctxp, thresh, /*fmt=*/NULL,
197 				     out->force_header ?
198 				     (metric_name ?: evsel->name) : "", 0);
199 		}
200 	} else {
201 		print_metric(config, ctxp, thresh, /*fmt=*/NULL,
202 			     out->force_header ?
203 			     (metric_name ?: evsel->name) : "", 0);
204 	}
205 
206 	expr__ctx_free(pctx);
207 }
208 
209 double test_generic_metric(struct metric_expr *mexp, int aggr_idx)
210 {
211 	struct expr_parse_ctx *pctx;
212 	double ratio = 0.0;
213 
214 	pctx = expr__ctx_new();
215 	if (!pctx)
216 		return NAN;
217 
218 	if (prepare_metric(/*config=*/NULL, mexp, /*evsel=*/NULL, pctx, aggr_idx) < 0)
219 		goto out;
220 
221 	if (expr__parse(&ratio, pctx, mexp->metric_expr))
222 		ratio = 0.0;
223 
224 out:
225 	expr__ctx_free(pctx);
226 	return ratio;
227 }
228 
229 static void perf_stat__print_metricgroup_header(struct perf_stat_config *config,
230 						struct evsel *evsel,
231 						void *ctxp,
232 						const char *name,
233 						struct perf_stat_output_ctx *out)
234 {
235 	bool need_full_name = perf_pmus__num_core_pmus() > 1;
236 	static const char *last_name;
237 	static const struct perf_pmu *last_pmu;
238 	char full_name[64];
239 
240 	/*
241 	 * A metricgroup may have several metric events,
242 	 * e.g.,TopdownL1 on e-core of ADL.
243 	 * The name has been output by the first metric
244 	 * event. Only align with other metics from
245 	 * different metric events.
246 	 */
247 	if (last_name && !strcmp(last_name, name) && last_pmu == evsel->pmu) {
248 		out->print_metricgroup_header(config, ctxp, NULL);
249 		return;
250 	}
251 
252 	if (need_full_name && evsel->pmu)
253 		scnprintf(full_name, sizeof(full_name), "%s (%s)", name, evsel->pmu->name);
254 	else
255 		scnprintf(full_name, sizeof(full_name), "%s", name);
256 
257 	out->print_metricgroup_header(config, ctxp, full_name);
258 
259 	last_name = name;
260 	last_pmu = evsel->pmu;
261 }
262 
263 /**
264  * perf_stat__print_shadow_stats_metricgroup - Print out metrics associated with the evsel
265  *					       For the non-default, all metrics associated
266  *					       with the evsel are printed.
267  *					       For the default mode, only the metrics from
268  *					       the same metricgroup and the name of the
269  *					       metricgroup are printed. To print the metrics
270  *					       from the next metricgroup (if available),
271  *					       invoke the function with correspoinding
272  *					       metric_expr.
273  */
274 void *perf_stat__print_shadow_stats_metricgroup(struct perf_stat_config *config,
275 						struct evsel *evsel,
276 						int aggr_idx,
277 						int *num,
278 						void *from,
279 						struct perf_stat_output_ctx *out)
280 {
281 	struct metric_event *me;
282 	struct metric_expr *mexp = from;
283 	void *ctxp = out->ctx;
284 	bool header_printed = false;
285 	const char *name = NULL;
286 	struct rblist *metric_events = &evsel->evlist->metric_events;
287 
288 	me = metricgroup__lookup(metric_events, evsel, false);
289 	if (me == NULL)
290 		return NULL;
291 
292 	if (!mexp)
293 		mexp = list_first_entry(&me->head, typeof(*mexp), nd);
294 
295 	list_for_each_entry_from(mexp, &me->head, nd) {
296 		/* Print the display name of the Default metricgroup */
297 		if (!config->metric_only && me->is_default) {
298 			if (!name)
299 				name = mexp->default_metricgroup_name;
300 			/*
301 			 * Two or more metricgroup may share the same metric
302 			 * event, e.g., TopdownL1 and TopdownL2 on SPR.
303 			 * Return and print the prefix, e.g., noise, running
304 			 * for the next metricgroup.
305 			 */
306 			if (strcmp(name, mexp->default_metricgroup_name))
307 				return (void *)mexp;
308 			/* Only print the name of the metricgroup once */
309 			if (!header_printed && !evsel->default_show_events) {
310 				header_printed = true;
311 				perf_stat__print_metricgroup_header(config, evsel, ctxp,
312 								    name, out);
313 			}
314 		}
315 
316 		if ((*num)++ > 0 && out->new_line)
317 			out->new_line(config, ctxp);
318 		generic_metric(config, mexp, evsel, aggr_idx, out);
319 	}
320 
321 	return NULL;
322 }
323 
324 void perf_stat__print_shadow_stats(struct perf_stat_config *config,
325 				   struct evsel *evsel,
326 				   int aggr_idx,
327 				   struct perf_stat_output_ctx *out)
328 {
329 	print_metric_t print_metric = out->print_metric;
330 	void *ctxp = out->ctx;
331 	int num = 0;
332 
333 	if (config->iostat_run)
334 		iostat_print_metric(config, evsel, out);
335 
336 	perf_stat__print_shadow_stats_metricgroup(config, evsel, aggr_idx,
337 						  &num, NULL, out);
338 
339 	if (num == 0) {
340 		print_metric(config, ctxp, METRIC_THRESHOLD_UNKNOWN,
341 			     /*fmt=*/NULL, /*unit=*/NULL, 0);
342 	}
343 }
344 
345 /**
346  * perf_stat__skip_metric_event - Skip the evsel in the Default metricgroup,
347  *				  if it's not running or not the metric event.
348  */
349 bool perf_stat__skip_metric_event(struct evsel *evsel)
350 {
351 	if (!evsel->default_metricgroup)
352 		return false;
353 
354 	return !metricgroup__lookup(&evsel->evlist->metric_events, evsel, false);
355 }
356