blob: 49e6f7c6fb703b12aa605a472e18e1badb35aef1 [file] [log] [blame]
Thomas Gleixner2025cf92019-05-29 07:18:02 -07001// SPDX-License-Identifier: GPL-2.0-only
Andi Kleenb18f3e32017-08-31 12:40:31 -07002/*
3 * Copyright (c) 2017, Intel Corporation.
Andi Kleenb18f3e32017-08-31 12:40:31 -07004 */
5
6/* Manage metrics and groups of metrics from JSON files */
7
8#include "metricgroup.h"
Arnaldo Carvalho de Melob4209022019-08-29 15:56:40 -03009#include "debug.h"
Andi Kleenb18f3e32017-08-31 12:40:31 -070010#include "evlist.h"
Arnaldo Carvalho de Melo0b8026e2019-08-21 10:54:14 -030011#include "evsel.h"
Andi Kleenb18f3e32017-08-31 12:40:31 -070012#include "strbuf.h"
13#include "pmu.h"
14#include "expr.h"
15#include "rblist.h"
Andi Kleenb18f3e32017-08-31 12:40:31 -070016#include <string.h>
Andi Kleenb18f3e32017-08-31 12:40:31 -070017#include <errno.h>
18#include "pmu-events/pmu-events.h"
Andi Kleenb18f3e32017-08-31 12:40:31 -070019#include "strlist.h"
20#include <assert.h>
Arnaldo Carvalho de Melobd9860b2019-06-25 21:13:51 -030021#include <linux/ctype.h>
Arnaldo Carvalho de Melob4209022019-08-29 15:56:40 -030022#include <linux/string.h>
Arnaldo Carvalho de Melod8f9da22019-07-04 12:06:20 -030023#include <linux/zalloc.h>
Arnaldo Carvalho de Melo0b8026e2019-08-21 10:54:14 -030024#include <subcmd/parse-options.h>
Kan Liangab483d82020-02-24 13:59:23 -080025#include <api/fs/fs.h>
26#include "util.h"
Andi Kleenb18f3e32017-08-31 12:40:31 -070027
28struct metric_event *metricgroup__lookup(struct rblist *metric_events,
Jiri Olsa32dcd022019-07-21 13:23:51 +020029 struct evsel *evsel,
Andi Kleenb18f3e32017-08-31 12:40:31 -070030 bool create)
31{
32 struct rb_node *nd;
33 struct metric_event me = {
34 .evsel = evsel
35 };
Andi Kleen4bd1bef2017-11-17 13:43:00 -080036
37 if (!metric_events)
38 return NULL;
39
Andi Kleenb18f3e32017-08-31 12:40:31 -070040 nd = rblist__find(metric_events, &me);
41 if (nd)
42 return container_of(nd, struct metric_event, nd);
43 if (create) {
44 rblist__add_node(metric_events, &me);
45 nd = rblist__find(metric_events, &me);
46 if (nd)
47 return container_of(nd, struct metric_event, nd);
48 }
49 return NULL;
50}
51
52static int metric_event_cmp(struct rb_node *rb_node, const void *entry)
53{
54 struct metric_event *a = container_of(rb_node,
55 struct metric_event,
56 nd);
57 const struct metric_event *b = entry;
58
59 if (a->evsel == b->evsel)
60 return 0;
61 if ((char *)a->evsel < (char *)b->evsel)
62 return -1;
63 return +1;
64}
65
66static struct rb_node *metric_event_new(struct rblist *rblist __maybe_unused,
67 const void *entry)
68{
69 struct metric_event *me = malloc(sizeof(struct metric_event));
70
71 if (!me)
72 return NULL;
73 memcpy(me, entry, sizeof(struct metric_event));
74 me->evsel = ((struct metric_event *)entry)->evsel;
75 INIT_LIST_HEAD(&me->head);
76 return &me->nd;
77}
78
79static void metricgroup__rblist_init(struct rblist *metric_events)
80{
81 rblist__init(metric_events);
82 metric_events->node_cmp = metric_event_cmp;
83 metric_events->node_new = metric_event_new;
84}
85
86struct egroup {
87 struct list_head nd;
Ian Rogersded80bd2020-05-15 15:17:32 -070088 struct expr_parse_ctx pctx;
Andi Kleenb18f3e32017-08-31 12:40:31 -070089 const char *metric_name;
90 const char *metric_expr;
Jin Yao287f2642019-08-28 13:59:31 +080091 const char *metric_unit;
Kajol Jain1e1a8732020-04-02 02:03:37 +053092 int runtime;
Ian Rogers7f9eca52020-05-20 11:20:07 -070093 bool has_constraint;
Andi Kleenb18f3e32017-08-31 12:40:31 -070094};
95
Ian Rogers24406892020-05-20 11:20:09 -070096/**
97 * Find a group of events in perf_evlist that correpond to those from a parsed
Ian Rogers05530a72020-05-20 11:20:10 -070098 * 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.
Ian Rogers24406892020-05-20 11:20:09 -0700101 * @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.
Ian Rogers05530a72020-05-20 11:20:10 -0700105 * @metric_no_merge: don't attempt to share events for the metric with other
106 * metrics.
Ian Rogers24406892020-05-20 11:20:09 -0700107 * @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
110 * with the metric.
111 * @evlist_used: in/out argument, bitmap tracking which evlist events are used.
112 * @return the first metric event or NULL on failure.
113 */
Jiri Olsa63503db2019-07-21 13:23:52 +0200114static struct evsel *find_evsel_group(struct evlist *perf_evlist,
Ian Rogersded80bd2020-05-15 15:17:32 -0700115 struct expr_parse_ctx *pctx,
Ian Rogers05530a72020-05-20 11:20:10 -0700116 bool metric_no_merge,
Ian Rogers24406892020-05-20 11:20:09 -0700117 bool has_constraint,
Kajol Jain58fc90f2020-02-21 15:41:21 +0530118 struct evsel **metric_events,
Ian Rogers45db55f2020-05-20 00:28:08 -0700119 unsigned long *evlist_used)
Andi Kleenb18f3e32017-08-31 12:40:31 -0700120{
Ian Rogers24406892020-05-20 11:20:09 -0700121 struct evsel *ev, *current_leader = NULL;
Ian Rogersded80bd2020-05-15 15:17:32 -0700122 double *val_ptr;
Ian Rogers24406892020-05-20 11:20:09 -0700123 int i = 0, matched_events = 0, events_to_match;
124 const int idnum = (int)hashmap__size(&pctx->ids);
125
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;
130 else
131 events_to_match = idnum;
Andi Kleenb18f3e32017-08-31 12:40:31 -0700132
133 evlist__for_each_entry (perf_evlist, ev) {
Ian Rogers24406892020-05-20 11:20:09 -0700134 /*
135 * Events with a constraint aren't grouped and match the first
136 * events available.
137 */
138 if (has_constraint && ev->weak_group)
Kajol Jain58fc90f2020-02-21 15:41:21 +0530139 continue;
Ian Rogers05530a72020-05-20 11:20:10 -0700140 /* Ignore event if already used and merging is disabled. */
141 if (metric_no_merge && test_bit(ev->idx, evlist_used))
142 continue;
Ian Rogers24406892020-05-20 11:20:09 -0700143 if (!has_constraint && ev->leader != current_leader) {
144 /*
145 * Start of a new group, discard the whole match and
146 * start again.
147 */
148 matched_events = 0;
Kajol Jain58fc90f2020-02-21 15:41:21 +0530149 memset(metric_events, 0,
150 sizeof(struct evsel *) * idnum);
Ian Rogers24406892020-05-20 11:20:09 -0700151 current_leader = ev->leader;
152 }
Ian Rogers05530a72020-05-20 11:20:10 -0700153 if (hashmap__find(&pctx->ids, ev->name, (void **)&val_ptr)) {
154 if (has_constraint) {
155 /*
156 * Events aren't grouped, ensure the same event
157 * isn't matched from two groups.
158 */
159 for (i = 0; i < matched_events; i++) {
160 if (!strcmp(ev->name,
161 metric_events[i]->name)) {
162 break;
163 }
164 }
165 if (i != matched_events)
166 continue;
167 }
Ian Rogers24406892020-05-20 11:20:09 -0700168 metric_events[matched_events++] = ev;
Ian Rogers05530a72020-05-20 11:20:10 -0700169 }
Ian Rogers24406892020-05-20 11:20:09 -0700170 if (matched_events == events_to_match)
171 break;
172 }
173
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;
179 break;
180 }
Andi Kleenb18f3e32017-08-31 12:40:31 -0700181 }
182 }
Jin Yaof01642e2019-08-28 13:59:32 +0800183
Ian Rogers24406892020-05-20 11:20:09 -0700184 if (matched_events != idnum) {
Jin Yaof01642e2019-08-28 13:59:32 +0800185 /* Not whole match */
186 return NULL;
187 }
188
189 metric_events[idnum] = NULL;
190
191 for (i = 0; i < idnum; i++) {
Kajol Jain58fc90f2020-02-21 15:41:21 +0530192 ev = metric_events[i];
Ian Rogers24406892020-05-20 11:20:09 -0700193 ev->metric_leader = ev;
Ian Rogers45db55f2020-05-20 00:28:08 -0700194 set_bit(ev->idx, evlist_used);
Jin Yaof01642e2019-08-28 13:59:32 +0800195 }
196
197 return metric_events[0];
Andi Kleenb18f3e32017-08-31 12:40:31 -0700198}
199
200static int metricgroup__setup_events(struct list_head *groups,
Ian Rogers05530a72020-05-20 11:20:10 -0700201 bool metric_no_merge,
Jiri Olsa63503db2019-07-21 13:23:52 +0200202 struct evlist *perf_evlist,
Andi Kleenb18f3e32017-08-31 12:40:31 -0700203 struct rblist *metric_events_list)
204{
205 struct metric_event *me;
206 struct metric_expr *expr;
207 int i = 0;
208 int ret = 0;
209 struct egroup *eg;
Ian Rogers24406892020-05-20 11:20:09 -0700210 struct evsel *evsel, *tmp;
Ian Rogers45db55f2020-05-20 00:28:08 -0700211 unsigned long *evlist_used;
Kajol Jain58fc90f2020-02-21 15:41:21 +0530212
Ian Rogers45db55f2020-05-20 00:28:08 -0700213 evlist_used = bitmap_alloc(perf_evlist->core.nr_entries);
214 if (!evlist_used)
215 return -ENOMEM;
Andi Kleenb18f3e32017-08-31 12:40:31 -0700216
217 list_for_each_entry (eg, groups, nd) {
Jiri Olsa32dcd022019-07-21 13:23:51 +0200218 struct evsel **metric_events;
Andi Kleenb18f3e32017-08-31 12:40:31 -0700219
Ian Rogersded80bd2020-05-15 15:17:32 -0700220 metric_events = calloc(sizeof(void *),
221 hashmap__size(&eg->pctx.ids) + 1);
Andi Kleenb18f3e32017-08-31 12:40:31 -0700222 if (!metric_events) {
223 ret = -ENOMEM;
224 break;
225 }
Ian Rogers24406892020-05-20 11:20:09 -0700226 evsel = find_evsel_group(perf_evlist, &eg->pctx,
Ian Rogers05530a72020-05-20 11:20:10 -0700227 metric_no_merge,
228 eg->has_constraint, metric_events,
229 evlist_used);
Andi Kleenb18f3e32017-08-31 12:40:31 -0700230 if (!evsel) {
231 pr_debug("Cannot resolve %s: %s\n",
232 eg->metric_name, eg->metric_expr);
Ian Rogersa159e2f2020-05-07 22:36:24 -0700233 free(metric_events);
Andi Kleenb18f3e32017-08-31 12:40:31 -0700234 continue;
235 }
Ian Rogersded80bd2020-05-15 15:17:32 -0700236 for (i = 0; metric_events[i]; i++)
Andi Kleenb18f3e32017-08-31 12:40:31 -0700237 metric_events[i]->collect_stat = true;
238 me = metricgroup__lookup(metric_events_list, evsel, true);
239 if (!me) {
240 ret = -ENOMEM;
Ian Rogersa159e2f2020-05-07 22:36:24 -0700241 free(metric_events);
Andi Kleenb18f3e32017-08-31 12:40:31 -0700242 break;
243 }
244 expr = malloc(sizeof(struct metric_expr));
245 if (!expr) {
246 ret = -ENOMEM;
Ian Rogersa159e2f2020-05-07 22:36:24 -0700247 free(metric_events);
Andi Kleenb18f3e32017-08-31 12:40:31 -0700248 break;
249 }
250 expr->metric_expr = eg->metric_expr;
251 expr->metric_name = eg->metric_name;
Jin Yao287f2642019-08-28 13:59:31 +0800252 expr->metric_unit = eg->metric_unit;
Andi Kleenb18f3e32017-08-31 12:40:31 -0700253 expr->metric_events = metric_events;
Kajol Jain1e1a8732020-04-02 02:03:37 +0530254 expr->runtime = eg->runtime;
Andi Kleenb18f3e32017-08-31 12:40:31 -0700255 list_add(&expr->nd, &me->head);
256 }
Kajol Jain58fc90f2020-02-21 15:41:21 +0530257
Ian Rogers24406892020-05-20 11:20:09 -0700258 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);
262 }
263 }
Ian Rogers45db55f2020-05-20 00:28:08 -0700264 bitmap_free(evlist_used);
Kajol Jain58fc90f2020-02-21 15:41:21 +0530265
Andi Kleenb18f3e32017-08-31 12:40:31 -0700266 return ret;
267}
268
269static bool match_metric(const char *n, const char *list)
270{
271 int len;
272 char *m;
273
274 if (!list)
275 return false;
276 if (!strcmp(list, "all"))
277 return true;
278 if (!n)
279 return !strcasecmp(list, "No_group");
280 len = strlen(list);
281 m = strcasestr(n, list);
282 if (!m)
283 return false;
284 if ((m == n || m[-1] == ';' || m[-1] == ' ') &&
285 (m[len] == 0 || m[len] == ';'))
286 return true;
287 return false;
288}
289
Andi Kleen71b0acc2017-08-31 12:40:32 -0700290struct mep {
291 struct rb_node nd;
292 const char *name;
293 struct strlist *metrics;
294};
295
296static int mep_cmp(struct rb_node *rb_node, const void *entry)
297{
298 struct mep *a = container_of(rb_node, struct mep, nd);
299 struct mep *b = (struct mep *)entry;
300
301 return strcmp(a->name, b->name);
302}
303
304static struct rb_node *mep_new(struct rblist *rl __maybe_unused,
305 const void *entry)
306{
307 struct mep *me = malloc(sizeof(struct mep));
308
309 if (!me)
310 return NULL;
311 memcpy(me, entry, sizeof(struct mep));
312 me->name = strdup(me->name);
313 if (!me->name)
314 goto out_me;
315 me->metrics = strlist__new(NULL, NULL);
316 if (!me->metrics)
317 goto out_name;
318 return &me->nd;
319out_name:
Arnaldo Carvalho de Melod8f9da22019-07-04 12:06:20 -0300320 zfree(&me->name);
Andi Kleen71b0acc2017-08-31 12:40:32 -0700321out_me:
322 free(me);
323 return NULL;
324}
325
326static struct mep *mep_lookup(struct rblist *groups, const char *name)
327{
328 struct rb_node *nd;
329 struct mep me = {
330 .name = name
331 };
332 nd = rblist__find(groups, &me);
333 if (nd)
334 return container_of(nd, struct mep, nd);
335 rblist__add_node(groups, &me);
336 nd = rblist__find(groups, &me);
337 if (nd)
338 return container_of(nd, struct mep, nd);
339 return NULL;
340}
341
342static void mep_delete(struct rblist *rl __maybe_unused,
343 struct rb_node *nd)
344{
345 struct mep *me = container_of(nd, struct mep, nd);
346
347 strlist__delete(me->metrics);
Arnaldo Carvalho de Melod8f9da22019-07-04 12:06:20 -0300348 zfree(&me->name);
Andi Kleen71b0acc2017-08-31 12:40:32 -0700349 free(me);
350}
351
352static void metricgroup__print_strlist(struct strlist *metrics, bool raw)
353{
354 struct str_node *sn;
355 int n = 0;
356
357 strlist__for_each_entry (sn, metrics) {
358 if (raw)
359 printf("%s%s", n > 0 ? " " : "", sn->s);
360 else
361 printf(" %s\n", sn->s);
362 n++;
363 }
364 if (raw)
365 putchar('\n');
366}
367
368void metricgroup__print(bool metrics, bool metricgroups, char *filter,
Jiri Olsa33bbc572019-02-13 13:32:41 +0100369 bool raw, bool details)
Andi Kleen71b0acc2017-08-31 12:40:32 -0700370{
Ganapatrao Kulkarni54e32dc2017-10-17 00:02:18 +0530371 struct pmu_events_map *map = perf_pmu__find_map(NULL);
Andi Kleen71b0acc2017-08-31 12:40:32 -0700372 struct pmu_event *pe;
373 int i;
374 struct rblist groups;
375 struct rb_node *node, *next;
376 struct strlist *metriclist = NULL;
377
378 if (!map)
379 return;
380
381 if (!metricgroups) {
382 metriclist = strlist__new(NULL, NULL);
383 if (!metriclist)
384 return;
385 }
386
387 rblist__init(&groups);
388 groups.node_new = mep_new;
389 groups.node_cmp = mep_cmp;
390 groups.node_delete = mep_delete;
391 for (i = 0; ; i++) {
392 const char *g;
393 pe = &map->table[i];
394
395 if (!pe->name && !pe->metric_group && !pe->metric_name)
396 break;
397 if (!pe->metric_expr)
398 continue;
399 g = pe->metric_group;
400 if (!g && pe->metric_name) {
401 if (pe->name)
402 continue;
403 g = "No_group";
404 }
405 if (g) {
406 char *omg;
407 char *mg = strdup(g);
408
409 if (!mg)
410 return;
411 omg = mg;
412 while ((g = strsep(&mg, ";")) != NULL) {
413 struct mep *me;
414 char *s;
415
Arnaldo Carvalho de Melo80e90732019-06-26 11:21:47 -0300416 g = skip_spaces(g);
Andi Kleen71b0acc2017-08-31 12:40:32 -0700417 if (*g == 0)
418 g = "No_group";
Andi Kleen71b0acc2017-08-31 12:40:32 -0700419 if (filter && !strstr(g, filter))
420 continue;
421 if (raw)
422 s = (char *)pe->metric_name;
423 else {
Michael Petlan95f04322018-07-30 17:35:04 -0400424 if (asprintf(&s, "%s\n%*s%s]",
425 pe->metric_name, 8, "[", pe->desc) < 0)
Andi Kleen71b0acc2017-08-31 12:40:32 -0700426 return;
Jiri Olsa33bbc572019-02-13 13:32:41 +0100427
428 if (details) {
429 if (asprintf(&s, "%s\n%*s%s]",
430 s, 8, "[", pe->metric_expr) < 0)
431 return;
432 }
Andi Kleen71b0acc2017-08-31 12:40:32 -0700433 }
434
435 if (!s)
436 continue;
437
438 if (!metricgroups) {
439 strlist__add(metriclist, s);
440 } else {
441 me = mep_lookup(&groups, g);
442 if (!me)
443 continue;
444 strlist__add(me->metrics, s);
445 }
446 }
447 free(omg);
448 }
449 }
450
451 if (metricgroups && !raw)
452 printf("\nMetric Groups:\n\n");
453 else if (metrics && !raw)
454 printf("\nMetrics:\n\n");
455
Davidlohr Buesoca227022018-12-06 11:18:16 -0800456 for (node = rb_first_cached(&groups.entries); node; node = next) {
Andi Kleen71b0acc2017-08-31 12:40:32 -0700457 struct mep *me = container_of(node, struct mep, nd);
458
459 if (metricgroups)
Andi Kleen9c344d12019-06-28 15:07:36 -0700460 printf("%s%s%s", me->name, metrics && !raw ? ":" : "", raw ? " " : "\n");
Andi Kleen71b0acc2017-08-31 12:40:32 -0700461 if (metrics)
462 metricgroup__print_strlist(me->metrics, raw);
463 next = rb_next(node);
464 rblist__remove_node(&groups, node);
465 }
466 if (!metricgroups)
467 metricgroup__print_strlist(metriclist, raw);
468 strlist__delete(metriclist);
469}
470
Kan Liangf7426342020-02-24 13:59:21 -0800471static void metricgroup__add_metric_weak_group(struct strbuf *events,
Ian Rogersded80bd2020-05-15 15:17:32 -0700472 struct expr_parse_ctx *ctx)
Kan Liangf7426342020-02-24 13:59:21 -0800473{
Ian Rogersded80bd2020-05-15 15:17:32 -0700474 struct hashmap_entry *cur;
Ian Rogers4e21c132020-05-20 11:20:05 -0700475 size_t bkt;
476 bool no_group = true, has_duration = false;
Kan Liangf7426342020-02-24 13:59:21 -0800477
Ian Rogersded80bd2020-05-15 15:17:32 -0700478 hashmap__for_each_entry((&ctx->ids), cur, bkt) {
479 pr_debug("found event %s\n", (const char *)cur->key);
Kan Liangf7426342020-02-24 13:59:21 -0800480 /*
481 * Duration time maps to a software event and can make
482 * groups not count. Always use it outside a
483 * group.
484 */
Ian Rogersded80bd2020-05-15 15:17:32 -0700485 if (!strcmp(cur->key, "duration_time")) {
Ian Rogers4e21c132020-05-20 11:20:05 -0700486 has_duration = true;
Kan Liangf7426342020-02-24 13:59:21 -0800487 continue;
488 }
489 strbuf_addf(events, "%s%s",
Ian Rogers4e21c132020-05-20 11:20:05 -0700490 no_group ? "{" : ",",
Ian Rogersded80bd2020-05-15 15:17:32 -0700491 (const char *)cur->key);
Kan Liangf7426342020-02-24 13:59:21 -0800492 no_group = false;
493 }
Ian Rogers4e21c132020-05-20 11:20:05 -0700494 if (!no_group) {
Kan Liangf7426342020-02-24 13:59:21 -0800495 strbuf_addf(events, "}:W");
Ian Rogers4e21c132020-05-20 11:20:05 -0700496 if (has_duration)
497 strbuf_addf(events, ",duration_time");
498 } else if (has_duration)
499 strbuf_addf(events, "duration_time");
Kan Liangf7426342020-02-24 13:59:21 -0800500}
501
Kan Liangab483d82020-02-24 13:59:23 -0800502static void metricgroup__add_metric_non_group(struct strbuf *events,
Ian Rogersded80bd2020-05-15 15:17:32 -0700503 struct expr_parse_ctx *ctx)
Kan Liangab483d82020-02-24 13:59:23 -0800504{
Ian Rogersded80bd2020-05-15 15:17:32 -0700505 struct hashmap_entry *cur;
506 size_t bkt;
Ian Rogerse2ce1052020-05-20 11:20:11 -0700507 bool first = true;
Kan Liangab483d82020-02-24 13:59:23 -0800508
Ian Rogerse2ce1052020-05-20 11:20:11 -0700509 hashmap__for_each_entry((&ctx->ids), cur, bkt) {
510 if (!first)
511 strbuf_addf(events, ",");
512 strbuf_addf(events, "%s", (const char *)cur->key);
513 first = false;
514 }
Kan Liangab483d82020-02-24 13:59:23 -0800515}
516
517static void metricgroup___watchdog_constraint_hint(const char *name, bool foot)
518{
519 static bool violate_nmi_constraint;
520
521 if (!foot) {
522 pr_warning("Splitting metric group %s into standalone metrics.\n", name);
523 violate_nmi_constraint = true;
524 return;
525 }
526
527 if (!violate_nmi_constraint)
528 return;
529
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"
532 " perf stat ...\n"
533 " echo 1 > /proc/sys/kernel/nmi_watchdog\n");
534}
535
536static bool metricgroup__has_constraint(struct pmu_event *pe)
537{
538 if (!pe->metric_constraint)
539 return false;
540
541 if (!strcmp(pe->metric_constraint, "NO_NMI_WATCHDOG") &&
542 sysctl__nmi_watchdog_enabled()) {
543 metricgroup___watchdog_constraint_hint(pe->metric_name, false);
544 return true;
545 }
546
547 return false;
548}
549
Kajol Jain1e1a8732020-04-02 02:03:37 +0530550int __weak arch_get_runtimeparam(void)
551{
552 return 1;
553}
554
Ian Rogers7f9eca52020-05-20 11:20:07 -0700555static int __metricgroup__add_metric(struct list_head *group_list,
Ian Rogers05530a72020-05-20 11:20:10 -0700556 struct pmu_event *pe,
557 bool metric_no_group,
558 int runtime)
Kajol Jain47352ab2020-04-02 02:03:36 +0530559{
Kajol Jain47352ab2020-04-02 02:03:36 +0530560 struct egroup *eg;
561
Kajol Jain47352ab2020-04-02 02:03:36 +0530562 eg = malloc(sizeof(*eg));
563 if (!eg)
564 return -ENOMEM;
565
Ian Rogersded80bd2020-05-15 15:17:32 -0700566 expr__ctx_init(&eg->pctx);
Kajol Jain47352ab2020-04-02 02:03:36 +0530567 eg->metric_name = pe->metric_name;
568 eg->metric_expr = pe->metric_expr;
569 eg->metric_unit = pe->unit;
Kajol Jain1e1a8732020-04-02 02:03:37 +0530570 eg->runtime = runtime;
Ian Rogers05530a72020-05-20 11:20:10 -0700571 eg->has_constraint = metric_no_group || metricgroup__has_constraint(pe);
Ian Rogersded80bd2020-05-15 15:17:32 -0700572
573 if (expr__find_other(pe->metric_expr, NULL, &eg->pctx, runtime) < 0) {
574 expr__ctx_clear(&eg->pctx);
575 free(eg);
576 return -EINVAL;
577 }
578
Ian Rogers6bf21022020-05-20 11:20:08 -0700579 if (list_empty(group_list))
580 list_add(&eg->nd, group_list);
581 else {
582 struct list_head *pos;
583
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);
587
588 if (hashmap__size(&eg->pctx.ids) <=
589 hashmap__size(&old->pctx.ids))
590 break;
591 }
592 list_add(&eg->nd, pos);
593 }
Kajol Jain47352ab2020-04-02 02:03:36 +0530594
595 return 0;
596}
597
Ian Rogers05530a72020-05-20 11:20:10 -0700598static int metricgroup__add_metric(const char *metric, bool metric_no_group,
599 struct strbuf *events,
Andi Kleenb18f3e32017-08-31 12:40:31 -0700600 struct list_head *group_list)
601{
Ganapatrao Kulkarni54e32dc2017-10-17 00:02:18 +0530602 struct pmu_events_map *map = perf_pmu__find_map(NULL);
Andi Kleenb18f3e32017-08-31 12:40:31 -0700603 struct pmu_event *pe;
Ian Rogers7f9eca52020-05-20 11:20:07 -0700604 struct egroup *eg;
Ian Rogers90810392020-05-20 11:20:06 -0700605 int i, ret;
606 bool has_match = false;
Andi Kleenb18f3e32017-08-31 12:40:31 -0700607
Andi Kleenb18f3e32017-08-31 12:40:31 -0700608 if (!map)
609 return 0;
610
611 for (i = 0; ; i++) {
612 pe = &map->table[i];
613
Ian Rogers90810392020-05-20 11:20:06 -0700614 if (!pe->name && !pe->metric_group && !pe->metric_name) {
615 /* End of pmu events. */
616 if (!has_match)
617 return -EINVAL;
Andi Kleenb18f3e32017-08-31 12:40:31 -0700618 break;
Ian Rogers90810392020-05-20 11:20:06 -0700619 }
Andi Kleenb18f3e32017-08-31 12:40:31 -0700620 if (!pe->metric_expr)
621 continue;
622 if (match_metric(pe->metric_group, metric) ||
623 match_metric(pe->metric_name, metric)) {
Ian Rogers90810392020-05-20 11:20:06 -0700624 has_match = true;
Andi Kleenb18f3e32017-08-31 12:40:31 -0700625 pr_debug("metric expr %s for %s\n", pe->metric_expr, pe->metric_name);
626
Kajol Jain1e1a8732020-04-02 02:03:37 +0530627 if (!strstr(pe->metric_expr, "?")) {
Ian Rogers7f9eca52020-05-20 11:20:07 -0700628 ret = __metricgroup__add_metric(group_list,
Ian Rogers05530a72020-05-20 11:20:10 -0700629 pe,
630 metric_no_group,
631 1);
Ian Rogers90810392020-05-20 11:20:06 -0700632 if (ret)
633 return ret;
Kajol Jain1e1a8732020-04-02 02:03:37 +0530634 } else {
635 int j, count;
636
637 count = arch_get_runtimeparam();
638
639 /* This loop is added to create multiple
640 * events depend on count value and add
641 * those events to group_list.
642 */
643
Ian Rogers90810392020-05-20 11:20:06 -0700644 for (j = 0; j < count; j++) {
Ian Rogers7f9eca52020-05-20 11:20:07 -0700645 ret = __metricgroup__add_metric(
Ian Rogers05530a72020-05-20 11:20:10 -0700646 group_list, pe,
647 metric_no_group, j);
Ian Rogers90810392020-05-20 11:20:06 -0700648 if (ret)
649 return ret;
650 }
Kajol Jain1e1a8732020-04-02 02:03:37 +0530651 }
Andi Kleenb18f3e32017-08-31 12:40:31 -0700652 }
653 }
Ian Rogers7f9eca52020-05-20 11:20:07 -0700654 list_for_each_entry(eg, group_list, nd) {
655 if (events->len > 0)
656 strbuf_addf(events, ",");
657
658 if (eg->has_constraint) {
659 metricgroup__add_metric_non_group(events,
660 &eg->pctx);
661 } else {
662 metricgroup__add_metric_weak_group(events,
663 &eg->pctx);
664 }
665 }
Ian Rogers90810392020-05-20 11:20:06 -0700666 return 0;
Andi Kleenb18f3e32017-08-31 12:40:31 -0700667}
668
Ian Rogers05530a72020-05-20 11:20:10 -0700669static int metricgroup__add_metric_list(const char *list, bool metric_no_group,
670 struct strbuf *events,
Andi Kleenb18f3e32017-08-31 12:40:31 -0700671 struct list_head *group_list)
672{
673 char *llist, *nlist, *p;
674 int ret = -EINVAL;
675
676 nlist = strdup(list);
677 if (!nlist)
678 return -ENOMEM;
679 llist = nlist;
Andi Kleen411bc312017-09-14 13:57:35 -0700680
681 strbuf_init(events, 100);
682 strbuf_addf(events, "%s", "");
683
Andi Kleenb18f3e32017-08-31 12:40:31 -0700684 while ((p = strsep(&llist, ",")) != NULL) {
Ian Rogers05530a72020-05-20 11:20:10 -0700685 ret = metricgroup__add_metric(p, metric_no_group, events,
686 group_list);
Andi Kleenb18f3e32017-08-31 12:40:31 -0700687 if (ret == -EINVAL) {
688 fprintf(stderr, "Cannot find metric or group `%s'\n",
689 p);
690 break;
691 }
692 }
693 free(nlist);
Kan Liangab483d82020-02-24 13:59:23 -0800694
695 if (!ret)
696 metricgroup___watchdog_constraint_hint(NULL, true);
697
Andi Kleenb18f3e32017-08-31 12:40:31 -0700698 return ret;
699}
700
701static void metricgroup__free_egroups(struct list_head *group_list)
702{
703 struct egroup *eg, *egtmp;
Andi Kleenb18f3e32017-08-31 12:40:31 -0700704
705 list_for_each_entry_safe (eg, egtmp, group_list, nd) {
Ian Rogersded80bd2020-05-15 15:17:32 -0700706 expr__ctx_clear(&eg->pctx);
Arnaldo Carvalho de Meloacc7bfb2019-07-04 12:20:21 -0300707 list_del_init(&eg->nd);
Andi Kleenb18f3e32017-08-31 12:40:31 -0700708 free(eg);
709 }
710}
711
Jiri Olsa8b4468a2020-06-02 23:47:33 +0200712static int parse_groups(struct evlist *perf_evlist, const char *str,
713 bool metric_no_group,
714 bool metric_no_merge,
Jiri Olsa68173bd2020-06-09 12:50:42 -0300715 struct perf_pmu *fake_pmu,
Jiri Olsa8b4468a2020-06-02 23:47:33 +0200716 struct rblist *metric_events)
Andi Kleenb18f3e32017-08-31 12:40:31 -0700717{
718 struct parse_events_error parse_error;
Andi Kleenb18f3e32017-08-31 12:40:31 -0700719 struct strbuf extra_events;
720 LIST_HEAD(group_list);
721 int ret;
722
723 if (metric_events->nr_entries == 0)
724 metricgroup__rblist_init(metric_events);
Ian Rogers05530a72020-05-20 11:20:10 -0700725 ret = metricgroup__add_metric_list(str, metric_no_group,
726 &extra_events, &group_list);
Andi Kleenb18f3e32017-08-31 12:40:31 -0700727 if (ret)
728 return ret;
729 pr_debug("adding %s\n", extra_events.buf);
Ian Rogersa910e462019-11-15 23:46:52 -0800730 bzero(&parse_error, sizeof(parse_error));
Jiri Olsa68173bd2020-06-09 12:50:42 -0300731 ret = __parse_events(perf_evlist, extra_events.buf, &parse_error, fake_pmu);
Andi Kleenb18f3e32017-08-31 12:40:31 -0700732 if (ret) {
Andi Kleen333b5662017-09-13 14:50:06 -0700733 parse_events_print_error(&parse_error, extra_events.buf);
Andi Kleenb18f3e32017-08-31 12:40:31 -0700734 goto out;
735 }
736 strbuf_release(&extra_events);
Ian Rogers05530a72020-05-20 11:20:10 -0700737 ret = metricgroup__setup_events(&group_list, metric_no_merge,
738 perf_evlist, metric_events);
Andi Kleenb18f3e32017-08-31 12:40:31 -0700739out:
740 metricgroup__free_egroups(&group_list);
741 return ret;
742}
Thomas Richter742d92f2018-06-26 09:17:01 +0200743
Jiri Olsa8b4468a2020-06-02 23:47:33 +0200744int metricgroup__parse_groups(const struct option *opt,
745 const char *str,
746 bool metric_no_group,
747 bool metric_no_merge,
748 struct rblist *metric_events)
749{
750 struct evlist *perf_evlist = *(struct evlist **)opt->value;
751
752 return parse_groups(perf_evlist, str, metric_no_group,
Jiri Olsa68173bd2020-06-09 12:50:42 -0300753 metric_no_merge, NULL, metric_events);
Jiri Olsa8b4468a2020-06-02 23:47:33 +0200754}
755
Thomas Richter742d92f2018-06-26 09:17:01 +0200756bool metricgroup__has_metric(const char *metric)
757{
758 struct pmu_events_map *map = perf_pmu__find_map(NULL);
759 struct pmu_event *pe;
760 int i;
761
762 if (!map)
763 return false;
764
765 for (i = 0; ; i++) {
766 pe = &map->table[i];
767
768 if (!pe->name && !pe->metric_group && !pe->metric_name)
769 break;
770 if (!pe->metric_expr)
771 continue;
772 if (match_metric(pe->metric_name, metric))
773 return true;
774 }
775 return false;
776}