xref: /openbmc/linux/tools/perf/util/metricgroup.c (revision 0bf49ffb)
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 "expr.h"
15 #include "rblist.h"
16 #include <string.h>
17 #include <errno.h>
18 #include "strlist.h"
19 #include <assert.h>
20 #include <linux/ctype.h>
21 #include <linux/string.h>
22 #include <linux/zalloc.h>
23 #include <subcmd/parse-options.h>
24 #include <api/fs/fs.h>
25 #include "util.h"
26 #include <asm/bug.h>
27 #include "cgroup.h"
28 
29 struct metric_event *metricgroup__lookup(struct rblist *metric_events,
30 					 struct evsel *evsel,
31 					 bool create)
32 {
33 	struct rb_node *nd;
34 	struct metric_event me = {
35 		.evsel = evsel
36 	};
37 
38 	if (!metric_events)
39 		return NULL;
40 
41 	nd = rblist__find(metric_events, &me);
42 	if (nd)
43 		return container_of(nd, struct metric_event, nd);
44 	if (create) {
45 		rblist__add_node(metric_events, &me);
46 		nd = rblist__find(metric_events, &me);
47 		if (nd)
48 			return container_of(nd, struct metric_event, nd);
49 	}
50 	return NULL;
51 }
52 
53 static int metric_event_cmp(struct rb_node *rb_node, const void *entry)
54 {
55 	struct metric_event *a = container_of(rb_node,
56 					      struct metric_event,
57 					      nd);
58 	const struct metric_event *b = entry;
59 
60 	if (a->evsel == b->evsel)
61 		return 0;
62 	if ((char *)a->evsel < (char *)b->evsel)
63 		return -1;
64 	return +1;
65 }
66 
67 static struct rb_node *metric_event_new(struct rblist *rblist __maybe_unused,
68 					const void *entry)
69 {
70 	struct metric_event *me = malloc(sizeof(struct metric_event));
71 
72 	if (!me)
73 		return NULL;
74 	memcpy(me, entry, sizeof(struct metric_event));
75 	me->evsel = ((struct metric_event *)entry)->evsel;
76 	INIT_LIST_HEAD(&me->head);
77 	return &me->nd;
78 }
79 
80 static void metric_event_delete(struct rblist *rblist __maybe_unused,
81 				struct rb_node *rb_node)
82 {
83 	struct metric_event *me = container_of(rb_node, struct metric_event, nd);
84 	struct metric_expr *expr, *tmp;
85 
86 	list_for_each_entry_safe(expr, tmp, &me->head, nd) {
87 		free(expr->metric_refs);
88 		free(expr->metric_events);
89 		free(expr);
90 	}
91 
92 	free(me);
93 }
94 
95 static void metricgroup__rblist_init(struct rblist *metric_events)
96 {
97 	rblist__init(metric_events);
98 	metric_events->node_cmp = metric_event_cmp;
99 	metric_events->node_new = metric_event_new;
100 	metric_events->node_delete = metric_event_delete;
101 }
102 
103 void metricgroup__rblist_exit(struct rblist *metric_events)
104 {
105 	rblist__exit(metric_events);
106 }
107 
108 /*
109  * A node in the list of referenced metrics. metric_expr
110  * is held as a convenience to avoid a search through the
111  * metric list.
112  */
113 struct metric_ref_node {
114 	const char *metric_name;
115 	const char *metric_expr;
116 	struct list_head list;
117 };
118 
119 struct metric {
120 	struct list_head nd;
121 	struct expr_parse_ctx pctx;
122 	const char *metric_name;
123 	const char *metric_expr;
124 	const char *metric_unit;
125 	struct list_head metric_refs;
126 	int metric_refs_cnt;
127 	int runtime;
128 	bool has_constraint;
129 };
130 
131 #define RECURSION_ID_MAX 1000
132 
133 struct expr_ids {
134 	struct expr_id	id[RECURSION_ID_MAX];
135 	int		cnt;
136 };
137 
138 static struct expr_id *expr_ids__alloc(struct expr_ids *ids)
139 {
140 	if (ids->cnt >= RECURSION_ID_MAX)
141 		return NULL;
142 	return &ids->id[ids->cnt++];
143 }
144 
145 static void expr_ids__exit(struct expr_ids *ids)
146 {
147 	int i;
148 
149 	for (i = 0; i < ids->cnt; i++)
150 		free(ids->id[i].id);
151 }
152 
153 static bool contains_event(struct evsel **metric_events, int num_events,
154 			const char *event_name)
155 {
156 	int i;
157 
158 	for (i = 0; i < num_events; i++) {
159 		if (!strcmp(metric_events[i]->name, event_name))
160 			return true;
161 	}
162 	return false;
163 }
164 
165 static bool evsel_same_pmu(struct evsel *ev1, struct evsel *ev2)
166 {
167 	if (!ev1->pmu_name || !ev2->pmu_name)
168 		return false;
169 
170 	return !strcmp(ev1->pmu_name, ev2->pmu_name);
171 }
172 
173 /**
174  * Find a group of events in perf_evlist that correspond to those from a parsed
175  * metric expression. Note, as find_evsel_group is called in the same order as
176  * perf_evlist was constructed, metric_no_merge doesn't need to test for
177  * underfilling a group.
178  * @perf_evlist: a list of events something like: {metric1 leader, metric1
179  * sibling, metric1 sibling}:W,duration_time,{metric2 leader, metric2 sibling,
180  * metric2 sibling}:W,duration_time
181  * @pctx: the parse context for the metric expression.
182  * @metric_no_merge: don't attempt to share events for the metric with other
183  * metrics.
184  * @has_constraint: is there a contraint on the group of events? In which case
185  * the events won't be grouped.
186  * @metric_events: out argument, null terminated array of evsel's associated
187  * with the metric.
188  * @evlist_used: in/out argument, bitmap tracking which evlist events are used.
189  * @return the first metric event or NULL on failure.
190  */
191 static struct evsel *find_evsel_group(struct evlist *perf_evlist,
192 				      struct expr_parse_ctx *pctx,
193 				      bool metric_no_merge,
194 				      bool has_constraint,
195 				      struct evsel **metric_events,
196 				      unsigned long *evlist_used)
197 {
198 	struct evsel *ev, *current_leader = NULL;
199 	struct expr_id_data *val_ptr;
200 	int i = 0, matched_events = 0, events_to_match;
201 	const int idnum = (int)hashmap__size(&pctx->ids);
202 
203 	/*
204 	 * duration_time is always grouped separately, when events are grouped
205 	 * (ie has_constraint is false) then ignore it in the matching loop and
206 	 * add it to metric_events at the end.
207 	 */
208 	if (!has_constraint &&
209 	    hashmap__find(&pctx->ids, "duration_time", (void **)&val_ptr))
210 		events_to_match = idnum - 1;
211 	else
212 		events_to_match = idnum;
213 
214 	evlist__for_each_entry (perf_evlist, ev) {
215 		/*
216 		 * Events with a constraint aren't grouped and match the first
217 		 * events available.
218 		 */
219 		if (has_constraint && ev->weak_group)
220 			continue;
221 		/* Ignore event if already used and merging is disabled. */
222 		if (metric_no_merge && test_bit(ev->idx, evlist_used))
223 			continue;
224 		if (!has_constraint && ev->leader != current_leader) {
225 			/*
226 			 * Start of a new group, discard the whole match and
227 			 * start again.
228 			 */
229 			matched_events = 0;
230 			memset(metric_events, 0,
231 				sizeof(struct evsel *) * idnum);
232 			current_leader = ev->leader;
233 		}
234 		/*
235 		 * Check for duplicate events with the same name. For example,
236 		 * uncore_imc/cas_count_read/ will turn into 6 events per socket
237 		 * on skylakex. Only the first such event is placed in
238 		 * metric_events. If events aren't grouped then this also
239 		 * ensures that the same event in different sibling groups
240 		 * aren't both added to metric_events.
241 		 */
242 		if (contains_event(metric_events, matched_events, ev->name))
243 			continue;
244 		/* Does this event belong to the parse context? */
245 		if (hashmap__find(&pctx->ids, ev->name, (void **)&val_ptr))
246 			metric_events[matched_events++] = ev;
247 
248 		if (matched_events == events_to_match)
249 			break;
250 	}
251 
252 	if (events_to_match != idnum) {
253 		/* Add the first duration_time. */
254 		evlist__for_each_entry(perf_evlist, ev) {
255 			if (!strcmp(ev->name, "duration_time")) {
256 				metric_events[matched_events++] = ev;
257 				break;
258 			}
259 		}
260 	}
261 
262 	if (matched_events != idnum) {
263 		/* Not a whole match */
264 		return NULL;
265 	}
266 
267 	metric_events[idnum] = NULL;
268 
269 	for (i = 0; i < idnum; i++) {
270 		ev = metric_events[i];
271 		/* Don't free the used events. */
272 		set_bit(ev->idx, evlist_used);
273 		/*
274 		 * The metric leader points to the identically named event in
275 		 * metric_events.
276 		 */
277 		ev->metric_leader = ev;
278 		/*
279 		 * Mark two events with identical names in the same group (or
280 		 * globally) as being in use as uncore events may be duplicated
281 		 * for each pmu. Set the metric leader of such events to be the
282 		 * event that appears in metric_events.
283 		 */
284 		evlist__for_each_entry_continue(perf_evlist, ev) {
285 			/*
286 			 * If events are grouped then the search can terminate
287 			 * when then group is left.
288 			 */
289 			if (!has_constraint &&
290 			    ev->leader != metric_events[i]->leader &&
291 			    evsel_same_pmu(ev->leader, metric_events[i]->leader))
292 				break;
293 			if (!strcmp(metric_events[i]->name, ev->name)) {
294 				set_bit(ev->idx, evlist_used);
295 				ev->metric_leader = metric_events[i];
296 			}
297 		}
298 	}
299 
300 	return metric_events[0];
301 }
302 
303 static int metricgroup__setup_events(struct list_head *groups,
304 				     bool metric_no_merge,
305 				     struct evlist *perf_evlist,
306 				     struct rblist *metric_events_list)
307 {
308 	struct metric_event *me;
309 	struct metric_expr *expr;
310 	int i = 0;
311 	int ret = 0;
312 	struct metric *m;
313 	struct evsel *evsel, *tmp;
314 	unsigned long *evlist_used;
315 
316 	evlist_used = bitmap_alloc(perf_evlist->core.nr_entries);
317 	if (!evlist_used)
318 		return -ENOMEM;
319 
320 	list_for_each_entry (m, groups, nd) {
321 		struct evsel **metric_events;
322 		struct metric_ref *metric_refs = NULL;
323 
324 		metric_events = calloc(sizeof(void *),
325 				hashmap__size(&m->pctx.ids) + 1);
326 		if (!metric_events) {
327 			ret = -ENOMEM;
328 			break;
329 		}
330 		evsel = find_evsel_group(perf_evlist, &m->pctx,
331 					 metric_no_merge,
332 					 m->has_constraint, metric_events,
333 					 evlist_used);
334 		if (!evsel) {
335 			pr_debug("Cannot resolve %s: %s\n",
336 					m->metric_name, m->metric_expr);
337 			free(metric_events);
338 			continue;
339 		}
340 		for (i = 0; metric_events[i]; i++)
341 			metric_events[i]->collect_stat = true;
342 		me = metricgroup__lookup(metric_events_list, evsel, true);
343 		if (!me) {
344 			ret = -ENOMEM;
345 			free(metric_events);
346 			break;
347 		}
348 		expr = malloc(sizeof(struct metric_expr));
349 		if (!expr) {
350 			ret = -ENOMEM;
351 			free(metric_events);
352 			break;
353 		}
354 
355 		/*
356 		 * Collect and store collected nested expressions
357 		 * for metric processing.
358 		 */
359 		if (m->metric_refs_cnt) {
360 			struct metric_ref_node *ref;
361 
362 			metric_refs = zalloc(sizeof(struct metric_ref) * (m->metric_refs_cnt + 1));
363 			if (!metric_refs) {
364 				ret = -ENOMEM;
365 				free(metric_events);
366 				free(expr);
367 				break;
368 			}
369 
370 			i = 0;
371 			list_for_each_entry(ref, &m->metric_refs, list) {
372 				/*
373 				 * Intentionally passing just const char pointers,
374 				 * originally from 'struct pmu_event' object.
375 				 * We don't need to change them, so there's no
376 				 * need to create our own copy.
377 				 */
378 				metric_refs[i].metric_name = ref->metric_name;
379 				metric_refs[i].metric_expr = ref->metric_expr;
380 				i++;
381 			}
382 		}
383 
384 		expr->metric_refs = metric_refs;
385 		expr->metric_expr = m->metric_expr;
386 		expr->metric_name = m->metric_name;
387 		expr->metric_unit = m->metric_unit;
388 		expr->metric_events = metric_events;
389 		expr->runtime = m->runtime;
390 		list_add(&expr->nd, &me->head);
391 	}
392 
393 	evlist__for_each_entry_safe(perf_evlist, tmp, evsel) {
394 		if (!test_bit(evsel->idx, evlist_used)) {
395 			evlist__remove(perf_evlist, evsel);
396 			evsel__delete(evsel);
397 		}
398 	}
399 	bitmap_free(evlist_used);
400 
401 	return ret;
402 }
403 
404 static bool match_metric(const char *n, const char *list)
405 {
406 	int len;
407 	char *m;
408 
409 	if (!list)
410 		return false;
411 	if (!strcmp(list, "all"))
412 		return true;
413 	if (!n)
414 		return !strcasecmp(list, "No_group");
415 	len = strlen(list);
416 	m = strcasestr(n, list);
417 	if (!m)
418 		return false;
419 	if ((m == n || m[-1] == ';' || m[-1] == ' ') &&
420 	    (m[len] == 0 || m[len] == ';'))
421 		return true;
422 	return false;
423 }
424 
425 static bool match_pe_metric(struct pmu_event *pe, const char *metric)
426 {
427 	return match_metric(pe->metric_group, metric) ||
428 	       match_metric(pe->metric_name, metric);
429 }
430 
431 struct mep {
432 	struct rb_node nd;
433 	const char *name;
434 	struct strlist *metrics;
435 };
436 
437 static int mep_cmp(struct rb_node *rb_node, const void *entry)
438 {
439 	struct mep *a = container_of(rb_node, struct mep, nd);
440 	struct mep *b = (struct mep *)entry;
441 
442 	return strcmp(a->name, b->name);
443 }
444 
445 static struct rb_node *mep_new(struct rblist *rl __maybe_unused,
446 					const void *entry)
447 {
448 	struct mep *me = malloc(sizeof(struct mep));
449 
450 	if (!me)
451 		return NULL;
452 	memcpy(me, entry, sizeof(struct mep));
453 	me->name = strdup(me->name);
454 	if (!me->name)
455 		goto out_me;
456 	me->metrics = strlist__new(NULL, NULL);
457 	if (!me->metrics)
458 		goto out_name;
459 	return &me->nd;
460 out_name:
461 	zfree(&me->name);
462 out_me:
463 	free(me);
464 	return NULL;
465 }
466 
467 static struct mep *mep_lookup(struct rblist *groups, const char *name)
468 {
469 	struct rb_node *nd;
470 	struct mep me = {
471 		.name = name
472 	};
473 	nd = rblist__find(groups, &me);
474 	if (nd)
475 		return container_of(nd, struct mep, nd);
476 	rblist__add_node(groups, &me);
477 	nd = rblist__find(groups, &me);
478 	if (nd)
479 		return container_of(nd, struct mep, nd);
480 	return NULL;
481 }
482 
483 static void mep_delete(struct rblist *rl __maybe_unused,
484 		       struct rb_node *nd)
485 {
486 	struct mep *me = container_of(nd, struct mep, nd);
487 
488 	strlist__delete(me->metrics);
489 	zfree(&me->name);
490 	free(me);
491 }
492 
493 static void metricgroup__print_strlist(struct strlist *metrics, bool raw)
494 {
495 	struct str_node *sn;
496 	int n = 0;
497 
498 	strlist__for_each_entry (sn, metrics) {
499 		if (raw)
500 			printf("%s%s", n > 0 ? " " : "", sn->s);
501 		else
502 			printf("  %s\n", sn->s);
503 		n++;
504 	}
505 	if (raw)
506 		putchar('\n');
507 }
508 
509 static int metricgroup__print_pmu_event(struct pmu_event *pe,
510 					bool metricgroups, char *filter,
511 					bool raw, bool details,
512 					struct rblist *groups,
513 					struct strlist *metriclist)
514 {
515 	const char *g;
516 	char *omg, *mg;
517 
518 	g = pe->metric_group;
519 	if (!g && pe->metric_name) {
520 		if (pe->name)
521 			return 0;
522 		g = "No_group";
523 	}
524 
525 	if (!g)
526 		return 0;
527 
528 	mg = strdup(g);
529 
530 	if (!mg)
531 		return -ENOMEM;
532 	omg = mg;
533 	while ((g = strsep(&mg, ";")) != NULL) {
534 		struct mep *me;
535 		char *s;
536 
537 		g = skip_spaces(g);
538 		if (*g == 0)
539 			g = "No_group";
540 		if (filter && !strstr(g, filter))
541 			continue;
542 		if (raw)
543 			s = (char *)pe->metric_name;
544 		else {
545 			if (asprintf(&s, "%s\n%*s%s]",
546 				     pe->metric_name, 8, "[", pe->desc) < 0)
547 				return -1;
548 			if (details) {
549 				if (asprintf(&s, "%s\n%*s%s]",
550 					     s, 8, "[", pe->metric_expr) < 0)
551 					return -1;
552 			}
553 		}
554 
555 		if (!s)
556 			continue;
557 
558 		if (!metricgroups) {
559 			strlist__add(metriclist, s);
560 		} else {
561 			me = mep_lookup(groups, g);
562 			if (!me)
563 				continue;
564 			strlist__add(me->metrics, s);
565 		}
566 
567 		if (!raw)
568 			free(s);
569 	}
570 	free(omg);
571 
572 	return 0;
573 }
574 
575 struct metricgroup_print_sys_idata {
576 	struct strlist *metriclist;
577 	char *filter;
578 	struct rblist *groups;
579 	bool metricgroups;
580 	bool raw;
581 	bool details;
582 };
583 
584 typedef int (*metricgroup_sys_event_iter_fn)(struct pmu_event *pe, void *);
585 
586 struct metricgroup_iter_data {
587 	metricgroup_sys_event_iter_fn fn;
588 	void *data;
589 };
590 
591 static int metricgroup__sys_event_iter(struct pmu_event *pe, void *data)
592 {
593 	struct metricgroup_iter_data *d = data;
594 	struct perf_pmu *pmu = NULL;
595 
596 	if (!pe->metric_expr || !pe->compat)
597 		return 0;
598 
599 	while ((pmu = perf_pmu__scan(pmu))) {
600 
601 		if (!pmu->id || strcmp(pmu->id, pe->compat))
602 			continue;
603 
604 		return d->fn(pe, d->data);
605 	}
606 
607 	return 0;
608 }
609 
610 static int metricgroup__print_sys_event_iter(struct pmu_event *pe, void *data)
611 {
612 	struct metricgroup_print_sys_idata *d = data;
613 
614 	return metricgroup__print_pmu_event(pe, d->metricgroups, d->filter, d->raw,
615 				     d->details, d->groups, d->metriclist);
616 }
617 
618 void metricgroup__print(bool metrics, bool metricgroups, char *filter,
619 			bool raw, bool details)
620 {
621 	struct pmu_events_map *map = perf_pmu__find_map(NULL);
622 	struct pmu_event *pe;
623 	int i;
624 	struct rblist groups;
625 	struct rb_node *node, *next;
626 	struct strlist *metriclist = NULL;
627 
628 	if (!metricgroups) {
629 		metriclist = strlist__new(NULL, NULL);
630 		if (!metriclist)
631 			return;
632 	}
633 
634 	rblist__init(&groups);
635 	groups.node_new = mep_new;
636 	groups.node_cmp = mep_cmp;
637 	groups.node_delete = mep_delete;
638 	for (i = 0; map; i++) {
639 		pe = &map->table[i];
640 
641 		if (!pe->name && !pe->metric_group && !pe->metric_name)
642 			break;
643 		if (!pe->metric_expr)
644 			continue;
645 		if (metricgroup__print_pmu_event(pe, metricgroups, filter,
646 						 raw, details, &groups,
647 						 metriclist) < 0)
648 			return;
649 	}
650 
651 	{
652 		struct metricgroup_iter_data data = {
653 			.fn = metricgroup__print_sys_event_iter,
654 			.data = (void *) &(struct metricgroup_print_sys_idata){
655 				.metriclist = metriclist,
656 				.metricgroups = metricgroups,
657 				.filter = filter,
658 				.raw = raw,
659 				.details = details,
660 				.groups = &groups,
661 			},
662 		};
663 
664 		pmu_for_each_sys_event(metricgroup__sys_event_iter, &data);
665 	}
666 
667 	if (!filter || !rblist__empty(&groups)) {
668 		if (metricgroups && !raw)
669 			printf("\nMetric Groups:\n\n");
670 		else if (metrics && !raw)
671 			printf("\nMetrics:\n\n");
672 	}
673 
674 	for (node = rb_first_cached(&groups.entries); node; node = next) {
675 		struct mep *me = container_of(node, struct mep, nd);
676 
677 		if (metricgroups)
678 			printf("%s%s%s", me->name, metrics && !raw ? ":" : "", raw ? " " : "\n");
679 		if (metrics)
680 			metricgroup__print_strlist(me->metrics, raw);
681 		next = rb_next(node);
682 		rblist__remove_node(&groups, node);
683 	}
684 	if (!metricgroups)
685 		metricgroup__print_strlist(metriclist, raw);
686 	strlist__delete(metriclist);
687 }
688 
689 static void metricgroup__add_metric_weak_group(struct strbuf *events,
690 					       struct expr_parse_ctx *ctx)
691 {
692 	struct hashmap_entry *cur;
693 	size_t bkt;
694 	bool no_group = true, has_duration = false;
695 
696 	hashmap__for_each_entry((&ctx->ids), cur, bkt) {
697 		pr_debug("found event %s\n", (const char *)cur->key);
698 		/*
699 		 * Duration time maps to a software event and can make
700 		 * groups not count. Always use it outside a
701 		 * group.
702 		 */
703 		if (!strcmp(cur->key, "duration_time")) {
704 			has_duration = true;
705 			continue;
706 		}
707 		strbuf_addf(events, "%s%s",
708 			no_group ? "{" : ",",
709 			(const char *)cur->key);
710 		no_group = false;
711 	}
712 	if (!no_group) {
713 		strbuf_addf(events, "}:W");
714 		if (has_duration)
715 			strbuf_addf(events, ",duration_time");
716 	} else if (has_duration)
717 		strbuf_addf(events, "duration_time");
718 }
719 
720 static void metricgroup__add_metric_non_group(struct strbuf *events,
721 					      struct expr_parse_ctx *ctx)
722 {
723 	struct hashmap_entry *cur;
724 	size_t bkt;
725 	bool first = true;
726 
727 	hashmap__for_each_entry((&ctx->ids), cur, bkt) {
728 		if (!first)
729 			strbuf_addf(events, ",");
730 		strbuf_addf(events, "%s", (const char *)cur->key);
731 		first = false;
732 	}
733 }
734 
735 static void metricgroup___watchdog_constraint_hint(const char *name, bool foot)
736 {
737 	static bool violate_nmi_constraint;
738 
739 	if (!foot) {
740 		pr_warning("Splitting metric group %s into standalone metrics.\n", name);
741 		violate_nmi_constraint = true;
742 		return;
743 	}
744 
745 	if (!violate_nmi_constraint)
746 		return;
747 
748 	pr_warning("Try disabling the NMI watchdog to comply NO_NMI_WATCHDOG metric constraint:\n"
749 		   "    echo 0 > /proc/sys/kernel/nmi_watchdog\n"
750 		   "    perf stat ...\n"
751 		   "    echo 1 > /proc/sys/kernel/nmi_watchdog\n");
752 }
753 
754 static bool metricgroup__has_constraint(struct pmu_event *pe)
755 {
756 	if (!pe->metric_constraint)
757 		return false;
758 
759 	if (!strcmp(pe->metric_constraint, "NO_NMI_WATCHDOG") &&
760 	    sysctl__nmi_watchdog_enabled()) {
761 		metricgroup___watchdog_constraint_hint(pe->metric_name, false);
762 		return true;
763 	}
764 
765 	return false;
766 }
767 
768 int __weak arch_get_runtimeparam(struct pmu_event *pe __maybe_unused)
769 {
770 	return 1;
771 }
772 
773 struct metricgroup_add_iter_data {
774 	struct list_head *metric_list;
775 	const char *metric;
776 	struct expr_ids *ids;
777 	int *ret;
778 	bool *has_match;
779 	bool metric_no_group;
780 };
781 
782 static int __add_metric(struct list_head *metric_list,
783 			struct pmu_event *pe,
784 			bool metric_no_group,
785 			int runtime,
786 			struct metric **mp,
787 			struct expr_id *parent,
788 			struct expr_ids *ids)
789 {
790 	struct metric_ref_node *ref;
791 	struct metric *m;
792 
793 	if (*mp == NULL) {
794 		/*
795 		 * We got in here for the parent group,
796 		 * allocate it and put it on the list.
797 		 */
798 		m = zalloc(sizeof(*m));
799 		if (!m)
800 			return -ENOMEM;
801 
802 		expr__ctx_init(&m->pctx);
803 		m->metric_name = pe->metric_name;
804 		m->metric_expr = pe->metric_expr;
805 		m->metric_unit = pe->unit;
806 		m->runtime = runtime;
807 		m->has_constraint = metric_no_group || metricgroup__has_constraint(pe);
808 		INIT_LIST_HEAD(&m->metric_refs);
809 		m->metric_refs_cnt = 0;
810 
811 		parent = expr_ids__alloc(ids);
812 		if (!parent) {
813 			free(m);
814 			return -EINVAL;
815 		}
816 
817 		parent->id = strdup(pe->metric_name);
818 		if (!parent->id) {
819 			free(m);
820 			return -ENOMEM;
821 		}
822 		*mp = m;
823 	} else {
824 		/*
825 		 * We got here for the referenced metric, via the
826 		 * recursive metricgroup__add_metric call, add
827 		 * it to the parent group.
828 		 */
829 		m = *mp;
830 
831 		ref = malloc(sizeof(*ref));
832 		if (!ref)
833 			return -ENOMEM;
834 
835 		/*
836 		 * Intentionally passing just const char pointers,
837 		 * from 'pe' object, so they never go away. We don't
838 		 * need to change them, so there's no need to create
839 		 * our own copy.
840 		 */
841 		ref->metric_name = pe->metric_name;
842 		ref->metric_expr = pe->metric_expr;
843 
844 		list_add(&ref->list, &m->metric_refs);
845 		m->metric_refs_cnt++;
846 	}
847 
848 	/* Force all found IDs in metric to have us as parent ID. */
849 	WARN_ON_ONCE(!parent);
850 	m->pctx.parent = parent;
851 
852 	/*
853 	 * For both the parent and referenced metrics, we parse
854 	 * all the metric's IDs and add it to the parent context.
855 	 */
856 	if (expr__find_other(pe->metric_expr, NULL, &m->pctx, runtime) < 0) {
857 		if (m->metric_refs_cnt == 0) {
858 			expr__ctx_clear(&m->pctx);
859 			free(m);
860 			*mp = NULL;
861 		}
862 		return -EINVAL;
863 	}
864 
865 	/*
866 	 * We add new group only in the 'parent' call,
867 	 * so bail out for referenced metric case.
868 	 */
869 	if (m->metric_refs_cnt)
870 		return 0;
871 
872 	if (list_empty(metric_list))
873 		list_add(&m->nd, metric_list);
874 	else {
875 		struct list_head *pos;
876 
877 		/* Place the largest groups at the front. */
878 		list_for_each_prev(pos, metric_list) {
879 			struct metric *old = list_entry(pos, struct metric, nd);
880 
881 			if (hashmap__size(&m->pctx.ids) <=
882 			    hashmap__size(&old->pctx.ids))
883 				break;
884 		}
885 		list_add(&m->nd, pos);
886 	}
887 
888 	return 0;
889 }
890 
891 #define map_for_each_event(__pe, __idx, __map)					\
892 	if (__map)								\
893 		for (__idx = 0, __pe = &__map->table[__idx];			\
894 		     __pe->name || __pe->metric_group || __pe->metric_name;	\
895 		     __pe = &__map->table[++__idx])
896 
897 #define map_for_each_metric(__pe, __idx, __map, __metric)		\
898 	map_for_each_event(__pe, __idx, __map)				\
899 		if (__pe->metric_expr &&				\
900 		    (match_metric(__pe->metric_group, __metric) ||	\
901 		     match_metric(__pe->metric_name, __metric)))
902 
903 static struct pmu_event *find_metric(const char *metric, struct pmu_events_map *map)
904 {
905 	struct pmu_event *pe;
906 	int i;
907 
908 	map_for_each_event(pe, i, map) {
909 		if (match_metric(pe->metric_name, metric))
910 			return pe;
911 	}
912 
913 	return NULL;
914 }
915 
916 static int recursion_check(struct metric *m, const char *id, struct expr_id **parent,
917 			   struct expr_ids *ids)
918 {
919 	struct expr_id_data *data;
920 	struct expr_id *p;
921 	int ret;
922 
923 	/*
924 	 * We get the parent referenced by 'id' argument and
925 	 * traverse through all the parent object IDs to check
926 	 * if we already processed 'id', if we did, it's recursion
927 	 * and we fail.
928 	 */
929 	ret = expr__get_id(&m->pctx, id, &data);
930 	if (ret)
931 		return ret;
932 
933 	p = expr_id_data__parent(data);
934 
935 	while (p->parent) {
936 		if (!strcmp(p->id, id)) {
937 			pr_err("failed: recursion detected for %s\n", id);
938 			return -1;
939 		}
940 		p = p->parent;
941 	}
942 
943 	/*
944 	 * If we are over the limit of static entris, the metric
945 	 * is too difficult/nested to process, fail as well.
946 	 */
947 	p = expr_ids__alloc(ids);
948 	if (!p) {
949 		pr_err("failed: too many nested metrics\n");
950 		return -EINVAL;
951 	}
952 
953 	p->id     = strdup(id);
954 	p->parent = expr_id_data__parent(data);
955 	*parent   = p;
956 
957 	return p->id ? 0 : -ENOMEM;
958 }
959 
960 static int add_metric(struct list_head *metric_list,
961 		      struct pmu_event *pe,
962 		      bool metric_no_group,
963 		      struct metric **mp,
964 		      struct expr_id *parent,
965 		      struct expr_ids *ids);
966 
967 static int __resolve_metric(struct metric *m,
968 			    bool metric_no_group,
969 			    struct list_head *metric_list,
970 			    struct pmu_events_map *map,
971 			    struct expr_ids *ids)
972 {
973 	struct hashmap_entry *cur;
974 	size_t bkt;
975 	bool all;
976 	int ret;
977 
978 	/*
979 	 * Iterate all the parsed IDs and if there's metric,
980 	 * add it to the context.
981 	 */
982 	do {
983 		all = true;
984 		hashmap__for_each_entry((&m->pctx.ids), cur, bkt) {
985 			struct expr_id *parent;
986 			struct pmu_event *pe;
987 
988 			pe = find_metric(cur->key, map);
989 			if (!pe)
990 				continue;
991 
992 			ret = recursion_check(m, cur->key, &parent, ids);
993 			if (ret)
994 				return ret;
995 
996 			all = false;
997 			/* The metric key itself needs to go out.. */
998 			expr__del_id(&m->pctx, cur->key);
999 
1000 			/* ... and it gets resolved to the parent context. */
1001 			ret = add_metric(metric_list, pe, metric_no_group, &m, parent, ids);
1002 			if (ret)
1003 				return ret;
1004 
1005 			/*
1006 			 * We added new metric to hashmap, so we need
1007 			 * to break the iteration and start over.
1008 			 */
1009 			break;
1010 		}
1011 	} while (!all);
1012 
1013 	return 0;
1014 }
1015 
1016 static int resolve_metric(bool metric_no_group,
1017 			  struct list_head *metric_list,
1018 			  struct pmu_events_map *map,
1019 			  struct expr_ids *ids)
1020 {
1021 	struct metric *m;
1022 	int err;
1023 
1024 	list_for_each_entry(m, metric_list, nd) {
1025 		err = __resolve_metric(m, metric_no_group, metric_list, map, ids);
1026 		if (err)
1027 			return err;
1028 	}
1029 	return 0;
1030 }
1031 
1032 static int add_metric(struct list_head *metric_list,
1033 		      struct pmu_event *pe,
1034 		      bool metric_no_group,
1035 		      struct metric **m,
1036 		      struct expr_id *parent,
1037 		      struct expr_ids *ids)
1038 {
1039 	struct metric *orig = *m;
1040 	int ret = 0;
1041 
1042 	pr_debug("metric expr %s for %s\n", pe->metric_expr, pe->metric_name);
1043 
1044 	if (!strstr(pe->metric_expr, "?")) {
1045 		ret = __add_metric(metric_list, pe, metric_no_group, 1, m, parent, ids);
1046 	} else {
1047 		int j, count;
1048 
1049 		count = arch_get_runtimeparam(pe);
1050 
1051 		/* This loop is added to create multiple
1052 		 * events depend on count value and add
1053 		 * those events to metric_list.
1054 		 */
1055 
1056 		for (j = 0; j < count && !ret; j++, *m = orig)
1057 			ret = __add_metric(metric_list, pe, metric_no_group, j, m, parent, ids);
1058 	}
1059 
1060 	return ret;
1061 }
1062 
1063 static int metricgroup__add_metric_sys_event_iter(struct pmu_event *pe,
1064 						  void *data)
1065 {
1066 	struct metricgroup_add_iter_data *d = data;
1067 	struct metric *m = NULL;
1068 	int ret;
1069 
1070 	if (!match_pe_metric(pe, d->metric))
1071 		return 0;
1072 
1073 	ret = add_metric(d->metric_list, pe, d->metric_no_group, &m, NULL, d->ids);
1074 	if (ret)
1075 		return ret;
1076 
1077 	ret = resolve_metric(d->metric_no_group,
1078 				     d->metric_list, NULL, d->ids);
1079 	if (ret)
1080 		return ret;
1081 
1082 	*(d->has_match) = true;
1083 
1084 	return *d->ret;
1085 }
1086 
1087 static int metricgroup__add_metric(const char *metric, bool metric_no_group,
1088 				   struct strbuf *events,
1089 				   struct list_head *metric_list,
1090 				   struct pmu_events_map *map)
1091 {
1092 	struct expr_ids ids = { .cnt = 0, };
1093 	struct pmu_event *pe;
1094 	struct metric *m;
1095 	LIST_HEAD(list);
1096 	int i, ret;
1097 	bool has_match = false;
1098 
1099 	map_for_each_metric(pe, i, map, metric) {
1100 		has_match = true;
1101 		m = NULL;
1102 
1103 		ret = add_metric(&list, pe, metric_no_group, &m, NULL, &ids);
1104 		if (ret)
1105 			goto out;
1106 
1107 		/*
1108 		 * Process any possible referenced metrics
1109 		 * included in the expression.
1110 		 */
1111 		ret = resolve_metric(metric_no_group,
1112 				     &list, map, &ids);
1113 		if (ret)
1114 			goto out;
1115 	}
1116 
1117 	{
1118 		struct metricgroup_iter_data data = {
1119 			.fn = metricgroup__add_metric_sys_event_iter,
1120 			.data = (void *) &(struct metricgroup_add_iter_data) {
1121 				.metric_list = &list,
1122 				.metric = metric,
1123 				.metric_no_group = metric_no_group,
1124 				.ids = &ids,
1125 				.has_match = &has_match,
1126 				.ret = &ret,
1127 			},
1128 		};
1129 
1130 		pmu_for_each_sys_event(metricgroup__sys_event_iter, &data);
1131 	}
1132 	/* End of pmu events. */
1133 	if (!has_match) {
1134 		ret = -EINVAL;
1135 		goto out;
1136 	}
1137 
1138 	list_for_each_entry(m, &list, nd) {
1139 		if (events->len > 0)
1140 			strbuf_addf(events, ",");
1141 
1142 		if (m->has_constraint) {
1143 			metricgroup__add_metric_non_group(events,
1144 							  &m->pctx);
1145 		} else {
1146 			metricgroup__add_metric_weak_group(events,
1147 							   &m->pctx);
1148 		}
1149 	}
1150 
1151 out:
1152 	/*
1153 	 * add to metric_list so that they can be released
1154 	 * even if it's failed
1155 	 */
1156 	list_splice(&list, metric_list);
1157 	expr_ids__exit(&ids);
1158 	return ret;
1159 }
1160 
1161 static int metricgroup__add_metric_list(const char *list, bool metric_no_group,
1162 					struct strbuf *events,
1163 					struct list_head *metric_list,
1164 					struct pmu_events_map *map)
1165 {
1166 	char *llist, *nlist, *p;
1167 	int ret = -EINVAL;
1168 
1169 	nlist = strdup(list);
1170 	if (!nlist)
1171 		return -ENOMEM;
1172 	llist = nlist;
1173 
1174 	strbuf_init(events, 100);
1175 	strbuf_addf(events, "%s", "");
1176 
1177 	while ((p = strsep(&llist, ",")) != NULL) {
1178 		ret = metricgroup__add_metric(p, metric_no_group, events,
1179 					      metric_list, map);
1180 		if (ret == -EINVAL) {
1181 			fprintf(stderr, "Cannot find metric or group `%s'\n",
1182 					p);
1183 			break;
1184 		}
1185 	}
1186 	free(nlist);
1187 
1188 	if (!ret)
1189 		metricgroup___watchdog_constraint_hint(NULL, true);
1190 
1191 	return ret;
1192 }
1193 
1194 static void metric__free_refs(struct metric *metric)
1195 {
1196 	struct metric_ref_node *ref, *tmp;
1197 
1198 	list_for_each_entry_safe(ref, tmp, &metric->metric_refs, list) {
1199 		list_del(&ref->list);
1200 		free(ref);
1201 	}
1202 }
1203 
1204 static void metricgroup__free_metrics(struct list_head *metric_list)
1205 {
1206 	struct metric *m, *tmp;
1207 
1208 	list_for_each_entry_safe (m, tmp, metric_list, nd) {
1209 		metric__free_refs(m);
1210 		expr__ctx_clear(&m->pctx);
1211 		list_del_init(&m->nd);
1212 		free(m);
1213 	}
1214 }
1215 
1216 static int parse_groups(struct evlist *perf_evlist, const char *str,
1217 			bool metric_no_group,
1218 			bool metric_no_merge,
1219 			struct perf_pmu *fake_pmu,
1220 			struct rblist *metric_events,
1221 			struct pmu_events_map *map)
1222 {
1223 	struct parse_events_error parse_error;
1224 	struct strbuf extra_events;
1225 	LIST_HEAD(metric_list);
1226 	int ret;
1227 
1228 	if (metric_events->nr_entries == 0)
1229 		metricgroup__rblist_init(metric_events);
1230 	ret = metricgroup__add_metric_list(str, metric_no_group,
1231 					   &extra_events, &metric_list, map);
1232 	if (ret)
1233 		goto out;
1234 	pr_debug("adding %s\n", extra_events.buf);
1235 	bzero(&parse_error, sizeof(parse_error));
1236 	ret = __parse_events(perf_evlist, extra_events.buf, &parse_error, fake_pmu);
1237 	if (ret) {
1238 		parse_events_print_error(&parse_error, extra_events.buf);
1239 		goto out;
1240 	}
1241 	ret = metricgroup__setup_events(&metric_list, metric_no_merge,
1242 					perf_evlist, metric_events);
1243 out:
1244 	metricgroup__free_metrics(&metric_list);
1245 	strbuf_release(&extra_events);
1246 	return ret;
1247 }
1248 
1249 int metricgroup__parse_groups(const struct option *opt,
1250 			      const char *str,
1251 			      bool metric_no_group,
1252 			      bool metric_no_merge,
1253 			      struct rblist *metric_events)
1254 {
1255 	struct evlist *perf_evlist = *(struct evlist **)opt->value;
1256 	struct pmu_events_map *map = perf_pmu__find_map(NULL);
1257 
1258 
1259 	return parse_groups(perf_evlist, str, metric_no_group,
1260 			    metric_no_merge, NULL, metric_events, map);
1261 }
1262 
1263 int metricgroup__parse_groups_test(struct evlist *evlist,
1264 				   struct pmu_events_map *map,
1265 				   const char *str,
1266 				   bool metric_no_group,
1267 				   bool metric_no_merge,
1268 				   struct rblist *metric_events)
1269 {
1270 	return parse_groups(evlist, str, metric_no_group,
1271 			    metric_no_merge, &perf_pmu__fake, metric_events, map);
1272 }
1273 
1274 bool metricgroup__has_metric(const char *metric)
1275 {
1276 	struct pmu_events_map *map = perf_pmu__find_map(NULL);
1277 	struct pmu_event *pe;
1278 	int i;
1279 
1280 	if (!map)
1281 		return false;
1282 
1283 	for (i = 0; ; i++) {
1284 		pe = &map->table[i];
1285 
1286 		if (!pe->name && !pe->metric_group && !pe->metric_name)
1287 			break;
1288 		if (!pe->metric_expr)
1289 			continue;
1290 		if (match_metric(pe->metric_name, metric))
1291 			return true;
1292 	}
1293 	return false;
1294 }
1295 
1296 int metricgroup__copy_metric_events(struct evlist *evlist, struct cgroup *cgrp,
1297 				    struct rblist *new_metric_events,
1298 				    struct rblist *old_metric_events)
1299 {
1300 	unsigned i;
1301 
1302 	for (i = 0; i < rblist__nr_entries(old_metric_events); i++) {
1303 		struct rb_node *nd;
1304 		struct metric_event *old_me, *new_me;
1305 		struct metric_expr *old_expr, *new_expr;
1306 		struct evsel *evsel;
1307 		size_t alloc_size;
1308 		int idx, nr;
1309 
1310 		nd = rblist__entry(old_metric_events, i);
1311 		old_me = container_of(nd, struct metric_event, nd);
1312 
1313 		evsel = evlist__find_evsel(evlist, old_me->evsel->idx);
1314 		if (!evsel)
1315 			return -EINVAL;
1316 		new_me = metricgroup__lookup(new_metric_events, evsel, true);
1317 		if (!new_me)
1318 			return -ENOMEM;
1319 
1320 		pr_debug("copying metric event for cgroup '%s': %s (idx=%d)\n",
1321 			 cgrp ? cgrp->name : "root", evsel->name, evsel->idx);
1322 
1323 		list_for_each_entry(old_expr, &old_me->head, nd) {
1324 			new_expr = malloc(sizeof(*new_expr));
1325 			if (!new_expr)
1326 				return -ENOMEM;
1327 
1328 			new_expr->metric_expr = old_expr->metric_expr;
1329 			new_expr->metric_name = old_expr->metric_name;
1330 			new_expr->metric_unit = old_expr->metric_unit;
1331 			new_expr->runtime = old_expr->runtime;
1332 
1333 			if (old_expr->metric_refs) {
1334 				/* calculate number of metric_events */
1335 				for (nr = 0; old_expr->metric_refs[nr].metric_name; nr++)
1336 					continue;
1337 				alloc_size = sizeof(*new_expr->metric_refs);
1338 				new_expr->metric_refs = calloc(nr + 1, alloc_size);
1339 				if (!new_expr->metric_refs) {
1340 					free(new_expr);
1341 					return -ENOMEM;
1342 				}
1343 
1344 				memcpy(new_expr->metric_refs, old_expr->metric_refs,
1345 				       nr * alloc_size);
1346 			} else {
1347 				new_expr->metric_refs = NULL;
1348 			}
1349 
1350 			/* calculate number of metric_events */
1351 			for (nr = 0; old_expr->metric_events[nr]; nr++)
1352 				continue;
1353 			alloc_size = sizeof(*new_expr->metric_events);
1354 			new_expr->metric_events = calloc(nr + 1, alloc_size);
1355 			if (!new_expr->metric_events) {
1356 				free(new_expr->metric_refs);
1357 				free(new_expr);
1358 				return -ENOMEM;
1359 			}
1360 
1361 			/* copy evsel in the same position */
1362 			for (idx = 0; idx < nr; idx++) {
1363 				evsel = old_expr->metric_events[idx];
1364 				evsel = evlist__find_evsel(evlist, evsel->idx);
1365 				if (evsel == NULL) {
1366 					free(new_expr->metric_events);
1367 					free(new_expr->metric_refs);
1368 					free(new_expr);
1369 					return -EINVAL;
1370 				}
1371 				new_expr->metric_events[idx] = evsel;
1372 			}
1373 
1374 			list_add(&new_expr->nd, &new_me->head);
1375 		}
1376 	}
1377 	return 0;
1378 }
1379