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 bool record_evsel(int *ind, struct perf_evsel **start,
99 struct perf_evsel **metric_events,
100 struct perf_evsel *ev)
102 metric_events[*ind] = ev;
105 if (++*ind == idnum) {
106 metric_events[*ind] = NULL;
112 static struct perf_evsel *find_evsel_group(struct perf_evlist *perf_evlist,
115 struct perf_evsel **metric_events)
117 struct perf_evsel *ev, *start = NULL;
120 evlist__for_each_entry (perf_evlist, ev) {
121 if (ev->collect_stat)
123 if (!strcmp(ev->name, ids[ind])) {
124 if (record_evsel(&ind, &start, idnum,
129 * We saw some other event that is not
130 * in our list of events. Discard
131 * the whole match and start again.
135 if (!strcmp(ev->name, ids[ind])) {
136 if (record_evsel(&ind, &start, idnum,
143 * This can happen when an alias expands to multiple
144 * events, like for uncore events.
145 * We don't support this case for now.
150 static int metricgroup__setup_events(struct list_head *groups,
151 struct perf_evlist *perf_evlist,
152 struct rblist *metric_events_list)
154 struct metric_event *me;
155 struct metric_expr *expr;
159 struct perf_evsel *evsel;
161 list_for_each_entry (eg, groups, nd) {
162 struct perf_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);
177 for (i = 0; i < eg->idnum; i++)
178 metric_events[i]->collect_stat = true;
179 me = metricgroup__lookup(metric_events_list, evsel, true);
185 expr = malloc(sizeof(struct metric_expr));
191 expr->metric_expr = eg->metric_expr;
192 expr->metric_name = eg->metric_name;
193 expr->metric_events = metric_events;
194 list_add(&expr->nd, &me->head);
199 static bool match_metric(const char *n, const char *list)
206 if (!strcmp(list, "all"))
209 return !strcasecmp(list, "No_group");
211 m = strcasestr(n, list);
214 if ((m == n || m[-1] == ';' || m[-1] == ' ') &&
215 (m[len] == 0 || m[len] == ';'))
223 struct strlist *metrics;
226 static int mep_cmp(struct rb_node *rb_node, const void *entry)
228 struct mep *a = container_of(rb_node, struct mep, nd);
229 struct mep *b = (struct mep *)entry;
231 return strcmp(a->name, b->name);
234 static struct rb_node *mep_new(struct rblist *rl __maybe_unused,
237 struct mep *me = malloc(sizeof(struct mep));
241 memcpy(me, entry, sizeof(struct mep));
242 me->name = strdup(me->name);
245 me->metrics = strlist__new(NULL, NULL);
250 free((char *)me->name);
256 static struct mep *mep_lookup(struct rblist *groups, const char *name)
262 nd = rblist__find(groups, &me);
264 return container_of(nd, struct mep, nd);
265 rblist__add_node(groups, &me);
266 nd = rblist__find(groups, &me);
268 return container_of(nd, struct mep, nd);
272 static void mep_delete(struct rblist *rl __maybe_unused,
275 struct mep *me = container_of(nd, struct mep, nd);
277 strlist__delete(me->metrics);
278 free((void *)me->name);
282 static void metricgroup__print_strlist(struct strlist *metrics, bool raw)
287 strlist__for_each_entry (sn, metrics) {
289 printf("%s%s", n > 0 ? " " : "", sn->s);
291 printf(" %s\n", sn->s);
298 void metricgroup__print(bool metrics, bool metricgroups, char *filter,
301 struct pmu_events_map *map = perf_pmu__find_map(NULL);
302 struct pmu_event *pe;
304 struct rblist groups;
305 struct rb_node *node, *next;
306 struct strlist *metriclist = NULL;
312 metriclist = strlist__new(NULL, NULL);
317 rblist__init(&groups);
318 groups.node_new = mep_new;
319 groups.node_cmp = mep_cmp;
320 groups.node_delete = mep_delete;
325 if (!pe->name && !pe->metric_group && !pe->metric_name)
327 if (!pe->metric_expr)
329 g = pe->metric_group;
330 if (!g && pe->metric_name) {
337 char *mg = strdup(g);
342 while ((g = strsep(&mg, ";")) != NULL) {
350 if (filter && !strstr(g, filter))
353 s = (char *)pe->metric_name;
355 if (asprintf(&s, "%s\n%*s%s]",
356 pe->metric_name, 8, "[", pe->desc) < 0)
364 strlist__add(metriclist, s);
366 me = mep_lookup(&groups, g);
369 strlist__add(me->metrics, s);
376 if (metricgroups && !raw)
377 printf("\nMetric Groups:\n\n");
378 else if (metrics && !raw)
379 printf("\nMetrics:\n\n");
381 for (node = rb_first(&groups.entries); node; node = next) {
382 struct mep *me = container_of(node, struct mep, nd);
385 printf("%s%s%s", me->name, metrics ? ":" : "", raw ? " " : "\n");
387 metricgroup__print_strlist(me->metrics, raw);
388 next = rb_next(node);
389 rblist__remove_node(&groups, node);
392 metricgroup__print_strlist(metriclist, raw);
393 strlist__delete(metriclist);
396 static int metricgroup__add_metric(const char *metric, struct strbuf *events,
397 struct list_head *group_list)
399 struct pmu_events_map *map = perf_pmu__find_map(NULL);
400 struct pmu_event *pe;
410 if (!pe->name && !pe->metric_group && !pe->metric_name)
412 if (!pe->metric_expr)
414 if (match_metric(pe->metric_group, metric) ||
415 match_metric(pe->metric_name, metric)) {
420 pr_debug("metric expr %s for %s\n", pe->metric_expr, pe->metric_name);
422 if (expr__find_other(pe->metric_expr,
423 NULL, &ids, &idnum) < 0)
426 strbuf_addf(events, ",");
427 for (j = 0; j < idnum; j++) {
428 pr_debug("found event %s\n", ids[j]);
429 strbuf_addf(events, "%s%s",
433 strbuf_addf(events, "}:W");
435 eg = malloc(sizeof(struct egroup));
442 eg->metric_name = pe->metric_name;
443 eg->metric_expr = pe->metric_expr;
444 list_add_tail(&eg->nd, group_list);
451 static int metricgroup__add_metric_list(const char *list, struct strbuf *events,
452 struct list_head *group_list)
454 char *llist, *nlist, *p;
457 nlist = strdup(list);
462 strbuf_init(events, 100);
463 strbuf_addf(events, "%s", "");
465 while ((p = strsep(&llist, ",")) != NULL) {
466 ret = metricgroup__add_metric(p, events, group_list);
467 if (ret == -EINVAL) {
468 fprintf(stderr, "Cannot find metric or group `%s'\n",
477 static void metricgroup__free_egroups(struct list_head *group_list)
479 struct egroup *eg, *egtmp;
482 list_for_each_entry_safe (eg, egtmp, group_list, nd) {
483 for (i = 0; i < eg->idnum; i++)
484 free((char *)eg->ids[i]);
490 int metricgroup__parse_groups(const struct option *opt,
492 struct rblist *metric_events)
494 struct parse_events_error parse_error;
495 struct perf_evlist *perf_evlist = *(struct perf_evlist **)opt->value;
496 struct strbuf extra_events;
497 LIST_HEAD(group_list);
500 if (metric_events->nr_entries == 0)
501 metricgroup__rblist_init(metric_events);
502 ret = metricgroup__add_metric_list(str, &extra_events, &group_list);
505 pr_debug("adding %s\n", extra_events.buf);
506 memset(&parse_error, 0, sizeof(struct parse_events_error));
507 ret = parse_events(perf_evlist, extra_events.buf, &parse_error);
509 parse_events_print_error(&parse_error, extra_events.buf);
512 strbuf_release(&extra_events);
513 ret = metricgroup__setup_events(&group_list, perf_evlist,
516 metricgroup__free_egroups(&group_list);
520 bool metricgroup__has_metric(const char *metric)
522 struct pmu_events_map *map = perf_pmu__find_map(NULL);
523 struct pmu_event *pe;
532 if (!pe->name && !pe->metric_group && !pe->metric_name)
534 if (!pe->metric_expr)
536 if (match_metric(pe->metric_name, metric))