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;
95 static struct evsel *find_evsel_group(struct evlist *perf_evlist,
98 struct evsel **metric_events)
104 evlist__for_each_entry (perf_evlist, ev) {
105 if (!strcmp(ev->name, ids[i])) {
106 if (!metric_events[i])
107 metric_events[i] = ev;
112 if (i + 1 == idnum) {
113 /* Discard the whole match and start again */
115 memset(metric_events, 0,
116 sizeof(struct evsel *) * idnum);
120 if (!strcmp(ev->name, ids[i]))
121 metric_events[i] = ev;
123 /* Discard the whole match and start again */
125 memset(metric_events, 0,
126 sizeof(struct evsel *) * idnum);
133 /* Not whole match */
137 metric_events[idnum] = NULL;
139 for (i = 0; i < idnum; i++) {
140 leader_found = false;
141 evlist__for_each_entry(perf_evlist, ev) {
142 if (!leader_found && (ev == metric_events[i]))
146 !strcmp(ev->name, metric_events[i]->name)) {
147 ev->metric_leader = metric_events[i];
152 return metric_events[0];
155 static int metricgroup__setup_events(struct list_head *groups,
156 struct evlist *perf_evlist,
157 struct rblist *metric_events_list)
159 struct metric_event *me;
160 struct metric_expr *expr;
166 list_for_each_entry (eg, groups, nd) {
167 struct evsel **metric_events;
169 metric_events = calloc(sizeof(void *), eg->idnum + 1);
170 if (!metric_events) {
174 evsel = find_evsel_group(perf_evlist, eg->ids, eg->idnum,
177 pr_debug("Cannot resolve %s: %s\n",
178 eg->metric_name, eg->metric_expr);
181 for (i = 0; i < eg->idnum; i++)
182 metric_events[i]->collect_stat = true;
183 me = metricgroup__lookup(metric_events_list, evsel, true);
188 expr = malloc(sizeof(struct metric_expr));
193 expr->metric_expr = eg->metric_expr;
194 expr->metric_name = eg->metric_name;
195 expr->metric_unit = eg->metric_unit;
196 expr->metric_events = metric_events;
197 list_add(&expr->nd, &me->head);
202 static bool match_metric(const char *n, const char *list)
209 if (!strcmp(list, "all"))
212 return !strcasecmp(list, "No_group");
214 m = strcasestr(n, list);
217 if ((m == n || m[-1] == ';' || m[-1] == ' ') &&
218 (m[len] == 0 || m[len] == ';'))
226 struct strlist *metrics;
229 static int mep_cmp(struct rb_node *rb_node, const void *entry)
231 struct mep *a = container_of(rb_node, struct mep, nd);
232 struct mep *b = (struct mep *)entry;
234 return strcmp(a->name, b->name);
237 static struct rb_node *mep_new(struct rblist *rl __maybe_unused,
240 struct mep *me = malloc(sizeof(struct mep));
244 memcpy(me, entry, sizeof(struct mep));
245 me->name = strdup(me->name);
248 me->metrics = strlist__new(NULL, NULL);
259 static struct mep *mep_lookup(struct rblist *groups, const char *name)
265 nd = rblist__find(groups, &me);
267 return container_of(nd, struct mep, nd);
268 rblist__add_node(groups, &me);
269 nd = rblist__find(groups, &me);
271 return container_of(nd, struct mep, nd);
275 static void mep_delete(struct rblist *rl __maybe_unused,
278 struct mep *me = container_of(nd, struct mep, nd);
280 strlist__delete(me->metrics);
285 static void metricgroup__print_strlist(struct strlist *metrics, bool raw)
290 strlist__for_each_entry (sn, metrics) {
292 printf("%s%s", n > 0 ? " " : "", sn->s);
294 printf(" %s\n", sn->s);
301 void metricgroup__print(bool metrics, bool metricgroups, char *filter,
302 bool raw, bool details)
304 struct pmu_events_map *map = perf_pmu__find_map(NULL);
305 struct pmu_event *pe;
307 struct rblist groups;
308 struct rb_node *node, *next;
309 struct strlist *metriclist = NULL;
315 metriclist = strlist__new(NULL, NULL);
320 rblist__init(&groups);
321 groups.node_new = mep_new;
322 groups.node_cmp = mep_cmp;
323 groups.node_delete = mep_delete;
328 if (!pe->name && !pe->metric_group && !pe->metric_name)
330 if (!pe->metric_expr)
332 g = pe->metric_group;
333 if (!g && pe->metric_name) {
340 char *mg = strdup(g);
345 while ((g = strsep(&mg, ";")) != NULL) {
352 if (filter && !strstr(g, filter))
355 s = (char *)pe->metric_name;
357 if (asprintf(&s, "%s\n%*s%s]",
358 pe->metric_name, 8, "[", pe->desc) < 0)
362 if (asprintf(&s, "%s\n%*s%s]",
363 s, 8, "[", pe->metric_expr) < 0)
372 strlist__add(metriclist, s);
374 me = mep_lookup(&groups, g);
377 strlist__add(me->metrics, s);
384 if (metricgroups && !raw)
385 printf("\nMetric Groups:\n\n");
386 else if (metrics && !raw)
387 printf("\nMetrics:\n\n");
389 for (node = rb_first_cached(&groups.entries); node; node = next) {
390 struct mep *me = container_of(node, struct mep, nd);
393 printf("%s%s%s", me->name, metrics && !raw ? ":" : "", raw ? " " : "\n");
395 metricgroup__print_strlist(me->metrics, raw);
396 next = rb_next(node);
397 rblist__remove_node(&groups, node);
400 metricgroup__print_strlist(metriclist, raw);
401 strlist__delete(metriclist);
404 static void metricgroup__add_metric_weak_group(struct strbuf *events,
408 bool no_group = false;
411 for (i = 0; i < idnum; i++) {
412 pr_debug("found event %s\n", ids[i]);
414 * Duration time maps to a software event and can make
415 * groups not count. Always use it outside a
418 if (!strcmp(ids[i], "duration_time")) {
420 strbuf_addf(events, "}:W,");
421 strbuf_addf(events, "duration_time");
425 strbuf_addf(events, "%s%s",
426 i == 0 || no_group ? "{" : ",",
431 strbuf_addf(events, "}:W");
434 static void metricgroup__add_metric_non_group(struct strbuf *events,
440 for (i = 0; i < idnum; i++)
441 strbuf_addf(events, ",%s", ids[i]);
444 static void metricgroup___watchdog_constraint_hint(const char *name, bool foot)
446 static bool violate_nmi_constraint;
449 pr_warning("Splitting metric group %s into standalone metrics.\n", name);
450 violate_nmi_constraint = true;
454 if (!violate_nmi_constraint)
457 pr_warning("Try disabling the NMI watchdog to comply NO_NMI_WATCHDOG metric constraint:\n"
458 " echo 0 > /proc/sys/kernel/nmi_watchdog\n"
460 " echo 1 > /proc/sys/kernel/nmi_watchdog\n");
463 static bool metricgroup__has_constraint(struct pmu_event *pe)
465 if (!pe->metric_constraint)
468 if (!strcmp(pe->metric_constraint, "NO_NMI_WATCHDOG") &&
469 sysctl__nmi_watchdog_enabled()) {
470 metricgroup___watchdog_constraint_hint(pe->metric_name, false);
477 static int metricgroup__add_metric(const char *metric, struct strbuf *events,
478 struct list_head *group_list)
480 struct pmu_events_map *map = perf_pmu__find_map(NULL);
481 struct pmu_event *pe;
482 int i, ret = -EINVAL;
490 if (!pe->name && !pe->metric_group && !pe->metric_name)
492 if (!pe->metric_expr)
494 if (match_metric(pe->metric_group, metric) ||
495 match_metric(pe->metric_name, metric)) {
500 pr_debug("metric expr %s for %s\n", pe->metric_expr, pe->metric_name);
502 if (expr__find_other(pe->metric_expr,
503 NULL, &ids, &idnum) < 0)
506 strbuf_addf(events, ",");
508 if (metricgroup__has_constraint(pe))
509 metricgroup__add_metric_non_group(events, ids, idnum);
511 metricgroup__add_metric_weak_group(events, ids, idnum);
513 eg = malloc(sizeof(struct egroup));
520 eg->metric_name = pe->metric_name;
521 eg->metric_expr = pe->metric_expr;
522 eg->metric_unit = pe->unit;
523 list_add_tail(&eg->nd, group_list);
530 static int metricgroup__add_metric_list(const char *list, struct strbuf *events,
531 struct list_head *group_list)
533 char *llist, *nlist, *p;
536 nlist = strdup(list);
541 strbuf_init(events, 100);
542 strbuf_addf(events, "%s", "");
544 while ((p = strsep(&llist, ",")) != NULL) {
545 ret = metricgroup__add_metric(p, events, group_list);
546 if (ret == -EINVAL) {
547 fprintf(stderr, "Cannot find metric or group `%s'\n",
555 metricgroup___watchdog_constraint_hint(NULL, true);
560 static void metricgroup__free_egroups(struct list_head *group_list)
562 struct egroup *eg, *egtmp;
565 list_for_each_entry_safe (eg, egtmp, group_list, nd) {
566 for (i = 0; i < eg->idnum; i++)
569 list_del_init(&eg->nd);
574 int metricgroup__parse_groups(const struct option *opt,
576 struct rblist *metric_events)
578 struct parse_events_error parse_error;
579 struct evlist *perf_evlist = *(struct evlist **)opt->value;
580 struct strbuf extra_events;
581 LIST_HEAD(group_list);
584 if (metric_events->nr_entries == 0)
585 metricgroup__rblist_init(metric_events);
586 ret = metricgroup__add_metric_list(str, &extra_events, &group_list);
589 pr_debug("adding %s\n", extra_events.buf);
590 bzero(&parse_error, sizeof(parse_error));
591 ret = parse_events(perf_evlist, extra_events.buf, &parse_error);
593 parse_events_print_error(&parse_error, extra_events.buf);
596 strbuf_release(&extra_events);
597 ret = metricgroup__setup_events(&group_list, perf_evlist,
600 metricgroup__free_egroups(&group_list);
604 bool metricgroup__has_metric(const char *metric)
606 struct pmu_events_map *map = perf_pmu__find_map(NULL);
607 struct pmu_event *pe;
616 if (!pe->name && !pe->metric_group && !pe->metric_name)
618 if (!pe->metric_expr)
620 if (match_metric(pe->metric_name, metric))