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>
25 #include <api/fs/fs.h>
28 struct metric_event *metricgroup__lookup(struct rblist *metric_events,
33 struct metric_event me = {
40 nd = rblist__find(metric_events, &me);
42 return container_of(nd, struct metric_event, nd);
44 rblist__add_node(metric_events, &me);
45 nd = rblist__find(metric_events, &me);
47 return container_of(nd, struct metric_event, nd);
52 static int metric_event_cmp(struct rb_node *rb_node, const void *entry)
54 struct metric_event *a = container_of(rb_node,
57 const struct metric_event *b = entry;
59 if (a->evsel == b->evsel)
61 if ((char *)a->evsel < (char *)b->evsel)
66 static struct rb_node *metric_event_new(struct rblist *rblist __maybe_unused,
69 struct metric_event *me = malloc(sizeof(struct metric_event));
73 memcpy(me, entry, sizeof(struct metric_event));
74 me->evsel = ((struct metric_event *)entry)->evsel;
75 INIT_LIST_HEAD(&me->head);
79 static void metricgroup__rblist_init(struct rblist *metric_events)
81 rblist__init(metric_events);
82 metric_events->node_cmp = metric_event_cmp;
83 metric_events->node_new = metric_event_new;
90 const char *metric_name;
91 const char *metric_expr;
92 const char *metric_unit;
96 static struct evsel *find_evsel_group(struct evlist *perf_evlist,
99 struct evsel **metric_events,
106 evlist__for_each_entry (perf_evlist, ev) {
107 if (evlist_used[j++])
109 if (!strcmp(ev->name, ids[i])) {
110 if (!metric_events[i])
111 metric_events[i] = ev;
116 /* Discard the whole match and start again */
118 memset(metric_events, 0,
119 sizeof(struct evsel *) * idnum);
121 if (!strcmp(ev->name, ids[i])) {
122 if (!metric_events[i])
123 metric_events[i] = ev;
132 /* Not whole match */
136 metric_events[idnum] = NULL;
138 for (i = 0; i < idnum; i++) {
139 leader_found = false;
140 evlist__for_each_entry(perf_evlist, ev) {
141 if (!leader_found && (ev == metric_events[i]))
145 !strcmp(ev->name, metric_events[i]->name)) {
146 ev->metric_leader = metric_events[i];
150 ev = metric_events[i];
151 evlist_used[ev->idx] = true;
154 return metric_events[0];
157 static int metricgroup__setup_events(struct list_head *groups,
158 struct evlist *perf_evlist,
159 struct rblist *metric_events_list)
161 struct metric_event *me;
162 struct metric_expr *expr;
169 evlist_used = calloc(perf_evlist->core.nr_entries, sizeof(bool));
175 list_for_each_entry (eg, groups, nd) {
176 struct evsel **metric_events;
178 metric_events = calloc(sizeof(void *), eg->idnum + 1);
179 if (!metric_events) {
183 evsel = find_evsel_group(perf_evlist, eg->ids, eg->idnum,
184 metric_events, evlist_used);
186 pr_debug("Cannot resolve %s: %s\n",
187 eg->metric_name, eg->metric_expr);
190 for (i = 0; i < eg->idnum; i++)
191 metric_events[i]->collect_stat = true;
192 me = metricgroup__lookup(metric_events_list, evsel, true);
197 expr = malloc(sizeof(struct metric_expr));
202 expr->metric_expr = eg->metric_expr;
203 expr->metric_name = eg->metric_name;
204 expr->metric_unit = eg->metric_unit;
205 expr->metric_events = metric_events;
206 expr->runtime = eg->runtime;
207 list_add(&expr->nd, &me->head);
215 static bool match_metric(const char *n, const char *list)
222 if (!strcmp(list, "all"))
225 return !strcasecmp(list, "No_group");
227 m = strcasestr(n, list);
230 if ((m == n || m[-1] == ';' || m[-1] == ' ') &&
231 (m[len] == 0 || m[len] == ';'))
239 struct strlist *metrics;
242 static int mep_cmp(struct rb_node *rb_node, const void *entry)
244 struct mep *a = container_of(rb_node, struct mep, nd);
245 struct mep *b = (struct mep *)entry;
247 return strcmp(a->name, b->name);
250 static struct rb_node *mep_new(struct rblist *rl __maybe_unused,
253 struct mep *me = malloc(sizeof(struct mep));
257 memcpy(me, entry, sizeof(struct mep));
258 me->name = strdup(me->name);
261 me->metrics = strlist__new(NULL, NULL);
272 static struct mep *mep_lookup(struct rblist *groups, const char *name)
278 nd = rblist__find(groups, &me);
280 return container_of(nd, struct mep, nd);
281 rblist__add_node(groups, &me);
282 nd = rblist__find(groups, &me);
284 return container_of(nd, struct mep, nd);
288 static void mep_delete(struct rblist *rl __maybe_unused,
291 struct mep *me = container_of(nd, struct mep, nd);
293 strlist__delete(me->metrics);
298 static void metricgroup__print_strlist(struct strlist *metrics, bool raw)
303 strlist__for_each_entry (sn, metrics) {
305 printf("%s%s", n > 0 ? " " : "", sn->s);
307 printf(" %s\n", sn->s);
314 void metricgroup__print(bool metrics, bool metricgroups, char *filter,
315 bool raw, bool details)
317 struct pmu_events_map *map = perf_pmu__find_map(NULL);
318 struct pmu_event *pe;
320 struct rblist groups;
321 struct rb_node *node, *next;
322 struct strlist *metriclist = NULL;
328 metriclist = strlist__new(NULL, NULL);
333 rblist__init(&groups);
334 groups.node_new = mep_new;
335 groups.node_cmp = mep_cmp;
336 groups.node_delete = mep_delete;
341 if (!pe->name && !pe->metric_group && !pe->metric_name)
343 if (!pe->metric_expr)
345 g = pe->metric_group;
346 if (!g && pe->metric_name) {
353 char *mg = strdup(g);
358 while ((g = strsep(&mg, ";")) != NULL) {
365 if (filter && !strstr(g, filter))
368 s = (char *)pe->metric_name;
370 if (asprintf(&s, "%s\n%*s%s]",
371 pe->metric_name, 8, "[", pe->desc) < 0)
375 if (asprintf(&s, "%s\n%*s%s]",
376 s, 8, "[", pe->metric_expr) < 0)
385 strlist__add(metriclist, s);
387 me = mep_lookup(&groups, g);
390 strlist__add(me->metrics, s);
397 if (metricgroups && !raw)
398 printf("\nMetric Groups:\n\n");
399 else if (metrics && !raw)
400 printf("\nMetrics:\n\n");
402 for (node = rb_first_cached(&groups.entries); node; node = next) {
403 struct mep *me = container_of(node, struct mep, nd);
406 printf("%s%s%s", me->name, metrics && !raw ? ":" : "", raw ? " " : "\n");
408 metricgroup__print_strlist(me->metrics, raw);
409 next = rb_next(node);
410 rblist__remove_node(&groups, node);
413 metricgroup__print_strlist(metriclist, raw);
414 strlist__delete(metriclist);
417 static void metricgroup__add_metric_weak_group(struct strbuf *events,
421 bool no_group = false;
424 for (i = 0; i < idnum; i++) {
425 pr_debug("found event %s\n", ids[i]);
427 * Duration time maps to a software event and can make
428 * groups not count. Always use it outside a
431 if (!strcmp(ids[i], "duration_time")) {
433 strbuf_addf(events, "}:W,");
434 strbuf_addf(events, "duration_time");
438 strbuf_addf(events, "%s%s",
439 i == 0 || no_group ? "{" : ",",
444 strbuf_addf(events, "}:W");
447 static void metricgroup__add_metric_non_group(struct strbuf *events,
453 for (i = 0; i < idnum; i++)
454 strbuf_addf(events, ",%s", ids[i]);
457 static void metricgroup___watchdog_constraint_hint(const char *name, bool foot)
459 static bool violate_nmi_constraint;
462 pr_warning("Splitting metric group %s into standalone metrics.\n", name);
463 violate_nmi_constraint = true;
467 if (!violate_nmi_constraint)
470 pr_warning("Try disabling the NMI watchdog to comply NO_NMI_WATCHDOG metric constraint:\n"
471 " echo 0 > /proc/sys/kernel/nmi_watchdog\n"
473 " echo 1 > /proc/sys/kernel/nmi_watchdog\n");
476 static bool metricgroup__has_constraint(struct pmu_event *pe)
478 if (!pe->metric_constraint)
481 if (!strcmp(pe->metric_constraint, "NO_NMI_WATCHDOG") &&
482 sysctl__nmi_watchdog_enabled()) {
483 metricgroup___watchdog_constraint_hint(pe->metric_name, false);
490 int __weak arch_get_runtimeparam(void)
495 static int __metricgroup__add_metric(struct strbuf *events,
496 struct list_head *group_list, struct pmu_event *pe, int runtime)
503 if (expr__find_other(pe->metric_expr, NULL, &ids, &idnum, runtime) < 0)
507 strbuf_addf(events, ",");
509 if (metricgroup__has_constraint(pe))
510 metricgroup__add_metric_non_group(events, ids, idnum);
512 metricgroup__add_metric_weak_group(events, ids, idnum);
514 eg = malloc(sizeof(*eg));
520 eg->metric_name = pe->metric_name;
521 eg->metric_expr = pe->metric_expr;
522 eg->metric_unit = pe->unit;
523 eg->runtime = runtime;
524 list_add_tail(&eg->nd, group_list);
529 static int metricgroup__add_metric(const char *metric, struct strbuf *events,
530 struct list_head *group_list)
532 struct pmu_events_map *map = perf_pmu__find_map(NULL);
533 struct pmu_event *pe;
534 int i, ret = -EINVAL;
542 if (!pe->name && !pe->metric_group && !pe->metric_name)
544 if (!pe->metric_expr)
546 if (match_metric(pe->metric_group, metric) ||
547 match_metric(pe->metric_name, metric)) {
549 pr_debug("metric expr %s for %s\n", pe->metric_expr, pe->metric_name);
551 if (!strstr(pe->metric_expr, "?")) {
552 ret = __metricgroup__add_metric(events, group_list, pe, 1);
556 count = arch_get_runtimeparam();
558 /* This loop is added to create multiple
559 * events depend on count value and add
560 * those events to group_list.
563 for (j = 0; j < count; j++)
564 ret = __metricgroup__add_metric(events, group_list, pe, j);
573 static int metricgroup__add_metric_list(const char *list, struct strbuf *events,
574 struct list_head *group_list)
576 char *llist, *nlist, *p;
579 nlist = strdup(list);
584 strbuf_init(events, 100);
585 strbuf_addf(events, "%s", "");
587 while ((p = strsep(&llist, ",")) != NULL) {
588 ret = metricgroup__add_metric(p, events, group_list);
589 if (ret == -EINVAL) {
590 fprintf(stderr, "Cannot find metric or group `%s'\n",
598 metricgroup___watchdog_constraint_hint(NULL, true);
603 static void metricgroup__free_egroups(struct list_head *group_list)
605 struct egroup *eg, *egtmp;
608 list_for_each_entry_safe (eg, egtmp, group_list, nd) {
609 for (i = 0; i < eg->idnum; i++)
612 list_del_init(&eg->nd);
617 int metricgroup__parse_groups(const struct option *opt,
619 struct rblist *metric_events)
621 struct parse_events_error parse_error;
622 struct evlist *perf_evlist = *(struct evlist **)opt->value;
623 struct strbuf extra_events;
624 LIST_HEAD(group_list);
627 if (metric_events->nr_entries == 0)
628 metricgroup__rblist_init(metric_events);
629 ret = metricgroup__add_metric_list(str, &extra_events, &group_list);
632 pr_debug("adding %s\n", extra_events.buf);
633 bzero(&parse_error, sizeof(parse_error));
634 ret = parse_events(perf_evlist, extra_events.buf, &parse_error);
636 parse_events_print_error(&parse_error, extra_events.buf);
639 strbuf_release(&extra_events);
640 ret = metricgroup__setup_events(&group_list, perf_evlist,
643 metricgroup__free_egroups(&group_list);
647 bool metricgroup__has_metric(const char *metric)
649 struct pmu_events_map *map = perf_pmu__find_map(NULL);
650 struct pmu_event *pe;
659 if (!pe->name && !pe->metric_group && !pe->metric_name)
661 if (!pe->metric_expr)
663 if (match_metric(pe->metric_name, metric))