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