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"
18 #include "pmu-events/pmu-events.h"
21 #include <linux/ctype.h>
22 #include <linux/string.h>
23 #include <linux/zalloc.h>
24 #include <subcmd/parse-options.h>
26 struct metric_event *metricgroup__lookup(struct rblist *metric_events,
31 struct metric_event me = {
38 nd = rblist__find(metric_events, &me);
40 return container_of(nd, struct metric_event, nd);
42 rblist__add_node(metric_events, &me);
43 nd = rblist__find(metric_events, &me);
45 return container_of(nd, struct metric_event, nd);
50 static int metric_event_cmp(struct rb_node *rb_node, const void *entry)
52 struct metric_event *a = container_of(rb_node,
55 const struct metric_event *b = entry;
57 if (a->evsel == b->evsel)
59 if ((char *)a->evsel < (char *)b->evsel)
64 static struct rb_node *metric_event_new(struct rblist *rblist __maybe_unused,
67 struct metric_event *me = malloc(sizeof(struct metric_event));
71 memcpy(me, entry, sizeof(struct metric_event));
72 me->evsel = ((struct metric_event *)entry)->evsel;
73 INIT_LIST_HEAD(&me->head);
77 static void metricgroup__rblist_init(struct rblist *metric_events)
79 rblist__init(metric_events);
80 metric_events->node_cmp = metric_event_cmp;
81 metric_events->node_new = metric_event_new;
88 const char *metric_name;
89 const char *metric_expr;
90 const char *metric_unit;
93 static struct evsel *find_evsel_group(struct evlist *perf_evlist,
96 struct evsel **metric_events)
102 evlist__for_each_entry (perf_evlist, ev) {
103 if (!strcmp(ev->name, ids[i])) {
104 if (!metric_events[i])
105 metric_events[i] = ev;
108 /* Discard the whole match and start again */
110 memset(metric_events, 0,
111 sizeof(struct evsel *) * idnum);
115 if (!strcmp(ev->name, ids[i]))
116 metric_events[i] = ev;
118 /* Discard the whole match and start again */
120 memset(metric_events, 0,
121 sizeof(struct evsel *) * idnum);
127 if (i != idnum - 1) {
128 /* Not whole match */
132 metric_events[idnum] = NULL;
134 for (i = 0; i < idnum; i++) {
135 leader_found = false;
136 evlist__for_each_entry(perf_evlist, ev) {
137 if (!leader_found && (ev == metric_events[i]))
141 !strcmp(ev->name, metric_events[i]->name)) {
142 ev->metric_leader = metric_events[i];
147 return metric_events[0];
150 static int metricgroup__setup_events(struct list_head *groups,
151 struct evlist *perf_evlist,
152 struct rblist *metric_events_list)
154 struct metric_event *me;
155 struct metric_expr *expr;
161 list_for_each_entry (eg, groups, nd) {
162 struct evsel **metric_events;
164 metric_events = calloc(sizeof(void *), eg->idnum + 1);
165 if (!metric_events) {
169 evsel = find_evsel_group(perf_evlist, eg->ids, eg->idnum,
172 pr_debug("Cannot resolve %s: %s\n",
173 eg->metric_name, eg->metric_expr);
176 for (i = 0; i < eg->idnum; i++)
177 metric_events[i]->collect_stat = true;
178 me = metricgroup__lookup(metric_events_list, evsel, true);
183 expr = malloc(sizeof(struct metric_expr));
188 expr->metric_expr = eg->metric_expr;
189 expr->metric_name = eg->metric_name;
190 expr->metric_unit = eg->metric_unit;
191 expr->metric_events = metric_events;
192 list_add(&expr->nd, &me->head);
197 static bool match_metric(const char *n, const char *list)
204 if (!strcmp(list, "all"))
207 return !strcasecmp(list, "No_group");
209 m = strcasestr(n, list);
212 if ((m == n || m[-1] == ';' || m[-1] == ' ') &&
213 (m[len] == 0 || m[len] == ';'))
221 struct strlist *metrics;
224 static int mep_cmp(struct rb_node *rb_node, const void *entry)
226 struct mep *a = container_of(rb_node, struct mep, nd);
227 struct mep *b = (struct mep *)entry;
229 return strcmp(a->name, b->name);
232 static struct rb_node *mep_new(struct rblist *rl __maybe_unused,
235 struct mep *me = malloc(sizeof(struct mep));
239 memcpy(me, entry, sizeof(struct mep));
240 me->name = strdup(me->name);
243 me->metrics = strlist__new(NULL, NULL);
254 static struct mep *mep_lookup(struct rblist *groups, const char *name)
260 nd = rblist__find(groups, &me);
262 return container_of(nd, struct mep, nd);
263 rblist__add_node(groups, &me);
264 nd = rblist__find(groups, &me);
266 return container_of(nd, struct mep, nd);
270 static void mep_delete(struct rblist *rl __maybe_unused,
273 struct mep *me = container_of(nd, struct mep, nd);
275 strlist__delete(me->metrics);
280 static void metricgroup__print_strlist(struct strlist *metrics, bool raw)
285 strlist__for_each_entry (sn, metrics) {
287 printf("%s%s", n > 0 ? " " : "", sn->s);
289 printf(" %s\n", sn->s);
296 void metricgroup__print(bool metrics, bool metricgroups, char *filter,
297 bool raw, bool details)
299 struct pmu_events_map *map = perf_pmu__find_map(NULL);
300 struct pmu_event *pe;
302 struct rblist groups;
303 struct rb_node *node, *next;
304 struct strlist *metriclist = NULL;
310 metriclist = strlist__new(NULL, NULL);
315 rblist__init(&groups);
316 groups.node_new = mep_new;
317 groups.node_cmp = mep_cmp;
318 groups.node_delete = mep_delete;
323 if (!pe->name && !pe->metric_group && !pe->metric_name)
325 if (!pe->metric_expr)
327 g = pe->metric_group;
328 if (!g && pe->metric_name) {
335 char *mg = strdup(g);
340 while ((g = strsep(&mg, ";")) != NULL) {
347 if (filter && !strstr(g, filter))
350 s = (char *)pe->metric_name;
352 if (asprintf(&s, "%s\n%*s%s]",
353 pe->metric_name, 8, "[", pe->desc) < 0)
357 if (asprintf(&s, "%s\n%*s%s]",
358 s, 8, "[", pe->metric_expr) < 0)
367 strlist__add(metriclist, s);
369 me = mep_lookup(&groups, g);
372 strlist__add(me->metrics, s);
379 if (metricgroups && !raw)
380 printf("\nMetric Groups:\n\n");
381 else if (metrics && !raw)
382 printf("\nMetrics:\n\n");
384 for (node = rb_first_cached(&groups.entries); node; node = next) {
385 struct mep *me = container_of(node, struct mep, nd);
388 printf("%s%s%s", me->name, metrics && !raw ? ":" : "", raw ? " " : "\n");
390 metricgroup__print_strlist(me->metrics, raw);
391 next = rb_next(node);
392 rblist__remove_node(&groups, node);
395 metricgroup__print_strlist(metriclist, raw);
396 strlist__delete(metriclist);
399 static int metricgroup__add_metric(const char *metric, struct strbuf *events,
400 struct list_head *group_list)
402 struct pmu_events_map *map = perf_pmu__find_map(NULL);
403 struct pmu_event *pe;
413 if (!pe->name && !pe->metric_group && !pe->metric_name)
415 if (!pe->metric_expr)
417 if (match_metric(pe->metric_group, metric) ||
418 match_metric(pe->metric_name, metric)) {
422 bool no_group = false;
424 pr_debug("metric expr %s for %s\n", pe->metric_expr, pe->metric_name);
426 if (expr__find_other(pe->metric_expr,
427 NULL, &ids, &idnum) < 0)
430 strbuf_addf(events, ",");
431 for (j = 0; j < idnum; j++) {
432 pr_debug("found event %s\n", ids[j]);
434 * Duration time maps to a software event and can make
435 * groups not count. Always use it outside a
438 if (!strcmp(ids[j], "duration_time")) {
440 strbuf_addf(events, "}:W,");
441 strbuf_addf(events, "duration_time");
445 strbuf_addf(events, "%s%s",
446 j == 0 || no_group ? "{" : ",",
451 strbuf_addf(events, "}:W");
453 eg = malloc(sizeof(struct egroup));
460 eg->metric_name = pe->metric_name;
461 eg->metric_expr = pe->metric_expr;
462 eg->metric_unit = pe->unit;
463 list_add_tail(&eg->nd, group_list);
470 static int metricgroup__add_metric_list(const char *list, struct strbuf *events,
471 struct list_head *group_list)
473 char *llist, *nlist, *p;
476 nlist = strdup(list);
481 strbuf_init(events, 100);
482 strbuf_addf(events, "%s", "");
484 while ((p = strsep(&llist, ",")) != NULL) {
485 ret = metricgroup__add_metric(p, events, group_list);
486 if (ret == -EINVAL) {
487 fprintf(stderr, "Cannot find metric or group `%s'\n",
496 static void metricgroup__free_egroups(struct list_head *group_list)
498 struct egroup *eg, *egtmp;
501 list_for_each_entry_safe (eg, egtmp, group_list, nd) {
502 for (i = 0; i < eg->idnum; i++)
505 list_del_init(&eg->nd);
510 int metricgroup__parse_groups(const struct option *opt,
512 struct rblist *metric_events)
514 struct parse_events_error parse_error;
515 struct evlist *perf_evlist = *(struct evlist **)opt->value;
516 struct strbuf extra_events;
517 LIST_HEAD(group_list);
520 if (metric_events->nr_entries == 0)
521 metricgroup__rblist_init(metric_events);
522 ret = metricgroup__add_metric_list(str, &extra_events, &group_list);
525 pr_debug("adding %s\n", extra_events.buf);
526 memset(&parse_error, 0, sizeof(struct parse_events_error));
527 ret = parse_events(perf_evlist, extra_events.buf, &parse_error);
529 parse_events_print_error(&parse_error, extra_events.buf);
532 strbuf_release(&extra_events);
533 ret = metricgroup__setup_events(&group_list, perf_evlist,
536 metricgroup__free_egroups(&group_list);
540 bool metricgroup__has_metric(const char *metric)
542 struct pmu_events_map *map = perf_pmu__find_map(NULL);
543 struct pmu_event *pe;
552 if (!pe->name && !pe->metric_group && !pe->metric_name)
554 if (!pe->metric_expr)
556 if (match_metric(pe->metric_name, metric))