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;
97 * Find a group of events in perf_evlist that correpond to those from a parsed
98 * metric expression. Note, as find_evsel_group is called in the same order as
99 * perf_evlist was constructed, metric_no_merge doesn't need to test for
100 * underfilling a group.
101 * @perf_evlist: a list of events something like: {metric1 leader, metric1
102 * sibling, metric1 sibling}:W,duration_time,{metric2 leader, metric2 sibling,
103 * metric2 sibling}:W,duration_time
104 * @pctx: the parse context for the metric expression.
105 * @metric_no_merge: don't attempt to share events for the metric with other
107 * @has_constraint: is there a contraint on the group of events? In which case
108 * the events won't be grouped.
109 * @metric_events: out argument, null terminated array of evsel's associated
111 * @evlist_used: in/out argument, bitmap tracking which evlist events are used.
112 * @return the first metric event or NULL on failure.
114 static struct evsel *find_evsel_group(struct evlist *perf_evlist,
115 struct expr_parse_ctx *pctx,
116 bool metric_no_merge,
118 struct evsel **metric_events,
119 unsigned long *evlist_used)
121 struct evsel *ev, *current_leader = NULL;
123 int i = 0, matched_events = 0, events_to_match;
124 const int idnum = (int)hashmap__size(&pctx->ids);
126 /* duration_time is grouped separately. */
127 if (!has_constraint &&
128 hashmap__find(&pctx->ids, "duration_time", (void **)&val_ptr))
129 events_to_match = idnum - 1;
131 events_to_match = idnum;
133 evlist__for_each_entry (perf_evlist, ev) {
135 * Events with a constraint aren't grouped and match the first
138 if (has_constraint && ev->weak_group)
140 /* Ignore event if already used and merging is disabled. */
141 if (metric_no_merge && test_bit(ev->idx, evlist_used))
143 if (!has_constraint && ev->leader != current_leader) {
145 * Start of a new group, discard the whole match and
149 memset(metric_events, 0,
150 sizeof(struct evsel *) * idnum);
151 current_leader = ev->leader;
153 if (hashmap__find(&pctx->ids, ev->name, (void **)&val_ptr)) {
154 if (has_constraint) {
156 * Events aren't grouped, ensure the same event
157 * isn't matched from two groups.
159 for (i = 0; i < matched_events; i++) {
160 if (!strcmp(ev->name,
161 metric_events[i]->name)) {
165 if (i != matched_events)
168 metric_events[matched_events++] = ev;
170 if (matched_events == events_to_match)
174 if (events_to_match != idnum) {
175 /* Add the first duration_time. */
176 evlist__for_each_entry(perf_evlist, ev) {
177 if (!strcmp(ev->name, "duration_time")) {
178 metric_events[matched_events++] = ev;
184 if (matched_events != idnum) {
185 /* Not whole match */
189 metric_events[idnum] = NULL;
191 for (i = 0; i < idnum; i++) {
192 ev = metric_events[i];
193 ev->metric_leader = ev;
194 set_bit(ev->idx, evlist_used);
197 return metric_events[0];
200 static int metricgroup__setup_events(struct list_head *groups,
201 bool metric_no_merge,
202 struct evlist *perf_evlist,
203 struct rblist *metric_events_list)
205 struct metric_event *me;
206 struct metric_expr *expr;
210 struct evsel *evsel, *tmp;
211 unsigned long *evlist_used;
213 evlist_used = bitmap_alloc(perf_evlist->core.nr_entries);
217 list_for_each_entry (eg, groups, nd) {
218 struct evsel **metric_events;
220 metric_events = calloc(sizeof(void *),
221 hashmap__size(&eg->pctx.ids) + 1);
222 if (!metric_events) {
226 evsel = find_evsel_group(perf_evlist, &eg->pctx,
228 eg->has_constraint, metric_events,
231 pr_debug("Cannot resolve %s: %s\n",
232 eg->metric_name, eg->metric_expr);
236 for (i = 0; metric_events[i]; i++)
237 metric_events[i]->collect_stat = true;
238 me = metricgroup__lookup(metric_events_list, evsel, true);
244 expr = malloc(sizeof(struct metric_expr));
250 expr->metric_expr = eg->metric_expr;
251 expr->metric_name = eg->metric_name;
252 expr->metric_unit = eg->metric_unit;
253 expr->metric_events = metric_events;
254 expr->runtime = eg->runtime;
255 list_add(&expr->nd, &me->head);
258 evlist__for_each_entry_safe(perf_evlist, tmp, evsel) {
259 if (!test_bit(evsel->idx, evlist_used)) {
260 evlist__remove(perf_evlist, evsel);
261 evsel__delete(evsel);
264 bitmap_free(evlist_used);
269 static bool match_metric(const char *n, const char *list)
276 if (!strcmp(list, "all"))
279 return !strcasecmp(list, "No_group");
281 m = strcasestr(n, list);
284 if ((m == n || m[-1] == ';' || m[-1] == ' ') &&
285 (m[len] == 0 || m[len] == ';'))
293 struct strlist *metrics;
296 static int mep_cmp(struct rb_node *rb_node, const void *entry)
298 struct mep *a = container_of(rb_node, struct mep, nd);
299 struct mep *b = (struct mep *)entry;
301 return strcmp(a->name, b->name);
304 static struct rb_node *mep_new(struct rblist *rl __maybe_unused,
307 struct mep *me = malloc(sizeof(struct mep));
311 memcpy(me, entry, sizeof(struct mep));
312 me->name = strdup(me->name);
315 me->metrics = strlist__new(NULL, NULL);
326 static struct mep *mep_lookup(struct rblist *groups, const char *name)
332 nd = rblist__find(groups, &me);
334 return container_of(nd, struct mep, nd);
335 rblist__add_node(groups, &me);
336 nd = rblist__find(groups, &me);
338 return container_of(nd, struct mep, nd);
342 static void mep_delete(struct rblist *rl __maybe_unused,
345 struct mep *me = container_of(nd, struct mep, nd);
347 strlist__delete(me->metrics);
352 static void metricgroup__print_strlist(struct strlist *metrics, bool raw)
357 strlist__for_each_entry (sn, metrics) {
359 printf("%s%s", n > 0 ? " " : "", sn->s);
361 printf(" %s\n", sn->s);
368 void metricgroup__print(bool metrics, bool metricgroups, char *filter,
369 bool raw, bool details)
371 struct pmu_events_map *map = perf_pmu__find_map(NULL);
372 struct pmu_event *pe;
374 struct rblist groups;
375 struct rb_node *node, *next;
376 struct strlist *metriclist = NULL;
382 metriclist = strlist__new(NULL, NULL);
387 rblist__init(&groups);
388 groups.node_new = mep_new;
389 groups.node_cmp = mep_cmp;
390 groups.node_delete = mep_delete;
395 if (!pe->name && !pe->metric_group && !pe->metric_name)
397 if (!pe->metric_expr)
399 g = pe->metric_group;
400 if (!g && pe->metric_name) {
407 char *mg = strdup(g);
412 while ((g = strsep(&mg, ";")) != NULL) {
419 if (filter && !strstr(g, filter))
422 s = (char *)pe->metric_name;
424 if (asprintf(&s, "%s\n%*s%s]",
425 pe->metric_name, 8, "[", pe->desc) < 0)
429 if (asprintf(&s, "%s\n%*s%s]",
430 s, 8, "[", pe->metric_expr) < 0)
439 strlist__add(metriclist, s);
441 me = mep_lookup(&groups, g);
444 strlist__add(me->metrics, s);
451 if (metricgroups && !raw)
452 printf("\nMetric Groups:\n\n");
453 else if (metrics && !raw)
454 printf("\nMetrics:\n\n");
456 for (node = rb_first_cached(&groups.entries); node; node = next) {
457 struct mep *me = container_of(node, struct mep, nd);
460 printf("%s%s%s", me->name, metrics && !raw ? ":" : "", raw ? " " : "\n");
462 metricgroup__print_strlist(me->metrics, raw);
463 next = rb_next(node);
464 rblist__remove_node(&groups, node);
467 metricgroup__print_strlist(metriclist, raw);
468 strlist__delete(metriclist);
471 static void metricgroup__add_metric_weak_group(struct strbuf *events,
472 struct expr_parse_ctx *ctx)
474 struct hashmap_entry *cur;
476 bool no_group = true, has_duration = false;
478 hashmap__for_each_entry((&ctx->ids), cur, bkt) {
479 pr_debug("found event %s\n", (const char *)cur->key);
481 * Duration time maps to a software event and can make
482 * groups not count. Always use it outside a
485 if (!strcmp(cur->key, "duration_time")) {
489 strbuf_addf(events, "%s%s",
490 no_group ? "{" : ",",
491 (const char *)cur->key);
495 strbuf_addf(events, "}:W");
497 strbuf_addf(events, ",duration_time");
498 } else if (has_duration)
499 strbuf_addf(events, "duration_time");
502 static void metricgroup__add_metric_non_group(struct strbuf *events,
503 struct expr_parse_ctx *ctx)
505 struct hashmap_entry *cur;
509 hashmap__for_each_entry((&ctx->ids), cur, bkt) {
511 strbuf_addf(events, ",");
512 strbuf_addf(events, "%s", (const char *)cur->key);
517 static void metricgroup___watchdog_constraint_hint(const char *name, bool foot)
519 static bool violate_nmi_constraint;
522 pr_warning("Splitting metric group %s into standalone metrics.\n", name);
523 violate_nmi_constraint = true;
527 if (!violate_nmi_constraint)
530 pr_warning("Try disabling the NMI watchdog to comply NO_NMI_WATCHDOG metric constraint:\n"
531 " echo 0 > /proc/sys/kernel/nmi_watchdog\n"
533 " echo 1 > /proc/sys/kernel/nmi_watchdog\n");
536 static bool metricgroup__has_constraint(struct pmu_event *pe)
538 if (!pe->metric_constraint)
541 if (!strcmp(pe->metric_constraint, "NO_NMI_WATCHDOG") &&
542 sysctl__nmi_watchdog_enabled()) {
543 metricgroup___watchdog_constraint_hint(pe->metric_name, false);
550 int __weak arch_get_runtimeparam(void)
555 static int __metricgroup__add_metric(struct list_head *group_list,
556 struct pmu_event *pe,
557 bool metric_no_group,
562 eg = malloc(sizeof(*eg));
566 expr__ctx_init(&eg->pctx);
567 eg->metric_name = pe->metric_name;
568 eg->metric_expr = pe->metric_expr;
569 eg->metric_unit = pe->unit;
570 eg->runtime = runtime;
571 eg->has_constraint = metric_no_group || metricgroup__has_constraint(pe);
573 if (expr__find_other(pe->metric_expr, NULL, &eg->pctx, runtime) < 0) {
574 expr__ctx_clear(&eg->pctx);
579 if (list_empty(group_list))
580 list_add(&eg->nd, group_list);
582 struct list_head *pos;
584 /* Place the largest groups at the front. */
585 list_for_each_prev(pos, group_list) {
586 struct egroup *old = list_entry(pos, struct egroup, nd);
588 if (hashmap__size(&eg->pctx.ids) <=
589 hashmap__size(&old->pctx.ids))
592 list_add(&eg->nd, pos);
598 static int metricgroup__add_metric(const char *metric, bool metric_no_group,
599 struct strbuf *events,
600 struct list_head *group_list,
601 struct pmu_events_map *map)
603 struct pmu_event *pe;
606 bool has_match = false;
611 if (!pe->name && !pe->metric_group && !pe->metric_name) {
612 /* End of pmu events. */
617 if (!pe->metric_expr)
619 if (match_metric(pe->metric_group, metric) ||
620 match_metric(pe->metric_name, metric)) {
622 pr_debug("metric expr %s for %s\n", pe->metric_expr, pe->metric_name);
624 if (!strstr(pe->metric_expr, "?")) {
625 ret = __metricgroup__add_metric(group_list,
634 count = arch_get_runtimeparam();
636 /* This loop is added to create multiple
637 * events depend on count value and add
638 * those events to group_list.
641 for (j = 0; j < count; j++) {
642 ret = __metricgroup__add_metric(
651 list_for_each_entry(eg, group_list, nd) {
653 strbuf_addf(events, ",");
655 if (eg->has_constraint) {
656 metricgroup__add_metric_non_group(events,
659 metricgroup__add_metric_weak_group(events,
666 static int metricgroup__add_metric_list(const char *list, bool metric_no_group,
667 struct strbuf *events,
668 struct list_head *group_list,
669 struct pmu_events_map *map)
671 char *llist, *nlist, *p;
674 nlist = strdup(list);
679 strbuf_init(events, 100);
680 strbuf_addf(events, "%s", "");
682 while ((p = strsep(&llist, ",")) != NULL) {
683 ret = metricgroup__add_metric(p, metric_no_group, events,
685 if (ret == -EINVAL) {
686 fprintf(stderr, "Cannot find metric or group `%s'\n",
694 metricgroup___watchdog_constraint_hint(NULL, true);
699 static void metricgroup__free_egroups(struct list_head *group_list)
701 struct egroup *eg, *egtmp;
703 list_for_each_entry_safe (eg, egtmp, group_list, nd) {
704 expr__ctx_clear(&eg->pctx);
705 list_del_init(&eg->nd);
710 static int parse_groups(struct evlist *perf_evlist, const char *str,
711 bool metric_no_group,
712 bool metric_no_merge,
713 struct perf_pmu *fake_pmu,
714 struct rblist *metric_events,
715 struct pmu_events_map *map)
717 struct parse_events_error parse_error;
718 struct strbuf extra_events;
719 LIST_HEAD(group_list);
722 if (metric_events->nr_entries == 0)
723 metricgroup__rblist_init(metric_events);
724 ret = metricgroup__add_metric_list(str, metric_no_group,
725 &extra_events, &group_list, map);
728 pr_debug("adding %s\n", extra_events.buf);
729 bzero(&parse_error, sizeof(parse_error));
730 ret = __parse_events(perf_evlist, extra_events.buf, &parse_error, fake_pmu);
732 parse_events_print_error(&parse_error, extra_events.buf);
735 strbuf_release(&extra_events);
736 ret = metricgroup__setup_events(&group_list, metric_no_merge,
737 perf_evlist, metric_events);
739 metricgroup__free_egroups(&group_list);
743 int metricgroup__parse_groups(const struct option *opt,
745 bool metric_no_group,
746 bool metric_no_merge,
747 struct rblist *metric_events)
749 struct evlist *perf_evlist = *(struct evlist **)opt->value;
750 struct pmu_events_map *map = perf_pmu__find_map(NULL);
755 return parse_groups(perf_evlist, str, metric_no_group,
756 metric_no_merge, NULL, metric_events, map);
759 bool metricgroup__has_metric(const char *metric)
761 struct pmu_events_map *map = perf_pmu__find_map(NULL);
762 struct pmu_event *pe;
771 if (!pe->name && !pe->metric_group && !pe->metric_name)
773 if (!pe->metric_expr)
775 if (match_metric(pe->metric_name, metric))