xref: /linux/tools/perf/util/metricgroup.c (revision be694c488a1e96e728517b26de9f15fed56b2e74)
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3  * Copyright (c) 2017, Intel Corporation.
4  */
5 
6 /* Manage metrics and groups of metrics from JSON files */
7 
8 #include "metricgroup.h"
9 #include "debug.h"
10 #include "evlist.h"
11 #include "evsel.h"
12 #include "strbuf.h"
13 #include "pmu.h"
14 #include "pmus.h"
15 #include "print-events.h"
16 #include "smt.h"
17 #include "tool_pmu.h"
18 #include "expr.h"
19 #include "rblist.h"
20 #include <string.h>
21 #include <errno.h>
22 #include "strlist.h"
23 #include <assert.h>
24 #include <linux/ctype.h>
25 #include <linux/list_sort.h>
26 #include <linux/string.h>
27 #include <linux/zalloc.h>
28 #include <perf/cpumap.h>
29 #include <subcmd/parse-options.h>
30 #include <api/fs/fs.h>
31 #include "util.h"
32 #include <asm/bug.h>
33 #include "cgroup.h"
34 #include "util/hashmap.h"
35 
36 struct metric_event *metricgroup__lookup(struct rblist *metric_events,
37 					 struct evsel *evsel,
38 					 bool create)
39 {
40 	struct rb_node *nd;
41 	struct metric_event me = {
42 		.evsel = evsel
43 	};
44 
45 	if (!metric_events)
46 		return NULL;
47 
48 	if (evsel && evsel->metric_leader)
49 		me.evsel = evsel->metric_leader;
50 	nd = rblist__find(metric_events, &me);
51 	if (nd)
52 		return container_of(nd, struct metric_event, nd);
53 	if (create) {
54 		rblist__add_node(metric_events, &me);
55 		nd = rblist__find(metric_events, &me);
56 		if (nd)
57 			return container_of(nd, struct metric_event, nd);
58 	}
59 	return NULL;
60 }
61 
62 static int metric_event_cmp(struct rb_node *rb_node, const void *entry)
63 {
64 	struct metric_event *a = container_of(rb_node,
65 					      struct metric_event,
66 					      nd);
67 	const struct metric_event *b = entry;
68 
69 	if (a->evsel == b->evsel)
70 		return 0;
71 	if ((char *)a->evsel < (char *)b->evsel)
72 		return -1;
73 	return +1;
74 }
75 
76 static struct rb_node *metric_event_new(struct rblist *rblist __maybe_unused,
77 					const void *entry)
78 {
79 	struct metric_event *me = malloc(sizeof(struct metric_event));
80 
81 	if (!me)
82 		return NULL;
83 	memcpy(me, entry, sizeof(struct metric_event));
84 	me->evsel = ((struct metric_event *)entry)->evsel;
85 	me->is_default = false;
86 	INIT_LIST_HEAD(&me->head);
87 	return &me->nd;
88 }
89 
90 static void metric_event_delete(struct rblist *rblist __maybe_unused,
91 				struct rb_node *rb_node)
92 {
93 	struct metric_event *me = container_of(rb_node, struct metric_event, nd);
94 	struct metric_expr *expr, *tmp;
95 
96 	list_for_each_entry_safe(expr, tmp, &me->head, nd) {
97 		zfree(&expr->metric_name);
98 		zfree(&expr->metric_refs);
99 		zfree(&expr->metric_events);
100 		free(expr);
101 	}
102 
103 	free(me);
104 }
105 
106 void metricgroup__rblist_init(struct rblist *metric_events)
107 {
108 	rblist__init(metric_events);
109 	metric_events->node_cmp = metric_event_cmp;
110 	metric_events->node_new = metric_event_new;
111 	metric_events->node_delete = metric_event_delete;
112 }
113 
114 void metricgroup__rblist_exit(struct rblist *metric_events)
115 {
116 	rblist__exit(metric_events);
117 }
118 
119 /**
120  * The metric under construction. The data held here will be placed in a
121  * metric_expr.
122  */
123 struct metric {
124 	struct list_head nd;
125 	/**
126 	 * The expression parse context importantly holding the IDs contained
127 	 * within the expression.
128 	 */
129 	struct expr_parse_ctx *pctx;
130 	const char *pmu;
131 	/** The name of the metric such as "IPC". */
132 	const char *metric_name;
133 	/** Modifier on the metric such as "u" or NULL for none. */
134 	const char *modifier;
135 	/** The expression to parse, for example, "instructions/cycles". */
136 	const char *metric_expr;
137 	/** Optional threshold expression where zero value is green, otherwise red. */
138 	const char *metric_threshold;
139 	/**
140 	 * The "ScaleUnit" that scales and adds a unit to the metric during
141 	 * output.
142 	 */
143 	const char *metric_unit;
144 	/**
145 	 * Optional name of the metric group reported
146 	 * if the Default metric group is being processed.
147 	 */
148 	const char *default_metricgroup_name;
149 	/** Optional null terminated array of referenced metrics. */
150 	struct metric_ref *metric_refs;
151 	/**
152 	 * Should events of the metric be grouped?
153 	 */
154 	bool group_events;
155 	/** Show events even if in the Default metric group. */
156 	bool default_show_events;
157 	/**
158 	 * Parsed events for the metric. Optional as events may be taken from a
159 	 * different metric whose group contains all the IDs necessary for this
160 	 * one.
161 	 */
162 	struct evlist *evlist;
163 };
164 
165 static void metric__watchdog_constraint_hint(const char *name, bool foot)
166 {
167 	static bool violate_nmi_constraint;
168 
169 	if (!foot) {
170 		pr_warning("Not grouping metric %s's events.\n", name);
171 		violate_nmi_constraint = true;
172 		return;
173 	}
174 
175 	if (!violate_nmi_constraint)
176 		return;
177 
178 	pr_warning("Try disabling the NMI watchdog to comply NO_NMI_WATCHDOG metric constraint:\n"
179 		   "    echo 0 > /proc/sys/kernel/nmi_watchdog\n"
180 		   "    perf stat ...\n"
181 		   "    echo 1 > /proc/sys/kernel/nmi_watchdog\n");
182 }
183 
184 static bool metric__group_events(const struct pmu_metric *pm, bool metric_no_threshold)
185 {
186 	switch (pm->event_grouping) {
187 	case MetricNoGroupEvents:
188 		return false;
189 	case MetricNoGroupEventsNmi:
190 		if (!sysctl__nmi_watchdog_enabled())
191 			return true;
192 		metric__watchdog_constraint_hint(pm->metric_name, /*foot=*/false);
193 		return false;
194 	case MetricNoGroupEventsSmt:
195 		return !smt_on();
196 	case MetricNoGroupEventsThresholdAndNmi:
197 		if (metric_no_threshold)
198 			return true;
199 		if (!sysctl__nmi_watchdog_enabled())
200 			return true;
201 		metric__watchdog_constraint_hint(pm->metric_name, /*foot=*/false);
202 		return false;
203 	case MetricGroupEvents:
204 	default:
205 		return true;
206 	}
207 }
208 
209 static void metric__free(struct metric *m)
210 {
211 	if (!m)
212 		return;
213 
214 	zfree(&m->metric_refs);
215 	expr__ctx_free(m->pctx);
216 	zfree(&m->modifier);
217 	evlist__delete(m->evlist);
218 	free(m);
219 }
220 
221 static struct metric *metric__new(const struct pmu_metric *pm,
222 				  const char *modifier,
223 				  bool metric_no_group,
224 				  bool metric_no_threshold,
225 				  int runtime,
226 				  const char *user_requested_cpu_list,
227 				  bool system_wide)
228 {
229 	struct metric *m;
230 
231 	m = zalloc(sizeof(*m));
232 	if (!m)
233 		return NULL;
234 
235 	m->pctx = expr__ctx_new();
236 	if (!m->pctx)
237 		goto out_err;
238 
239 	m->pmu = pm->pmu ?: "cpu";
240 	m->metric_name = pm->metric_name;
241 	m->default_metricgroup_name = pm->default_metricgroup_name ?: "";
242 	m->modifier = NULL;
243 	if (modifier) {
244 		m->modifier = strdup(modifier);
245 		if (!m->modifier)
246 			goto out_err;
247 	}
248 	m->metric_expr = pm->metric_expr;
249 	m->metric_threshold = pm->metric_threshold;
250 	m->metric_unit = pm->unit;
251 	m->pctx->sctx.user_requested_cpu_list = NULL;
252 	if (user_requested_cpu_list) {
253 		m->pctx->sctx.user_requested_cpu_list = strdup(user_requested_cpu_list);
254 		if (!m->pctx->sctx.user_requested_cpu_list)
255 			goto out_err;
256 	}
257 	m->pctx->sctx.runtime = runtime;
258 	m->pctx->sctx.system_wide = system_wide;
259 	m->group_events = !metric_no_group && metric__group_events(pm, metric_no_threshold);
260 	m->default_show_events = pm->default_show_events;
261 	m->metric_refs = NULL;
262 	m->evlist = NULL;
263 
264 	return m;
265 out_err:
266 	metric__free(m);
267 	return NULL;
268 }
269 
270 static bool contains_metric_id(struct evsel **metric_events, int num_events,
271 			       const char *metric_id)
272 {
273 	int i;
274 
275 	for (i = 0; i < num_events; i++) {
276 		if (!strcmp(evsel__metric_id(metric_events[i]), metric_id))
277 			return true;
278 	}
279 	return false;
280 }
281 
282 /**
283  * setup_metric_events - Find a group of events in metric_evlist that correspond
284  *                       to the IDs from a parsed metric expression.
285  * @pmu: The PMU for the IDs.
286  * @ids: the metric IDs to match.
287  * @metric_evlist: the list of perf events.
288  * @out_metric_events: holds the created metric events array.
289  */
290 static int setup_metric_events(const char *pmu, struct hashmap *ids,
291 			       struct evlist *metric_evlist,
292 			       struct evsel ***out_metric_events)
293 {
294 	struct evsel **metric_events;
295 	const char *metric_id;
296 	struct evsel *ev;
297 	size_t ids_size, matched_events, i;
298 	bool all_pmus = !strcmp(pmu, "all") || !strcmp(pmu, "default_core") ||
299 			perf_pmus__num_core_pmus() == 1 || !is_pmu_core(pmu);
300 
301 	*out_metric_events = NULL;
302 	ids_size = hashmap__size(ids);
303 
304 	metric_events = calloc(ids_size + 1, sizeof(void *));
305 	if (!metric_events)
306 		return -ENOMEM;
307 
308 	matched_events = 0;
309 	evlist__for_each_entry(metric_evlist, ev) {
310 		struct expr_id_data *val_ptr;
311 
312 		/* Don't match events for the wrong hybrid PMU. */
313 		if (!all_pmus && ev->pmu && evsel__is_hybrid(ev) &&
314 		    strcmp(ev->pmu->name, pmu))
315 			continue;
316 		/*
317 		 * Check for duplicate events with the same name. For
318 		 * example, uncore_imc/cas_count_read/ will turn into 6
319 		 * events per socket on skylakex. Only the first such
320 		 * event is placed in metric_events.
321 		 */
322 		metric_id = evsel__metric_id(ev);
323 		if (contains_metric_id(metric_events, matched_events, metric_id))
324 			continue;
325 		/*
326 		 * Does this event belong to the parse context? For
327 		 * combined or shared groups, this metric may not care
328 		 * about this event.
329 		 */
330 		if (hashmap__find(ids, metric_id, &val_ptr)) {
331 			pr_debug("Matched metric-id %s to %s\n", metric_id, evsel__name(ev));
332 			metric_events[matched_events++] = ev;
333 
334 			if (matched_events >= ids_size)
335 				break;
336 		}
337 	}
338 	if (matched_events < ids_size) {
339 		free(metric_events);
340 		return -EINVAL;
341 	}
342 	for (i = 0; i < ids_size; i++) {
343 		ev = metric_events[i];
344 		ev->collect_stat = true;
345 
346 		/*
347 		 * The metric leader points to the identically named
348 		 * event in metric_events.
349 		 */
350 		ev->metric_leader = ev;
351 		/*
352 		 * Mark two events with identical names in the same
353 		 * group (or globally) as being in use as uncore events
354 		 * may be duplicated for each pmu. Set the metric leader
355 		 * of such events to be the event that appears in
356 		 * metric_events.
357 		 */
358 		metric_id = evsel__metric_id(ev);
359 		evlist__for_each_entry_continue(metric_evlist, ev) {
360 			if (!strcmp(evsel__metric_id(ev), metric_id))
361 				ev->metric_leader = metric_events[i];
362 		}
363 	}
364 	*out_metric_events = metric_events;
365 	return 0;
366 }
367 
368 static bool match_metric_or_groups(const char *metric_or_groups, const char *sought)
369 {
370 	int len;
371 	const char *m;
372 
373 	if (!sought)
374 		return false;
375 	if (!strcmp(sought, "all"))
376 		return true;
377 	if (!metric_or_groups)
378 		return !strcasecmp(sought, "No_group");
379 	len = strlen(sought);
380 	if (!strncasecmp(metric_or_groups, sought, len) &&
381 	    (metric_or_groups[len] == 0 || metric_or_groups[len] == ';'))
382 		return true;
383 	m = strchr(metric_or_groups, ';');
384 	return m && match_metric_or_groups(m + 1, sought);
385 }
386 
387 static bool match_pm_metric_or_groups(const struct pmu_metric *pm, const char *pmu,
388 				      const char *metric_or_groups)
389 {
390 	const char *pm_pmu = pm->pmu ?: "cpu";
391 	struct perf_pmu *perf_pmu = NULL;
392 
393 	if (pm->pmu)
394 		perf_pmu = perf_pmus__find(pm->pmu);
395 
396 	if (strcmp(pmu, "all") && strcmp(pm_pmu, pmu) &&
397 	   (perf_pmu && !perf_pmu__name_wildcard_match(perf_pmu, pmu)))
398 		return false;
399 
400 	return match_metric_or_groups(pm->metric_group, metric_or_groups) ||
401 	       match_metric_or_groups(pm->metric_name, metric_or_groups);
402 }
403 
404 struct metricgroup_iter_data {
405 	pmu_metric_iter_fn fn;
406 	void *data;
407 };
408 
409 static int metricgroup__sys_event_iter(const struct pmu_metric *pm,
410 				       const struct pmu_metrics_table *table,
411 				       void *data)
412 {
413 	struct metricgroup_iter_data *d = data;
414 	struct perf_pmu *pmu = NULL;
415 
416 	if (!pm->metric_expr || !pm->compat)
417 		return 0;
418 
419 	/* Only process with the iterator if there is a a PMU that matches the ID. */
420 	pmu = perf_pmus__scan_for_uncore_id(pmu, pm->compat);
421 	return pmu ? d->fn(pm, table, d->data) : 0;
422 }
423 
424 int metricgroup__for_each_metric(const struct pmu_metrics_table *table, pmu_metric_iter_fn fn,
425 				 void *data)
426 {
427 	struct metricgroup_iter_data sys_data = {
428 		.fn = fn,
429 		.data = data,
430 	};
431 	const struct pmu_metrics_table *tables[2] = {
432 		table,
433 		pmu_metrics_table__default(),
434 	};
435 
436 	for (size_t i = 0; i < ARRAY_SIZE(tables); i++) {
437 		int ret;
438 
439 		if (!tables[i])
440 			continue;
441 
442 		ret = pmu_metrics_table__for_each_metric(tables[i], fn, data);
443 		if (ret)
444 			return ret;
445 	}
446 
447 	return pmu_for_each_sys_metric(metricgroup__sys_event_iter, &sys_data);
448 }
449 
450 static const char *code_characters = ",-=@";
451 
452 static int encode_metric_id(struct strbuf *sb, const char *x)
453 {
454 	int ret = 0;
455 
456 	for (; *x; x++) {
457 		const char *c = strchr(code_characters, *x);
458 		if (c) {
459 			ret = strbuf_addch(sb, '!');
460 			if (ret)
461 				break;
462 
463 			ret = strbuf_addch(sb, '0' + (c - code_characters));
464 			if (ret)
465 				break;
466 		} else {
467 			ret = strbuf_addch(sb, *x);
468 			if (ret)
469 				break;
470 		}
471 	}
472 	return ret;
473 }
474 
475 static int decode_metric_id(struct strbuf *sb, const char *x)
476 {
477 	const char *orig = x;
478 	size_t i;
479 	char c;
480 	int ret;
481 
482 	for (; *x; x++) {
483 		c = *x;
484 		if (*x == '!') {
485 			x++;
486 			i = *x - '0';
487 			if (i > strlen(code_characters)) {
488 				pr_err("Bad metric-id encoding in: '%s'", orig);
489 				return -1;
490 			}
491 			c = code_characters[i];
492 		}
493 		ret = strbuf_addch(sb, c);
494 		if (ret)
495 			return ret;
496 	}
497 	return 0;
498 }
499 
500 static int decode_all_metric_ids(struct evlist *perf_evlist, const char *modifier)
501 {
502 	struct evsel *ev;
503 	struct strbuf sb = STRBUF_INIT;
504 	char *cur;
505 	int ret = 0;
506 
507 	evlist__for_each_entry(perf_evlist, ev) {
508 		if (!ev->metric_id)
509 			continue;
510 
511 		ret = strbuf_setlen(&sb, 0);
512 		if (ret)
513 			break;
514 
515 		ret = decode_metric_id(&sb, ev->metric_id);
516 		if (ret)
517 			break;
518 
519 		free((char *)ev->metric_id);
520 		ev->metric_id = strdup(sb.buf);
521 		if (!ev->metric_id) {
522 			ret = -ENOMEM;
523 			break;
524 		}
525 		/*
526 		 * If the name is just the parsed event, use the metric-id to
527 		 * give a more friendly display version.
528 		 */
529 		if (strstr(ev->name, "metric-id=")) {
530 			bool has_slash = false;
531 
532 			zfree(&ev->name);
533 			for (cur = strchr(sb.buf, '@') ; cur; cur = strchr(++cur, '@')) {
534 				*cur = '/';
535 				has_slash = true;
536 			}
537 
538 			if (modifier) {
539 				if (!has_slash && !strchr(sb.buf, ':')) {
540 					ret = strbuf_addch(&sb, ':');
541 					if (ret)
542 						break;
543 				}
544 				ret = strbuf_addstr(&sb, modifier);
545 				if (ret)
546 					break;
547 			}
548 			ev->name = strdup(sb.buf);
549 			if (!ev->name) {
550 				ret = -ENOMEM;
551 				break;
552 			}
553 		}
554 	}
555 	strbuf_release(&sb);
556 	return ret;
557 }
558 
559 static int metricgroup__build_event_string(struct strbuf *events,
560 					   const struct expr_parse_ctx *ctx,
561 					   const char *modifier,
562 					   bool group_events)
563 {
564 	struct hashmap_entry *cur;
565 	size_t bkt;
566 	bool no_group = true, has_tool_events = false;
567 	bool tool_events[TOOL_PMU__EVENT_MAX] = {false};
568 	int ret = 0;
569 
570 #define RETURN_IF_NON_ZERO(x) do { if (x) return x; } while (0)
571 
572 	hashmap__for_each_entry(ctx->ids, cur, bkt) {
573 		const char *sep, *rsep, *id = cur->pkey;
574 		enum tool_pmu_event ev;
575 
576 		pr_debug("found event %s\n", id);
577 
578 		/* Always move tool events outside of the group. */
579 		ev = tool_pmu__str_to_event(id);
580 		if (ev != TOOL_PMU__EVENT_NONE) {
581 			has_tool_events = true;
582 			tool_events[ev] = true;
583 			continue;
584 		}
585 		/* Separate events with commas and open the group if necessary. */
586 		if (no_group) {
587 			if (group_events) {
588 				ret = strbuf_addch(events, '{');
589 				RETURN_IF_NON_ZERO(ret);
590 			}
591 
592 			no_group = false;
593 		} else {
594 			ret = strbuf_addch(events, ',');
595 			RETURN_IF_NON_ZERO(ret);
596 		}
597 		/*
598 		 * Encode the ID as an event string. Add a qualifier for
599 		 * metric_id that is the original name except with characters
600 		 * that parse-events can't parse replaced. For example,
601 		 * 'msr@tsc@' gets added as msr/tsc,metric-id=msr!3tsc!3/
602 		 */
603 		sep = strchr(id, '@');
604 		if (sep != NULL) {
605 			ret = strbuf_add(events, id, sep - id);
606 			RETURN_IF_NON_ZERO(ret);
607 			ret = strbuf_addch(events, '/');
608 			RETURN_IF_NON_ZERO(ret);
609 			rsep = strrchr(sep, '@');
610 			ret = strbuf_add(events, sep + 1, rsep - sep - 1);
611 			RETURN_IF_NON_ZERO(ret);
612 			ret = strbuf_addstr(events, ",metric-id=");
613 			RETURN_IF_NON_ZERO(ret);
614 			sep = rsep;
615 		} else {
616 			sep = strchr(id, ':');
617 			if (sep != NULL) {
618 				ret = strbuf_add(events, id, sep - id);
619 				RETURN_IF_NON_ZERO(ret);
620 			} else {
621 				ret = strbuf_addstr(events, id);
622 				RETURN_IF_NON_ZERO(ret);
623 			}
624 			ret = strbuf_addstr(events, "/metric-id=");
625 			RETURN_IF_NON_ZERO(ret);
626 		}
627 		ret = encode_metric_id(events, id);
628 		RETURN_IF_NON_ZERO(ret);
629 		ret = strbuf_addstr(events, "/");
630 		RETURN_IF_NON_ZERO(ret);
631 
632 		if (sep != NULL) {
633 			ret = strbuf_addstr(events, sep + 1);
634 			RETURN_IF_NON_ZERO(ret);
635 		}
636 		if (modifier) {
637 			ret = strbuf_addstr(events, modifier);
638 			RETURN_IF_NON_ZERO(ret);
639 		}
640 	}
641 	if (!no_group && group_events) {
642 		ret = strbuf_addf(events, "}:W");
643 		RETURN_IF_NON_ZERO(ret);
644 	}
645 	if (has_tool_events) {
646 		int i;
647 
648 		tool_pmu__for_each_event(i) {
649 			if (tool_events[i]) {
650 				if (!no_group) {
651 					ret = strbuf_addch(events, ',');
652 					RETURN_IF_NON_ZERO(ret);
653 				}
654 				no_group = false;
655 				ret = strbuf_addstr(events, tool_pmu__event_to_str(i));
656 				RETURN_IF_NON_ZERO(ret);
657 			}
658 		}
659 	}
660 
661 	return ret;
662 #undef RETURN_IF_NON_ZERO
663 }
664 
665 int __weak arch_get_runtimeparam(const struct pmu_metric *pm __maybe_unused)
666 {
667 	return 1;
668 }
669 
670 /*
671  * A singly linked list on the stack of the names of metrics being
672  * processed. Used to identify recursion.
673  */
674 struct visited_metric {
675 	const char *name;
676 	const struct visited_metric *parent;
677 };
678 
679 struct metricgroup_add_iter_data {
680 	struct list_head *metric_list;
681 	const char *pmu;
682 	const char *metric_name;
683 	const char *modifier;
684 	int *ret;
685 	bool *has_match;
686 	bool metric_no_group;
687 	bool metric_no_threshold;
688 	const char *user_requested_cpu_list;
689 	bool system_wide;
690 	struct metric *root_metric;
691 	const struct visited_metric *visited;
692 	const struct pmu_metrics_table *table;
693 };
694 
695 static int add_metric(struct list_head *metric_list,
696 		      const struct pmu_metric *pm,
697 		      const char *modifier,
698 		      bool metric_no_group,
699 		      bool metric_no_threshold,
700 		      const char *user_requested_cpu_list,
701 		      bool system_wide,
702 		      struct metric *root_metric,
703 		      const struct visited_metric *visited,
704 		      const struct pmu_metrics_table *table);
705 
706 static int metricgroup__find_metric_callback(const struct pmu_metric *pm,
707 					     const struct pmu_metrics_table *table  __maybe_unused,
708 					     void *vdata)
709 {
710 	struct pmu_metric *copied_pm = vdata;
711 
712 	memcpy(copied_pm, pm, sizeof(*pm));
713 	return 0;
714 }
715 
716 /**
717  * resolve_metric - Locate metrics within the root metric and recursively add
718  *                    references to them.
719  * @metric_list: The list the metric is added to.
720  * @pmu: The PMU name to resolve metrics on, or "all" for all PMUs.
721  * @modifier: if non-null event modifiers like "u".
722  * @metric_no_group: Should events written to events be grouped "{}" or
723  *                   global. Grouping is the default but due to multiplexing the
724  *                   user may override.
725  * @user_requested_cpu_list: Command line specified CPUs to record on.
726  * @system_wide: Are events for all processes recorded.
727  * @root_metric: Metrics may reference other metrics to form a tree. In this
728  *               case the root_metric holds all the IDs and a list of referenced
729  *               metrics. When adding a root this argument is NULL.
730  * @visited: A singly linked list of metric names being added that is used to
731  *           detect recursion.
732  * @table: The table that is searched for metrics, most commonly the table for the
733  *       architecture perf is running upon.
734  */
735 static int resolve_metric(struct list_head *metric_list,
736 			  struct perf_pmu *pmu,
737 			  const char *modifier,
738 			  bool metric_no_group,
739 			  bool metric_no_threshold,
740 			  const char *user_requested_cpu_list,
741 			  bool system_wide,
742 			  struct metric *root_metric,
743 			  const struct visited_metric *visited,
744 			  const struct pmu_metrics_table *table)
745 {
746 	struct hashmap_entry *cur;
747 	size_t bkt;
748 	struct to_resolve {
749 		/* The metric to resolve. */
750 		struct pmu_metric pm;
751 		/*
752 		 * The key in the IDs map, this may differ from in case,
753 		 * etc. from pm->metric_name.
754 		 */
755 		const char *key;
756 	} *pending = NULL;
757 	int i, ret = 0, pending_cnt = 0;
758 
759 	/*
760 	 * Iterate all the parsed IDs and if there's a matching metric and it to
761 	 * the pending array.
762 	 */
763 	hashmap__for_each_entry(root_metric->pctx->ids, cur, bkt) {
764 		struct pmu_metric pm;
765 
766 		if (pmu_metrics_table__find_metric(table, pmu, cur->pkey,
767 						   metricgroup__find_metric_callback,
768 						   &pm) != PMU_METRICS__NOT_FOUND) {
769 			pending = realloc(pending,
770 					(pending_cnt + 1) * sizeof(struct to_resolve));
771 			if (!pending)
772 				return -ENOMEM;
773 
774 			memcpy(&pending[pending_cnt].pm, &pm, sizeof(pm));
775 			pending[pending_cnt].key = cur->pkey;
776 			pending_cnt++;
777 		}
778 	}
779 
780 	/* Remove the metric IDs from the context. */
781 	for (i = 0; i < pending_cnt; i++)
782 		expr__del_id(root_metric->pctx, pending[i].key);
783 
784 	/*
785 	 * Recursively add all the metrics, IDs are added to the root metric's
786 	 * context.
787 	 */
788 	for (i = 0; i < pending_cnt; i++) {
789 		ret = add_metric(metric_list, &pending[i].pm, modifier, metric_no_group,
790 				 metric_no_threshold, user_requested_cpu_list, system_wide,
791 				 root_metric, visited, table);
792 		if (ret)
793 			break;
794 	}
795 
796 	free(pending);
797 	return ret;
798 }
799 
800 /**
801  * __add_metric - Add a metric to metric_list.
802  * @metric_list: The list the metric is added to.
803  * @pm: The pmu_metric containing the metric to be added.
804  * @modifier: if non-null event modifiers like "u".
805  * @metric_no_group: Should events written to events be grouped "{}" or
806  *                   global. Grouping is the default but due to multiplexing the
807  *                   user may override.
808  * @metric_no_threshold: Should threshold expressions be ignored?
809  * @runtime: A special argument for the parser only known at runtime.
810  * @user_requested_cpu_list: Command line specified CPUs to record on.
811  * @system_wide: Are events for all processes recorded.
812  * @root_metric: Metrics may reference other metrics to form a tree. In this
813  *               case the root_metric holds all the IDs and a list of referenced
814  *               metrics. When adding a root this argument is NULL.
815  * @visited: A singly linked list of metric names being added that is used to
816  *           detect recursion.
817  * @table: The table that is searched for metrics, most commonly the table for the
818  *       architecture perf is running upon.
819  */
820 static int __add_metric(struct list_head *metric_list,
821 			const struct pmu_metric *pm,
822 			const char *modifier,
823 			bool metric_no_group,
824 			bool metric_no_threshold,
825 			int runtime,
826 			const char *user_requested_cpu_list,
827 			bool system_wide,
828 			struct metric *root_metric,
829 			const struct visited_metric *visited,
830 			const struct pmu_metrics_table *table)
831 {
832 	const struct visited_metric *vm;
833 	int ret;
834 	bool is_root = !root_metric;
835 	const char *expr;
836 	struct visited_metric visited_node = {
837 		.name = pm->metric_name,
838 		.parent = visited,
839 	};
840 
841 	for (vm = visited; vm; vm = vm->parent) {
842 		if (!strcmp(pm->metric_name, vm->name)) {
843 			pr_err("failed: recursion detected for %s\n", pm->metric_name);
844 			return -1;
845 		}
846 	}
847 
848 	if (is_root) {
849 		/*
850 		 * This metric is the root of a tree and may reference other
851 		 * metrics that are added recursively.
852 		 */
853 		root_metric = metric__new(pm, modifier, metric_no_group, metric_no_threshold,
854 					  runtime, user_requested_cpu_list, system_wide);
855 		if (!root_metric)
856 			return -ENOMEM;
857 
858 	} else {
859 		int cnt = 0;
860 
861 		/*
862 		 * This metric was referenced in a metric higher in the
863 		 * tree. Check if the same metric is already resolved in the
864 		 * metric_refs list.
865 		 */
866 		if (root_metric->metric_refs) {
867 			for (; root_metric->metric_refs[cnt].metric_name; cnt++) {
868 				if (!strcmp(pm->metric_name,
869 					    root_metric->metric_refs[cnt].metric_name))
870 					return 0;
871 			}
872 		}
873 
874 		/* Create reference. Need space for the entry and the terminator. */
875 		root_metric->metric_refs = realloc(root_metric->metric_refs,
876 						(cnt + 2) * sizeof(struct metric_ref));
877 		if (!root_metric->metric_refs)
878 			return -ENOMEM;
879 
880 		/*
881 		 * Intentionally passing just const char pointers,
882 		 * from 'pe' object, so they never go away. We don't
883 		 * need to change them, so there's no need to create
884 		 * our own copy.
885 		 */
886 		root_metric->metric_refs[cnt].metric_name = pm->metric_name;
887 		root_metric->metric_refs[cnt].metric_expr = pm->metric_expr;
888 
889 		/* Null terminate array. */
890 		root_metric->metric_refs[cnt+1].metric_name = NULL;
891 		root_metric->metric_refs[cnt+1].metric_expr = NULL;
892 	}
893 
894 	/*
895 	 * For both the parent and referenced metrics, we parse
896 	 * all the metric's IDs and add it to the root context.
897 	 */
898 	ret = 0;
899 	expr = pm->metric_expr;
900 	if (is_root && pm->metric_threshold) {
901 		/*
902 		 * Threshold expressions are built off the actual metric. Switch
903 		 * to use that in case of additional necessary events. Change
904 		 * the visited node name to avoid this being flagged as
905 		 * recursion. If the threshold events are disabled, just use the
906 		 * metric's name as a reference. This allows metric threshold
907 		 * computation if there are sufficient events.
908 		 */
909 		assert(strstr(pm->metric_threshold, pm->metric_name));
910 		expr = metric_no_threshold ? pm->metric_name : pm->metric_threshold;
911 		visited_node.name = "__threshold__";
912 	}
913 
914 	ret = expr__find_ids(expr, NULL, root_metric->pctx);
915 
916 	if (!ret) {
917 		/* Resolve referenced metrics. */
918 		struct perf_pmu *pmu;
919 
920 		if (pm->pmu && pm->pmu[0] != '\0')
921 			pmu = perf_pmus__find(pm->pmu);
922 		else
923 			pmu = perf_pmus__scan_core(/*pmu=*/ NULL);
924 
925 		ret = resolve_metric(metric_list, pmu, modifier, metric_no_group,
926 				     metric_no_threshold, user_requested_cpu_list,
927 				     system_wide, root_metric, &visited_node,
928 				     table);
929 	}
930 	if (ret) {
931 		if (is_root)
932 			metric__free(root_metric);
933 
934 	} else if (is_root)
935 		list_add(&root_metric->nd, metric_list);
936 
937 	return ret;
938 }
939 
940 static int add_metric(struct list_head *metric_list,
941 		      const struct pmu_metric *pm,
942 		      const char *modifier,
943 		      bool metric_no_group,
944 		      bool metric_no_threshold,
945 		      const char *user_requested_cpu_list,
946 		      bool system_wide,
947 		      struct metric *root_metric,
948 		      const struct visited_metric *visited,
949 		      const struct pmu_metrics_table *table)
950 {
951 	int ret = 0;
952 
953 	pr_debug("metric expr %s for %s\n", pm->metric_expr, pm->metric_name);
954 
955 	if (!strstr(pm->metric_expr, "?")) {
956 		ret = __add_metric(metric_list, pm, modifier, metric_no_group,
957 				   metric_no_threshold, 0, user_requested_cpu_list,
958 				   system_wide, root_metric, visited, table);
959 	} else {
960 		int j, count;
961 
962 		count = arch_get_runtimeparam(pm);
963 
964 		/* This loop is added to create multiple
965 		 * events depend on count value and add
966 		 * those events to metric_list.
967 		 */
968 
969 		for (j = 0; j < count && !ret; j++)
970 			ret = __add_metric(metric_list, pm, modifier, metric_no_group,
971 					   metric_no_threshold, j, user_requested_cpu_list,
972 					   system_wide, root_metric, visited, table);
973 	}
974 
975 	return ret;
976 }
977 
978 /**
979  * metric_list_cmp - list_sort comparator that sorts metrics with more events to
980  *                   the front. tool events are excluded from the count.
981  */
982 static int metric_list_cmp(void *priv __maybe_unused, const struct list_head *l,
983 			   const struct list_head *r)
984 {
985 	const struct metric *left = container_of(l, struct metric, nd);
986 	const struct metric *right = container_of(r, struct metric, nd);
987 	struct expr_id_data *data;
988 	int i, left_count, right_count;
989 
990 	left_count = hashmap__size(left->pctx->ids);
991 	tool_pmu__for_each_event(i) {
992 		if (!expr__get_id(left->pctx, tool_pmu__event_to_str(i), &data))
993 			left_count--;
994 	}
995 
996 	right_count = hashmap__size(right->pctx->ids);
997 	tool_pmu__for_each_event(i) {
998 		if (!expr__get_id(right->pctx, tool_pmu__event_to_str(i), &data))
999 			right_count--;
1000 	}
1001 
1002 	return right_count - left_count;
1003 }
1004 
1005 /**
1006  * default_metricgroup_cmp - Implements complex key for the Default metricgroup
1007  *			     that first sorts by default_metricgroup_name, then
1008  *			     metric_name.
1009  */
1010 static int default_metricgroup_cmp(void *priv __maybe_unused,
1011 				   const struct list_head *l,
1012 				   const struct list_head *r)
1013 {
1014 	const struct metric *left = container_of(l, struct metric, nd);
1015 	const struct metric *right = container_of(r, struct metric, nd);
1016 	int diff = strcmp(right->default_metricgroup_name, left->default_metricgroup_name);
1017 
1018 	if (diff)
1019 		return diff;
1020 
1021 	return strcmp(right->metric_name, left->metric_name);
1022 }
1023 
1024 struct metricgroup__add_metric_data {
1025 	struct list_head *list;
1026 	const char *pmu;
1027 	const char *metric_name;
1028 	const char *modifier;
1029 	const char *user_requested_cpu_list;
1030 	bool metric_no_group;
1031 	bool metric_no_threshold;
1032 	bool system_wide;
1033 	bool has_match;
1034 };
1035 
1036 static int metricgroup__add_metric_callback(const struct pmu_metric *pm,
1037 					    const struct pmu_metrics_table *table,
1038 					    void *vdata)
1039 {
1040 	struct metricgroup__add_metric_data *data = vdata;
1041 	int ret = 0;
1042 
1043 	if (pm->metric_expr && match_pm_metric_or_groups(pm, data->pmu, data->metric_name)) {
1044 		bool metric_no_group = data->metric_no_group ||
1045 			match_metric_or_groups(pm->metricgroup_no_group, data->metric_name);
1046 
1047 		data->has_match = true;
1048 		ret = add_metric(data->list, pm, data->modifier, metric_no_group,
1049 				 data->metric_no_threshold, data->user_requested_cpu_list,
1050 				 data->system_wide, /*root_metric=*/NULL,
1051 				 /*visited_metrics=*/NULL, table);
1052 	}
1053 	return ret;
1054 }
1055 
1056 /**
1057  * metricgroup__add_metric - Find and add a metric, or a metric group.
1058  * @pmu: The PMU name to search for metrics on, or "all" for all PMUs.
1059  * @metric_name: The name of the metric or metric group. For example, "IPC"
1060  *               could be the name of a metric and "TopDownL1" the name of a
1061  *               metric group.
1062  * @modifier: if non-null event modifiers like "u".
1063  * @metric_no_group: Should events written to events be grouped "{}" or
1064  *                   global. Grouping is the default but due to multiplexing the
1065  *                   user may override.
1066  * @user_requested_cpu_list: Command line specified CPUs to record on.
1067  * @system_wide: Are events for all processes recorded.
1068  * @metric_list: The list that the metric or metric group are added to.
1069  * @table: The table that is searched for metrics, most commonly the table for the
1070  *       architecture perf is running upon.
1071  */
1072 static int metricgroup__add_metric(const char *pmu, const char *metric_name, const char *modifier,
1073 				   bool metric_no_group, bool metric_no_threshold,
1074 				   const char *user_requested_cpu_list,
1075 				   bool system_wide,
1076 				   struct list_head *metric_list,
1077 				   const struct pmu_metrics_table *table)
1078 {
1079 	LIST_HEAD(list);
1080 	int ret;
1081 	struct metricgroup__add_metric_data data = {
1082 		.list = &list,
1083 		.pmu = pmu,
1084 		.metric_name = metric_name,
1085 		.modifier = modifier,
1086 		.metric_no_group = metric_no_group,
1087 		.metric_no_threshold = metric_no_threshold,
1088 		.user_requested_cpu_list = user_requested_cpu_list,
1089 		.system_wide = system_wide,
1090 		.has_match = false,
1091 	};
1092 
1093 	/*
1094 	 * Iterate over all metrics seeing if metric matches either the
1095 	 * name or group. When it does add the metric to the list.
1096 	 */
1097 	ret = metricgroup__for_each_metric(table, metricgroup__add_metric_callback, &data);
1098 	if (!ret && !data.has_match)
1099 		ret = -ENOENT;
1100 
1101 	/*
1102 	 * add to metric_list so that they can be released
1103 	 * even if it's failed
1104 	 */
1105 	list_splice(&list, metric_list);
1106 	return ret;
1107 }
1108 
1109 /**
1110  * metricgroup__add_metric_list - Find and add metrics, or metric groups,
1111  *                                specified in a list.
1112  * @pmu: A pmu to restrict the metrics to, or "all" for all PMUS.
1113  * @list: the list of metrics or metric groups. For example, "IPC,CPI,TopDownL1"
1114  *        would match the IPC and CPI metrics, and TopDownL1 would match all
1115  *        the metrics in the TopDownL1 group.
1116  * @metric_no_group: Should events written to events be grouped "{}" or
1117  *                   global. Grouping is the default but due to multiplexing the
1118  *                   user may override.
1119  * @user_requested_cpu_list: Command line specified CPUs to record on.
1120  * @system_wide: Are events for all processes recorded.
1121  * @metric_list: The list that metrics are added to.
1122  * @table: The table that is searched for metrics, most commonly the table for the
1123  *       architecture perf is running upon.
1124  */
1125 static int metricgroup__add_metric_list(const char *pmu, const char *list,
1126 					bool metric_no_group,
1127 					bool metric_no_threshold,
1128 					const char *user_requested_cpu_list,
1129 					bool system_wide, struct list_head *metric_list,
1130 					const struct pmu_metrics_table *table)
1131 {
1132 	char *list_itr, *list_copy, *metric_name, *modifier;
1133 	int ret, count = 0;
1134 
1135 	list_copy = strdup(list);
1136 	if (!list_copy)
1137 		return -ENOMEM;
1138 	list_itr = list_copy;
1139 
1140 	while ((metric_name = strsep(&list_itr, ",")) != NULL) {
1141 		modifier = strchr(metric_name, ':');
1142 		if (modifier)
1143 			*modifier++ = '\0';
1144 
1145 		ret = metricgroup__add_metric(pmu, metric_name, modifier,
1146 					      metric_no_group, metric_no_threshold,
1147 					      user_requested_cpu_list,
1148 					      system_wide, metric_list, table);
1149 		if (ret == -EINVAL)
1150 			pr_err("Fail to parse metric or group `%s'\n", metric_name);
1151 		else if (ret == -ENOENT)
1152 			pr_err("Cannot find metric or group `%s'\n", metric_name);
1153 
1154 		if (ret)
1155 			break;
1156 
1157 		count++;
1158 	}
1159 	free(list_copy);
1160 
1161 	if (!ret) {
1162 		/*
1163 		 * Warn about nmi_watchdog if any parsed metrics had the
1164 		 * NO_NMI_WATCHDOG constraint.
1165 		 */
1166 		metric__watchdog_constraint_hint(NULL, /*foot=*/true);
1167 		/* No metrics. */
1168 		if (count == 0)
1169 			return -EINVAL;
1170 	}
1171 	return ret;
1172 }
1173 
1174 static void metricgroup__free_metrics(struct list_head *metric_list)
1175 {
1176 	struct metric *m, *tmp;
1177 
1178 	list_for_each_entry_safe (m, tmp, metric_list, nd) {
1179 		list_del_init(&m->nd);
1180 		metric__free(m);
1181 	}
1182 }
1183 
1184 /**
1185  * find_tool_events - Search for the pressence of tool events in metric_list.
1186  * @metric_list: List to take metrics from.
1187  * @tool_events: Array of false values, indices corresponding to tool events set
1188  *               to true if tool event is found.
1189  */
1190 static void find_tool_events(const struct list_head *metric_list,
1191 			     bool tool_events[TOOL_PMU__EVENT_MAX])
1192 {
1193 	struct metric *m;
1194 
1195 	list_for_each_entry(m, metric_list, nd) {
1196 		int i;
1197 
1198 		tool_pmu__for_each_event(i) {
1199 			struct expr_id_data *data;
1200 
1201 			if (!tool_events[i] &&
1202 			    !expr__get_id(m->pctx, tool_pmu__event_to_str(i), &data))
1203 				tool_events[i] = true;
1204 		}
1205 	}
1206 }
1207 
1208 /**
1209  * build_combined_expr_ctx - Make an expr_parse_ctx with all !group_events
1210  *                           metric IDs, as the IDs are held in a set,
1211  *                           duplicates will be removed.
1212  * @metric_list: List to take metrics from.
1213  * @combined: Out argument for result.
1214  */
1215 static int build_combined_expr_ctx(const struct list_head *metric_list,
1216 				   struct expr_parse_ctx **combined)
1217 {
1218 	struct hashmap_entry *cur;
1219 	size_t bkt;
1220 	struct metric *m;
1221 	char *dup;
1222 	int ret;
1223 
1224 	*combined = expr__ctx_new();
1225 	if (!*combined)
1226 		return -ENOMEM;
1227 
1228 	list_for_each_entry(m, metric_list, nd) {
1229 		if (!m->group_events && !m->modifier) {
1230 			hashmap__for_each_entry(m->pctx->ids, cur, bkt) {
1231 				dup = strdup(cur->pkey);
1232 				if (!dup) {
1233 					ret = -ENOMEM;
1234 					goto err_out;
1235 				}
1236 				ret = expr__add_id(*combined, dup);
1237 				if (ret)
1238 					goto err_out;
1239 			}
1240 		}
1241 	}
1242 	return 0;
1243 err_out:
1244 	expr__ctx_free(*combined);
1245 	*combined = NULL;
1246 	return ret;
1247 }
1248 
1249 /**
1250  * parse_ids - Build the event string for the ids and parse them creating an
1251  *             evlist. The encoded metric_ids are decoded.
1252  * @metric_no_merge: is metric sharing explicitly disabled.
1253  * @fake_pmu: use a fake PMU when testing metrics not supported by the current CPU.
1254  * @ids: the event identifiers parsed from a metric.
1255  * @modifier: any modifiers added to the events.
1256  * @group_events: should events be placed in a weak group.
1257  * @tool_events: entries set true if the tool event of index could be present in
1258  *               the overall list of metrics.
1259  * @out_evlist: the created list of events.
1260  */
1261 static int parse_ids(bool metric_no_merge, bool fake_pmu,
1262 		     struct expr_parse_ctx *ids, const char *modifier,
1263 		     bool group_events, const bool tool_events[TOOL_PMU__EVENT_MAX],
1264 		     struct evlist **out_evlist,
1265 		     const char *filter_pmu)
1266 {
1267 	struct parse_events_error parse_error;
1268 	struct evlist *parsed_evlist;
1269 	struct strbuf events = STRBUF_INIT;
1270 	int ret;
1271 
1272 	*out_evlist = NULL;
1273 	if (!metric_no_merge || hashmap__size(ids->ids) == 0) {
1274 		bool added_event = false;
1275 		int i;
1276 		/*
1277 		 * We may fail to share events between metrics because a tool
1278 		 * event isn't present in one metric. For example, a ratio of
1279 		 * cache misses doesn't need duration_time but the same events
1280 		 * may be used for a misses per second. Events without sharing
1281 		 * implies multiplexing, that is best avoided, so place
1282 		 * all tool events in every group.
1283 		 *
1284 		 * Also, there may be no ids/events in the expression parsing
1285 		 * context because of constant evaluation, e.g.:
1286 		 *    event1 if #smt_on else 0
1287 		 * Add a tool event to avoid a parse error on an empty string.
1288 		 */
1289 		tool_pmu__for_each_event(i) {
1290 			if (tool_events[i]) {
1291 				char *tmp = strdup(tool_pmu__event_to_str(i));
1292 
1293 				if (!tmp)
1294 					return -ENOMEM;
1295 				ids__insert(ids->ids, tmp);
1296 				added_event = true;
1297 			}
1298 		}
1299 		if (!added_event && hashmap__size(ids->ids) == 0) {
1300 			char *tmp = strdup("duration_time");
1301 
1302 			if (!tmp)
1303 				return -ENOMEM;
1304 			ids__insert(ids->ids, tmp);
1305 		}
1306 	}
1307 	ret = metricgroup__build_event_string(&events, ids, modifier,
1308 					      group_events);
1309 	if (ret)
1310 		return ret;
1311 
1312 	parsed_evlist = evlist__new();
1313 	if (!parsed_evlist) {
1314 		ret = -ENOMEM;
1315 		goto err_out;
1316 	}
1317 	pr_debug("Parsing metric events '%s'\n", events.buf);
1318 	parse_events_error__init(&parse_error);
1319 	ret = __parse_events(parsed_evlist, events.buf, filter_pmu,
1320 			     &parse_error, fake_pmu, /*warn_if_reordered=*/false,
1321 			     /*fake_tp=*/false);
1322 	if (ret) {
1323 		parse_events_error__print(&parse_error, events.buf);
1324 		goto err_out;
1325 	}
1326 	ret = decode_all_metric_ids(parsed_evlist, modifier);
1327 	if (ret)
1328 		goto err_out;
1329 
1330 	*out_evlist = parsed_evlist;
1331 	parsed_evlist = NULL;
1332 err_out:
1333 	parse_events_error__exit(&parse_error);
1334 	evlist__delete(parsed_evlist);
1335 	strbuf_release(&events);
1336 	return ret;
1337 }
1338 
1339 /* How many times will a given evsel be used in a set of metrics? */
1340 static int count_uses(struct list_head *metric_list, struct evsel *evsel)
1341 {
1342 	const char *metric_id = evsel__metric_id(evsel);
1343 	struct metric *m;
1344 	int uses = 0;
1345 
1346 	list_for_each_entry(m, metric_list, nd) {
1347 		if (hashmap__find(m->pctx->ids, metric_id, NULL))
1348 			uses++;
1349 	}
1350 	return uses;
1351 }
1352 
1353 /*
1354  * Select the evsel that stat-display will use to trigger shadow/metric
1355  * printing. Pick the least shared non-tool evsel, encouraging metrics to be
1356  * with a hardware counter that is specific to them.
1357  */
1358 static struct evsel *pick_display_evsel(struct list_head *metric_list,
1359 					struct evsel **metric_events)
1360 {
1361 	struct evsel *selected = metric_events[0];
1362 	size_t selected_uses;
1363 	bool selected_is_tool;
1364 
1365 	if (!selected)
1366 		return NULL;
1367 
1368 	selected_uses = count_uses(metric_list, selected);
1369 	selected_is_tool = evsel__is_tool(selected);
1370 	for (int i = 1; metric_events[i]; i++) {
1371 		struct evsel *candidate = metric_events[i];
1372 		size_t candidate_uses = count_uses(metric_list, candidate);
1373 
1374 		if ((selected_is_tool && !evsel__is_tool(candidate)) ||
1375 		    (candidate_uses < selected_uses)) {
1376 			selected = candidate;
1377 			selected_uses = candidate_uses;
1378 			selected_is_tool = evsel__is_tool(selected);
1379 		}
1380 	}
1381 	return selected;
1382 }
1383 
1384 static int parse_groups(struct evlist *perf_evlist,
1385 			const char *pmu, const char *str,
1386 			bool metric_no_group,
1387 			bool metric_no_merge,
1388 			bool metric_no_threshold,
1389 			const char *user_requested_cpu_list,
1390 			bool system_wide,
1391 			bool fake_pmu,
1392 			const struct pmu_metrics_table *table)
1393 {
1394 	struct evlist *combined_evlist = NULL;
1395 	LIST_HEAD(metric_list);
1396 	struct metric *m;
1397 	bool tool_events[TOOL_PMU__EVENT_MAX] = {false};
1398 	bool is_default = !strcmp(str, "Default");
1399 	int ret;
1400 
1401 	ret = metricgroup__add_metric_list(pmu, str, metric_no_group, metric_no_threshold,
1402 					   user_requested_cpu_list,
1403 					   system_wide, &metric_list, table);
1404 	if (ret)
1405 		goto out;
1406 
1407 	/* Sort metrics from largest to smallest. */
1408 	list_sort(NULL, &metric_list, metric_list_cmp);
1409 
1410 	if (!metric_no_merge) {
1411 		struct expr_parse_ctx *combined = NULL;
1412 
1413 		find_tool_events(&metric_list, tool_events);
1414 
1415 		ret = build_combined_expr_ctx(&metric_list, &combined);
1416 
1417 		if (!ret && combined && hashmap__size(combined->ids)) {
1418 			ret = parse_ids(metric_no_merge, fake_pmu, combined,
1419 					/*modifier=*/NULL,
1420 					/*group_events=*/false,
1421 					tool_events,
1422 					&combined_evlist,
1423 					(pmu && strcmp(pmu, "all") == 0) ? NULL : pmu);
1424 		}
1425 		if (combined)
1426 			expr__ctx_free(combined);
1427 
1428 		if (ret)
1429 			goto out;
1430 	}
1431 
1432 	if (is_default)
1433 		list_sort(NULL, &metric_list, default_metricgroup_cmp);
1434 
1435 	list_for_each_entry(m, &metric_list, nd) {
1436 		struct metric_event *me;
1437 		struct evsel **metric_events;
1438 		struct evlist *metric_evlist = NULL;
1439 		struct metric *n;
1440 		struct metric_expr *expr;
1441 
1442 		if (combined_evlist && !m->group_events) {
1443 			metric_evlist = combined_evlist;
1444 		} else if (!metric_no_merge) {
1445 			/*
1446 			 * See if the IDs for this metric are a subset of an
1447 			 * earlier metric.
1448 			 */
1449 			list_for_each_entry(n, &metric_list, nd) {
1450 				if (m == n)
1451 					break;
1452 
1453 				if (n->evlist == NULL)
1454 					continue;
1455 
1456 				if ((!m->modifier && n->modifier) ||
1457 				    (m->modifier && !n->modifier) ||
1458 				    (m->modifier && n->modifier &&
1459 					    strcmp(m->modifier, n->modifier)))
1460 					continue;
1461 
1462 				if ((!m->pmu && n->pmu) ||
1463 				    (m->pmu && !n->pmu) ||
1464 				    (m->pmu && n->pmu && strcmp(m->pmu, n->pmu)))
1465 					continue;
1466 
1467 				if (expr__subset_of_ids(n->pctx, m->pctx)) {
1468 					pr_debug("Events in '%s' fully contained within '%s'\n",
1469 						 m->metric_name, n->metric_name);
1470 					metric_evlist = n->evlist;
1471 					break;
1472 				}
1473 
1474 			}
1475 		}
1476 		if (!metric_evlist) {
1477 			ret = parse_ids(metric_no_merge, fake_pmu, m->pctx, m->modifier,
1478 					m->group_events, tool_events, &m->evlist,
1479 					(pmu && strcmp(pmu, "all") == 0) ? NULL : pmu);
1480 			if (ret)
1481 				goto out;
1482 
1483 			metric_evlist = m->evlist;
1484 		}
1485 		ret = setup_metric_events(fake_pmu ? "all" : m->pmu, m->pctx->ids,
1486 					  metric_evlist, &metric_events);
1487 		if (ret) {
1488 			pr_err("Cannot resolve IDs for %s: %s\n",
1489 				m->metric_name, m->metric_expr);
1490 			goto out;
1491 		}
1492 
1493 		me = metricgroup__lookup(&perf_evlist->metric_events,
1494 					 pick_display_evsel(&metric_list, metric_events),
1495 					 /*create=*/true);
1496 
1497 		expr = malloc(sizeof(struct metric_expr));
1498 		if (!expr) {
1499 			ret = -ENOMEM;
1500 			free(metric_events);
1501 			goto out;
1502 		}
1503 
1504 		expr->metric_refs = m->metric_refs;
1505 		m->metric_refs = NULL;
1506 		expr->metric_expr = m->metric_expr;
1507 		if (m->modifier) {
1508 			char *tmp;
1509 
1510 			if (asprintf(&tmp, "%s:%s", m->metric_name, m->modifier) < 0)
1511 				expr->metric_name = NULL;
1512 			else
1513 				expr->metric_name = tmp;
1514 		} else
1515 			expr->metric_name = strdup(m->metric_name);
1516 
1517 		if (!expr->metric_name) {
1518 			ret = -ENOMEM;
1519 			free(expr);
1520 			free(metric_events);
1521 			goto out;
1522 		}
1523 		if (m->default_show_events) {
1524 			struct evsel *pos;
1525 
1526 			for (int i = 0; metric_events[i]; i++)
1527 				metric_events[i]->default_show_events = true;
1528 			evlist__for_each_entry(metric_evlist, pos) {
1529 				if (pos->metric_leader && pos->metric_leader->default_show_events)
1530 					pos->default_show_events = true;
1531 			}
1532 		}
1533 		expr->metric_threshold = m->metric_threshold;
1534 		expr->metric_unit = m->metric_unit;
1535 		expr->metric_events = metric_events;
1536 		expr->runtime = m->pctx->sctx.runtime;
1537 		expr->default_metricgroup_name = m->default_metricgroup_name;
1538 		me->is_default = is_default;
1539 		list_add(&expr->nd, &me->head);
1540 	}
1541 
1542 
1543 	if (combined_evlist) {
1544 		evlist__splice_list_tail(perf_evlist, &combined_evlist->core.entries);
1545 		evlist__delete(combined_evlist);
1546 	}
1547 
1548 	list_for_each_entry(m, &metric_list, nd) {
1549 		if (m->evlist)
1550 			evlist__splice_list_tail(perf_evlist, &m->evlist->core.entries);
1551 	}
1552 
1553 out:
1554 	metricgroup__free_metrics(&metric_list);
1555 	return ret;
1556 }
1557 
1558 int metricgroup__parse_groups(struct evlist *perf_evlist,
1559 			      const char *pmu,
1560 			      const char *str,
1561 			      bool metric_no_group,
1562 			      bool metric_no_merge,
1563 			      bool metric_no_threshold,
1564 			      const char *user_requested_cpu_list,
1565 			      bool system_wide,
1566 			      bool hardware_aware_grouping)
1567 {
1568 	const struct pmu_metrics_table *table = pmu_metrics_table__find();
1569 
1570 	if (hardware_aware_grouping)
1571 		pr_debug("Use hardware aware grouping instead of traditional metric grouping method\n");
1572 
1573 	return parse_groups(perf_evlist, pmu, str, metric_no_group, metric_no_merge,
1574 			    metric_no_threshold, user_requested_cpu_list, system_wide,
1575 			    /*fake_pmu=*/false, table);
1576 }
1577 
1578 int metricgroup__parse_groups_test(struct evlist *evlist,
1579 				   const struct pmu_metrics_table *table,
1580 				   const char *str)
1581 {
1582 	return parse_groups(evlist, "all", str,
1583 			    /*metric_no_group=*/false,
1584 			    /*metric_no_merge=*/false,
1585 			    /*metric_no_threshold=*/false,
1586 			    /*user_requested_cpu_list=*/NULL,
1587 			    /*system_wide=*/false,
1588 			    /*fake_pmu=*/true, table);
1589 }
1590 
1591 struct metricgroup__has_metric_data {
1592 	const char *pmu;
1593 	const char *metric_or_groups;
1594 };
1595 static int metricgroup__has_metric_or_groups_callback(const struct pmu_metric *pm,
1596 						      const struct pmu_metrics_table *table
1597 							__maybe_unused,
1598 						      void *vdata)
1599 {
1600 	struct metricgroup__has_metric_data *data = vdata;
1601 
1602 	return match_pm_metric_or_groups(pm, data->pmu, data->metric_or_groups) ? 1 : 0;
1603 }
1604 
1605 bool metricgroup__has_metric_or_groups(const char *pmu, const char *metric_or_groups)
1606 {
1607 	const struct pmu_metrics_table *table = pmu_metrics_table__find();
1608 	struct metricgroup__has_metric_data data = {
1609 		.pmu = pmu,
1610 		.metric_or_groups = metric_or_groups,
1611 	};
1612 
1613 	return metricgroup__for_each_metric(table,
1614 					    metricgroup__has_metric_or_groups_callback,
1615 					    &data)
1616 		? true : false;
1617 }
1618 
1619 static int metricgroup__topdown_max_level_callback(const struct pmu_metric *pm,
1620 					    const struct pmu_metrics_table *table __maybe_unused,
1621 					    void *data)
1622 {
1623 	unsigned int *max_level = data;
1624 	unsigned int level;
1625 	const char *p = strstr(pm->metric_group ?: "", "TopdownL");
1626 
1627 	if (!p || p[8] == '\0')
1628 		return 0;
1629 
1630 	level = p[8] - '0';
1631 	if (level > *max_level)
1632 		*max_level = level;
1633 
1634 	return 0;
1635 }
1636 
1637 unsigned int metricgroups__topdown_max_level(void)
1638 {
1639 	unsigned int max_level = 0;
1640 	const struct pmu_metrics_table *table = pmu_metrics_table__find();
1641 
1642 	if (!table)
1643 		return false;
1644 
1645 	pmu_metrics_table__for_each_metric(table, metricgroup__topdown_max_level_callback,
1646 					  &max_level);
1647 	return max_level;
1648 }
1649 
1650 int metricgroup__copy_metric_events(struct evlist *evlist, struct cgroup *cgrp,
1651 				    struct rblist *new_metric_events,
1652 				    struct rblist *old_metric_events)
1653 {
1654 	unsigned int i;
1655 
1656 	for (i = 0; i < rblist__nr_entries(old_metric_events); i++) {
1657 		struct rb_node *nd;
1658 		struct metric_event *old_me, *new_me;
1659 		struct metric_expr *old_expr, *new_expr;
1660 		struct evsel *evsel;
1661 		size_t alloc_size;
1662 		int idx, nr;
1663 
1664 		nd = rblist__entry(old_metric_events, i);
1665 		old_me = container_of(nd, struct metric_event, nd);
1666 
1667 		evsel = evlist__find_evsel(evlist, old_me->evsel->core.idx);
1668 		if (!evsel)
1669 			return -EINVAL;
1670 		new_me = metricgroup__lookup(new_metric_events, evsel, /*create=*/true);
1671 		if (!new_me)
1672 			return -ENOMEM;
1673 
1674 		pr_debug("copying metric event for cgroup '%s': %s (idx=%d)\n",
1675 			 cgrp ? cgrp->name : "root", evsel->name, evsel->core.idx);
1676 
1677 		new_me->is_default = old_me->is_default;
1678 		list_for_each_entry(old_expr, &old_me->head, nd) {
1679 			new_expr = malloc(sizeof(*new_expr));
1680 			if (!new_expr)
1681 				return -ENOMEM;
1682 
1683 			new_expr->metric_expr = old_expr->metric_expr;
1684 			new_expr->metric_threshold = old_expr->metric_threshold;
1685 			new_expr->metric_name = strdup(old_expr->metric_name);
1686 			if (!new_expr->metric_name)
1687 				return -ENOMEM;
1688 
1689 			new_expr->metric_unit = old_expr->metric_unit;
1690 			new_expr->runtime = old_expr->runtime;
1691 			new_expr->default_metricgroup_name = old_expr->default_metricgroup_name;
1692 
1693 			if (old_expr->metric_refs) {
1694 				/* calculate number of metric_events */
1695 				for (nr = 0; old_expr->metric_refs[nr].metric_name; nr++)
1696 					continue;
1697 				alloc_size = sizeof(*new_expr->metric_refs);
1698 				new_expr->metric_refs = calloc(nr + 1, alloc_size);
1699 				if (!new_expr->metric_refs) {
1700 					free(new_expr);
1701 					return -ENOMEM;
1702 				}
1703 
1704 				memcpy(new_expr->metric_refs, old_expr->metric_refs,
1705 				       nr * alloc_size);
1706 			} else {
1707 				new_expr->metric_refs = NULL;
1708 			}
1709 
1710 			/* calculate number of metric_events */
1711 			for (nr = 0; old_expr->metric_events[nr]; nr++)
1712 				continue;
1713 			alloc_size = sizeof(*new_expr->metric_events);
1714 			new_expr->metric_events = calloc(nr + 1, alloc_size);
1715 			if (!new_expr->metric_events) {
1716 				zfree(&new_expr->metric_refs);
1717 				free(new_expr);
1718 				return -ENOMEM;
1719 			}
1720 
1721 			/* copy evsel in the same position */
1722 			for (idx = 0; idx < nr; idx++) {
1723 				evsel = old_expr->metric_events[idx];
1724 				evsel = evlist__find_evsel(evlist, evsel->core.idx);
1725 				if (evsel == NULL) {
1726 					zfree(&new_expr->metric_events);
1727 					zfree(&new_expr->metric_refs);
1728 					free(new_expr);
1729 					return -EINVAL;
1730 				}
1731 				new_expr->metric_events[idx] = evsel;
1732 			}
1733 
1734 			list_add(&new_expr->nd, &new_me->head);
1735 		}
1736 	}
1737 	return 0;
1738 }
1739