1 // SPDX-License-Identifier: GPL-2.0-only
3 * Copyright (c) 2017, Intel Corporation.
6 /* Manage metrics and groups of metrics from JSON files */
8 #include "metricgroup.h"
17 #include "pmu-events/pmu-events.h"
20 #include <linux/ctype.h>
22 struct metric_event *metricgroup__lookup(struct rblist *metric_events,
23 struct perf_evsel *evsel,
27 struct metric_event me = {
34 nd = rblist__find(metric_events, &me);
36 return container_of(nd, struct metric_event, nd);
38 rblist__add_node(metric_events, &me);
39 nd = rblist__find(metric_events, &me);
41 return container_of(nd, struct metric_event, nd);
46 static int metric_event_cmp(struct rb_node *rb_node, const void *entry)
48 struct metric_event *a = container_of(rb_node,
51 const struct metric_event *b = entry;
53 if (a->evsel == b->evsel)
55 if ((char *)a->evsel < (char *)b->evsel)
60 static struct rb_node *metric_event_new(struct rblist *rblist __maybe_unused,
63 struct metric_event *me = malloc(sizeof(struct metric_event));
67 memcpy(me, entry, sizeof(struct metric_event));
68 me->evsel = ((struct metric_event *)entry)->evsel;
69 INIT_LIST_HEAD(&me->head);
73 static void metricgroup__rblist_init(struct rblist *metric_events)
75 rblist__init(metric_events);
76 metric_events->node_cmp = metric_event_cmp;
77 metric_events->node_new = metric_event_new;
84 const char *metric_name;
85 const char *metric_expr;
88 static bool record_evsel(int *ind, struct perf_evsel **start,
90 struct perf_evsel **metric_events,
91 struct perf_evsel *ev)
93 metric_events[*ind] = ev;
96 if (++*ind == idnum) {
97 metric_events[*ind] = NULL;
103 static struct perf_evsel *find_evsel_group(struct perf_evlist *perf_evlist,
106 struct perf_evsel **metric_events)
108 struct perf_evsel *ev, *start = NULL;
111 evlist__for_each_entry (perf_evlist, ev) {
112 if (ev->collect_stat)
114 if (!strcmp(ev->name, ids[ind])) {
115 if (record_evsel(&ind, &start, idnum,
120 * We saw some other event that is not
121 * in our list of events. Discard
122 * the whole match and start again.
126 if (!strcmp(ev->name, ids[ind])) {
127 if (record_evsel(&ind, &start, idnum,
134 * This can happen when an alias expands to multiple
135 * events, like for uncore events.
136 * We don't support this case for now.
141 static int metricgroup__setup_events(struct list_head *groups,
142 struct perf_evlist *perf_evlist,
143 struct rblist *metric_events_list)
145 struct metric_event *me;
146 struct metric_expr *expr;
150 struct perf_evsel *evsel;
152 list_for_each_entry (eg, groups, nd) {
153 struct perf_evsel **metric_events;
155 metric_events = calloc(sizeof(void *), eg->idnum + 1);
156 if (!metric_events) {
160 evsel = find_evsel_group(perf_evlist, eg->ids, eg->idnum,
163 pr_debug("Cannot resolve %s: %s\n",
164 eg->metric_name, eg->metric_expr);
167 for (i = 0; i < eg->idnum; i++)
168 metric_events[i]->collect_stat = true;
169 me = metricgroup__lookup(metric_events_list, evsel, true);
174 expr = malloc(sizeof(struct metric_expr));
179 expr->metric_expr = eg->metric_expr;
180 expr->metric_name = eg->metric_name;
181 expr->metric_events = metric_events;
182 list_add(&expr->nd, &me->head);
187 static bool match_metric(const char *n, const char *list)
194 if (!strcmp(list, "all"))
197 return !strcasecmp(list, "No_group");
199 m = strcasestr(n, list);
202 if ((m == n || m[-1] == ';' || m[-1] == ' ') &&
203 (m[len] == 0 || m[len] == ';'))
211 struct strlist *metrics;
214 static int mep_cmp(struct rb_node *rb_node, const void *entry)
216 struct mep *a = container_of(rb_node, struct mep, nd);
217 struct mep *b = (struct mep *)entry;
219 return strcmp(a->name, b->name);
222 static struct rb_node *mep_new(struct rblist *rl __maybe_unused,
225 struct mep *me = malloc(sizeof(struct mep));
229 memcpy(me, entry, sizeof(struct mep));
230 me->name = strdup(me->name);
233 me->metrics = strlist__new(NULL, NULL);
238 free((char *)me->name);
244 static struct mep *mep_lookup(struct rblist *groups, const char *name)
250 nd = rblist__find(groups, &me);
252 return container_of(nd, struct mep, nd);
253 rblist__add_node(groups, &me);
254 nd = rblist__find(groups, &me);
256 return container_of(nd, struct mep, nd);
260 static void mep_delete(struct rblist *rl __maybe_unused,
263 struct mep *me = container_of(nd, struct mep, nd);
265 strlist__delete(me->metrics);
266 free((void *)me->name);
270 static void metricgroup__print_strlist(struct strlist *metrics, bool raw)
275 strlist__for_each_entry (sn, metrics) {
277 printf("%s%s", n > 0 ? " " : "", sn->s);
279 printf(" %s\n", sn->s);
286 void metricgroup__print(bool metrics, bool metricgroups, char *filter,
287 bool raw, bool details)
289 struct pmu_events_map *map = perf_pmu__find_map(NULL);
290 struct pmu_event *pe;
292 struct rblist groups;
293 struct rb_node *node, *next;
294 struct strlist *metriclist = NULL;
300 metriclist = strlist__new(NULL, NULL);
305 rblist__init(&groups);
306 groups.node_new = mep_new;
307 groups.node_cmp = mep_cmp;
308 groups.node_delete = mep_delete;
313 if (!pe->name && !pe->metric_group && !pe->metric_name)
315 if (!pe->metric_expr)
317 g = pe->metric_group;
318 if (!g && pe->metric_name) {
325 char *mg = strdup(g);
330 while ((g = strsep(&mg, ";")) != NULL) {
337 if (filter && !strstr(g, filter))
340 s = (char *)pe->metric_name;
342 if (asprintf(&s, "%s\n%*s%s]",
343 pe->metric_name, 8, "[", pe->desc) < 0)
347 if (asprintf(&s, "%s\n%*s%s]",
348 s, 8, "[", pe->metric_expr) < 0)
357 strlist__add(metriclist, s);
359 me = mep_lookup(&groups, g);
362 strlist__add(me->metrics, s);
369 if (metricgroups && !raw)
370 printf("\nMetric Groups:\n\n");
371 else if (metrics && !raw)
372 printf("\nMetrics:\n\n");
374 for (node = rb_first_cached(&groups.entries); node; node = next) {
375 struct mep *me = container_of(node, struct mep, nd);
378 printf("%s%s%s", me->name, metrics && !raw ? ":" : "", raw ? " " : "\n");
380 metricgroup__print_strlist(me->metrics, raw);
381 next = rb_next(node);
382 rblist__remove_node(&groups, node);
385 metricgroup__print_strlist(metriclist, raw);
386 strlist__delete(metriclist);
389 static int metricgroup__add_metric(const char *metric, struct strbuf *events,
390 struct list_head *group_list)
392 struct pmu_events_map *map = perf_pmu__find_map(NULL);
393 struct pmu_event *pe;
403 if (!pe->name && !pe->metric_group && !pe->metric_name)
405 if (!pe->metric_expr)
407 if (match_metric(pe->metric_group, metric) ||
408 match_metric(pe->metric_name, metric)) {
412 bool no_group = false;
414 pr_debug("metric expr %s for %s\n", pe->metric_expr, pe->metric_name);
416 if (expr__find_other(pe->metric_expr,
417 NULL, &ids, &idnum) < 0)
420 strbuf_addf(events, ",");
421 for (j = 0; j < idnum; j++) {
422 pr_debug("found event %s\n", ids[j]);
424 * Duration time maps to a software event and can make
425 * groups not count. Always use it outside a
428 if (!strcmp(ids[j], "duration_time")) {
430 strbuf_addf(events, "}:W,");
431 strbuf_addf(events, "duration_time");
435 strbuf_addf(events, "%s%s",
436 j == 0 || no_group ? "{" : ",",
441 strbuf_addf(events, "}:W");
443 eg = malloc(sizeof(struct egroup));
450 eg->metric_name = pe->metric_name;
451 eg->metric_expr = pe->metric_expr;
452 list_add_tail(&eg->nd, group_list);
459 static int metricgroup__add_metric_list(const char *list, struct strbuf *events,
460 struct list_head *group_list)
462 char *llist, *nlist, *p;
465 nlist = strdup(list);
470 strbuf_init(events, 100);
471 strbuf_addf(events, "%s", "");
473 while ((p = strsep(&llist, ",")) != NULL) {
474 ret = metricgroup__add_metric(p, events, group_list);
475 if (ret == -EINVAL) {
476 fprintf(stderr, "Cannot find metric or group `%s'\n",
485 static void metricgroup__free_egroups(struct list_head *group_list)
487 struct egroup *eg, *egtmp;
490 list_for_each_entry_safe (eg, egtmp, group_list, nd) {
491 for (i = 0; i < eg->idnum; i++)
492 free((char *)eg->ids[i]);
498 int metricgroup__parse_groups(const struct option *opt,
500 struct rblist *metric_events)
502 struct parse_events_error parse_error;
503 struct perf_evlist *perf_evlist = *(struct perf_evlist **)opt->value;
504 struct strbuf extra_events;
505 LIST_HEAD(group_list);
508 if (metric_events->nr_entries == 0)
509 metricgroup__rblist_init(metric_events);
510 ret = metricgroup__add_metric_list(str, &extra_events, &group_list);
513 pr_debug("adding %s\n", extra_events.buf);
514 memset(&parse_error, 0, sizeof(struct parse_events_error));
515 ret = parse_events(perf_evlist, extra_events.buf, &parse_error);
517 parse_events_print_error(&parse_error, extra_events.buf);
520 strbuf_release(&extra_events);
521 ret = metricgroup__setup_events(&group_list, perf_evlist,
524 metricgroup__free_egroups(&group_list);
528 bool metricgroup__has_metric(const char *metric)
530 struct pmu_events_map *map = perf_pmu__find_map(NULL);
531 struct pmu_event *pe;
540 if (!pe->name && !pe->metric_group && !pe->metric_name)
542 if (!pe->metric_expr)
544 if (match_metric(pe->metric_name, metric))