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
,
105 evlist__for_each_entry (perf_evlist
, ev
) {
106 if (evlist_used
[j
++])
108 if (!strcmp(ev
->name
, ids
[i
])) {
109 if (!metric_events
[i
])
110 metric_events
[i
] = ev
;
115 /* Discard the whole match and start again */
117 memset(metric_events
, 0,
118 sizeof(struct evsel
*) * idnum
);
120 if (!strcmp(ev
->name
, ids
[i
])) {
121 if (!metric_events
[i
])
122 metric_events
[i
] = ev
;
131 /* Not whole match */
135 metric_events
[idnum
] = NULL
;
137 for (i
= 0; i
< idnum
; i
++) {
138 leader_found
= false;
139 evlist__for_each_entry(perf_evlist
, ev
) {
140 if (!leader_found
&& (ev
== metric_events
[i
]))
144 !strcmp(ev
->name
, metric_events
[i
]->name
)) {
145 ev
->metric_leader
= metric_events
[i
];
149 ev
= metric_events
[i
];
150 evlist_used
[ev
->idx
] = true;
153 return metric_events
[0];
156 static int metricgroup__setup_events(struct list_head
*groups
,
157 struct evlist
*perf_evlist
,
158 struct rblist
*metric_events_list
)
160 struct metric_event
*me
;
161 struct metric_expr
*expr
;
168 evlist_used
= calloc(perf_evlist
->core
.nr_entries
, sizeof(bool));
174 list_for_each_entry (eg
, groups
, nd
) {
175 struct evsel
**metric_events
;
177 metric_events
= calloc(sizeof(void *), eg
->idnum
+ 1);
178 if (!metric_events
) {
182 evsel
= find_evsel_group(perf_evlist
, eg
->ids
, eg
->idnum
,
183 metric_events
, evlist_used
);
185 pr_debug("Cannot resolve %s: %s\n",
186 eg
->metric_name
, eg
->metric_expr
);
189 for (i
= 0; i
< eg
->idnum
; i
++)
190 metric_events
[i
]->collect_stat
= true;
191 me
= metricgroup__lookup(metric_events_list
, evsel
, true);
196 expr
= malloc(sizeof(struct metric_expr
));
201 expr
->metric_expr
= eg
->metric_expr
;
202 expr
->metric_name
= eg
->metric_name
;
203 expr
->metric_unit
= eg
->metric_unit
;
204 expr
->metric_events
= metric_events
;
205 list_add(&expr
->nd
, &me
->head
);
213 static bool match_metric(const char *n
, const char *list
)
220 if (!strcmp(list
, "all"))
223 return !strcasecmp(list
, "No_group");
225 m
= strcasestr(n
, list
);
228 if ((m
== n
|| m
[-1] == ';' || m
[-1] == ' ') &&
229 (m
[len
] == 0 || m
[len
] == ';'))
237 struct strlist
*metrics
;
240 static int mep_cmp(struct rb_node
*rb_node
, const void *entry
)
242 struct mep
*a
= container_of(rb_node
, struct mep
, nd
);
243 struct mep
*b
= (struct mep
*)entry
;
245 return strcmp(a
->name
, b
->name
);
248 static struct rb_node
*mep_new(struct rblist
*rl __maybe_unused
,
251 struct mep
*me
= malloc(sizeof(struct mep
));
255 memcpy(me
, entry
, sizeof(struct mep
));
256 me
->name
= strdup(me
->name
);
259 me
->metrics
= strlist__new(NULL
, NULL
);
270 static struct mep
*mep_lookup(struct rblist
*groups
, const char *name
)
276 nd
= rblist__find(groups
, &me
);
278 return container_of(nd
, struct mep
, nd
);
279 rblist__add_node(groups
, &me
);
280 nd
= rblist__find(groups
, &me
);
282 return container_of(nd
, struct mep
, nd
);
286 static void mep_delete(struct rblist
*rl __maybe_unused
,
289 struct mep
*me
= container_of(nd
, struct mep
, nd
);
291 strlist__delete(me
->metrics
);
296 static void metricgroup__print_strlist(struct strlist
*metrics
, bool raw
)
301 strlist__for_each_entry (sn
, metrics
) {
303 printf("%s%s", n
> 0 ? " " : "", sn
->s
);
305 printf(" %s\n", sn
->s
);
312 void metricgroup__print(bool metrics
, bool metricgroups
, char *filter
,
313 bool raw
, bool details
)
315 struct pmu_events_map
*map
= perf_pmu__find_map(NULL
);
316 struct pmu_event
*pe
;
318 struct rblist groups
;
319 struct rb_node
*node
, *next
;
320 struct strlist
*metriclist
= NULL
;
326 metriclist
= strlist__new(NULL
, NULL
);
331 rblist__init(&groups
);
332 groups
.node_new
= mep_new
;
333 groups
.node_cmp
= mep_cmp
;
334 groups
.node_delete
= mep_delete
;
339 if (!pe
->name
&& !pe
->metric_group
&& !pe
->metric_name
)
341 if (!pe
->metric_expr
)
343 g
= pe
->metric_group
;
344 if (!g
&& pe
->metric_name
) {
351 char *mg
= strdup(g
);
356 while ((g
= strsep(&mg
, ";")) != NULL
) {
363 if (filter
&& !strstr(g
, filter
))
366 s
= (char *)pe
->metric_name
;
368 if (asprintf(&s
, "%s\n%*s%s]",
369 pe
->metric_name
, 8, "[", pe
->desc
) < 0)
373 if (asprintf(&s
, "%s\n%*s%s]",
374 s
, 8, "[", pe
->metric_expr
) < 0)
383 strlist__add(metriclist
, s
);
385 me
= mep_lookup(&groups
, g
);
388 strlist__add(me
->metrics
, s
);
395 if (metricgroups
&& !raw
)
396 printf("\nMetric Groups:\n\n");
397 else if (metrics
&& !raw
)
398 printf("\nMetrics:\n\n");
400 for (node
= rb_first_cached(&groups
.entries
); node
; node
= next
) {
401 struct mep
*me
= container_of(node
, struct mep
, nd
);
404 printf("%s%s%s", me
->name
, metrics
&& !raw
? ":" : "", raw
? " " : "\n");
406 metricgroup__print_strlist(me
->metrics
, raw
);
407 next
= rb_next(node
);
408 rblist__remove_node(&groups
, node
);
411 metricgroup__print_strlist(metriclist
, raw
);
412 strlist__delete(metriclist
);
415 static void metricgroup__add_metric_weak_group(struct strbuf
*events
,
419 bool no_group
= false;
422 for (i
= 0; i
< idnum
; i
++) {
423 pr_debug("found event %s\n", ids
[i
]);
425 * Duration time maps to a software event and can make
426 * groups not count. Always use it outside a
429 if (!strcmp(ids
[i
], "duration_time")) {
431 strbuf_addf(events
, "}:W,");
432 strbuf_addf(events
, "duration_time");
436 strbuf_addf(events
, "%s%s",
437 i
== 0 || no_group
? "{" : ",",
442 strbuf_addf(events
, "}:W");
445 static void metricgroup__add_metric_non_group(struct strbuf
*events
,
451 for (i
= 0; i
< idnum
; i
++)
452 strbuf_addf(events
, ",%s", ids
[i
]);
455 static void metricgroup___watchdog_constraint_hint(const char *name
, bool foot
)
457 static bool violate_nmi_constraint
;
460 pr_warning("Splitting metric group %s into standalone metrics.\n", name
);
461 violate_nmi_constraint
= true;
465 if (!violate_nmi_constraint
)
468 pr_warning("Try disabling the NMI watchdog to comply NO_NMI_WATCHDOG metric constraint:\n"
469 " echo 0 > /proc/sys/kernel/nmi_watchdog\n"
471 " echo 1 > /proc/sys/kernel/nmi_watchdog\n");
474 static bool metricgroup__has_constraint(struct pmu_event
*pe
)
476 if (!pe
->metric_constraint
)
479 if (!strcmp(pe
->metric_constraint
, "NO_NMI_WATCHDOG") &&
480 sysctl__nmi_watchdog_enabled()) {
481 metricgroup___watchdog_constraint_hint(pe
->metric_name
, false);
488 static int metricgroup__add_metric(const char *metric
, struct strbuf
*events
,
489 struct list_head
*group_list
)
491 struct pmu_events_map
*map
= perf_pmu__find_map(NULL
);
492 struct pmu_event
*pe
;
493 int i
, ret
= -EINVAL
;
501 if (!pe
->name
&& !pe
->metric_group
&& !pe
->metric_name
)
503 if (!pe
->metric_expr
)
505 if (match_metric(pe
->metric_group
, metric
) ||
506 match_metric(pe
->metric_name
, metric
)) {
511 pr_debug("metric expr %s for %s\n", pe
->metric_expr
, pe
->metric_name
);
513 if (expr__find_other(pe
->metric_expr
,
514 NULL
, &ids
, &idnum
) < 0)
517 strbuf_addf(events
, ",");
519 if (metricgroup__has_constraint(pe
))
520 metricgroup__add_metric_non_group(events
, ids
, idnum
);
522 metricgroup__add_metric_weak_group(events
, ids
, idnum
);
524 eg
= malloc(sizeof(struct egroup
));
531 eg
->metric_name
= pe
->metric_name
;
532 eg
->metric_expr
= pe
->metric_expr
;
533 eg
->metric_unit
= pe
->unit
;
534 list_add_tail(&eg
->nd
, group_list
);
541 static int metricgroup__add_metric_list(const char *list
, struct strbuf
*events
,
542 struct list_head
*group_list
)
544 char *llist
, *nlist
, *p
;
547 nlist
= strdup(list
);
552 strbuf_init(events
, 100);
553 strbuf_addf(events
, "%s", "");
555 while ((p
= strsep(&llist
, ",")) != NULL
) {
556 ret
= metricgroup__add_metric(p
, events
, group_list
);
557 if (ret
== -EINVAL
) {
558 fprintf(stderr
, "Cannot find metric or group `%s'\n",
566 metricgroup___watchdog_constraint_hint(NULL
, true);
571 static void metricgroup__free_egroups(struct list_head
*group_list
)
573 struct egroup
*eg
, *egtmp
;
576 list_for_each_entry_safe (eg
, egtmp
, group_list
, nd
) {
577 for (i
= 0; i
< eg
->idnum
; i
++)
580 list_del_init(&eg
->nd
);
585 int metricgroup__parse_groups(const struct option
*opt
,
587 struct rblist
*metric_events
)
589 struct parse_events_error parse_error
;
590 struct evlist
*perf_evlist
= *(struct evlist
**)opt
->value
;
591 struct strbuf extra_events
;
592 LIST_HEAD(group_list
);
595 if (metric_events
->nr_entries
== 0)
596 metricgroup__rblist_init(metric_events
);
597 ret
= metricgroup__add_metric_list(str
, &extra_events
, &group_list
);
600 pr_debug("adding %s\n", extra_events
.buf
);
601 bzero(&parse_error
, sizeof(parse_error
));
602 ret
= parse_events(perf_evlist
, extra_events
.buf
, &parse_error
);
604 parse_events_print_error(&parse_error
, extra_events
.buf
);
607 strbuf_release(&extra_events
);
608 ret
= metricgroup__setup_events(&group_list
, perf_evlist
,
611 metricgroup__free_egroups(&group_list
);
615 bool metricgroup__has_metric(const char *metric
)
617 struct pmu_events_map
*map
= perf_pmu__find_map(NULL
);
618 struct pmu_event
*pe
;
627 if (!pe
->name
&& !pe
->metric_group
&& !pe
->metric_name
)
629 if (!pe
->metric_expr
)
631 if (match_metric(pe
->metric_name
, metric
))