xref: /linux/tools/perf/util/metricgroup.c (revision e7e1badd80282ff7ee07a5176f0620f57c798934)
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"
27b18f3e36SAndi Kleen 
28b18f3e36SAndi Kleen struct metric_event *metricgroup__lookup(struct rblist *metric_events,
2932dcd021SJiri Olsa 					 struct evsel *evsel,
30b18f3e36SAndi Kleen 					 bool create)
31b18f3e36SAndi Kleen {
32b18f3e36SAndi Kleen 	struct rb_node *nd;
33b18f3e36SAndi Kleen 	struct metric_event me = {
34b18f3e36SAndi Kleen 		.evsel = evsel
35b18f3e36SAndi Kleen 	};
364bd1bef8SAndi Kleen 
374bd1bef8SAndi Kleen 	if (!metric_events)
384bd1bef8SAndi Kleen 		return NULL;
394bd1bef8SAndi Kleen 
40b18f3e36SAndi Kleen 	nd = rblist__find(metric_events, &me);
41b18f3e36SAndi Kleen 	if (nd)
42b18f3e36SAndi Kleen 		return container_of(nd, struct metric_event, nd);
43b18f3e36SAndi Kleen 	if (create) {
44b18f3e36SAndi Kleen 		rblist__add_node(metric_events, &me);
45b18f3e36SAndi Kleen 		nd = rblist__find(metric_events, &me);
46b18f3e36SAndi Kleen 		if (nd)
47b18f3e36SAndi Kleen 			return container_of(nd, struct metric_event, nd);
48b18f3e36SAndi Kleen 	}
49b18f3e36SAndi Kleen 	return NULL;
50b18f3e36SAndi Kleen }
51b18f3e36SAndi Kleen 
52b18f3e36SAndi Kleen static int metric_event_cmp(struct rb_node *rb_node, const void *entry)
53b18f3e36SAndi Kleen {
54b18f3e36SAndi Kleen 	struct metric_event *a = container_of(rb_node,
55b18f3e36SAndi Kleen 					      struct metric_event,
56b18f3e36SAndi Kleen 					      nd);
57b18f3e36SAndi Kleen 	const struct metric_event *b = entry;
58b18f3e36SAndi Kleen 
59b18f3e36SAndi Kleen 	if (a->evsel == b->evsel)
60b18f3e36SAndi Kleen 		return 0;
61b18f3e36SAndi Kleen 	if ((char *)a->evsel < (char *)b->evsel)
62b18f3e36SAndi Kleen 		return -1;
63b18f3e36SAndi Kleen 	return +1;
64b18f3e36SAndi Kleen }
65b18f3e36SAndi Kleen 
66b18f3e36SAndi Kleen static struct rb_node *metric_event_new(struct rblist *rblist __maybe_unused,
67b18f3e36SAndi Kleen 					const void *entry)
68b18f3e36SAndi Kleen {
69b18f3e36SAndi Kleen 	struct metric_event *me = malloc(sizeof(struct metric_event));
70b18f3e36SAndi Kleen 
71b18f3e36SAndi Kleen 	if (!me)
72b18f3e36SAndi Kleen 		return NULL;
73b18f3e36SAndi Kleen 	memcpy(me, entry, sizeof(struct metric_event));
74b18f3e36SAndi Kleen 	me->evsel = ((struct metric_event *)entry)->evsel;
75b18f3e36SAndi Kleen 	INIT_LIST_HEAD(&me->head);
76b18f3e36SAndi Kleen 	return &me->nd;
77b18f3e36SAndi Kleen }
78b18f3e36SAndi Kleen 
799afe5658SJiri Olsa static void metric_event_delete(struct rblist *rblist __maybe_unused,
809afe5658SJiri Olsa 				struct rb_node *rb_node)
819afe5658SJiri Olsa {
829afe5658SJiri Olsa 	struct metric_event *me = container_of(rb_node, struct metric_event, nd);
839afe5658SJiri Olsa 	struct metric_expr *expr, *tmp;
849afe5658SJiri Olsa 
859afe5658SJiri Olsa 	list_for_each_entry_safe(expr, tmp, &me->head, nd) {
869afe5658SJiri Olsa 		free(expr);
879afe5658SJiri Olsa 	}
889afe5658SJiri Olsa 
899afe5658SJiri Olsa 	free(me);
909afe5658SJiri Olsa }
919afe5658SJiri Olsa 
92b18f3e36SAndi Kleen static void metricgroup__rblist_init(struct rblist *metric_events)
93b18f3e36SAndi Kleen {
94b18f3e36SAndi Kleen 	rblist__init(metric_events);
95b18f3e36SAndi Kleen 	metric_events->node_cmp = metric_event_cmp;
96b18f3e36SAndi Kleen 	metric_events->node_new = metric_event_new;
979afe5658SJiri Olsa 	metric_events->node_delete = metric_event_delete;
989afe5658SJiri Olsa }
999afe5658SJiri Olsa 
1009afe5658SJiri Olsa void metricgroup__rblist_exit(struct rblist *metric_events)
1019afe5658SJiri Olsa {
1029afe5658SJiri Olsa 	rblist__exit(metric_events);
103b18f3e36SAndi Kleen }
104b18f3e36SAndi Kleen 
105b18f3e36SAndi Kleen struct egroup {
106b18f3e36SAndi Kleen 	struct list_head nd;
107ded80bdaSIan Rogers 	struct expr_parse_ctx pctx;
108b18f3e36SAndi Kleen 	const char *metric_name;
109b18f3e36SAndi Kleen 	const char *metric_expr;
110287f2649SJin Yao 	const char *metric_unit;
1111e1a873dSKajol Jain 	int runtime;
1127f9eca51SIan Rogers 	bool has_constraint;
113b18f3e36SAndi Kleen };
114b18f3e36SAndi Kleen 
1152440689dSIan Rogers /**
1162440689dSIan Rogers  * Find a group of events in perf_evlist that correpond to those from a parsed
11705530a79SIan Rogers  * metric expression. Note, as find_evsel_group is called in the same order as
11805530a79SIan Rogers  * perf_evlist was constructed, metric_no_merge doesn't need to test for
11905530a79SIan Rogers  * underfilling a group.
1202440689dSIan Rogers  * @perf_evlist: a list of events something like: {metric1 leader, metric1
1212440689dSIan Rogers  * sibling, metric1 sibling}:W,duration_time,{metric2 leader, metric2 sibling,
1222440689dSIan Rogers  * metric2 sibling}:W,duration_time
1232440689dSIan Rogers  * @pctx: the parse context for the metric expression.
12405530a79SIan Rogers  * @metric_no_merge: don't attempt to share events for the metric with other
12505530a79SIan Rogers  * metrics.
1262440689dSIan Rogers  * @has_constraint: is there a contraint on the group of events? In which case
1272440689dSIan Rogers  * the events won't be grouped.
1282440689dSIan Rogers  * @metric_events: out argument, null terminated array of evsel's associated
1292440689dSIan Rogers  * with the metric.
1302440689dSIan Rogers  * @evlist_used: in/out argument, bitmap tracking which evlist events are used.
1312440689dSIan Rogers  * @return the first metric event or NULL on failure.
1322440689dSIan Rogers  */
13363503dbaSJiri Olsa static struct evsel *find_evsel_group(struct evlist *perf_evlist,
134ded80bdaSIan Rogers 				      struct expr_parse_ctx *pctx,
13505530a79SIan Rogers 				      bool metric_no_merge,
1362440689dSIan Rogers 				      bool has_constraint,
13758fc90fdSKajol Jain 				      struct evsel **metric_events,
13845db55f2SIan Rogers 				      unsigned long *evlist_used)
139b18f3e36SAndi Kleen {
1402440689dSIan Rogers 	struct evsel *ev, *current_leader = NULL;
141070b3b5aSJiri Olsa 	struct expr_id_data *val_ptr;
1422440689dSIan Rogers 	int i = 0, matched_events = 0, events_to_match;
1432440689dSIan Rogers 	const int idnum = (int)hashmap__size(&pctx->ids);
1442440689dSIan Rogers 
1452440689dSIan Rogers 	/* duration_time is grouped separately. */
1462440689dSIan Rogers 	if (!has_constraint &&
1472440689dSIan Rogers 	    hashmap__find(&pctx->ids, "duration_time", (void **)&val_ptr))
1482440689dSIan Rogers 		events_to_match = idnum - 1;
1492440689dSIan Rogers 	else
1502440689dSIan Rogers 		events_to_match = idnum;
151b18f3e36SAndi Kleen 
152b18f3e36SAndi Kleen 	evlist__for_each_entry (perf_evlist, ev) {
1532440689dSIan Rogers 		/*
1542440689dSIan Rogers 		 * Events with a constraint aren't grouped and match the first
1552440689dSIan Rogers 		 * events available.
1562440689dSIan Rogers 		 */
1572440689dSIan Rogers 		if (has_constraint && ev->weak_group)
15858fc90fdSKajol Jain 			continue;
15905530a79SIan Rogers 		/* Ignore event if already used and merging is disabled. */
16005530a79SIan Rogers 		if (metric_no_merge && test_bit(ev->idx, evlist_used))
16105530a79SIan Rogers 			continue;
1622440689dSIan Rogers 		if (!has_constraint && ev->leader != current_leader) {
1632440689dSIan Rogers 			/*
1642440689dSIan Rogers 			 * Start of a new group, discard the whole match and
1652440689dSIan Rogers 			 * start again.
1662440689dSIan Rogers 			 */
1672440689dSIan Rogers 			matched_events = 0;
168f01642e4SJin Yao 			memset(metric_events, 0,
169f01642e4SJin Yao 				sizeof(struct evsel *) * idnum);
1702440689dSIan Rogers 			current_leader = ev->leader;
1712440689dSIan Rogers 		}
17205530a79SIan Rogers 		if (hashmap__find(&pctx->ids, ev->name, (void **)&val_ptr)) {
17305530a79SIan Rogers 			if (has_constraint) {
17405530a79SIan Rogers 				/*
17505530a79SIan Rogers 				 * Events aren't grouped, ensure the same event
17605530a79SIan Rogers 				 * isn't matched from two groups.
17705530a79SIan Rogers 				 */
17805530a79SIan Rogers 				for (i = 0; i < matched_events; i++) {
17905530a79SIan Rogers 					if (!strcmp(ev->name,
18005530a79SIan Rogers 						    metric_events[i]->name)) {
18105530a79SIan Rogers 						break;
18205530a79SIan Rogers 					}
18305530a79SIan Rogers 				}
18405530a79SIan Rogers 				if (i != matched_events)
18505530a79SIan Rogers 					continue;
18605530a79SIan Rogers 			}
1872440689dSIan Rogers 			metric_events[matched_events++] = ev;
18805530a79SIan Rogers 		}
1892440689dSIan Rogers 		if (matched_events == events_to_match)
1902440689dSIan Rogers 			break;
1912440689dSIan Rogers 	}
1922440689dSIan Rogers 
1932440689dSIan Rogers 	if (events_to_match != idnum) {
1942440689dSIan Rogers 		/* Add the first duration_time. */
1952440689dSIan Rogers 		evlist__for_each_entry(perf_evlist, ev) {
1962440689dSIan Rogers 			if (!strcmp(ev->name, "duration_time")) {
1972440689dSIan Rogers 				metric_events[matched_events++] = ev;
1982440689dSIan Rogers 				break;
1992440689dSIan Rogers 			}
200b18f3e36SAndi Kleen 		}
201b18f3e36SAndi Kleen 	}
202f01642e4SJin Yao 
2032440689dSIan Rogers 	if (matched_events != idnum) {
204f01642e4SJin Yao 		/* Not whole match */
205b18f3e36SAndi Kleen 		return NULL;
206b18f3e36SAndi Kleen 	}
207b18f3e36SAndi Kleen 
208f01642e4SJin Yao 	metric_events[idnum] = NULL;
209f01642e4SJin Yao 
210f01642e4SJin Yao 	for (i = 0; i < idnum; i++) {
21158fc90fdSKajol Jain 		ev = metric_events[i];
2122440689dSIan Rogers 		ev->metric_leader = ev;
21345db55f2SIan Rogers 		set_bit(ev->idx, evlist_used);
214f01642e4SJin Yao 	}
215f01642e4SJin Yao 
216f01642e4SJin Yao 	return metric_events[0];
217f01642e4SJin Yao }
218f01642e4SJin Yao 
219b18f3e36SAndi Kleen static int metricgroup__setup_events(struct list_head *groups,
22005530a79SIan Rogers 				     bool metric_no_merge,
22163503dbaSJiri Olsa 				     struct evlist *perf_evlist,
222b18f3e36SAndi Kleen 				     struct rblist *metric_events_list)
223b18f3e36SAndi Kleen {
224b18f3e36SAndi Kleen 	struct metric_event *me;
225b18f3e36SAndi Kleen 	struct metric_expr *expr;
226b18f3e36SAndi Kleen 	int i = 0;
227b18f3e36SAndi Kleen 	int ret = 0;
228b18f3e36SAndi Kleen 	struct egroup *eg;
2292440689dSIan Rogers 	struct evsel *evsel, *tmp;
23045db55f2SIan Rogers 	unsigned long *evlist_used;
23158fc90fdSKajol Jain 
23245db55f2SIan Rogers 	evlist_used = bitmap_alloc(perf_evlist->core.nr_entries);
23345db55f2SIan Rogers 	if (!evlist_used)
23445db55f2SIan Rogers 		return -ENOMEM;
235b18f3e36SAndi Kleen 
236b18f3e36SAndi Kleen 	list_for_each_entry (eg, groups, nd) {
23732dcd021SJiri Olsa 		struct evsel **metric_events;
238b18f3e36SAndi Kleen 
239ded80bdaSIan Rogers 		metric_events = calloc(sizeof(void *),
240ded80bdaSIan Rogers 				hashmap__size(&eg->pctx.ids) + 1);
241b18f3e36SAndi Kleen 		if (!metric_events) {
242b18f3e36SAndi Kleen 			ret = -ENOMEM;
243b18f3e36SAndi Kleen 			break;
244b18f3e36SAndi Kleen 		}
2452440689dSIan Rogers 		evsel = find_evsel_group(perf_evlist, &eg->pctx,
24605530a79SIan Rogers 					 metric_no_merge,
2472440689dSIan Rogers 					 eg->has_constraint, metric_events,
248ded80bdaSIan Rogers 					 evlist_used);
249b18f3e36SAndi Kleen 		if (!evsel) {
250b18f3e36SAndi Kleen 			pr_debug("Cannot resolve %s: %s\n",
251b18f3e36SAndi Kleen 					eg->metric_name, eg->metric_expr);
252a159e2feSIan Rogers 			free(metric_events);
253b18f3e36SAndi Kleen 			continue;
254b18f3e36SAndi Kleen 		}
255ded80bdaSIan Rogers 		for (i = 0; metric_events[i]; i++)
256b18f3e36SAndi Kleen 			metric_events[i]->collect_stat = true;
257b18f3e36SAndi Kleen 		me = metricgroup__lookup(metric_events_list, evsel, true);
258b18f3e36SAndi Kleen 		if (!me) {
259b18f3e36SAndi Kleen 			ret = -ENOMEM;
260a159e2feSIan Rogers 			free(metric_events);
261b18f3e36SAndi Kleen 			break;
262b18f3e36SAndi Kleen 		}
263b18f3e36SAndi Kleen 		expr = malloc(sizeof(struct metric_expr));
264b18f3e36SAndi Kleen 		if (!expr) {
265b18f3e36SAndi Kleen 			ret = -ENOMEM;
266a159e2feSIan Rogers 			free(metric_events);
267b18f3e36SAndi Kleen 			break;
268b18f3e36SAndi Kleen 		}
269b18f3e36SAndi Kleen 		expr->metric_expr = eg->metric_expr;
270b18f3e36SAndi Kleen 		expr->metric_name = eg->metric_name;
271287f2649SJin Yao 		expr->metric_unit = eg->metric_unit;
272b18f3e36SAndi Kleen 		expr->metric_events = metric_events;
2731e1a873dSKajol Jain 		expr->runtime = eg->runtime;
274b18f3e36SAndi Kleen 		list_add(&expr->nd, &me->head);
275b18f3e36SAndi Kleen 	}
27658fc90fdSKajol Jain 
2772440689dSIan Rogers 	evlist__for_each_entry_safe(perf_evlist, tmp, evsel) {
2782440689dSIan Rogers 		if (!test_bit(evsel->idx, evlist_used)) {
2792440689dSIan Rogers 			evlist__remove(perf_evlist, evsel);
2802440689dSIan Rogers 			evsel__delete(evsel);
2812440689dSIan Rogers 		}
2822440689dSIan Rogers 	}
28345db55f2SIan Rogers 	bitmap_free(evlist_used);
28458fc90fdSKajol Jain 
285b18f3e36SAndi Kleen 	return ret;
286b18f3e36SAndi Kleen }
287b18f3e36SAndi Kleen 
288b18f3e36SAndi Kleen static bool match_metric(const char *n, const char *list)
289b18f3e36SAndi Kleen {
290b18f3e36SAndi Kleen 	int len;
291b18f3e36SAndi Kleen 	char *m;
292b18f3e36SAndi Kleen 
293b18f3e36SAndi Kleen 	if (!list)
294b18f3e36SAndi Kleen 		return false;
295b18f3e36SAndi Kleen 	if (!strcmp(list, "all"))
296b18f3e36SAndi Kleen 		return true;
297b18f3e36SAndi Kleen 	if (!n)
298b18f3e36SAndi Kleen 		return !strcasecmp(list, "No_group");
299b18f3e36SAndi Kleen 	len = strlen(list);
300b18f3e36SAndi Kleen 	m = strcasestr(n, list);
301b18f3e36SAndi Kleen 	if (!m)
302b18f3e36SAndi Kleen 		return false;
303b18f3e36SAndi Kleen 	if ((m == n || m[-1] == ';' || m[-1] == ' ') &&
304b18f3e36SAndi Kleen 	    (m[len] == 0 || m[len] == ';'))
305b18f3e36SAndi Kleen 		return true;
306b18f3e36SAndi Kleen 	return false;
307b18f3e36SAndi Kleen }
308b18f3e36SAndi Kleen 
30971b0acceSAndi Kleen struct mep {
31071b0acceSAndi Kleen 	struct rb_node nd;
31171b0acceSAndi Kleen 	const char *name;
31271b0acceSAndi Kleen 	struct strlist *metrics;
31371b0acceSAndi Kleen };
31471b0acceSAndi Kleen 
31571b0acceSAndi Kleen static int mep_cmp(struct rb_node *rb_node, const void *entry)
31671b0acceSAndi Kleen {
31771b0acceSAndi Kleen 	struct mep *a = container_of(rb_node, struct mep, nd);
31871b0acceSAndi Kleen 	struct mep *b = (struct mep *)entry;
31971b0acceSAndi Kleen 
32071b0acceSAndi Kleen 	return strcmp(a->name, b->name);
32171b0acceSAndi Kleen }
32271b0acceSAndi Kleen 
32371b0acceSAndi Kleen static struct rb_node *mep_new(struct rblist *rl __maybe_unused,
32471b0acceSAndi Kleen 					const void *entry)
32571b0acceSAndi Kleen {
32671b0acceSAndi Kleen 	struct mep *me = malloc(sizeof(struct mep));
32771b0acceSAndi Kleen 
32871b0acceSAndi Kleen 	if (!me)
32971b0acceSAndi Kleen 		return NULL;
33071b0acceSAndi Kleen 	memcpy(me, entry, sizeof(struct mep));
33171b0acceSAndi Kleen 	me->name = strdup(me->name);
33271b0acceSAndi Kleen 	if (!me->name)
33371b0acceSAndi Kleen 		goto out_me;
33471b0acceSAndi Kleen 	me->metrics = strlist__new(NULL, NULL);
33571b0acceSAndi Kleen 	if (!me->metrics)
33671b0acceSAndi Kleen 		goto out_name;
33771b0acceSAndi Kleen 	return &me->nd;
33871b0acceSAndi Kleen out_name:
339d8f9da24SArnaldo Carvalho de Melo 	zfree(&me->name);
34071b0acceSAndi Kleen out_me:
34171b0acceSAndi Kleen 	free(me);
34271b0acceSAndi Kleen 	return NULL;
34371b0acceSAndi Kleen }
34471b0acceSAndi Kleen 
34571b0acceSAndi Kleen static struct mep *mep_lookup(struct rblist *groups, const char *name)
34671b0acceSAndi Kleen {
34771b0acceSAndi Kleen 	struct rb_node *nd;
34871b0acceSAndi Kleen 	struct mep me = {
34971b0acceSAndi Kleen 		.name = name
35071b0acceSAndi Kleen 	};
35171b0acceSAndi Kleen 	nd = rblist__find(groups, &me);
35271b0acceSAndi Kleen 	if (nd)
35371b0acceSAndi Kleen 		return container_of(nd, struct mep, nd);
35471b0acceSAndi Kleen 	rblist__add_node(groups, &me);
35571b0acceSAndi Kleen 	nd = rblist__find(groups, &me);
35671b0acceSAndi Kleen 	if (nd)
35771b0acceSAndi Kleen 		return container_of(nd, struct mep, nd);
35871b0acceSAndi Kleen 	return NULL;
35971b0acceSAndi Kleen }
36071b0acceSAndi Kleen 
36171b0acceSAndi Kleen static void mep_delete(struct rblist *rl __maybe_unused,
36271b0acceSAndi Kleen 		       struct rb_node *nd)
36371b0acceSAndi Kleen {
36471b0acceSAndi Kleen 	struct mep *me = container_of(nd, struct mep, nd);
36571b0acceSAndi Kleen 
36671b0acceSAndi Kleen 	strlist__delete(me->metrics);
367d8f9da24SArnaldo Carvalho de Melo 	zfree(&me->name);
36871b0acceSAndi Kleen 	free(me);
36971b0acceSAndi Kleen }
37071b0acceSAndi Kleen 
37171b0acceSAndi Kleen static void metricgroup__print_strlist(struct strlist *metrics, bool raw)
37271b0acceSAndi Kleen {
37371b0acceSAndi Kleen 	struct str_node *sn;
37471b0acceSAndi Kleen 	int n = 0;
37571b0acceSAndi Kleen 
37671b0acceSAndi Kleen 	strlist__for_each_entry (sn, metrics) {
37771b0acceSAndi Kleen 		if (raw)
37871b0acceSAndi Kleen 			printf("%s%s", n > 0 ? " " : "", sn->s);
37971b0acceSAndi Kleen 		else
38071b0acceSAndi Kleen 			printf("  %s\n", sn->s);
38171b0acceSAndi Kleen 		n++;
38271b0acceSAndi Kleen 	}
38371b0acceSAndi Kleen 	if (raw)
38471b0acceSAndi Kleen 		putchar('\n');
38571b0acceSAndi Kleen }
38671b0acceSAndi Kleen 
38771b0acceSAndi Kleen void metricgroup__print(bool metrics, bool metricgroups, char *filter,
38833bbc571SJiri Olsa 			bool raw, bool details)
38971b0acceSAndi Kleen {
39054e32dc0SGanapatrao Kulkarni 	struct pmu_events_map *map = perf_pmu__find_map(NULL);
39171b0acceSAndi Kleen 	struct pmu_event *pe;
39271b0acceSAndi Kleen 	int i;
39371b0acceSAndi Kleen 	struct rblist groups;
39471b0acceSAndi Kleen 	struct rb_node *node, *next;
39571b0acceSAndi Kleen 	struct strlist *metriclist = NULL;
39671b0acceSAndi Kleen 
39771b0acceSAndi Kleen 	if (!map)
39871b0acceSAndi Kleen 		return;
39971b0acceSAndi Kleen 
40071b0acceSAndi Kleen 	if (!metricgroups) {
40171b0acceSAndi Kleen 		metriclist = strlist__new(NULL, NULL);
40271b0acceSAndi Kleen 		if (!metriclist)
40371b0acceSAndi Kleen 			return;
40471b0acceSAndi Kleen 	}
40571b0acceSAndi Kleen 
40671b0acceSAndi Kleen 	rblist__init(&groups);
40771b0acceSAndi Kleen 	groups.node_new = mep_new;
40871b0acceSAndi Kleen 	groups.node_cmp = mep_cmp;
40971b0acceSAndi Kleen 	groups.node_delete = mep_delete;
41071b0acceSAndi Kleen 	for (i = 0; ; i++) {
41171b0acceSAndi Kleen 		const char *g;
41271b0acceSAndi Kleen 		pe = &map->table[i];
41371b0acceSAndi Kleen 
41471b0acceSAndi Kleen 		if (!pe->name && !pe->metric_group && !pe->metric_name)
41571b0acceSAndi Kleen 			break;
41671b0acceSAndi Kleen 		if (!pe->metric_expr)
41771b0acceSAndi Kleen 			continue;
41871b0acceSAndi Kleen 		g = pe->metric_group;
41971b0acceSAndi Kleen 		if (!g && pe->metric_name) {
42071b0acceSAndi Kleen 			if (pe->name)
42171b0acceSAndi Kleen 				continue;
42271b0acceSAndi Kleen 			g = "No_group";
42371b0acceSAndi Kleen 		}
42471b0acceSAndi Kleen 		if (g) {
42571b0acceSAndi Kleen 			char *omg;
42671b0acceSAndi Kleen 			char *mg = strdup(g);
42771b0acceSAndi Kleen 
42871b0acceSAndi Kleen 			if (!mg)
42971b0acceSAndi Kleen 				return;
43071b0acceSAndi Kleen 			omg = mg;
43171b0acceSAndi Kleen 			while ((g = strsep(&mg, ";")) != NULL) {
43271b0acceSAndi Kleen 				struct mep *me;
43371b0acceSAndi Kleen 				char *s;
43471b0acceSAndi Kleen 
43580e9073fSArnaldo Carvalho de Melo 				g = skip_spaces(g);
43671b0acceSAndi Kleen 				if (*g == 0)
43771b0acceSAndi Kleen 					g = "No_group";
43871b0acceSAndi Kleen 				if (filter && !strstr(g, filter))
43971b0acceSAndi Kleen 					continue;
44071b0acceSAndi Kleen 				if (raw)
44171b0acceSAndi Kleen 					s = (char *)pe->metric_name;
44271b0acceSAndi Kleen 				else {
44395f04328SMichael Petlan 					if (asprintf(&s, "%s\n%*s%s]",
44495f04328SMichael Petlan 						     pe->metric_name, 8, "[", pe->desc) < 0)
44571b0acceSAndi Kleen 						return;
44633bbc571SJiri Olsa 
44733bbc571SJiri Olsa 					if (details) {
44833bbc571SJiri Olsa 						if (asprintf(&s, "%s\n%*s%s]",
44933bbc571SJiri Olsa 							     s, 8, "[", pe->metric_expr) < 0)
45033bbc571SJiri Olsa 							return;
45133bbc571SJiri Olsa 					}
45271b0acceSAndi Kleen 				}
45371b0acceSAndi Kleen 
45471b0acceSAndi Kleen 				if (!s)
45571b0acceSAndi Kleen 					continue;
45671b0acceSAndi Kleen 
45771b0acceSAndi Kleen 				if (!metricgroups) {
45871b0acceSAndi Kleen 					strlist__add(metriclist, s);
45971b0acceSAndi Kleen 				} else {
46071b0acceSAndi Kleen 					me = mep_lookup(&groups, g);
46171b0acceSAndi Kleen 					if (!me)
46271b0acceSAndi Kleen 						continue;
46371b0acceSAndi Kleen 					strlist__add(me->metrics, s);
46471b0acceSAndi Kleen 				}
46571b0acceSAndi Kleen 			}
46671b0acceSAndi Kleen 			free(omg);
46771b0acceSAndi Kleen 		}
46871b0acceSAndi Kleen 	}
46971b0acceSAndi Kleen 
47071b0acceSAndi Kleen 	if (metricgroups && !raw)
47171b0acceSAndi Kleen 		printf("\nMetric Groups:\n\n");
47271b0acceSAndi Kleen 	else if (metrics && !raw)
47371b0acceSAndi Kleen 		printf("\nMetrics:\n\n");
47471b0acceSAndi Kleen 
475ca227029SDavidlohr Bueso 	for (node = rb_first_cached(&groups.entries); node; node = next) {
47671b0acceSAndi Kleen 		struct mep *me = container_of(node, struct mep, nd);
47771b0acceSAndi Kleen 
47871b0acceSAndi Kleen 		if (metricgroups)
4799c344d15SAndi Kleen 			printf("%s%s%s", me->name, metrics && !raw ? ":" : "", raw ? " " : "\n");
48071b0acceSAndi Kleen 		if (metrics)
48171b0acceSAndi Kleen 			metricgroup__print_strlist(me->metrics, raw);
48271b0acceSAndi Kleen 		next = rb_next(node);
48371b0acceSAndi Kleen 		rblist__remove_node(&groups, node);
48471b0acceSAndi Kleen 	}
48571b0acceSAndi Kleen 	if (!metricgroups)
48671b0acceSAndi Kleen 		metricgroup__print_strlist(metriclist, raw);
48771b0acceSAndi Kleen 	strlist__delete(metriclist);
48871b0acceSAndi Kleen }
48971b0acceSAndi Kleen 
490f742634aSKan Liang static void metricgroup__add_metric_weak_group(struct strbuf *events,
491ded80bdaSIan Rogers 					       struct expr_parse_ctx *ctx)
492f742634aSKan Liang {
493ded80bdaSIan Rogers 	struct hashmap_entry *cur;
4944e21c13aSIan Rogers 	size_t bkt;
4954e21c13aSIan Rogers 	bool no_group = true, has_duration = false;
496f742634aSKan Liang 
497ded80bdaSIan Rogers 	hashmap__for_each_entry((&ctx->ids), cur, bkt) {
498ded80bdaSIan Rogers 		pr_debug("found event %s\n", (const char *)cur->key);
499f742634aSKan Liang 		/*
500f742634aSKan Liang 		 * Duration time maps to a software event and can make
501f742634aSKan Liang 		 * groups not count. Always use it outside a
502f742634aSKan Liang 		 * group.
503f742634aSKan Liang 		 */
504ded80bdaSIan Rogers 		if (!strcmp(cur->key, "duration_time")) {
5054e21c13aSIan Rogers 			has_duration = true;
506f742634aSKan Liang 			continue;
507f742634aSKan Liang 		}
508f742634aSKan Liang 		strbuf_addf(events, "%s%s",
5094e21c13aSIan Rogers 			no_group ? "{" : ",",
510ded80bdaSIan Rogers 			(const char *)cur->key);
511f742634aSKan Liang 		no_group = false;
512f742634aSKan Liang 	}
5134e21c13aSIan Rogers 	if (!no_group) {
514f742634aSKan Liang 		strbuf_addf(events, "}:W");
5154e21c13aSIan Rogers 		if (has_duration)
5164e21c13aSIan Rogers 			strbuf_addf(events, ",duration_time");
5174e21c13aSIan Rogers 	} else if (has_duration)
5184e21c13aSIan Rogers 		strbuf_addf(events, "duration_time");
519f742634aSKan Liang }
520f742634aSKan Liang 
521ab483d8bSKan Liang static void metricgroup__add_metric_non_group(struct strbuf *events,
522ded80bdaSIan Rogers 					      struct expr_parse_ctx *ctx)
523ab483d8bSKan Liang {
524ded80bdaSIan Rogers 	struct hashmap_entry *cur;
525ded80bdaSIan Rogers 	size_t bkt;
526e2ce1059SIan Rogers 	bool first = true;
527ab483d8bSKan Liang 
528e2ce1059SIan Rogers 	hashmap__for_each_entry((&ctx->ids), cur, bkt) {
529e2ce1059SIan Rogers 		if (!first)
530e2ce1059SIan Rogers 			strbuf_addf(events, ",");
531e2ce1059SIan Rogers 		strbuf_addf(events, "%s", (const char *)cur->key);
532e2ce1059SIan Rogers 		first = false;
533e2ce1059SIan Rogers 	}
534ab483d8bSKan Liang }
535ab483d8bSKan Liang 
536ab483d8bSKan Liang static void metricgroup___watchdog_constraint_hint(const char *name, bool foot)
537ab483d8bSKan Liang {
538ab483d8bSKan Liang 	static bool violate_nmi_constraint;
539ab483d8bSKan Liang 
540ab483d8bSKan Liang 	if (!foot) {
541ab483d8bSKan Liang 		pr_warning("Splitting metric group %s into standalone metrics.\n", name);
542ab483d8bSKan Liang 		violate_nmi_constraint = true;
543ab483d8bSKan Liang 		return;
544ab483d8bSKan Liang 	}
545ab483d8bSKan Liang 
546ab483d8bSKan Liang 	if (!violate_nmi_constraint)
547ab483d8bSKan Liang 		return;
548ab483d8bSKan Liang 
549ab483d8bSKan Liang 	pr_warning("Try disabling the NMI watchdog to comply NO_NMI_WATCHDOG metric constraint:\n"
550ab483d8bSKan Liang 		   "    echo 0 > /proc/sys/kernel/nmi_watchdog\n"
551ab483d8bSKan Liang 		   "    perf stat ...\n"
552ab483d8bSKan Liang 		   "    echo 1 > /proc/sys/kernel/nmi_watchdog\n");
553ab483d8bSKan Liang }
554ab483d8bSKan Liang 
555ab483d8bSKan Liang static bool metricgroup__has_constraint(struct pmu_event *pe)
556ab483d8bSKan Liang {
557ab483d8bSKan Liang 	if (!pe->metric_constraint)
558ab483d8bSKan Liang 		return false;
559ab483d8bSKan Liang 
560ab483d8bSKan Liang 	if (!strcmp(pe->metric_constraint, "NO_NMI_WATCHDOG") &&
561ab483d8bSKan Liang 	    sysctl__nmi_watchdog_enabled()) {
562ab483d8bSKan Liang 		metricgroup___watchdog_constraint_hint(pe->metric_name, false);
563ab483d8bSKan Liang 		return true;
564ab483d8bSKan Liang 	}
565ab483d8bSKan Liang 
566ab483d8bSKan Liang 	return false;
567ab483d8bSKan Liang }
568ab483d8bSKan Liang 
5691e1a873dSKajol Jain int __weak arch_get_runtimeparam(void)
5701e1a873dSKajol Jain {
5711e1a873dSKajol Jain 	return 1;
5721e1a873dSKajol Jain }
5731e1a873dSKajol Jain 
574*e7e1baddSJiri Olsa static int __add_metric(struct list_head *group_list,
57505530a79SIan Rogers 			struct pmu_event *pe,
57605530a79SIan Rogers 			bool metric_no_group,
57705530a79SIan Rogers 			int runtime)
57847352abaSKajol Jain {
57947352abaSKajol Jain 	struct egroup *eg;
58047352abaSKajol Jain 
58147352abaSKajol Jain 	eg = malloc(sizeof(*eg));
58247352abaSKajol Jain 	if (!eg)
58347352abaSKajol Jain 		return -ENOMEM;
58447352abaSKajol Jain 
585ded80bdaSIan Rogers 	expr__ctx_init(&eg->pctx);
58647352abaSKajol Jain 	eg->metric_name = pe->metric_name;
58747352abaSKajol Jain 	eg->metric_expr = pe->metric_expr;
58847352abaSKajol Jain 	eg->metric_unit = pe->unit;
5891e1a873dSKajol Jain 	eg->runtime = runtime;
59005530a79SIan Rogers 	eg->has_constraint = metric_no_group || metricgroup__has_constraint(pe);
591ded80bdaSIan Rogers 
592ded80bdaSIan Rogers 	if (expr__find_other(pe->metric_expr, NULL, &eg->pctx, runtime) < 0) {
593ded80bdaSIan Rogers 		expr__ctx_clear(&eg->pctx);
594ded80bdaSIan Rogers 		free(eg);
595ded80bdaSIan Rogers 		return -EINVAL;
596ded80bdaSIan Rogers 	}
597ded80bdaSIan Rogers 
5986bf2102bSIan Rogers 	if (list_empty(group_list))
5996bf2102bSIan Rogers 		list_add(&eg->nd, group_list);
6006bf2102bSIan Rogers 	else {
6016bf2102bSIan Rogers 		struct list_head *pos;
6026bf2102bSIan Rogers 
6036bf2102bSIan Rogers 		/* Place the largest groups at the front. */
6046bf2102bSIan Rogers 		list_for_each_prev(pos, group_list) {
6056bf2102bSIan Rogers 			struct egroup *old = list_entry(pos, struct egroup, nd);
6066bf2102bSIan Rogers 
6076bf2102bSIan Rogers 			if (hashmap__size(&eg->pctx.ids) <=
6086bf2102bSIan Rogers 			    hashmap__size(&old->pctx.ids))
6096bf2102bSIan Rogers 				break;
6106bf2102bSIan Rogers 		}
6116bf2102bSIan Rogers 		list_add(&eg->nd, pos);
6126bf2102bSIan Rogers 	}
61347352abaSKajol Jain 
61447352abaSKajol Jain 	return 0;
61547352abaSKajol Jain }
61647352abaSKajol Jain 
617ce391940SJiri Olsa #define map_for_each_event(__pe, __idx, __map)				\
618ce391940SJiri Olsa 	for (__idx = 0, __pe = &__map->table[__idx];			\
619ce391940SJiri Olsa 	     __pe->name || __pe->metric_group || __pe->metric_name;	\
620ce391940SJiri Olsa 	     __pe = &__map->table[++__idx])
621ce391940SJiri Olsa 
622ce391940SJiri Olsa #define map_for_each_metric(__pe, __idx, __map, __metric)		\
623ce391940SJiri Olsa 	map_for_each_event(__pe, __idx, __map)				\
624ce391940SJiri Olsa 		if (__pe->metric_expr &&				\
625ce391940SJiri Olsa 		    (match_metric(__pe->metric_group, __metric) ||	\
626ce391940SJiri Olsa 		     match_metric(__pe->metric_name, __metric)))
627ce391940SJiri Olsa 
628a29c164aSJiri Olsa static int add_metric(struct list_head *group_list,
629a29c164aSJiri Olsa 		      struct pmu_event *pe,
630a29c164aSJiri Olsa 		      bool metric_no_group)
631a29c164aSJiri Olsa {
632a29c164aSJiri Olsa 	int ret = 0;
633a29c164aSJiri Olsa 
634a29c164aSJiri Olsa 	pr_debug("metric expr %s for %s\n", pe->metric_expr, pe->metric_name);
635a29c164aSJiri Olsa 
636a29c164aSJiri Olsa 	if (!strstr(pe->metric_expr, "?")) {
637*e7e1baddSJiri Olsa 		ret = __add_metric(group_list, pe, metric_no_group, 1);
638a29c164aSJiri Olsa 	} else {
639a29c164aSJiri Olsa 		int j, count;
640a29c164aSJiri Olsa 
641a29c164aSJiri Olsa 		count = arch_get_runtimeparam();
642a29c164aSJiri Olsa 
643a29c164aSJiri Olsa 		/* This loop is added to create multiple
644a29c164aSJiri Olsa 		 * events depend on count value and add
645a29c164aSJiri Olsa 		 * those events to group_list.
646a29c164aSJiri Olsa 		 */
647a29c164aSJiri Olsa 
648a29c164aSJiri Olsa 		for (j = 0; j < count && !ret; j++) {
649*e7e1baddSJiri Olsa 			ret = __add_metric(group_list, pe, metric_no_group, j);
650a29c164aSJiri Olsa 		}
651a29c164aSJiri Olsa 	}
652a29c164aSJiri Olsa 
653a29c164aSJiri Olsa 	return ret;
654a29c164aSJiri Olsa }
655a29c164aSJiri Olsa 
65605530a79SIan Rogers static int metricgroup__add_metric(const char *metric, bool metric_no_group,
65705530a79SIan Rogers 				   struct strbuf *events,
6581381396bSJiri Olsa 				   struct list_head *group_list,
6591381396bSJiri Olsa 				   struct pmu_events_map *map)
660b18f3e36SAndi Kleen {
661b18f3e36SAndi Kleen 	struct pmu_event *pe;
6627f9eca51SIan Rogers 	struct egroup *eg;
66390810399SIan Rogers 	int i, ret;
66490810399SIan Rogers 	bool has_match = false;
665b18f3e36SAndi Kleen 
666ce391940SJiri Olsa 	map_for_each_metric(pe, i, map, metric) {
667ce391940SJiri Olsa 		has_match = true;
668b18f3e36SAndi Kleen 
669a29c164aSJiri Olsa 		ret = add_metric(group_list, pe, metric_no_group);
67090810399SIan Rogers 		if (ret)
67190810399SIan Rogers 			return ret;
67290810399SIan Rogers 	}
673ce391940SJiri Olsa 
674ce391940SJiri Olsa 	/* End of pmu events. */
675ce391940SJiri Olsa 	if (!has_match)
676ce391940SJiri Olsa 		return -EINVAL;
677ce391940SJiri Olsa 
6787f9eca51SIan Rogers 	list_for_each_entry(eg, group_list, nd) {
6797f9eca51SIan Rogers 		if (events->len > 0)
6807f9eca51SIan Rogers 			strbuf_addf(events, ",");
6817f9eca51SIan Rogers 
6827f9eca51SIan Rogers 		if (eg->has_constraint) {
6837f9eca51SIan Rogers 			metricgroup__add_metric_non_group(events,
6847f9eca51SIan Rogers 							  &eg->pctx);
6857f9eca51SIan Rogers 		} else {
6867f9eca51SIan Rogers 			metricgroup__add_metric_weak_group(events,
6877f9eca51SIan Rogers 							   &eg->pctx);
6887f9eca51SIan Rogers 		}
6897f9eca51SIan Rogers 	}
69090810399SIan Rogers 	return 0;
69190810399SIan Rogers }
692b18f3e36SAndi Kleen 
69305530a79SIan Rogers static int metricgroup__add_metric_list(const char *list, bool metric_no_group,
69405530a79SIan Rogers 					struct strbuf *events,
6951381396bSJiri Olsa 					struct list_head *group_list,
6961381396bSJiri Olsa 					struct pmu_events_map *map)
697b18f3e36SAndi Kleen {
698b18f3e36SAndi Kleen 	char *llist, *nlist, *p;
699b18f3e36SAndi Kleen 	int ret = -EINVAL;
700b18f3e36SAndi Kleen 
701b18f3e36SAndi Kleen 	nlist = strdup(list);
702b18f3e36SAndi Kleen 	if (!nlist)
703b18f3e36SAndi Kleen 		return -ENOMEM;
704b18f3e36SAndi Kleen 	llist = nlist;
705411bc316SAndi Kleen 
706411bc316SAndi Kleen 	strbuf_init(events, 100);
707411bc316SAndi Kleen 	strbuf_addf(events, "%s", "");
708411bc316SAndi Kleen 
709b18f3e36SAndi Kleen 	while ((p = strsep(&llist, ",")) != NULL) {
71005530a79SIan Rogers 		ret = metricgroup__add_metric(p, metric_no_group, events,
7111381396bSJiri Olsa 					      group_list, map);
712b18f3e36SAndi Kleen 		if (ret == -EINVAL) {
713b18f3e36SAndi Kleen 			fprintf(stderr, "Cannot find metric or group `%s'\n",
714b18f3e36SAndi Kleen 					p);
715b18f3e36SAndi Kleen 			break;
716b18f3e36SAndi Kleen 		}
717b18f3e36SAndi Kleen 	}
718b18f3e36SAndi Kleen 	free(nlist);
719ab483d8bSKan Liang 
720ab483d8bSKan Liang 	if (!ret)
721ab483d8bSKan Liang 		metricgroup___watchdog_constraint_hint(NULL, true);
722ab483d8bSKan Liang 
723b18f3e36SAndi Kleen 	return ret;
724b18f3e36SAndi Kleen }
725b18f3e36SAndi Kleen 
726b18f3e36SAndi Kleen static void metricgroup__free_egroups(struct list_head *group_list)
727b18f3e36SAndi Kleen {
728b18f3e36SAndi Kleen 	struct egroup *eg, *egtmp;
729b18f3e36SAndi Kleen 
730b18f3e36SAndi Kleen 	list_for_each_entry_safe (eg, egtmp, group_list, nd) {
731ded80bdaSIan Rogers 		expr__ctx_clear(&eg->pctx);
732acc7bfb3SArnaldo Carvalho de Melo 		list_del_init(&eg->nd);
733b18f3e36SAndi Kleen 		free(eg);
734b18f3e36SAndi Kleen 	}
735b18f3e36SAndi Kleen }
736b18f3e36SAndi Kleen 
7378b4468a2SJiri Olsa static int parse_groups(struct evlist *perf_evlist, const char *str,
73805530a79SIan Rogers 			bool metric_no_group,
73905530a79SIan Rogers 			bool metric_no_merge,
74068173bdaSJiri Olsa 			struct perf_pmu *fake_pmu,
7411381396bSJiri Olsa 			struct rblist *metric_events,
7421381396bSJiri Olsa 			struct pmu_events_map *map)
743b18f3e36SAndi Kleen {
744b18f3e36SAndi Kleen 	struct parse_events_error parse_error;
745b18f3e36SAndi Kleen 	struct strbuf extra_events;
746b18f3e36SAndi Kleen 	LIST_HEAD(group_list);
747b18f3e36SAndi Kleen 	int ret;
748b18f3e36SAndi Kleen 
749b18f3e36SAndi Kleen 	if (metric_events->nr_entries == 0)
750b18f3e36SAndi Kleen 		metricgroup__rblist_init(metric_events);
75105530a79SIan Rogers 	ret = metricgroup__add_metric_list(str, metric_no_group,
7521381396bSJiri Olsa 					   &extra_events, &group_list, map);
753b18f3e36SAndi Kleen 	if (ret)
754b18f3e36SAndi Kleen 		return ret;
755b18f3e36SAndi Kleen 	pr_debug("adding %s\n", extra_events.buf);
756a910e466SIan Rogers 	bzero(&parse_error, sizeof(parse_error));
75768173bdaSJiri Olsa 	ret = __parse_events(perf_evlist, extra_events.buf, &parse_error, fake_pmu);
758b18f3e36SAndi Kleen 	if (ret) {
759333b5665SAndi Kleen 		parse_events_print_error(&parse_error, extra_events.buf);
760b18f3e36SAndi Kleen 		goto out;
761b18f3e36SAndi Kleen 	}
762b18f3e36SAndi Kleen 	strbuf_release(&extra_events);
76305530a79SIan Rogers 	ret = metricgroup__setup_events(&group_list, metric_no_merge,
76405530a79SIan Rogers 					perf_evlist, metric_events);
765b18f3e36SAndi Kleen out:
766b18f3e36SAndi Kleen 	metricgroup__free_egroups(&group_list);
767b18f3e36SAndi Kleen 	return ret;
768b18f3e36SAndi Kleen }
769742d92ffSThomas Richter 
7708b4468a2SJiri Olsa int metricgroup__parse_groups(const struct option *opt,
7718b4468a2SJiri Olsa 			      const char *str,
7728b4468a2SJiri Olsa 			      bool metric_no_group,
7738b4468a2SJiri Olsa 			      bool metric_no_merge,
7748b4468a2SJiri Olsa 			      struct rblist *metric_events)
7758b4468a2SJiri Olsa {
7768b4468a2SJiri Olsa 	struct evlist *perf_evlist = *(struct evlist **)opt->value;
7771381396bSJiri Olsa 	struct pmu_events_map *map = perf_pmu__find_map(NULL);
7781381396bSJiri Olsa 
7791381396bSJiri Olsa 	if (!map)
7801381396bSJiri Olsa 		return 0;
7818b4468a2SJiri Olsa 
7828b4468a2SJiri Olsa 	return parse_groups(perf_evlist, str, metric_no_group,
7831381396bSJiri Olsa 			    metric_no_merge, NULL, metric_events, map);
7848b4468a2SJiri Olsa }
7858b4468a2SJiri Olsa 
786f78ac00aSJiri Olsa int metricgroup__parse_groups_test(struct evlist *evlist,
787f78ac00aSJiri Olsa 				   struct pmu_events_map *map,
788f78ac00aSJiri Olsa 				   const char *str,
789f78ac00aSJiri Olsa 				   bool metric_no_group,
790f78ac00aSJiri Olsa 				   bool metric_no_merge,
791f78ac00aSJiri Olsa 				   struct rblist *metric_events)
792f78ac00aSJiri Olsa {
793f78ac00aSJiri Olsa 	return parse_groups(evlist, str, metric_no_group,
794f78ac00aSJiri Olsa 			    metric_no_merge, &perf_pmu__fake, metric_events, map);
795f78ac00aSJiri Olsa }
796f78ac00aSJiri Olsa 
797742d92ffSThomas Richter bool metricgroup__has_metric(const char *metric)
798742d92ffSThomas Richter {
799742d92ffSThomas Richter 	struct pmu_events_map *map = perf_pmu__find_map(NULL);
800742d92ffSThomas Richter 	struct pmu_event *pe;
801742d92ffSThomas Richter 	int i;
802742d92ffSThomas Richter 
803742d92ffSThomas Richter 	if (!map)
804742d92ffSThomas Richter 		return false;
805742d92ffSThomas Richter 
806742d92ffSThomas Richter 	for (i = 0; ; i++) {
807742d92ffSThomas Richter 		pe = &map->table[i];
808742d92ffSThomas Richter 
809742d92ffSThomas Richter 		if (!pe->name && !pe->metric_group && !pe->metric_name)
810742d92ffSThomas Richter 			break;
811742d92ffSThomas Richter 		if (!pe->metric_expr)
812742d92ffSThomas Richter 			continue;
813742d92ffSThomas Richter 		if (match_metric(pe->metric_name, metric))
814742d92ffSThomas Richter 			return true;
815742d92ffSThomas Richter 	}
816742d92ffSThomas Richter 	return false;
817742d92ffSThomas Richter }
818