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