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;
88 struct expr_parse_ctx pctx;
89 const char *metric_name;
90 const char *metric_expr;
91 const char *metric_unit;
95 static struct evsel *find_evsel_group(struct evlist *perf_evlist,
96 struct expr_parse_ctx *pctx,
97 struct evsel **metric_events,
102 const size_t idnum = hashmap__size(&pctx->ids);
107 evlist__for_each_entry (perf_evlist, ev) {
108 if (evlist_used[j++])
110 if (hashmap__find(&pctx->ids, ev->name, (void **)&val_ptr)) {
111 if (!metric_events[i])
112 metric_events[i] = ev;
117 /* Discard the whole match and start again */
119 memset(metric_events, 0,
120 sizeof(struct evsel *) * idnum);
125 /* Not whole match */
129 metric_events[idnum] = NULL;
131 for (i = 0; i < idnum; i++) {
132 leader_found = false;
133 evlist__for_each_entry(perf_evlist, ev) {
134 if (!leader_found && (ev == metric_events[i]))
138 !strcmp(ev->name, metric_events[i]->name)) {
139 ev->metric_leader = metric_events[i];
143 ev = metric_events[i];
144 evlist_used[ev->idx] = true;
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;
162 evlist_used = calloc(perf_evlist->core.nr_entries, sizeof(bool));
168 list_for_each_entry (eg, groups, nd) {
169 struct evsel **metric_events;
171 metric_events = calloc(sizeof(void *),
172 hashmap__size(&eg->pctx.ids) + 1);
173 if (!metric_events) {
177 evsel = find_evsel_group(perf_evlist, &eg->pctx, metric_events,
180 pr_debug("Cannot resolve %s: %s\n",
181 eg->metric_name, eg->metric_expr);
184 for (i = 0; metric_events[i]; i++)
185 metric_events[i]->collect_stat = true;
186 me = metricgroup__lookup(metric_events_list, evsel, true);
191 expr = malloc(sizeof(struct metric_expr));
196 expr->metric_expr = eg->metric_expr;
197 expr->metric_name = eg->metric_name;
198 expr->metric_unit = eg->metric_unit;
199 expr->metric_events = metric_events;
200 expr->runtime = eg->runtime;
201 list_add(&expr->nd, &me->head);
209 static bool match_metric(const char *n, const char *list)
216 if (!strcmp(list, "all"))
219 return !strcasecmp(list, "No_group");
221 m = strcasestr(n, list);
224 if ((m == n || m[-1] == ';' || m[-1] == ' ') &&
225 (m[len] == 0 || m[len] == ';'))
233 struct strlist *metrics;
236 static int mep_cmp(struct rb_node *rb_node, const void *entry)
238 struct mep *a = container_of(rb_node, struct mep, nd);
239 struct mep *b = (struct mep *)entry;
241 return strcmp(a->name, b->name);
244 static struct rb_node *mep_new(struct rblist *rl __maybe_unused,
247 struct mep *me = malloc(sizeof(struct mep));
251 memcpy(me, entry, sizeof(struct mep));
252 me->name = strdup(me->name);
255 me->metrics = strlist__new(NULL, NULL);
266 static struct mep *mep_lookup(struct rblist *groups, const char *name)
272 nd = rblist__find(groups, &me);
274 return container_of(nd, struct mep, nd);
275 rblist__add_node(groups, &me);
276 nd = rblist__find(groups, &me);
278 return container_of(nd, struct mep, nd);
282 static void mep_delete(struct rblist *rl __maybe_unused,
285 struct mep *me = container_of(nd, struct mep, nd);
287 strlist__delete(me->metrics);
292 static void metricgroup__print_strlist(struct strlist *metrics, bool raw)
297 strlist__for_each_entry (sn, metrics) {
299 printf("%s%s", n > 0 ? " " : "", sn->s);
301 printf(" %s\n", sn->s);
308 void metricgroup__print(bool metrics, bool metricgroups, char *filter,
309 bool raw, bool details)
311 struct pmu_events_map *map = perf_pmu__find_map(NULL);
312 struct pmu_event *pe;
314 struct rblist groups;
315 struct rb_node *node, *next;
316 struct strlist *metriclist = NULL;
322 metriclist = strlist__new(NULL, NULL);
327 rblist__init(&groups);
328 groups.node_new = mep_new;
329 groups.node_cmp = mep_cmp;
330 groups.node_delete = mep_delete;
335 if (!pe->name && !pe->metric_group && !pe->metric_name)
337 if (!pe->metric_expr)
339 g = pe->metric_group;
340 if (!g && pe->metric_name) {
347 char *mg = strdup(g);
352 while ((g = strsep(&mg, ";")) != NULL) {
359 if (filter && !strstr(g, filter))
362 s = (char *)pe->metric_name;
364 if (asprintf(&s, "%s\n%*s%s]",
365 pe->metric_name, 8, "[", pe->desc) < 0)
369 if (asprintf(&s, "%s\n%*s%s]",
370 s, 8, "[", pe->metric_expr) < 0)
379 strlist__add(metriclist, s);
381 me = mep_lookup(&groups, g);
384 strlist__add(me->metrics, s);
391 if (metricgroups && !raw)
392 printf("\nMetric Groups:\n\n");
393 else if (metrics && !raw)
394 printf("\nMetrics:\n\n");
396 for (node = rb_first_cached(&groups.entries); node; node = next) {
397 struct mep *me = container_of(node, struct mep, nd);
400 printf("%s%s%s", me->name, metrics && !raw ? ":" : "", raw ? " " : "\n");
402 metricgroup__print_strlist(me->metrics, raw);
403 next = rb_next(node);
404 rblist__remove_node(&groups, node);
407 metricgroup__print_strlist(metriclist, raw);
408 strlist__delete(metriclist);
411 static void metricgroup__add_metric_weak_group(struct strbuf *events,
412 struct expr_parse_ctx *ctx)
414 struct hashmap_entry *cur;
416 bool no_group = false;
418 hashmap__for_each_entry((&ctx->ids), cur, bkt) {
419 pr_debug("found event %s\n", (const char *)cur->key);
421 * Duration time maps to a software event and can make
422 * groups not count. Always use it outside a
425 if (!strcmp(cur->key, "duration_time")) {
427 strbuf_addf(events, "}:W,");
428 strbuf_addf(events, "duration_time");
432 strbuf_addf(events, "%s%s",
433 i == 0 || no_group ? "{" : ",",
434 (const char *)cur->key);
439 strbuf_addf(events, "}:W");
442 static void metricgroup__add_metric_non_group(struct strbuf *events,
443 struct expr_parse_ctx *ctx)
445 struct hashmap_entry *cur;
448 hashmap__for_each_entry((&ctx->ids), cur, bkt)
449 strbuf_addf(events, ",%s", (const char *)cur->key);
452 static void metricgroup___watchdog_constraint_hint(const char *name, bool foot)
454 static bool violate_nmi_constraint;
457 pr_warning("Splitting metric group %s into standalone metrics.\n", name);
458 violate_nmi_constraint = true;
462 if (!violate_nmi_constraint)
465 pr_warning("Try disabling the NMI watchdog to comply NO_NMI_WATCHDOG metric constraint:\n"
466 " echo 0 > /proc/sys/kernel/nmi_watchdog\n"
468 " echo 1 > /proc/sys/kernel/nmi_watchdog\n");
471 static bool metricgroup__has_constraint(struct pmu_event *pe)
473 if (!pe->metric_constraint)
476 if (!strcmp(pe->metric_constraint, "NO_NMI_WATCHDOG") &&
477 sysctl__nmi_watchdog_enabled()) {
478 metricgroup___watchdog_constraint_hint(pe->metric_name, false);
485 int __weak arch_get_runtimeparam(void)
490 static int __metricgroup__add_metric(struct strbuf *events,
491 struct list_head *group_list, struct pmu_event *pe, int runtime)
495 eg = malloc(sizeof(*eg));
499 expr__ctx_init(&eg->pctx);
500 eg->metric_name = pe->metric_name;
501 eg->metric_expr = pe->metric_expr;
502 eg->metric_unit = pe->unit;
503 eg->runtime = runtime;
505 if (expr__find_other(pe->metric_expr, NULL, &eg->pctx, runtime) < 0) {
506 expr__ctx_clear(&eg->pctx);
512 strbuf_addf(events, ",");
514 if (metricgroup__has_constraint(pe))
515 metricgroup__add_metric_non_group(events, &eg->pctx);
517 metricgroup__add_metric_weak_group(events, &eg->pctx);
519 list_add_tail(&eg->nd, group_list);
524 static int metricgroup__add_metric(const char *metric, struct strbuf *events,
525 struct list_head *group_list)
527 struct pmu_events_map *map = perf_pmu__find_map(NULL);
528 struct pmu_event *pe;
529 int i, ret = -EINVAL;
537 if (!pe->name && !pe->metric_group && !pe->metric_name)
539 if (!pe->metric_expr)
541 if (match_metric(pe->metric_group, metric) ||
542 match_metric(pe->metric_name, metric)) {
544 pr_debug("metric expr %s for %s\n", pe->metric_expr, pe->metric_name);
546 if (!strstr(pe->metric_expr, "?")) {
547 ret = __metricgroup__add_metric(events, group_list, pe, 1);
551 count = arch_get_runtimeparam();
553 /* This loop is added to create multiple
554 * events depend on count value and add
555 * those events to group_list.
558 for (j = 0; j < count; j++)
559 ret = __metricgroup__add_metric(events, group_list, pe, j);
568 static int metricgroup__add_metric_list(const char *list, struct strbuf *events,
569 struct list_head *group_list)
571 char *llist, *nlist, *p;
574 nlist = strdup(list);
579 strbuf_init(events, 100);
580 strbuf_addf(events, "%s", "");
582 while ((p = strsep(&llist, ",")) != NULL) {
583 ret = metricgroup__add_metric(p, events, group_list);
584 if (ret == -EINVAL) {
585 fprintf(stderr, "Cannot find metric or group `%s'\n",
593 metricgroup___watchdog_constraint_hint(NULL, true);
598 static void metricgroup__free_egroups(struct list_head *group_list)
600 struct egroup *eg, *egtmp;
602 list_for_each_entry_safe (eg, egtmp, group_list, nd) {
603 expr__ctx_clear(&eg->pctx);
604 list_del_init(&eg->nd);
609 int metricgroup__parse_groups(const struct option *opt,
611 struct rblist *metric_events)
613 struct parse_events_error parse_error;
614 struct evlist *perf_evlist = *(struct evlist **)opt->value;
615 struct strbuf extra_events;
616 LIST_HEAD(group_list);
619 if (metric_events->nr_entries == 0)
620 metricgroup__rblist_init(metric_events);
621 ret = metricgroup__add_metric_list(str, &extra_events, &group_list);
624 pr_debug("adding %s\n", extra_events.buf);
625 bzero(&parse_error, sizeof(parse_error));
626 ret = parse_events(perf_evlist, extra_events.buf, &parse_error);
628 parse_events_print_error(&parse_error, extra_events.buf);
631 strbuf_release(&extra_events);
632 ret = metricgroup__setup_events(&group_list, perf_evlist,
635 metricgroup__free_egroups(&group_list);
639 bool metricgroup__has_metric(const char *metric)
641 struct pmu_events_map *map = perf_pmu__find_map(NULL);
642 struct pmu_event *pe;
651 if (!pe->name && !pe->metric_group && !pe->metric_name)
653 if (!pe->metric_expr)
655 if (match_metric(pe->metric_name, metric))