xref: /linux/tools/perf/util/metricgroup.c (revision f6fb0960f920e3040088992f32bbceded7a74322)
12025cf9eSThomas Gleixner // SPDX-License-Identifier: GPL-2.0-only
2b18f3e36SAndi Kleen /*
3b18f3e36SAndi Kleen  * Copyright (c) 2017, Intel Corporation.
4b18f3e36SAndi Kleen  */
5b18f3e36SAndi Kleen 
6b18f3e36SAndi Kleen /* Manage metrics and groups of metrics from JSON files */
7b18f3e36SAndi Kleen 
8b18f3e36SAndi Kleen #include "metricgroup.h"
9b4209025SArnaldo Carvalho de Melo #include "debug.h"
10b18f3e36SAndi Kleen #include "evlist.h"
110b8026e8SArnaldo Carvalho de Melo #include "evsel.h"
12b18f3e36SAndi Kleen #include "strbuf.h"
13b18f3e36SAndi Kleen #include "pmu.h"
14b18f3e36SAndi Kleen #include "expr.h"
15b18f3e36SAndi Kleen #include "rblist.h"
16b18f3e36SAndi Kleen #include <string.h>
17b18f3e36SAndi Kleen #include <errno.h>
18b18f3e36SAndi Kleen #include "pmu-events/pmu-events.h"
19b18f3e36SAndi Kleen #include "strlist.h"
20b18f3e36SAndi Kleen #include <assert.h>
21bd9860bfSArnaldo Carvalho de Melo #include <linux/ctype.h>
22b4209025SArnaldo Carvalho de Melo #include <linux/string.h>
23d8f9da24SArnaldo Carvalho de Melo #include <linux/zalloc.h>
240b8026e8SArnaldo Carvalho de Melo #include <subcmd/parse-options.h>
25ab483d8bSKan Liang #include <api/fs/fs.h>
26ab483d8bSKan Liang #include "util.h"
27*f6fb0960SJiri Olsa #include <asm/bug.h>
28b18f3e36SAndi Kleen 
29b18f3e36SAndi Kleen struct metric_event *metricgroup__lookup(struct rblist *metric_events,
3032dcd021SJiri Olsa 					 struct evsel *evsel,
31b18f3e36SAndi Kleen 					 bool create)
32b18f3e36SAndi Kleen {
33b18f3e36SAndi Kleen 	struct rb_node *nd;
34b18f3e36SAndi Kleen 	struct metric_event me = {
35b18f3e36SAndi Kleen 		.evsel = evsel
36b18f3e36SAndi Kleen 	};
374bd1bef8SAndi Kleen 
384bd1bef8SAndi Kleen 	if (!metric_events)
394bd1bef8SAndi Kleen 		return NULL;
404bd1bef8SAndi Kleen 
41b18f3e36SAndi Kleen 	nd = rblist__find(metric_events, &me);
42b18f3e36SAndi Kleen 	if (nd)
43b18f3e36SAndi Kleen 		return container_of(nd, struct metric_event, nd);
44b18f3e36SAndi Kleen 	if (create) {
45b18f3e36SAndi Kleen 		rblist__add_node(metric_events, &me);
46b18f3e36SAndi Kleen 		nd = rblist__find(metric_events, &me);
47b18f3e36SAndi Kleen 		if (nd)
48b18f3e36SAndi Kleen 			return container_of(nd, struct metric_event, nd);
49b18f3e36SAndi Kleen 	}
50b18f3e36SAndi Kleen 	return NULL;
51b18f3e36SAndi Kleen }
52b18f3e36SAndi Kleen 
53b18f3e36SAndi Kleen static int metric_event_cmp(struct rb_node *rb_node, const void *entry)
54b18f3e36SAndi Kleen {
55b18f3e36SAndi Kleen 	struct metric_event *a = container_of(rb_node,
56b18f3e36SAndi Kleen 					      struct metric_event,
57b18f3e36SAndi Kleen 					      nd);
58b18f3e36SAndi Kleen 	const struct metric_event *b = entry;
59b18f3e36SAndi Kleen 
60b18f3e36SAndi Kleen 	if (a->evsel == b->evsel)
61b18f3e36SAndi Kleen 		return 0;
62b18f3e36SAndi Kleen 	if ((char *)a->evsel < (char *)b->evsel)
63b18f3e36SAndi Kleen 		return -1;
64b18f3e36SAndi Kleen 	return +1;
65b18f3e36SAndi Kleen }
66b18f3e36SAndi Kleen 
67b18f3e36SAndi Kleen static struct rb_node *metric_event_new(struct rblist *rblist __maybe_unused,
68b18f3e36SAndi Kleen 					const void *entry)
69b18f3e36SAndi Kleen {
70b18f3e36SAndi Kleen 	struct metric_event *me = malloc(sizeof(struct metric_event));
71b18f3e36SAndi Kleen 
72b18f3e36SAndi Kleen 	if (!me)
73b18f3e36SAndi Kleen 		return NULL;
74b18f3e36SAndi Kleen 	memcpy(me, entry, sizeof(struct metric_event));
75b18f3e36SAndi Kleen 	me->evsel = ((struct metric_event *)entry)->evsel;
76b18f3e36SAndi Kleen 	INIT_LIST_HEAD(&me->head);
77b18f3e36SAndi Kleen 	return &me->nd;
78b18f3e36SAndi Kleen }
79b18f3e36SAndi Kleen 
809afe5658SJiri Olsa static void metric_event_delete(struct rblist *rblist __maybe_unused,
819afe5658SJiri Olsa 				struct rb_node *rb_node)
829afe5658SJiri Olsa {
839afe5658SJiri Olsa 	struct metric_event *me = container_of(rb_node, struct metric_event, nd);
849afe5658SJiri Olsa 	struct metric_expr *expr, *tmp;
859afe5658SJiri Olsa 
869afe5658SJiri Olsa 	list_for_each_entry_safe(expr, tmp, &me->head, nd) {
874ea28967SJiri Olsa 		free(expr->metric_refs);
889afe5658SJiri Olsa 		free(expr);
899afe5658SJiri Olsa 	}
909afe5658SJiri Olsa 
919afe5658SJiri Olsa 	free(me);
929afe5658SJiri Olsa }
939afe5658SJiri Olsa 
94b18f3e36SAndi Kleen static void metricgroup__rblist_init(struct rblist *metric_events)
95b18f3e36SAndi Kleen {
96b18f3e36SAndi Kleen 	rblist__init(metric_events);
97b18f3e36SAndi Kleen 	metric_events->node_cmp = metric_event_cmp;
98b18f3e36SAndi Kleen 	metric_events->node_new = metric_event_new;
999afe5658SJiri Olsa 	metric_events->node_delete = metric_event_delete;
1009afe5658SJiri Olsa }
1019afe5658SJiri Olsa 
1029afe5658SJiri Olsa void metricgroup__rblist_exit(struct rblist *metric_events)
1039afe5658SJiri Olsa {
1049afe5658SJiri Olsa 	rblist__exit(metric_events);
105b18f3e36SAndi Kleen }
106b18f3e36SAndi Kleen 
10783de0b7dSJiri Olsa /*
10883de0b7dSJiri Olsa  * A node in the list of referenced metrics. metric_expr
10983de0b7dSJiri Olsa  * is held as a convenience to avoid a search through the
11083de0b7dSJiri Olsa  * metric list.
11183de0b7dSJiri Olsa  */
11283de0b7dSJiri Olsa struct metric_ref_node {
11383de0b7dSJiri Olsa 	const char *metric_name;
11483de0b7dSJiri Olsa 	const char *metric_expr;
11583de0b7dSJiri Olsa 	struct list_head list;
11683de0b7dSJiri Olsa };
11783de0b7dSJiri Olsa 
118b18f3e36SAndi Kleen struct egroup {
119b18f3e36SAndi Kleen 	struct list_head nd;
120ded80bdaSIan Rogers 	struct expr_parse_ctx pctx;
121b18f3e36SAndi Kleen 	const char *metric_name;
122b18f3e36SAndi Kleen 	const char *metric_expr;
123287f2649SJin Yao 	const char *metric_unit;
12483de0b7dSJiri Olsa 	struct list_head metric_refs;
12583de0b7dSJiri Olsa 	int metric_refs_cnt;
1261e1a873dSKajol Jain 	int runtime;
1277f9eca51SIan Rogers 	bool has_constraint;
128b18f3e36SAndi Kleen };
129b18f3e36SAndi Kleen 
130*f6fb0960SJiri Olsa #define RECURSION_ID_MAX 1000
131*f6fb0960SJiri Olsa 
132*f6fb0960SJiri Olsa struct expr_ids {
133*f6fb0960SJiri Olsa 	struct expr_id	id[RECURSION_ID_MAX];
134*f6fb0960SJiri Olsa 	int		cnt;
135*f6fb0960SJiri Olsa };
136*f6fb0960SJiri Olsa 
137*f6fb0960SJiri Olsa static struct expr_id *expr_ids__alloc(struct expr_ids *ids)
138*f6fb0960SJiri Olsa {
139*f6fb0960SJiri Olsa 	if (ids->cnt >= RECURSION_ID_MAX)
140*f6fb0960SJiri Olsa 		return NULL;
141*f6fb0960SJiri Olsa 	return &ids->id[ids->cnt++];
142*f6fb0960SJiri Olsa }
143*f6fb0960SJiri Olsa 
144*f6fb0960SJiri Olsa static void expr_ids__exit(struct expr_ids *ids)
145*f6fb0960SJiri Olsa {
146*f6fb0960SJiri Olsa 	int i;
147*f6fb0960SJiri Olsa 
148*f6fb0960SJiri Olsa 	for (i = 0; i < ids->cnt; i++)
149*f6fb0960SJiri Olsa 		free(ids->id[i].id);
150*f6fb0960SJiri Olsa }
151*f6fb0960SJiri Olsa 
1522440689dSIan Rogers /**
1532440689dSIan Rogers  * Find a group of events in perf_evlist that correpond to those from a parsed
15405530a79SIan Rogers  * metric expression. Note, as find_evsel_group is called in the same order as
15505530a79SIan Rogers  * perf_evlist was constructed, metric_no_merge doesn't need to test for
15605530a79SIan Rogers  * underfilling a group.
1572440689dSIan Rogers  * @perf_evlist: a list of events something like: {metric1 leader, metric1
1582440689dSIan Rogers  * sibling, metric1 sibling}:W,duration_time,{metric2 leader, metric2 sibling,
1592440689dSIan Rogers  * metric2 sibling}:W,duration_time
1602440689dSIan Rogers  * @pctx: the parse context for the metric expression.
16105530a79SIan Rogers  * @metric_no_merge: don't attempt to share events for the metric with other
16205530a79SIan Rogers  * metrics.
1632440689dSIan Rogers  * @has_constraint: is there a contraint on the group of events? In which case
1642440689dSIan Rogers  * the events won't be grouped.
1652440689dSIan Rogers  * @metric_events: out argument, null terminated array of evsel's associated
1662440689dSIan Rogers  * with the metric.
1672440689dSIan Rogers  * @evlist_used: in/out argument, bitmap tracking which evlist events are used.
1682440689dSIan Rogers  * @return the first metric event or NULL on failure.
1692440689dSIan Rogers  */
17063503dbaSJiri Olsa static struct evsel *find_evsel_group(struct evlist *perf_evlist,
171ded80bdaSIan Rogers 				      struct expr_parse_ctx *pctx,
17205530a79SIan Rogers 				      bool metric_no_merge,
1732440689dSIan Rogers 				      bool has_constraint,
17458fc90fdSKajol Jain 				      struct evsel **metric_events,
17545db55f2SIan Rogers 				      unsigned long *evlist_used)
176b18f3e36SAndi Kleen {
1772440689dSIan Rogers 	struct evsel *ev, *current_leader = NULL;
178070b3b5aSJiri Olsa 	struct expr_id_data *val_ptr;
1792440689dSIan Rogers 	int i = 0, matched_events = 0, events_to_match;
1802440689dSIan Rogers 	const int idnum = (int)hashmap__size(&pctx->ids);
1812440689dSIan Rogers 
1822440689dSIan Rogers 	/* duration_time is grouped separately. */
1832440689dSIan Rogers 	if (!has_constraint &&
1842440689dSIan Rogers 	    hashmap__find(&pctx->ids, "duration_time", (void **)&val_ptr))
1852440689dSIan Rogers 		events_to_match = idnum - 1;
1862440689dSIan Rogers 	else
1872440689dSIan Rogers 		events_to_match = idnum;
188b18f3e36SAndi Kleen 
189b18f3e36SAndi Kleen 	evlist__for_each_entry (perf_evlist, ev) {
1902440689dSIan Rogers 		/*
1912440689dSIan Rogers 		 * Events with a constraint aren't grouped and match the first
1922440689dSIan Rogers 		 * events available.
1932440689dSIan Rogers 		 */
1942440689dSIan Rogers 		if (has_constraint && ev->weak_group)
19558fc90fdSKajol Jain 			continue;
19605530a79SIan Rogers 		/* Ignore event if already used and merging is disabled. */
19705530a79SIan Rogers 		if (metric_no_merge && test_bit(ev->idx, evlist_used))
19805530a79SIan Rogers 			continue;
1992440689dSIan Rogers 		if (!has_constraint && ev->leader != current_leader) {
2002440689dSIan Rogers 			/*
2012440689dSIan Rogers 			 * Start of a new group, discard the whole match and
2022440689dSIan Rogers 			 * start again.
2032440689dSIan Rogers 			 */
2042440689dSIan Rogers 			matched_events = 0;
205f01642e4SJin Yao 			memset(metric_events, 0,
206f01642e4SJin Yao 				sizeof(struct evsel *) * idnum);
2072440689dSIan Rogers 			current_leader = ev->leader;
2082440689dSIan Rogers 		}
20905530a79SIan Rogers 		if (hashmap__find(&pctx->ids, ev->name, (void **)&val_ptr)) {
21005530a79SIan Rogers 			if (has_constraint) {
21105530a79SIan Rogers 				/*
21205530a79SIan Rogers 				 * Events aren't grouped, ensure the same event
21305530a79SIan Rogers 				 * isn't matched from two groups.
21405530a79SIan Rogers 				 */
21505530a79SIan Rogers 				for (i = 0; i < matched_events; i++) {
21605530a79SIan Rogers 					if (!strcmp(ev->name,
21705530a79SIan Rogers 						    metric_events[i]->name)) {
21805530a79SIan Rogers 						break;
21905530a79SIan Rogers 					}
22005530a79SIan Rogers 				}
22105530a79SIan Rogers 				if (i != matched_events)
22205530a79SIan Rogers 					continue;
22305530a79SIan Rogers 			}
2242440689dSIan Rogers 			metric_events[matched_events++] = ev;
22505530a79SIan Rogers 		}
2262440689dSIan Rogers 		if (matched_events == events_to_match)
2272440689dSIan Rogers 			break;
2282440689dSIan Rogers 	}
2292440689dSIan Rogers 
2302440689dSIan Rogers 	if (events_to_match != idnum) {
2312440689dSIan Rogers 		/* Add the first duration_time. */
2322440689dSIan Rogers 		evlist__for_each_entry(perf_evlist, ev) {
2332440689dSIan Rogers 			if (!strcmp(ev->name, "duration_time")) {
2342440689dSIan Rogers 				metric_events[matched_events++] = ev;
2352440689dSIan Rogers 				break;
2362440689dSIan Rogers 			}
237b18f3e36SAndi Kleen 		}
238b18f3e36SAndi Kleen 	}
239f01642e4SJin Yao 
2402440689dSIan Rogers 	if (matched_events != idnum) {
241f01642e4SJin Yao 		/* Not whole match */
242b18f3e36SAndi Kleen 		return NULL;
243b18f3e36SAndi Kleen 	}
244b18f3e36SAndi Kleen 
245f01642e4SJin Yao 	metric_events[idnum] = NULL;
246f01642e4SJin Yao 
247f01642e4SJin Yao 	for (i = 0; i < idnum; i++) {
24858fc90fdSKajol Jain 		ev = metric_events[i];
2492440689dSIan Rogers 		ev->metric_leader = ev;
25045db55f2SIan Rogers 		set_bit(ev->idx, evlist_used);
251f01642e4SJin Yao 	}
252f01642e4SJin Yao 
253f01642e4SJin Yao 	return metric_events[0];
254f01642e4SJin Yao }
255f01642e4SJin Yao 
256b18f3e36SAndi Kleen static int metricgroup__setup_events(struct list_head *groups,
25705530a79SIan Rogers 				     bool metric_no_merge,
25863503dbaSJiri Olsa 				     struct evlist *perf_evlist,
259b18f3e36SAndi Kleen 				     struct rblist *metric_events_list)
260b18f3e36SAndi Kleen {
261b18f3e36SAndi Kleen 	struct metric_event *me;
262b18f3e36SAndi Kleen 	struct metric_expr *expr;
263b18f3e36SAndi Kleen 	int i = 0;
264b18f3e36SAndi Kleen 	int ret = 0;
265b18f3e36SAndi Kleen 	struct egroup *eg;
2662440689dSIan Rogers 	struct evsel *evsel, *tmp;
26745db55f2SIan Rogers 	unsigned long *evlist_used;
26858fc90fdSKajol Jain 
26945db55f2SIan Rogers 	evlist_used = bitmap_alloc(perf_evlist->core.nr_entries);
27045db55f2SIan Rogers 	if (!evlist_used)
27145db55f2SIan Rogers 		return -ENOMEM;
272b18f3e36SAndi Kleen 
273b18f3e36SAndi Kleen 	list_for_each_entry (eg, groups, nd) {
27432dcd021SJiri Olsa 		struct evsel **metric_events;
2754ea28967SJiri Olsa 		struct metric_ref *metric_refs = NULL;
276b18f3e36SAndi Kleen 
277ded80bdaSIan Rogers 		metric_events = calloc(sizeof(void *),
278ded80bdaSIan Rogers 				hashmap__size(&eg->pctx.ids) + 1);
279b18f3e36SAndi Kleen 		if (!metric_events) {
280b18f3e36SAndi Kleen 			ret = -ENOMEM;
281b18f3e36SAndi Kleen 			break;
282b18f3e36SAndi Kleen 		}
2832440689dSIan Rogers 		evsel = find_evsel_group(perf_evlist, &eg->pctx,
28405530a79SIan Rogers 					 metric_no_merge,
2852440689dSIan Rogers 					 eg->has_constraint, metric_events,
286ded80bdaSIan Rogers 					 evlist_used);
287b18f3e36SAndi Kleen 		if (!evsel) {
288b18f3e36SAndi Kleen 			pr_debug("Cannot resolve %s: %s\n",
289b18f3e36SAndi Kleen 					eg->metric_name, eg->metric_expr);
290a159e2feSIan Rogers 			free(metric_events);
291b18f3e36SAndi Kleen 			continue;
292b18f3e36SAndi Kleen 		}
293ded80bdaSIan Rogers 		for (i = 0; metric_events[i]; i++)
294b18f3e36SAndi Kleen 			metric_events[i]->collect_stat = true;
295b18f3e36SAndi Kleen 		me = metricgroup__lookup(metric_events_list, evsel, true);
296b18f3e36SAndi Kleen 		if (!me) {
297b18f3e36SAndi Kleen 			ret = -ENOMEM;
298a159e2feSIan Rogers 			free(metric_events);
299b18f3e36SAndi Kleen 			break;
300b18f3e36SAndi Kleen 		}
301b18f3e36SAndi Kleen 		expr = malloc(sizeof(struct metric_expr));
302b18f3e36SAndi Kleen 		if (!expr) {
303b18f3e36SAndi Kleen 			ret = -ENOMEM;
304a159e2feSIan Rogers 			free(metric_events);
305b18f3e36SAndi Kleen 			break;
306b18f3e36SAndi Kleen 		}
3074ea28967SJiri Olsa 
3084ea28967SJiri Olsa 		/*
3094ea28967SJiri Olsa 		 * Collect and store collected nested expressions
3104ea28967SJiri Olsa 		 * for metric processing.
3114ea28967SJiri Olsa 		 */
3124ea28967SJiri Olsa 		if (eg->metric_refs_cnt) {
3134ea28967SJiri Olsa 			struct metric_ref_node *ref;
3144ea28967SJiri Olsa 
3154ea28967SJiri Olsa 			metric_refs = zalloc(sizeof(struct metric_ref) * (eg->metric_refs_cnt + 1));
3164ea28967SJiri Olsa 			if (!metric_refs) {
3174ea28967SJiri Olsa 				ret = -ENOMEM;
3184ea28967SJiri Olsa 				free(metric_events);
3194ea28967SJiri Olsa 				break;
3204ea28967SJiri Olsa 			}
3214ea28967SJiri Olsa 
3224ea28967SJiri Olsa 			i = 0;
3234ea28967SJiri Olsa 			list_for_each_entry(ref, &eg->metric_refs, list) {
3244ea28967SJiri Olsa 				/*
3254ea28967SJiri Olsa 				 * Intentionally passing just const char pointers,
3264ea28967SJiri Olsa 				 * originally from 'struct pmu_event' object.
3274ea28967SJiri Olsa 				 * We don't need to change them, so there's no
3284ea28967SJiri Olsa 				 * need to create our own copy.
3294ea28967SJiri Olsa 				 */
3304ea28967SJiri Olsa 				metric_refs[i].metric_name = ref->metric_name;
3314ea28967SJiri Olsa 				metric_refs[i].metric_expr = ref->metric_expr;
3324ea28967SJiri Olsa 				i++;
3334ea28967SJiri Olsa 			}
3344ea28967SJiri Olsa 		};
3354ea28967SJiri Olsa 
3364ea28967SJiri Olsa 		expr->metric_refs = metric_refs;
337b18f3e36SAndi Kleen 		expr->metric_expr = eg->metric_expr;
338b18f3e36SAndi Kleen 		expr->metric_name = eg->metric_name;
339287f2649SJin Yao 		expr->metric_unit = eg->metric_unit;
340b18f3e36SAndi Kleen 		expr->metric_events = metric_events;
3411e1a873dSKajol Jain 		expr->runtime = eg->runtime;
342b18f3e36SAndi Kleen 		list_add(&expr->nd, &me->head);
343b18f3e36SAndi Kleen 	}
34458fc90fdSKajol Jain 
3452440689dSIan Rogers 	evlist__for_each_entry_safe(perf_evlist, tmp, evsel) {
3462440689dSIan Rogers 		if (!test_bit(evsel->idx, evlist_used)) {
3472440689dSIan Rogers 			evlist__remove(perf_evlist, evsel);
3482440689dSIan Rogers 			evsel__delete(evsel);
3492440689dSIan Rogers 		}
3502440689dSIan Rogers 	}
35145db55f2SIan Rogers 	bitmap_free(evlist_used);
35258fc90fdSKajol Jain 
353b18f3e36SAndi Kleen 	return ret;
354b18f3e36SAndi Kleen }
355b18f3e36SAndi Kleen 
356b18f3e36SAndi Kleen static bool match_metric(const char *n, const char *list)
357b18f3e36SAndi Kleen {
358b18f3e36SAndi Kleen 	int len;
359b18f3e36SAndi Kleen 	char *m;
360b18f3e36SAndi Kleen 
361b18f3e36SAndi Kleen 	if (!list)
362b18f3e36SAndi Kleen 		return false;
363b18f3e36SAndi Kleen 	if (!strcmp(list, "all"))
364b18f3e36SAndi Kleen 		return true;
365b18f3e36SAndi Kleen 	if (!n)
366b18f3e36SAndi Kleen 		return !strcasecmp(list, "No_group");
367b18f3e36SAndi Kleen 	len = strlen(list);
368b18f3e36SAndi Kleen 	m = strcasestr(n, list);
369b18f3e36SAndi Kleen 	if (!m)
370b18f3e36SAndi Kleen 		return false;
371b18f3e36SAndi Kleen 	if ((m == n || m[-1] == ';' || m[-1] == ' ') &&
372b18f3e36SAndi Kleen 	    (m[len] == 0 || m[len] == ';'))
373b18f3e36SAndi Kleen 		return true;
374b18f3e36SAndi Kleen 	return false;
375b18f3e36SAndi Kleen }
376b18f3e36SAndi Kleen 
37771b0acceSAndi Kleen struct mep {
37871b0acceSAndi Kleen 	struct rb_node nd;
37971b0acceSAndi Kleen 	const char *name;
38071b0acceSAndi Kleen 	struct strlist *metrics;
38171b0acceSAndi Kleen };
38271b0acceSAndi Kleen 
38371b0acceSAndi Kleen static int mep_cmp(struct rb_node *rb_node, const void *entry)
38471b0acceSAndi Kleen {
38571b0acceSAndi Kleen 	struct mep *a = container_of(rb_node, struct mep, nd);
38671b0acceSAndi Kleen 	struct mep *b = (struct mep *)entry;
38771b0acceSAndi Kleen 
38871b0acceSAndi Kleen 	return strcmp(a->name, b->name);
38971b0acceSAndi Kleen }
39071b0acceSAndi Kleen 
39171b0acceSAndi Kleen static struct rb_node *mep_new(struct rblist *rl __maybe_unused,
39271b0acceSAndi Kleen 					const void *entry)
39371b0acceSAndi Kleen {
39471b0acceSAndi Kleen 	struct mep *me = malloc(sizeof(struct mep));
39571b0acceSAndi Kleen 
39671b0acceSAndi Kleen 	if (!me)
39771b0acceSAndi Kleen 		return NULL;
39871b0acceSAndi Kleen 	memcpy(me, entry, sizeof(struct mep));
39971b0acceSAndi Kleen 	me->name = strdup(me->name);
40071b0acceSAndi Kleen 	if (!me->name)
40171b0acceSAndi Kleen 		goto out_me;
40271b0acceSAndi Kleen 	me->metrics = strlist__new(NULL, NULL);
40371b0acceSAndi Kleen 	if (!me->metrics)
40471b0acceSAndi Kleen 		goto out_name;
40571b0acceSAndi Kleen 	return &me->nd;
40671b0acceSAndi Kleen out_name:
407d8f9da24SArnaldo Carvalho de Melo 	zfree(&me->name);
40871b0acceSAndi Kleen out_me:
40971b0acceSAndi Kleen 	free(me);
41071b0acceSAndi Kleen 	return NULL;
41171b0acceSAndi Kleen }
41271b0acceSAndi Kleen 
41371b0acceSAndi Kleen static struct mep *mep_lookup(struct rblist *groups, const char *name)
41471b0acceSAndi Kleen {
41571b0acceSAndi Kleen 	struct rb_node *nd;
41671b0acceSAndi Kleen 	struct mep me = {
41771b0acceSAndi Kleen 		.name = name
41871b0acceSAndi Kleen 	};
41971b0acceSAndi Kleen 	nd = rblist__find(groups, &me);
42071b0acceSAndi Kleen 	if (nd)
42171b0acceSAndi Kleen 		return container_of(nd, struct mep, nd);
42271b0acceSAndi Kleen 	rblist__add_node(groups, &me);
42371b0acceSAndi Kleen 	nd = rblist__find(groups, &me);
42471b0acceSAndi Kleen 	if (nd)
42571b0acceSAndi Kleen 		return container_of(nd, struct mep, nd);
42671b0acceSAndi Kleen 	return NULL;
42771b0acceSAndi Kleen }
42871b0acceSAndi Kleen 
42971b0acceSAndi Kleen static void mep_delete(struct rblist *rl __maybe_unused,
43071b0acceSAndi Kleen 		       struct rb_node *nd)
43171b0acceSAndi Kleen {
43271b0acceSAndi Kleen 	struct mep *me = container_of(nd, struct mep, nd);
43371b0acceSAndi Kleen 
43471b0acceSAndi Kleen 	strlist__delete(me->metrics);
435d8f9da24SArnaldo Carvalho de Melo 	zfree(&me->name);
43671b0acceSAndi Kleen 	free(me);
43771b0acceSAndi Kleen }
43871b0acceSAndi Kleen 
43971b0acceSAndi Kleen static void metricgroup__print_strlist(struct strlist *metrics, bool raw)
44071b0acceSAndi Kleen {
44171b0acceSAndi Kleen 	struct str_node *sn;
44271b0acceSAndi Kleen 	int n = 0;
44371b0acceSAndi Kleen 
44471b0acceSAndi Kleen 	strlist__for_each_entry (sn, metrics) {
44571b0acceSAndi Kleen 		if (raw)
44671b0acceSAndi Kleen 			printf("%s%s", n > 0 ? " " : "", sn->s);
44771b0acceSAndi Kleen 		else
44871b0acceSAndi Kleen 			printf("  %s\n", sn->s);
44971b0acceSAndi Kleen 		n++;
45071b0acceSAndi Kleen 	}
45171b0acceSAndi Kleen 	if (raw)
45271b0acceSAndi Kleen 		putchar('\n');
45371b0acceSAndi Kleen }
45471b0acceSAndi Kleen 
45571b0acceSAndi Kleen void metricgroup__print(bool metrics, bool metricgroups, char *filter,
45633bbc571SJiri Olsa 			bool raw, bool details)
45771b0acceSAndi Kleen {
45854e32dc0SGanapatrao Kulkarni 	struct pmu_events_map *map = perf_pmu__find_map(NULL);
45971b0acceSAndi Kleen 	struct pmu_event *pe;
46071b0acceSAndi Kleen 	int i;
46171b0acceSAndi Kleen 	struct rblist groups;
46271b0acceSAndi Kleen 	struct rb_node *node, *next;
46371b0acceSAndi Kleen 	struct strlist *metriclist = NULL;
46471b0acceSAndi Kleen 
46571b0acceSAndi Kleen 	if (!map)
46671b0acceSAndi Kleen 		return;
46771b0acceSAndi Kleen 
46871b0acceSAndi Kleen 	if (!metricgroups) {
46971b0acceSAndi Kleen 		metriclist = strlist__new(NULL, NULL);
47071b0acceSAndi Kleen 		if (!metriclist)
47171b0acceSAndi Kleen 			return;
47271b0acceSAndi Kleen 	}
47371b0acceSAndi Kleen 
47471b0acceSAndi Kleen 	rblist__init(&groups);
47571b0acceSAndi Kleen 	groups.node_new = mep_new;
47671b0acceSAndi Kleen 	groups.node_cmp = mep_cmp;
47771b0acceSAndi Kleen 	groups.node_delete = mep_delete;
47871b0acceSAndi Kleen 	for (i = 0; ; i++) {
47971b0acceSAndi Kleen 		const char *g;
48071b0acceSAndi Kleen 		pe = &map->table[i];
48171b0acceSAndi Kleen 
48271b0acceSAndi Kleen 		if (!pe->name && !pe->metric_group && !pe->metric_name)
48371b0acceSAndi Kleen 			break;
48471b0acceSAndi Kleen 		if (!pe->metric_expr)
48571b0acceSAndi Kleen 			continue;
48671b0acceSAndi Kleen 		g = pe->metric_group;
48771b0acceSAndi Kleen 		if (!g && pe->metric_name) {
48871b0acceSAndi Kleen 			if (pe->name)
48971b0acceSAndi Kleen 				continue;
49071b0acceSAndi Kleen 			g = "No_group";
49171b0acceSAndi Kleen 		}
49271b0acceSAndi Kleen 		if (g) {
49371b0acceSAndi Kleen 			char *omg;
49471b0acceSAndi Kleen 			char *mg = strdup(g);
49571b0acceSAndi Kleen 
49671b0acceSAndi Kleen 			if (!mg)
49771b0acceSAndi Kleen 				return;
49871b0acceSAndi Kleen 			omg = mg;
49971b0acceSAndi Kleen 			while ((g = strsep(&mg, ";")) != NULL) {
50071b0acceSAndi Kleen 				struct mep *me;
50171b0acceSAndi Kleen 				char *s;
50271b0acceSAndi Kleen 
50380e9073fSArnaldo Carvalho de Melo 				g = skip_spaces(g);
50471b0acceSAndi Kleen 				if (*g == 0)
50571b0acceSAndi Kleen 					g = "No_group";
50671b0acceSAndi Kleen 				if (filter && !strstr(g, filter))
50771b0acceSAndi Kleen 					continue;
50871b0acceSAndi Kleen 				if (raw)
50971b0acceSAndi Kleen 					s = (char *)pe->metric_name;
51071b0acceSAndi Kleen 				else {
51195f04328SMichael Petlan 					if (asprintf(&s, "%s\n%*s%s]",
51295f04328SMichael Petlan 						     pe->metric_name, 8, "[", pe->desc) < 0)
51371b0acceSAndi Kleen 						return;
51433bbc571SJiri Olsa 
51533bbc571SJiri Olsa 					if (details) {
51633bbc571SJiri Olsa 						if (asprintf(&s, "%s\n%*s%s]",
51733bbc571SJiri Olsa 							     s, 8, "[", pe->metric_expr) < 0)
51833bbc571SJiri Olsa 							return;
51933bbc571SJiri Olsa 					}
52071b0acceSAndi Kleen 				}
52171b0acceSAndi Kleen 
52271b0acceSAndi Kleen 				if (!s)
52371b0acceSAndi Kleen 					continue;
52471b0acceSAndi Kleen 
52571b0acceSAndi Kleen 				if (!metricgroups) {
52671b0acceSAndi Kleen 					strlist__add(metriclist, s);
52771b0acceSAndi Kleen 				} else {
52871b0acceSAndi Kleen 					me = mep_lookup(&groups, g);
52971b0acceSAndi Kleen 					if (!me)
53071b0acceSAndi Kleen 						continue;
53171b0acceSAndi Kleen 					strlist__add(me->metrics, s);
53271b0acceSAndi Kleen 				}
53371b0acceSAndi Kleen 			}
53471b0acceSAndi Kleen 			free(omg);
53571b0acceSAndi Kleen 		}
53671b0acceSAndi Kleen 	}
53771b0acceSAndi Kleen 
53871b0acceSAndi Kleen 	if (metricgroups && !raw)
53971b0acceSAndi Kleen 		printf("\nMetric Groups:\n\n");
54071b0acceSAndi Kleen 	else if (metrics && !raw)
54171b0acceSAndi Kleen 		printf("\nMetrics:\n\n");
54271b0acceSAndi Kleen 
543ca227029SDavidlohr Bueso 	for (node = rb_first_cached(&groups.entries); node; node = next) {
54471b0acceSAndi Kleen 		struct mep *me = container_of(node, struct mep, nd);
54571b0acceSAndi Kleen 
54671b0acceSAndi Kleen 		if (metricgroups)
5479c344d15SAndi Kleen 			printf("%s%s%s", me->name, metrics && !raw ? ":" : "", raw ? " " : "\n");
54871b0acceSAndi Kleen 		if (metrics)
54971b0acceSAndi Kleen 			metricgroup__print_strlist(me->metrics, raw);
55071b0acceSAndi Kleen 		next = rb_next(node);
55171b0acceSAndi Kleen 		rblist__remove_node(&groups, node);
55271b0acceSAndi Kleen 	}
55371b0acceSAndi Kleen 	if (!metricgroups)
55471b0acceSAndi Kleen 		metricgroup__print_strlist(metriclist, raw);
55571b0acceSAndi Kleen 	strlist__delete(metriclist);
55671b0acceSAndi Kleen }
55771b0acceSAndi Kleen 
558f742634aSKan Liang static void metricgroup__add_metric_weak_group(struct strbuf *events,
559ded80bdaSIan Rogers 					       struct expr_parse_ctx *ctx)
560f742634aSKan Liang {
561ded80bdaSIan Rogers 	struct hashmap_entry *cur;
5624e21c13aSIan Rogers 	size_t bkt;
5634e21c13aSIan Rogers 	bool no_group = true, has_duration = false;
564f742634aSKan Liang 
565ded80bdaSIan Rogers 	hashmap__for_each_entry((&ctx->ids), cur, bkt) {
566ded80bdaSIan Rogers 		pr_debug("found event %s\n", (const char *)cur->key);
567f742634aSKan Liang 		/*
568f742634aSKan Liang 		 * Duration time maps to a software event and can make
569f742634aSKan Liang 		 * groups not count. Always use it outside a
570f742634aSKan Liang 		 * group.
571f742634aSKan Liang 		 */
572ded80bdaSIan Rogers 		if (!strcmp(cur->key, "duration_time")) {
5734e21c13aSIan Rogers 			has_duration = true;
574f742634aSKan Liang 			continue;
575f742634aSKan Liang 		}
576f742634aSKan Liang 		strbuf_addf(events, "%s%s",
5774e21c13aSIan Rogers 			no_group ? "{" : ",",
578ded80bdaSIan Rogers 			(const char *)cur->key);
579f742634aSKan Liang 		no_group = false;
580f742634aSKan Liang 	}
5814e21c13aSIan Rogers 	if (!no_group) {
582f742634aSKan Liang 		strbuf_addf(events, "}:W");
5834e21c13aSIan Rogers 		if (has_duration)
5844e21c13aSIan Rogers 			strbuf_addf(events, ",duration_time");
5854e21c13aSIan Rogers 	} else if (has_duration)
5864e21c13aSIan Rogers 		strbuf_addf(events, "duration_time");
587f742634aSKan Liang }
588f742634aSKan Liang 
589ab483d8bSKan Liang static void metricgroup__add_metric_non_group(struct strbuf *events,
590ded80bdaSIan Rogers 					      struct expr_parse_ctx *ctx)
591ab483d8bSKan Liang {
592ded80bdaSIan Rogers 	struct hashmap_entry *cur;
593ded80bdaSIan Rogers 	size_t bkt;
594e2ce1059SIan Rogers 	bool first = true;
595ab483d8bSKan Liang 
596e2ce1059SIan Rogers 	hashmap__for_each_entry((&ctx->ids), cur, bkt) {
597e2ce1059SIan Rogers 		if (!first)
598e2ce1059SIan Rogers 			strbuf_addf(events, ",");
599e2ce1059SIan Rogers 		strbuf_addf(events, "%s", (const char *)cur->key);
600e2ce1059SIan Rogers 		first = false;
601e2ce1059SIan Rogers 	}
602ab483d8bSKan Liang }
603ab483d8bSKan Liang 
604ab483d8bSKan Liang static void metricgroup___watchdog_constraint_hint(const char *name, bool foot)
605ab483d8bSKan Liang {
606ab483d8bSKan Liang 	static bool violate_nmi_constraint;
607ab483d8bSKan Liang 
608ab483d8bSKan Liang 	if (!foot) {
609ab483d8bSKan Liang 		pr_warning("Splitting metric group %s into standalone metrics.\n", name);
610ab483d8bSKan Liang 		violate_nmi_constraint = true;
611ab483d8bSKan Liang 		return;
612ab483d8bSKan Liang 	}
613ab483d8bSKan Liang 
614ab483d8bSKan Liang 	if (!violate_nmi_constraint)
615ab483d8bSKan Liang 		return;
616ab483d8bSKan Liang 
617ab483d8bSKan Liang 	pr_warning("Try disabling the NMI watchdog to comply NO_NMI_WATCHDOG metric constraint:\n"
618ab483d8bSKan Liang 		   "    echo 0 > /proc/sys/kernel/nmi_watchdog\n"
619ab483d8bSKan Liang 		   "    perf stat ...\n"
620ab483d8bSKan Liang 		   "    echo 1 > /proc/sys/kernel/nmi_watchdog\n");
621ab483d8bSKan Liang }
622ab483d8bSKan Liang 
623ab483d8bSKan Liang static bool metricgroup__has_constraint(struct pmu_event *pe)
624ab483d8bSKan Liang {
625ab483d8bSKan Liang 	if (!pe->metric_constraint)
626ab483d8bSKan Liang 		return false;
627ab483d8bSKan Liang 
628ab483d8bSKan Liang 	if (!strcmp(pe->metric_constraint, "NO_NMI_WATCHDOG") &&
629ab483d8bSKan Liang 	    sysctl__nmi_watchdog_enabled()) {
630ab483d8bSKan Liang 		metricgroup___watchdog_constraint_hint(pe->metric_name, false);
631ab483d8bSKan Liang 		return true;
632ab483d8bSKan Liang 	}
633ab483d8bSKan Liang 
634ab483d8bSKan Liang 	return false;
635ab483d8bSKan Liang }
636ab483d8bSKan Liang 
6371e1a873dSKajol Jain int __weak arch_get_runtimeparam(void)
6381e1a873dSKajol Jain {
6391e1a873dSKajol Jain 	return 1;
6401e1a873dSKajol Jain }
6411e1a873dSKajol Jain 
642e7e1baddSJiri Olsa static int __add_metric(struct list_head *group_list,
64305530a79SIan Rogers 			struct pmu_event *pe,
64405530a79SIan Rogers 			bool metric_no_group,
64583de0b7dSJiri Olsa 			int runtime,
646*f6fb0960SJiri Olsa 			struct egroup **egp,
647*f6fb0960SJiri Olsa 			struct expr_id *parent,
648*f6fb0960SJiri Olsa 			struct expr_ids *ids)
64947352abaSKajol Jain {
65083de0b7dSJiri Olsa 	struct metric_ref_node *ref;
65147352abaSKajol Jain 	struct egroup *eg;
65247352abaSKajol Jain 
65383de0b7dSJiri Olsa 	if (*egp == NULL) {
65483de0b7dSJiri Olsa 		/*
65583de0b7dSJiri Olsa 		 * We got in here for the parent group,
65683de0b7dSJiri Olsa 		 * allocate it and put it on the list.
65783de0b7dSJiri Olsa 		 */
658*f6fb0960SJiri Olsa 		eg = zalloc(sizeof(*eg));
65947352abaSKajol Jain 		if (!eg)
66047352abaSKajol Jain 			return -ENOMEM;
66147352abaSKajol Jain 
662ded80bdaSIan Rogers 		expr__ctx_init(&eg->pctx);
66347352abaSKajol Jain 		eg->metric_name = pe->metric_name;
66447352abaSKajol Jain 		eg->metric_expr = pe->metric_expr;
66547352abaSKajol Jain 		eg->metric_unit = pe->unit;
6661e1a873dSKajol Jain 		eg->runtime = runtime;
66705530a79SIan Rogers 		eg->has_constraint = metric_no_group || metricgroup__has_constraint(pe);
66883de0b7dSJiri Olsa 		INIT_LIST_HEAD(&eg->metric_refs);
66983de0b7dSJiri Olsa 		eg->metric_refs_cnt = 0;
67083de0b7dSJiri Olsa 		*egp = eg;
671*f6fb0960SJiri Olsa 
672*f6fb0960SJiri Olsa 		parent = expr_ids__alloc(ids);
673*f6fb0960SJiri Olsa 		if (!parent) {
674*f6fb0960SJiri Olsa 			free(eg);
675*f6fb0960SJiri Olsa 			return -EINVAL;
676*f6fb0960SJiri Olsa 		}
677*f6fb0960SJiri Olsa 
678*f6fb0960SJiri Olsa 		parent->id = strdup(pe->metric_name);
679*f6fb0960SJiri Olsa 		if (!parent->id) {
680*f6fb0960SJiri Olsa 			free(eg);
681*f6fb0960SJiri Olsa 			return -ENOMEM;
682*f6fb0960SJiri Olsa 		}
68383de0b7dSJiri Olsa 	} else {
68483de0b7dSJiri Olsa 		/*
68583de0b7dSJiri Olsa 		 * We got here for the referenced metric, via the
68683de0b7dSJiri Olsa 		 * recursive metricgroup__add_metric call, add
68783de0b7dSJiri Olsa 		 * it to the parent group.
68883de0b7dSJiri Olsa 		 */
68983de0b7dSJiri Olsa 		eg = *egp;
690ded80bdaSIan Rogers 
69183de0b7dSJiri Olsa 		ref = malloc(sizeof(*ref));
69283de0b7dSJiri Olsa 		if (!ref)
69383de0b7dSJiri Olsa 			return -ENOMEM;
69483de0b7dSJiri Olsa 
69583de0b7dSJiri Olsa 		/*
69683de0b7dSJiri Olsa 		 * Intentionally passing just const char pointers,
69783de0b7dSJiri Olsa 		 * from 'pe' object, so they never go away. We don't
69883de0b7dSJiri Olsa 		 * need to change them, so there's no need to create
69983de0b7dSJiri Olsa 		 * our own copy.
70083de0b7dSJiri Olsa 		 */
70183de0b7dSJiri Olsa 		ref->metric_name = pe->metric_name;
70283de0b7dSJiri Olsa 		ref->metric_expr = pe->metric_expr;
70383de0b7dSJiri Olsa 
70483de0b7dSJiri Olsa 		list_add(&ref->list, &eg->metric_refs);
70583de0b7dSJiri Olsa 		eg->metric_refs_cnt++;
70683de0b7dSJiri Olsa 	}
70783de0b7dSJiri Olsa 
708*f6fb0960SJiri Olsa 	/* Force all found IDs in metric to have us as parent ID. */
709*f6fb0960SJiri Olsa 	WARN_ON_ONCE(!parent);
710*f6fb0960SJiri Olsa 	eg->pctx.parent = parent;
711*f6fb0960SJiri Olsa 
71283de0b7dSJiri Olsa 	/*
71383de0b7dSJiri Olsa 	 * For both the parent and referenced metrics, we parse
71483de0b7dSJiri Olsa 	 * all the metric's IDs and add it to the parent context.
71583de0b7dSJiri Olsa 	 */
716ded80bdaSIan Rogers 	if (expr__find_other(pe->metric_expr, NULL, &eg->pctx, runtime) < 0) {
717ded80bdaSIan Rogers 		expr__ctx_clear(&eg->pctx);
718ded80bdaSIan Rogers 		free(eg);
719ded80bdaSIan Rogers 		return -EINVAL;
720ded80bdaSIan Rogers 	}
721ded80bdaSIan Rogers 
72283de0b7dSJiri Olsa 	/*
72383de0b7dSJiri Olsa 	 * We add new group only in the 'parent' call,
72483de0b7dSJiri Olsa 	 * so bail out for referenced metric case.
72583de0b7dSJiri Olsa 	 */
72683de0b7dSJiri Olsa 	if (eg->metric_refs_cnt)
72783de0b7dSJiri Olsa 		return 0;
72883de0b7dSJiri Olsa 
7296bf2102bSIan Rogers 	if (list_empty(group_list))
7306bf2102bSIan Rogers 		list_add(&eg->nd, group_list);
7316bf2102bSIan Rogers 	else {
7326bf2102bSIan Rogers 		struct list_head *pos;
7336bf2102bSIan Rogers 
7346bf2102bSIan Rogers 		/* Place the largest groups at the front. */
7356bf2102bSIan Rogers 		list_for_each_prev(pos, group_list) {
7366bf2102bSIan Rogers 			struct egroup *old = list_entry(pos, struct egroup, nd);
7376bf2102bSIan Rogers 
7386bf2102bSIan Rogers 			if (hashmap__size(&eg->pctx.ids) <=
7396bf2102bSIan Rogers 			    hashmap__size(&old->pctx.ids))
7406bf2102bSIan Rogers 				break;
7416bf2102bSIan Rogers 		}
7426bf2102bSIan Rogers 		list_add(&eg->nd, pos);
7436bf2102bSIan Rogers 	}
74447352abaSKajol Jain 
74547352abaSKajol Jain 	return 0;
74647352abaSKajol Jain }
74747352abaSKajol Jain 
748ce391940SJiri Olsa #define map_for_each_event(__pe, __idx, __map)				\
749ce391940SJiri Olsa 	for (__idx = 0, __pe = &__map->table[__idx];			\
750ce391940SJiri Olsa 	     __pe->name || __pe->metric_group || __pe->metric_name;	\
751ce391940SJiri Olsa 	     __pe = &__map->table[++__idx])
752ce391940SJiri Olsa 
753ce391940SJiri Olsa #define map_for_each_metric(__pe, __idx, __map, __metric)		\
754ce391940SJiri Olsa 	map_for_each_event(__pe, __idx, __map)				\
755ce391940SJiri Olsa 		if (__pe->metric_expr &&				\
756ce391940SJiri Olsa 		    (match_metric(__pe->metric_group, __metric) ||	\
757ce391940SJiri Olsa 		     match_metric(__pe->metric_name, __metric)))
758ce391940SJiri Olsa 
75983de0b7dSJiri Olsa static struct pmu_event *find_metric(const char *metric, struct pmu_events_map *map)
76083de0b7dSJiri Olsa {
76183de0b7dSJiri Olsa 	struct pmu_event *pe;
76283de0b7dSJiri Olsa 	int i;
76383de0b7dSJiri Olsa 
76483de0b7dSJiri Olsa 	map_for_each_event(pe, i, map) {
76583de0b7dSJiri Olsa 		if (match_metric(pe->metric_name, metric))
76683de0b7dSJiri Olsa 			return pe;
76783de0b7dSJiri Olsa 	}
76883de0b7dSJiri Olsa 
76983de0b7dSJiri Olsa 	return NULL;
77083de0b7dSJiri Olsa }
77183de0b7dSJiri Olsa 
772*f6fb0960SJiri Olsa static int recursion_check(struct egroup *eg, const char *id, struct expr_id **parent,
773*f6fb0960SJiri Olsa 			   struct expr_ids *ids)
774*f6fb0960SJiri Olsa {
775*f6fb0960SJiri Olsa 	struct expr_id_data *data;
776*f6fb0960SJiri Olsa 	struct expr_id *p;
777*f6fb0960SJiri Olsa 	int ret;
778*f6fb0960SJiri Olsa 
779*f6fb0960SJiri Olsa 	/*
780*f6fb0960SJiri Olsa 	 * We get the parent referenced by 'id' argument and
781*f6fb0960SJiri Olsa 	 * traverse through all the parent object IDs to check
782*f6fb0960SJiri Olsa 	 * if we already processed 'id', if we did, it's recursion
783*f6fb0960SJiri Olsa 	 * and we fail.
784*f6fb0960SJiri Olsa 	 */
785*f6fb0960SJiri Olsa 	ret = expr__get_id(&eg->pctx, id, &data);
786*f6fb0960SJiri Olsa 	if (ret)
787*f6fb0960SJiri Olsa 		return ret;
788*f6fb0960SJiri Olsa 
789*f6fb0960SJiri Olsa 	p = data->parent;
790*f6fb0960SJiri Olsa 
791*f6fb0960SJiri Olsa 	while (p->parent) {
792*f6fb0960SJiri Olsa 		if (!strcmp(p->id, id)) {
793*f6fb0960SJiri Olsa 			pr_err("failed: recursion detected for %s\n", id);
794*f6fb0960SJiri Olsa 			return -1;
795*f6fb0960SJiri Olsa 		}
796*f6fb0960SJiri Olsa 		p = p->parent;
797*f6fb0960SJiri Olsa 	}
798*f6fb0960SJiri Olsa 
799*f6fb0960SJiri Olsa 	/*
800*f6fb0960SJiri Olsa 	 * If we are over the limit of static entris, the metric
801*f6fb0960SJiri Olsa 	 * is too difficult/nested to process, fail as well.
802*f6fb0960SJiri Olsa 	 */
803*f6fb0960SJiri Olsa 	p = expr_ids__alloc(ids);
804*f6fb0960SJiri Olsa 	if (!p) {
805*f6fb0960SJiri Olsa 		pr_err("failed: too many nested metrics\n");
806*f6fb0960SJiri Olsa 		return -EINVAL;
807*f6fb0960SJiri Olsa 	}
808*f6fb0960SJiri Olsa 
809*f6fb0960SJiri Olsa 	p->id     = strdup(id);
810*f6fb0960SJiri Olsa 	p->parent = data->parent;
811*f6fb0960SJiri Olsa 	*parent   = p;
812*f6fb0960SJiri Olsa 
813*f6fb0960SJiri Olsa 	return p->id ? 0 : -ENOMEM;
814*f6fb0960SJiri Olsa }
815*f6fb0960SJiri Olsa 
816a29c164aSJiri Olsa static int add_metric(struct list_head *group_list,
817a29c164aSJiri Olsa 		      struct pmu_event *pe,
81883de0b7dSJiri Olsa 		      bool metric_no_group,
819*f6fb0960SJiri Olsa 		      struct egroup **egp,
820*f6fb0960SJiri Olsa 		      struct expr_id *parent,
821*f6fb0960SJiri Olsa 		      struct expr_ids *ids);
82283de0b7dSJiri Olsa 
82383de0b7dSJiri Olsa static int __resolve_metric(struct egroup *eg,
82483de0b7dSJiri Olsa 			    bool metric_no_group,
82583de0b7dSJiri Olsa 			    struct list_head *group_list,
826*f6fb0960SJiri Olsa 			    struct pmu_events_map *map,
827*f6fb0960SJiri Olsa 			    struct expr_ids *ids)
828a29c164aSJiri Olsa {
82983de0b7dSJiri Olsa 	struct hashmap_entry *cur;
83083de0b7dSJiri Olsa 	size_t bkt;
83183de0b7dSJiri Olsa 	bool all;
83283de0b7dSJiri Olsa 	int ret;
83383de0b7dSJiri Olsa 
83483de0b7dSJiri Olsa 	/*
83583de0b7dSJiri Olsa 	 * Iterate all the parsed IDs and if there's metric,
83683de0b7dSJiri Olsa 	 * add it to the context.
83783de0b7dSJiri Olsa 	 */
83883de0b7dSJiri Olsa 	do {
83983de0b7dSJiri Olsa 		all = true;
84083de0b7dSJiri Olsa 		hashmap__for_each_entry((&eg->pctx.ids), cur, bkt) {
841*f6fb0960SJiri Olsa 			struct expr_id *parent;
84283de0b7dSJiri Olsa 			struct pmu_event *pe;
84383de0b7dSJiri Olsa 
84483de0b7dSJiri Olsa 			pe = find_metric(cur->key, map);
84583de0b7dSJiri Olsa 			if (!pe)
84683de0b7dSJiri Olsa 				continue;
84783de0b7dSJiri Olsa 
848*f6fb0960SJiri Olsa 			ret = recursion_check(eg, cur->key, &parent, ids);
849*f6fb0960SJiri Olsa 			if (ret)
850*f6fb0960SJiri Olsa 				return ret;
851*f6fb0960SJiri Olsa 
85283de0b7dSJiri Olsa 			all = false;
85383de0b7dSJiri Olsa 			/* The metric key itself needs to go out.. */
85483de0b7dSJiri Olsa 			expr__del_id(&eg->pctx, cur->key);
85583de0b7dSJiri Olsa 
85683de0b7dSJiri Olsa 			/* ... and it gets resolved to the parent context. */
857*f6fb0960SJiri Olsa 			ret = add_metric(group_list, pe, metric_no_group, &eg, parent, ids);
85883de0b7dSJiri Olsa 			if (ret)
85983de0b7dSJiri Olsa 				return ret;
86083de0b7dSJiri Olsa 
86183de0b7dSJiri Olsa 			/*
86283de0b7dSJiri Olsa 			 * We added new metric to hashmap, so we need
86383de0b7dSJiri Olsa 			 * to break the iteration and start over.
86483de0b7dSJiri Olsa 			 */
86583de0b7dSJiri Olsa 			break;
86683de0b7dSJiri Olsa 		}
86783de0b7dSJiri Olsa 	} while (!all);
86883de0b7dSJiri Olsa 
86983de0b7dSJiri Olsa 	return 0;
87083de0b7dSJiri Olsa }
87183de0b7dSJiri Olsa 
87283de0b7dSJiri Olsa static int resolve_metric(bool metric_no_group,
87383de0b7dSJiri Olsa 			  struct list_head *metric_list,
874*f6fb0960SJiri Olsa 			  struct pmu_events_map *map,
875*f6fb0960SJiri Olsa 			  struct expr_ids *ids)
87683de0b7dSJiri Olsa {
87783de0b7dSJiri Olsa 	struct egroup *eg;
87883de0b7dSJiri Olsa 	int err;
87983de0b7dSJiri Olsa 
88083de0b7dSJiri Olsa 	list_for_each_entry(eg, metric_list, nd) {
881*f6fb0960SJiri Olsa 		err = __resolve_metric(eg, metric_no_group, metric_list, map, ids);
88283de0b7dSJiri Olsa 		if (err)
88383de0b7dSJiri Olsa 			return err;
88483de0b7dSJiri Olsa 	}
88583de0b7dSJiri Olsa 	return 0;
88683de0b7dSJiri Olsa }
88783de0b7dSJiri Olsa 
88883de0b7dSJiri Olsa static int add_metric(struct list_head *group_list,
88983de0b7dSJiri Olsa 		      struct pmu_event *pe,
89083de0b7dSJiri Olsa 		      bool metric_no_group,
891*f6fb0960SJiri Olsa 		      struct egroup **egp,
892*f6fb0960SJiri Olsa 		      struct expr_id *parent,
893*f6fb0960SJiri Olsa 		      struct expr_ids *ids)
89483de0b7dSJiri Olsa {
89583de0b7dSJiri Olsa 	struct egroup *orig = *egp;
896a29c164aSJiri Olsa 	int ret = 0;
897a29c164aSJiri Olsa 
898a29c164aSJiri Olsa 	pr_debug("metric expr %s for %s\n", pe->metric_expr, pe->metric_name);
899a29c164aSJiri Olsa 
900a29c164aSJiri Olsa 	if (!strstr(pe->metric_expr, "?")) {
901*f6fb0960SJiri Olsa 		ret = __add_metric(group_list, pe, metric_no_group, 1, egp, parent, ids);
902a29c164aSJiri Olsa 	} else {
903a29c164aSJiri Olsa 		int j, count;
904a29c164aSJiri Olsa 
905a29c164aSJiri Olsa 		count = arch_get_runtimeparam();
906a29c164aSJiri Olsa 
907a29c164aSJiri Olsa 		/* This loop is added to create multiple
908a29c164aSJiri Olsa 		 * events depend on count value and add
909a29c164aSJiri Olsa 		 * those events to group_list.
910a29c164aSJiri Olsa 		 */
911a29c164aSJiri Olsa 
91283de0b7dSJiri Olsa 		for (j = 0; j < count && !ret; j++, *egp = orig)
913*f6fb0960SJiri Olsa 			ret = __add_metric(group_list, pe, metric_no_group, j, egp, parent, ids);
914a29c164aSJiri Olsa 	}
915a29c164aSJiri Olsa 
916a29c164aSJiri Olsa 	return ret;
917a29c164aSJiri Olsa }
918a29c164aSJiri Olsa 
91905530a79SIan Rogers static int metricgroup__add_metric(const char *metric, bool metric_no_group,
92005530a79SIan Rogers 				   struct strbuf *events,
9211381396bSJiri Olsa 				   struct list_head *group_list,
9221381396bSJiri Olsa 				   struct pmu_events_map *map)
923b18f3e36SAndi Kleen {
924*f6fb0960SJiri Olsa 	struct expr_ids ids = { .cnt = 0, };
925b18f3e36SAndi Kleen 	struct pmu_event *pe;
9267f9eca51SIan Rogers 	struct egroup *eg;
92798461d9dSJiri Olsa 	LIST_HEAD(list);
92890810399SIan Rogers 	int i, ret;
92990810399SIan Rogers 	bool has_match = false;
930b18f3e36SAndi Kleen 
931ce391940SJiri Olsa 	map_for_each_metric(pe, i, map, metric) {
932ce391940SJiri Olsa 		has_match = true;
93383de0b7dSJiri Olsa 		eg = NULL;
934b18f3e36SAndi Kleen 
935*f6fb0960SJiri Olsa 		ret = add_metric(&list, pe, metric_no_group, &eg, NULL, &ids);
93683de0b7dSJiri Olsa 		if (ret)
93783de0b7dSJiri Olsa 			return ret;
93883de0b7dSJiri Olsa 
93983de0b7dSJiri Olsa 		/*
94083de0b7dSJiri Olsa 		 * Process any possible referenced metrics
94183de0b7dSJiri Olsa 		 * included in the expression.
94283de0b7dSJiri Olsa 		 */
94383de0b7dSJiri Olsa 		ret = resolve_metric(metric_no_group,
944*f6fb0960SJiri Olsa 				     &list, map, &ids);
94590810399SIan Rogers 		if (ret)
94690810399SIan Rogers 			return ret;
94790810399SIan Rogers 	}
948ce391940SJiri Olsa 
949ce391940SJiri Olsa 	/* End of pmu events. */
950ce391940SJiri Olsa 	if (!has_match)
951ce391940SJiri Olsa 		return -EINVAL;
952ce391940SJiri Olsa 
95398461d9dSJiri Olsa 	list_for_each_entry(eg, &list, nd) {
9547f9eca51SIan Rogers 		if (events->len > 0)
9557f9eca51SIan Rogers 			strbuf_addf(events, ",");
9567f9eca51SIan Rogers 
9577f9eca51SIan Rogers 		if (eg->has_constraint) {
9587f9eca51SIan Rogers 			metricgroup__add_metric_non_group(events,
9597f9eca51SIan Rogers 							  &eg->pctx);
9607f9eca51SIan Rogers 		} else {
9617f9eca51SIan Rogers 			metricgroup__add_metric_weak_group(events,
9627f9eca51SIan Rogers 							   &eg->pctx);
9637f9eca51SIan Rogers 		}
9647f9eca51SIan Rogers 	}
96598461d9dSJiri Olsa 
96698461d9dSJiri Olsa 	list_splice(&list, group_list);
967*f6fb0960SJiri Olsa 	expr_ids__exit(&ids);
96890810399SIan Rogers 	return 0;
96990810399SIan Rogers }
970b18f3e36SAndi Kleen 
97105530a79SIan Rogers static int metricgroup__add_metric_list(const char *list, bool metric_no_group,
97205530a79SIan Rogers 					struct strbuf *events,
9731381396bSJiri Olsa 					struct list_head *group_list,
9741381396bSJiri Olsa 					struct pmu_events_map *map)
975b18f3e36SAndi Kleen {
976b18f3e36SAndi Kleen 	char *llist, *nlist, *p;
977b18f3e36SAndi Kleen 	int ret = -EINVAL;
978b18f3e36SAndi Kleen 
979b18f3e36SAndi Kleen 	nlist = strdup(list);
980b18f3e36SAndi Kleen 	if (!nlist)
981b18f3e36SAndi Kleen 		return -ENOMEM;
982b18f3e36SAndi Kleen 	llist = nlist;
983411bc316SAndi Kleen 
984411bc316SAndi Kleen 	strbuf_init(events, 100);
985411bc316SAndi Kleen 	strbuf_addf(events, "%s", "");
986411bc316SAndi Kleen 
987b18f3e36SAndi Kleen 	while ((p = strsep(&llist, ",")) != NULL) {
98805530a79SIan Rogers 		ret = metricgroup__add_metric(p, metric_no_group, events,
9891381396bSJiri Olsa 					      group_list, map);
990b18f3e36SAndi Kleen 		if (ret == -EINVAL) {
991b18f3e36SAndi Kleen 			fprintf(stderr, "Cannot find metric or group `%s'\n",
992b18f3e36SAndi Kleen 					p);
993b18f3e36SAndi Kleen 			break;
994b18f3e36SAndi Kleen 		}
995b18f3e36SAndi Kleen 	}
996b18f3e36SAndi Kleen 	free(nlist);
997ab483d8bSKan Liang 
998ab483d8bSKan Liang 	if (!ret)
999ab483d8bSKan Liang 		metricgroup___watchdog_constraint_hint(NULL, true);
1000ab483d8bSKan Liang 
1001b18f3e36SAndi Kleen 	return ret;
1002b18f3e36SAndi Kleen }
1003b18f3e36SAndi Kleen 
100483de0b7dSJiri Olsa static void egroup__free_refs(struct egroup *egroup)
100583de0b7dSJiri Olsa {
100683de0b7dSJiri Olsa 	struct metric_ref_node *ref, *tmp;
100783de0b7dSJiri Olsa 
100883de0b7dSJiri Olsa 	list_for_each_entry_safe(ref, tmp, &egroup->metric_refs, list) {
100983de0b7dSJiri Olsa 		list_del(&ref->list);
101083de0b7dSJiri Olsa 		free(ref);
101183de0b7dSJiri Olsa 	}
101283de0b7dSJiri Olsa }
101383de0b7dSJiri Olsa 
1014b18f3e36SAndi Kleen static void metricgroup__free_egroups(struct list_head *group_list)
1015b18f3e36SAndi Kleen {
1016b18f3e36SAndi Kleen 	struct egroup *eg, *egtmp;
1017b18f3e36SAndi Kleen 
1018b18f3e36SAndi Kleen 	list_for_each_entry_safe (eg, egtmp, group_list, nd) {
101983de0b7dSJiri Olsa 		egroup__free_refs(eg);
1020ded80bdaSIan Rogers 		expr__ctx_clear(&eg->pctx);
1021acc7bfb3SArnaldo Carvalho de Melo 		list_del_init(&eg->nd);
1022b18f3e36SAndi Kleen 		free(eg);
1023b18f3e36SAndi Kleen 	}
1024b18f3e36SAndi Kleen }
1025b18f3e36SAndi Kleen 
10268b4468a2SJiri Olsa static int parse_groups(struct evlist *perf_evlist, const char *str,
102705530a79SIan Rogers 			bool metric_no_group,
102805530a79SIan Rogers 			bool metric_no_merge,
102968173bdaSJiri Olsa 			struct perf_pmu *fake_pmu,
10301381396bSJiri Olsa 			struct rblist *metric_events,
10311381396bSJiri Olsa 			struct pmu_events_map *map)
1032b18f3e36SAndi Kleen {
1033b18f3e36SAndi Kleen 	struct parse_events_error parse_error;
1034b18f3e36SAndi Kleen 	struct strbuf extra_events;
1035b18f3e36SAndi Kleen 	LIST_HEAD(group_list);
1036b18f3e36SAndi Kleen 	int ret;
1037b18f3e36SAndi Kleen 
1038b18f3e36SAndi Kleen 	if (metric_events->nr_entries == 0)
1039b18f3e36SAndi Kleen 		metricgroup__rblist_init(metric_events);
104005530a79SIan Rogers 	ret = metricgroup__add_metric_list(str, metric_no_group,
10411381396bSJiri Olsa 					   &extra_events, &group_list, map);
1042b18f3e36SAndi Kleen 	if (ret)
1043b18f3e36SAndi Kleen 		return ret;
1044b18f3e36SAndi Kleen 	pr_debug("adding %s\n", extra_events.buf);
1045a910e466SIan Rogers 	bzero(&parse_error, sizeof(parse_error));
104668173bdaSJiri Olsa 	ret = __parse_events(perf_evlist, extra_events.buf, &parse_error, fake_pmu);
1047b18f3e36SAndi Kleen 	if (ret) {
1048333b5665SAndi Kleen 		parse_events_print_error(&parse_error, extra_events.buf);
1049b18f3e36SAndi Kleen 		goto out;
1050b18f3e36SAndi Kleen 	}
1051b18f3e36SAndi Kleen 	strbuf_release(&extra_events);
105205530a79SIan Rogers 	ret = metricgroup__setup_events(&group_list, metric_no_merge,
105305530a79SIan Rogers 					perf_evlist, metric_events);
1054b18f3e36SAndi Kleen out:
1055b18f3e36SAndi Kleen 	metricgroup__free_egroups(&group_list);
1056b18f3e36SAndi Kleen 	return ret;
1057b18f3e36SAndi Kleen }
1058742d92ffSThomas Richter 
10598b4468a2SJiri Olsa int metricgroup__parse_groups(const struct option *opt,
10608b4468a2SJiri Olsa 			      const char *str,
10618b4468a2SJiri Olsa 			      bool metric_no_group,
10628b4468a2SJiri Olsa 			      bool metric_no_merge,
10638b4468a2SJiri Olsa 			      struct rblist *metric_events)
10648b4468a2SJiri Olsa {
10658b4468a2SJiri Olsa 	struct evlist *perf_evlist = *(struct evlist **)opt->value;
10661381396bSJiri Olsa 	struct pmu_events_map *map = perf_pmu__find_map(NULL);
10671381396bSJiri Olsa 
10681381396bSJiri Olsa 	if (!map)
10691381396bSJiri Olsa 		return 0;
10708b4468a2SJiri Olsa 
10718b4468a2SJiri Olsa 	return parse_groups(perf_evlist, str, metric_no_group,
10721381396bSJiri Olsa 			    metric_no_merge, NULL, metric_events, map);
10738b4468a2SJiri Olsa }
10748b4468a2SJiri Olsa 
1075f78ac00aSJiri Olsa int metricgroup__parse_groups_test(struct evlist *evlist,
1076f78ac00aSJiri Olsa 				   struct pmu_events_map *map,
1077f78ac00aSJiri Olsa 				   const char *str,
1078f78ac00aSJiri Olsa 				   bool metric_no_group,
1079f78ac00aSJiri Olsa 				   bool metric_no_merge,
1080f78ac00aSJiri Olsa 				   struct rblist *metric_events)
1081f78ac00aSJiri Olsa {
1082f78ac00aSJiri Olsa 	return parse_groups(evlist, str, metric_no_group,
1083f78ac00aSJiri Olsa 			    metric_no_merge, &perf_pmu__fake, metric_events, map);
1084f78ac00aSJiri Olsa }
1085f78ac00aSJiri Olsa 
1086742d92ffSThomas Richter bool metricgroup__has_metric(const char *metric)
1087742d92ffSThomas Richter {
1088742d92ffSThomas Richter 	struct pmu_events_map *map = perf_pmu__find_map(NULL);
1089742d92ffSThomas Richter 	struct pmu_event *pe;
1090742d92ffSThomas Richter 	int i;
1091742d92ffSThomas Richter 
1092742d92ffSThomas Richter 	if (!map)
1093742d92ffSThomas Richter 		return false;
1094742d92ffSThomas Richter 
1095742d92ffSThomas Richter 	for (i = 0; ; i++) {
1096742d92ffSThomas Richter 		pe = &map->table[i];
1097742d92ffSThomas Richter 
1098742d92ffSThomas Richter 		if (!pe->name && !pe->metric_group && !pe->metric_name)
1099742d92ffSThomas Richter 			break;
1100742d92ffSThomas Richter 		if (!pe->metric_expr)
1101742d92ffSThomas Richter 			continue;
1102742d92ffSThomas Richter 		if (match_metric(pe->metric_name, metric))
1103742d92ffSThomas Richter 			return true;
1104742d92ffSThomas Richter 	}
1105742d92ffSThomas Richter 	return false;
1106742d92ffSThomas Richter }
1107