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>
21 #include <linux/zalloc.h>
23 struct metric_event *metricgroup__lookup(struct rblist *metric_events,
28 struct metric_event me = {
35 nd = rblist__find(metric_events, &me);
37 return container_of(nd, struct metric_event, nd);
39 rblist__add_node(metric_events, &me);
40 nd = rblist__find(metric_events, &me);
42 return container_of(nd, struct metric_event, nd);
47 static int metric_event_cmp(struct rb_node *rb_node, const void *entry)
49 struct metric_event *a = container_of(rb_node,
52 const struct metric_event *b = entry;
54 if (a->evsel == b->evsel)
56 if ((char *)a->evsel < (char *)b->evsel)
61 static struct rb_node *metric_event_new(struct rblist *rblist __maybe_unused,
64 struct metric_event *me = malloc(sizeof(struct metric_event));
68 memcpy(me, entry, sizeof(struct metric_event));
69 me->evsel = ((struct metric_event *)entry)->evsel;
70 INIT_LIST_HEAD(&me->head);
74 static void metricgroup__rblist_init(struct rblist *metric_events)
76 rblist__init(metric_events);
77 metric_events->node_cmp = metric_event_cmp;
78 metric_events->node_new = metric_event_new;
85 const char *metric_name;
86 const char *metric_expr;
89 static bool record_evsel(int *ind, struct evsel **start,
91 struct evsel **metric_events,
94 metric_events[*ind] = ev;
97 if (++*ind == idnum) {
98 metric_events[*ind] = NULL;
104 static struct evsel *find_evsel_group(struct perf_evlist *perf_evlist,
107 struct evsel **metric_events)
109 struct evsel *ev, *start = NULL;
112 evlist__for_each_entry (perf_evlist, ev) {
113 if (ev->collect_stat)
115 if (!strcmp(ev->name, ids[ind])) {
116 if (record_evsel(&ind, &start, idnum,
121 * We saw some other event that is not
122 * in our list of events. Discard
123 * the whole match and start again.
127 if (!strcmp(ev->name, ids[ind])) {
128 if (record_evsel(&ind, &start, idnum,
135 * This can happen when an alias expands to multiple
136 * events, like for uncore events.
137 * We don't support this case for now.
142 static int metricgroup__setup_events(struct list_head *groups,
143 struct perf_evlist *perf_evlist,
144 struct rblist *metric_events_list)
146 struct metric_event *me;
147 struct metric_expr *expr;
153 list_for_each_entry (eg, groups, nd) {
154 struct evsel **metric_events;
156 metric_events = calloc(sizeof(void *), eg->idnum + 1);
157 if (!metric_events) {
161 evsel = find_evsel_group(perf_evlist, eg->ids, eg->idnum,
164 pr_debug("Cannot resolve %s: %s\n",
165 eg->metric_name, eg->metric_expr);
168 for (i = 0; i < eg->idnum; i++)
169 metric_events[i]->collect_stat = true;
170 me = metricgroup__lookup(metric_events_list, evsel, true);
175 expr = malloc(sizeof(struct metric_expr));
180 expr->metric_expr = eg->metric_expr;
181 expr->metric_name = eg->metric_name;
182 expr->metric_events = metric_events;
183 list_add(&expr->nd, &me->head);
188 static bool match_metric(const char *n, const char *list)
195 if (!strcmp(list, "all"))
198 return !strcasecmp(list, "No_group");
200 m = strcasestr(n, list);
203 if ((m == n || m[-1] == ';' || m[-1] == ' ') &&
204 (m[len] == 0 || m[len] == ';'))
212 struct strlist *metrics;
215 static int mep_cmp(struct rb_node *rb_node, const void *entry)
217 struct mep *a = container_of(rb_node, struct mep, nd);
218 struct mep *b = (struct mep *)entry;
220 return strcmp(a->name, b->name);
223 static struct rb_node *mep_new(struct rblist *rl __maybe_unused,
226 struct mep *me = malloc(sizeof(struct mep));
230 memcpy(me, entry, sizeof(struct mep));
231 me->name = strdup(me->name);
234 me->metrics = strlist__new(NULL, NULL);
245 static struct mep *mep_lookup(struct rblist *groups, const char *name)
251 nd = rblist__find(groups, &me);
253 return container_of(nd, struct mep, nd);
254 rblist__add_node(groups, &me);
255 nd = rblist__find(groups, &me);
257 return container_of(nd, struct mep, nd);
261 static void mep_delete(struct rblist *rl __maybe_unused,
264 struct mep *me = container_of(nd, struct mep, nd);
266 strlist__delete(me->metrics);
271 static void metricgroup__print_strlist(struct strlist *metrics, bool raw)
276 strlist__for_each_entry (sn, metrics) {
278 printf("%s%s", n > 0 ? " " : "", sn->s);
280 printf(" %s\n", sn->s);
287 void metricgroup__print(bool metrics, bool metricgroups, char *filter,
288 bool raw, bool details)
290 struct pmu_events_map *map = perf_pmu__find_map(NULL);
291 struct pmu_event *pe;
293 struct rblist groups;
294 struct rb_node *node, *next;
295 struct strlist *metriclist = NULL;
301 metriclist = strlist__new(NULL, NULL);
306 rblist__init(&groups);
307 groups.node_new = mep_new;
308 groups.node_cmp = mep_cmp;
309 groups.node_delete = mep_delete;
314 if (!pe->name && !pe->metric_group && !pe->metric_name)
316 if (!pe->metric_expr)
318 g = pe->metric_group;
319 if (!g && pe->metric_name) {
326 char *mg = strdup(g);
331 while ((g = strsep(&mg, ";")) != NULL) {
338 if (filter && !strstr(g, filter))
341 s = (char *)pe->metric_name;
343 if (asprintf(&s, "%s\n%*s%s]",
344 pe->metric_name, 8, "[", pe->desc) < 0)
348 if (asprintf(&s, "%s\n%*s%s]",
349 s, 8, "[", pe->metric_expr) < 0)
358 strlist__add(metriclist, s);
360 me = mep_lookup(&groups, g);
363 strlist__add(me->metrics, s);
370 if (metricgroups && !raw)
371 printf("\nMetric Groups:\n\n");
372 else if (metrics && !raw)
373 printf("\nMetrics:\n\n");
375 for (node = rb_first_cached(&groups.entries); node; node = next) {
376 struct mep *me = container_of(node, struct mep, nd);
379 printf("%s%s%s", me->name, metrics && !raw ? ":" : "", raw ? " " : "\n");
381 metricgroup__print_strlist(me->metrics, raw);
382 next = rb_next(node);
383 rblist__remove_node(&groups, node);
386 metricgroup__print_strlist(metriclist, raw);
387 strlist__delete(metriclist);
390 static int metricgroup__add_metric(const char *metric, struct strbuf *events,
391 struct list_head *group_list)
393 struct pmu_events_map *map = perf_pmu__find_map(NULL);
394 struct pmu_event *pe;
404 if (!pe->name && !pe->metric_group && !pe->metric_name)
406 if (!pe->metric_expr)
408 if (match_metric(pe->metric_group, metric) ||
409 match_metric(pe->metric_name, metric)) {
413 bool no_group = false;
415 pr_debug("metric expr %s for %s\n", pe->metric_expr, pe->metric_name);
417 if (expr__find_other(pe->metric_expr,
418 NULL, &ids, &idnum) < 0)
421 strbuf_addf(events, ",");
422 for (j = 0; j < idnum; j++) {
423 pr_debug("found event %s\n", ids[j]);
425 * Duration time maps to a software event and can make
426 * groups not count. Always use it outside a
429 if (!strcmp(ids[j], "duration_time")) {
431 strbuf_addf(events, "}:W,");
432 strbuf_addf(events, "duration_time");
436 strbuf_addf(events, "%s%s",
437 j == 0 || no_group ? "{" : ",",
442 strbuf_addf(events, "}:W");
444 eg = malloc(sizeof(struct egroup));
451 eg->metric_name = pe->metric_name;
452 eg->metric_expr = pe->metric_expr;
453 list_add_tail(&eg->nd, group_list);
460 static int metricgroup__add_metric_list(const char *list, struct strbuf *events,
461 struct list_head *group_list)
463 char *llist, *nlist, *p;
466 nlist = strdup(list);
471 strbuf_init(events, 100);
472 strbuf_addf(events, "%s", "");
474 while ((p = strsep(&llist, ",")) != NULL) {
475 ret = metricgroup__add_metric(p, events, group_list);
476 if (ret == -EINVAL) {
477 fprintf(stderr, "Cannot find metric or group `%s'\n",
486 static void metricgroup__free_egroups(struct list_head *group_list)
488 struct egroup *eg, *egtmp;
491 list_for_each_entry_safe (eg, egtmp, group_list, nd) {
492 for (i = 0; i < eg->idnum; i++)
495 list_del_init(&eg->nd);
500 int metricgroup__parse_groups(const struct option *opt,
502 struct rblist *metric_events)
504 struct parse_events_error parse_error;
505 struct perf_evlist *perf_evlist = *(struct perf_evlist **)opt->value;
506 struct strbuf extra_events;
507 LIST_HEAD(group_list);
510 if (metric_events->nr_entries == 0)
511 metricgroup__rblist_init(metric_events);
512 ret = metricgroup__add_metric_list(str, &extra_events, &group_list);
515 pr_debug("adding %s\n", extra_events.buf);
516 memset(&parse_error, 0, sizeof(struct parse_events_error));
517 ret = parse_events(perf_evlist, extra_events.buf, &parse_error);
519 parse_events_print_error(&parse_error, extra_events.buf);
522 strbuf_release(&extra_events);
523 ret = metricgroup__setup_events(&group_list, perf_evlist,
526 metricgroup__free_egroups(&group_list);
530 bool metricgroup__has_metric(const char *metric)
532 struct pmu_events_map *map = perf_pmu__find_map(NULL);
533 struct pmu_event *pe;
542 if (!pe->name && !pe->metric_group && !pe->metric_name)
544 if (!pe->metric_expr)
546 if (match_metric(pe->metric_name, metric))