2 * Copyright (c) 2017, Intel Corporation.
4 * This program is free software; you can redistribute it and/or modify it
5 * under the terms and conditions of the GNU General Public License,
6 * version 2, as published by the Free Software Foundation.
8 * This program is distributed in the hope it will be useful, but WITHOUT
9 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
10 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
15 /* Manage metrics and groups of metrics from JSON files */
17 #include "metricgroup.h"
26 #include "pmu-events/pmu-events.h"
31 struct metric_event *metricgroup__lookup(struct rblist *metric_events,
32 struct perf_evsel *evsel,
36 struct metric_event me = {
43 nd = rblist__find(metric_events, &me);
45 return container_of(nd, struct metric_event, nd);
47 rblist__add_node(metric_events, &me);
48 nd = rblist__find(metric_events, &me);
50 return container_of(nd, struct metric_event, nd);
55 static int metric_event_cmp(struct rb_node *rb_node, const void *entry)
57 struct metric_event *a = container_of(rb_node,
60 const struct metric_event *b = entry;
62 if (a->evsel == b->evsel)
64 if ((char *)a->evsel < (char *)b->evsel)
69 static struct rb_node *metric_event_new(struct rblist *rblist __maybe_unused,
72 struct metric_event *me = malloc(sizeof(struct metric_event));
76 memcpy(me, entry, sizeof(struct metric_event));
77 me->evsel = ((struct metric_event *)entry)->evsel;
78 INIT_LIST_HEAD(&me->head);
82 static void metricgroup__rblist_init(struct rblist *metric_events)
84 rblist__init(metric_events);
85 metric_events->node_cmp = metric_event_cmp;
86 metric_events->node_new = metric_event_new;
93 const char *metric_name;
94 const char *metric_expr;
97 static struct perf_evsel *find_evsel(struct perf_evlist *perf_evlist,
100 struct perf_evsel **metric_events)
102 struct perf_evsel *ev, *start = NULL;
105 evlist__for_each_entry (perf_evlist, ev) {
106 if (!strcmp(ev->name, ids[ind])) {
107 metric_events[ind] = ev;
110 if (++ind == idnum) {
111 metric_events[ind] = NULL;
120 * This can happen when an alias expands to multiple
121 * events, like for uncore events.
122 * We don't support this case for now.
127 static int metricgroup__setup_events(struct list_head *groups,
128 struct perf_evlist *perf_evlist,
129 struct rblist *metric_events_list)
131 struct metric_event *me;
132 struct metric_expr *expr;
136 struct perf_evsel *evsel;
138 list_for_each_entry (eg, groups, nd) {
139 struct perf_evsel **metric_events;
141 metric_events = calloc(sizeof(void *), eg->idnum + 1);
142 if (!metric_events) {
146 evsel = find_evsel(perf_evlist, eg->ids, eg->idnum,
149 pr_debug("Cannot resolve %s: %s\n",
150 eg->metric_name, eg->metric_expr);
153 for (i = 0; i < eg->idnum; i++)
154 metric_events[i]->collect_stat = true;
155 me = metricgroup__lookup(metric_events_list, evsel, true);
160 expr = malloc(sizeof(struct metric_expr));
165 expr->metric_expr = eg->metric_expr;
166 expr->metric_name = eg->metric_name;
167 expr->metric_events = metric_events;
168 list_add(&expr->nd, &me->head);
173 static bool match_metric(const char *n, const char *list)
180 if (!strcmp(list, "all"))
183 return !strcasecmp(list, "No_group");
185 m = strcasestr(n, list);
188 if ((m == n || m[-1] == ';' || m[-1] == ' ') &&
189 (m[len] == 0 || m[len] == ';'))
197 struct strlist *metrics;
200 static int mep_cmp(struct rb_node *rb_node, const void *entry)
202 struct mep *a = container_of(rb_node, struct mep, nd);
203 struct mep *b = (struct mep *)entry;
205 return strcmp(a->name, b->name);
208 static struct rb_node *mep_new(struct rblist *rl __maybe_unused,
211 struct mep *me = malloc(sizeof(struct mep));
215 memcpy(me, entry, sizeof(struct mep));
216 me->name = strdup(me->name);
219 me->metrics = strlist__new(NULL, NULL);
224 free((char *)me->name);
230 static struct mep *mep_lookup(struct rblist *groups, const char *name)
236 nd = rblist__find(groups, &me);
238 return container_of(nd, struct mep, nd);
239 rblist__add_node(groups, &me);
240 nd = rblist__find(groups, &me);
242 return container_of(nd, struct mep, nd);
246 static void mep_delete(struct rblist *rl __maybe_unused,
249 struct mep *me = container_of(nd, struct mep, nd);
251 strlist__delete(me->metrics);
252 free((void *)me->name);
256 static void metricgroup__print_strlist(struct strlist *metrics, bool raw)
261 strlist__for_each_entry (sn, metrics) {
263 printf("%s%s", n > 0 ? " " : "", sn->s);
265 printf(" %s\n", sn->s);
272 void metricgroup__print(bool metrics, bool metricgroups, char *filter,
275 struct pmu_events_map *map = perf_pmu__find_map(NULL);
276 struct pmu_event *pe;
278 struct rblist groups;
279 struct rb_node *node, *next;
280 struct strlist *metriclist = NULL;
286 metriclist = strlist__new(NULL, NULL);
291 rblist__init(&groups);
292 groups.node_new = mep_new;
293 groups.node_cmp = mep_cmp;
294 groups.node_delete = mep_delete;
299 if (!pe->name && !pe->metric_group && !pe->metric_name)
301 if (!pe->metric_expr)
303 g = pe->metric_group;
304 if (!g && pe->metric_name) {
311 char *mg = strdup(g);
316 while ((g = strsep(&mg, ";")) != NULL) {
324 if (filter && !strstr(g, filter))
327 s = (char *)pe->metric_name;
329 if (asprintf(&s, "%s\n%*s%s]",
330 pe->metric_name, 8, "[", pe->desc) < 0)
338 strlist__add(metriclist, s);
340 me = mep_lookup(&groups, g);
343 strlist__add(me->metrics, s);
350 if (metricgroups && !raw)
351 printf("\nMetric Groups:\n\n");
352 else if (metrics && !raw)
353 printf("\nMetrics:\n\n");
355 for (node = rb_first_cached(&groups.entries); node; node = next) {
356 struct mep *me = container_of(node, struct mep, nd);
359 printf("%s%s%s", me->name, metrics ? ":" : "", raw ? " " : "\n");
361 metricgroup__print_strlist(me->metrics, raw);
362 next = rb_next(node);
363 rblist__remove_node(&groups, node);
366 metricgroup__print_strlist(metriclist, raw);
367 strlist__delete(metriclist);
370 static int metricgroup__add_metric(const char *metric, struct strbuf *events,
371 struct list_head *group_list)
373 struct pmu_events_map *map = perf_pmu__find_map(NULL);
374 struct pmu_event *pe;
384 if (!pe->name && !pe->metric_group && !pe->metric_name)
386 if (!pe->metric_expr)
388 if (match_metric(pe->metric_group, metric) ||
389 match_metric(pe->metric_name, metric)) {
394 pr_debug("metric expr %s for %s\n", pe->metric_expr, pe->metric_name);
396 if (expr__find_other(pe->metric_expr,
397 NULL, &ids, &idnum) < 0)
400 strbuf_addf(events, ",");
401 for (j = 0; j < idnum; j++) {
402 pr_debug("found event %s\n", ids[j]);
403 strbuf_addf(events, "%s%s",
407 strbuf_addf(events, "}:W");
409 eg = malloc(sizeof(struct egroup));
416 eg->metric_name = pe->metric_name;
417 eg->metric_expr = pe->metric_expr;
418 list_add_tail(&eg->nd, group_list);
425 static int metricgroup__add_metric_list(const char *list, struct strbuf *events,
426 struct list_head *group_list)
428 char *llist, *nlist, *p;
431 nlist = strdup(list);
436 strbuf_init(events, 100);
437 strbuf_addf(events, "%s", "");
439 while ((p = strsep(&llist, ",")) != NULL) {
440 ret = metricgroup__add_metric(p, events, group_list);
441 if (ret == -EINVAL) {
442 fprintf(stderr, "Cannot find metric or group `%s'\n",
451 static void metricgroup__free_egroups(struct list_head *group_list)
453 struct egroup *eg, *egtmp;
456 list_for_each_entry_safe (eg, egtmp, group_list, nd) {
457 for (i = 0; i < eg->idnum; i++)
458 free((char *)eg->ids[i]);
464 int metricgroup__parse_groups(const struct option *opt,
466 struct rblist *metric_events)
468 struct parse_events_error parse_error;
469 struct perf_evlist *perf_evlist = *(struct perf_evlist **)opt->value;
470 struct strbuf extra_events;
471 LIST_HEAD(group_list);
474 if (metric_events->nr_entries == 0)
475 metricgroup__rblist_init(metric_events);
476 ret = metricgroup__add_metric_list(str, &extra_events, &group_list);
479 pr_debug("adding %s\n", extra_events.buf);
480 memset(&parse_error, 0, sizeof(struct parse_events_error));
481 ret = parse_events(perf_evlist, extra_events.buf, &parse_error);
483 parse_events_print_error(&parse_error, extra_events.buf);
486 strbuf_release(&extra_events);
487 ret = metricgroup__setup_events(&group_list, perf_evlist,
490 metricgroup__free_egroups(&group_list);
494 bool metricgroup__has_metric(const char *metric)
496 struct pmu_events_map *map = perf_pmu__find_map(NULL);
497 struct pmu_event *pe;
506 if (!pe->name && !pe->metric_group && !pe->metric_name)
508 if (!pe->metric_expr)
510 if (match_metric(pe->metric_name, metric))