blob: b071df373f8b923e5188033f7a1d04a04b50d930 [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;
88 int idnum;
89 const char **ids;
90 const char *metric_name;
91 const char *metric_expr;
Jin Yao287f2642019-08-28 13:59:31 +080092 const char *metric_unit;
Kajol Jain1e1a8732020-04-02 02:03:37 +053093 int runtime;
Andi Kleenb18f3e32017-08-31 12:40:31 -070094};
95
Jiri Olsa63503db2019-07-21 13:23:52 +020096static struct evsel *find_evsel_group(struct evlist *perf_evlist,
Jiri Olsa32dcd022019-07-21 13:23:51 +020097 const char **ids,
98 int idnum,
Kajol Jain58fc90f2020-02-21 15:41:21 +053099 struct evsel **metric_events,
100 bool *evlist_used)
Andi Kleenb18f3e32017-08-31 12:40:31 -0700101{
Jin Yaof01642e2019-08-28 13:59:32 +0800102 struct evsel *ev;
Kajol Jain58fc90f2020-02-21 15:41:21 +0530103 int i = 0, j = 0;
Jin Yaof01642e2019-08-28 13:59:32 +0800104 bool leader_found;
Andi Kleenb18f3e32017-08-31 12:40:31 -0700105
106 evlist__for_each_entry (perf_evlist, ev) {
Kajol Jain58fc90f2020-02-21 15:41:21 +0530107 if (evlist_used[j++])
108 continue;
Jin Yaof01642e2019-08-28 13:59:32 +0800109 if (!strcmp(ev->name, ids[i])) {
110 if (!metric_events[i])
111 metric_events[i] = ev;
Kajol Jaineb573e72019-11-20 14:10:59 +0530112 i++;
113 if (i == idnum)
114 break;
Andi Kleenb18f3e32017-08-31 12:40:31 -0700115 } else {
Kajol Jain58fc90f2020-02-21 15:41:21 +0530116 /* Discard the whole match and start again */
117 i = 0;
118 memset(metric_events, 0,
119 sizeof(struct evsel *) * idnum);
Jin Yaof01642e2019-08-28 13:59:32 +0800120
Kajol Jain58fc90f2020-02-21 15:41:21 +0530121 if (!strcmp(ev->name, ids[i])) {
122 if (!metric_events[i])
123 metric_events[i] = ev;
124 i++;
125 if (i == idnum)
126 break;
Andi Kleen2f87f332019-06-24 12:37:10 -0700127 }
Andi Kleenb18f3e32017-08-31 12:40:31 -0700128 }
129 }
Jin Yaof01642e2019-08-28 13:59:32 +0800130
Kajol Jaineb573e72019-11-20 14:10:59 +0530131 if (i != idnum) {
Jin Yaof01642e2019-08-28 13:59:32 +0800132 /* Not whole match */
133 return NULL;
134 }
135
136 metric_events[idnum] = NULL;
137
138 for (i = 0; i < idnum; i++) {
139 leader_found = false;
140 evlist__for_each_entry(perf_evlist, ev) {
141 if (!leader_found && (ev == metric_events[i]))
142 leader_found = true;
143
144 if (leader_found &&
145 !strcmp(ev->name, metric_events[i]->name)) {
146 ev->metric_leader = metric_events[i];
147 }
Kajol Jain58fc90f2020-02-21 15:41:21 +0530148 j++;
Jin Yaof01642e2019-08-28 13:59:32 +0800149 }
Kajol Jain58fc90f2020-02-21 15:41:21 +0530150 ev = metric_events[i];
151 evlist_used[ev->idx] = true;
Jin Yaof01642e2019-08-28 13:59:32 +0800152 }
153
154 return metric_events[0];
Andi Kleenb18f3e32017-08-31 12:40:31 -0700155}
156
157static int metricgroup__setup_events(struct list_head *groups,
Jiri Olsa63503db2019-07-21 13:23:52 +0200158 struct evlist *perf_evlist,
Andi Kleenb18f3e32017-08-31 12:40:31 -0700159 struct rblist *metric_events_list)
160{
161 struct metric_event *me;
162 struct metric_expr *expr;
163 int i = 0;
164 int ret = 0;
165 struct egroup *eg;
Jiri Olsa32dcd022019-07-21 13:23:51 +0200166 struct evsel *evsel;
Kajol Jain58fc90f2020-02-21 15:41:21 +0530167 bool *evlist_used;
168
169 evlist_used = calloc(perf_evlist->core.nr_entries, sizeof(bool));
170 if (!evlist_used) {
171 ret = -ENOMEM;
172 return ret;
173 }
Andi Kleenb18f3e32017-08-31 12:40:31 -0700174
175 list_for_each_entry (eg, groups, nd) {
Jiri Olsa32dcd022019-07-21 13:23:51 +0200176 struct evsel **metric_events;
Andi Kleenb18f3e32017-08-31 12:40:31 -0700177
178 metric_events = calloc(sizeof(void *), eg->idnum + 1);
179 if (!metric_events) {
180 ret = -ENOMEM;
181 break;
182 }
Andi Kleen2f87f332019-06-24 12:37:10 -0700183 evsel = find_evsel_group(perf_evlist, eg->ids, eg->idnum,
Kajol Jain58fc90f2020-02-21 15:41:21 +0530184 metric_events, evlist_used);
Andi Kleenb18f3e32017-08-31 12:40:31 -0700185 if (!evsel) {
186 pr_debug("Cannot resolve %s: %s\n",
187 eg->metric_name, eg->metric_expr);
188 continue;
189 }
190 for (i = 0; i < eg->idnum; i++)
191 metric_events[i]->collect_stat = true;
192 me = metricgroup__lookup(metric_events_list, evsel, true);
193 if (!me) {
194 ret = -ENOMEM;
195 break;
196 }
197 expr = malloc(sizeof(struct metric_expr));
198 if (!expr) {
199 ret = -ENOMEM;
200 break;
201 }
202 expr->metric_expr = eg->metric_expr;
203 expr->metric_name = eg->metric_name;
Jin Yao287f2642019-08-28 13:59:31 +0800204 expr->metric_unit = eg->metric_unit;
Andi Kleenb18f3e32017-08-31 12:40:31 -0700205 expr->metric_events = metric_events;
Kajol Jain1e1a8732020-04-02 02:03:37 +0530206 expr->runtime = eg->runtime;
Andi Kleenb18f3e32017-08-31 12:40:31 -0700207 list_add(&expr->nd, &me->head);
208 }
Kajol Jain58fc90f2020-02-21 15:41:21 +0530209
210 free(evlist_used);
211
Andi Kleenb18f3e32017-08-31 12:40:31 -0700212 return ret;
213}
214
215static bool match_metric(const char *n, const char *list)
216{
217 int len;
218 char *m;
219
220 if (!list)
221 return false;
222 if (!strcmp(list, "all"))
223 return true;
224 if (!n)
225 return !strcasecmp(list, "No_group");
226 len = strlen(list);
227 m = strcasestr(n, list);
228 if (!m)
229 return false;
230 if ((m == n || m[-1] == ';' || m[-1] == ' ') &&
231 (m[len] == 0 || m[len] == ';'))
232 return true;
233 return false;
234}
235
Andi Kleen71b0acc2017-08-31 12:40:32 -0700236struct mep {
237 struct rb_node nd;
238 const char *name;
239 struct strlist *metrics;
240};
241
242static int mep_cmp(struct rb_node *rb_node, const void *entry)
243{
244 struct mep *a = container_of(rb_node, struct mep, nd);
245 struct mep *b = (struct mep *)entry;
246
247 return strcmp(a->name, b->name);
248}
249
250static struct rb_node *mep_new(struct rblist *rl __maybe_unused,
251 const void *entry)
252{
253 struct mep *me = malloc(sizeof(struct mep));
254
255 if (!me)
256 return NULL;
257 memcpy(me, entry, sizeof(struct mep));
258 me->name = strdup(me->name);
259 if (!me->name)
260 goto out_me;
261 me->metrics = strlist__new(NULL, NULL);
262 if (!me->metrics)
263 goto out_name;
264 return &me->nd;
265out_name:
Arnaldo Carvalho de Melod8f9da22019-07-04 12:06:20 -0300266 zfree(&me->name);
Andi Kleen71b0acc2017-08-31 12:40:32 -0700267out_me:
268 free(me);
269 return NULL;
270}
271
272static struct mep *mep_lookup(struct rblist *groups, const char *name)
273{
274 struct rb_node *nd;
275 struct mep me = {
276 .name = name
277 };
278 nd = rblist__find(groups, &me);
279 if (nd)
280 return container_of(nd, struct mep, nd);
281 rblist__add_node(groups, &me);
282 nd = rblist__find(groups, &me);
283 if (nd)
284 return container_of(nd, struct mep, nd);
285 return NULL;
286}
287
288static void mep_delete(struct rblist *rl __maybe_unused,
289 struct rb_node *nd)
290{
291 struct mep *me = container_of(nd, struct mep, nd);
292
293 strlist__delete(me->metrics);
Arnaldo Carvalho de Melod8f9da22019-07-04 12:06:20 -0300294 zfree(&me->name);
Andi Kleen71b0acc2017-08-31 12:40:32 -0700295 free(me);
296}
297
298static void metricgroup__print_strlist(struct strlist *metrics, bool raw)
299{
300 struct str_node *sn;
301 int n = 0;
302
303 strlist__for_each_entry (sn, metrics) {
304 if (raw)
305 printf("%s%s", n > 0 ? " " : "", sn->s);
306 else
307 printf(" %s\n", sn->s);
308 n++;
309 }
310 if (raw)
311 putchar('\n');
312}
313
314void metricgroup__print(bool metrics, bool metricgroups, char *filter,
Jiri Olsa33bbc572019-02-13 13:32:41 +0100315 bool raw, bool details)
Andi Kleen71b0acc2017-08-31 12:40:32 -0700316{
Ganapatrao Kulkarni54e32dc2017-10-17 00:02:18 +0530317 struct pmu_events_map *map = perf_pmu__find_map(NULL);
Andi Kleen71b0acc2017-08-31 12:40:32 -0700318 struct pmu_event *pe;
319 int i;
320 struct rblist groups;
321 struct rb_node *node, *next;
322 struct strlist *metriclist = NULL;
323
324 if (!map)
325 return;
326
327 if (!metricgroups) {
328 metriclist = strlist__new(NULL, NULL);
329 if (!metriclist)
330 return;
331 }
332
333 rblist__init(&groups);
334 groups.node_new = mep_new;
335 groups.node_cmp = mep_cmp;
336 groups.node_delete = mep_delete;
337 for (i = 0; ; i++) {
338 const char *g;
339 pe = &map->table[i];
340
341 if (!pe->name && !pe->metric_group && !pe->metric_name)
342 break;
343 if (!pe->metric_expr)
344 continue;
345 g = pe->metric_group;
346 if (!g && pe->metric_name) {
347 if (pe->name)
348 continue;
349 g = "No_group";
350 }
351 if (g) {
352 char *omg;
353 char *mg = strdup(g);
354
355 if (!mg)
356 return;
357 omg = mg;
358 while ((g = strsep(&mg, ";")) != NULL) {
359 struct mep *me;
360 char *s;
361
Arnaldo Carvalho de Melo80e90732019-06-26 11:21:47 -0300362 g = skip_spaces(g);
Andi Kleen71b0acc2017-08-31 12:40:32 -0700363 if (*g == 0)
364 g = "No_group";
Andi Kleen71b0acc2017-08-31 12:40:32 -0700365 if (filter && !strstr(g, filter))
366 continue;
367 if (raw)
368 s = (char *)pe->metric_name;
369 else {
Michael Petlan95f04322018-07-30 17:35:04 -0400370 if (asprintf(&s, "%s\n%*s%s]",
371 pe->metric_name, 8, "[", pe->desc) < 0)
Andi Kleen71b0acc2017-08-31 12:40:32 -0700372 return;
Jiri Olsa33bbc572019-02-13 13:32:41 +0100373
374 if (details) {
375 if (asprintf(&s, "%s\n%*s%s]",
376 s, 8, "[", pe->metric_expr) < 0)
377 return;
378 }
Andi Kleen71b0acc2017-08-31 12:40:32 -0700379 }
380
381 if (!s)
382 continue;
383
384 if (!metricgroups) {
385 strlist__add(metriclist, s);
386 } else {
387 me = mep_lookup(&groups, g);
388 if (!me)
389 continue;
390 strlist__add(me->metrics, s);
391 }
392 }
393 free(omg);
394 }
395 }
396
397 if (metricgroups && !raw)
398 printf("\nMetric Groups:\n\n");
399 else if (metrics && !raw)
400 printf("\nMetrics:\n\n");
401
Davidlohr Buesoca227022018-12-06 11:18:16 -0800402 for (node = rb_first_cached(&groups.entries); node; node = next) {
Andi Kleen71b0acc2017-08-31 12:40:32 -0700403 struct mep *me = container_of(node, struct mep, nd);
404
405 if (metricgroups)
Andi Kleen9c344d12019-06-28 15:07:36 -0700406 printf("%s%s%s", me->name, metrics && !raw ? ":" : "", raw ? " " : "\n");
Andi Kleen71b0acc2017-08-31 12:40:32 -0700407 if (metrics)
408 metricgroup__print_strlist(me->metrics, raw);
409 next = rb_next(node);
410 rblist__remove_node(&groups, node);
411 }
412 if (!metricgroups)
413 metricgroup__print_strlist(metriclist, raw);
414 strlist__delete(metriclist);
415}
416
Kan Liangf7426342020-02-24 13:59:21 -0800417static void metricgroup__add_metric_weak_group(struct strbuf *events,
418 const char **ids,
419 int idnum)
420{
421 bool no_group = false;
422 int i;
423
424 for (i = 0; i < idnum; i++) {
425 pr_debug("found event %s\n", ids[i]);
426 /*
427 * Duration time maps to a software event and can make
428 * groups not count. Always use it outside a
429 * group.
430 */
431 if (!strcmp(ids[i], "duration_time")) {
432 if (i > 0)
433 strbuf_addf(events, "}:W,");
434 strbuf_addf(events, "duration_time");
435 no_group = true;
436 continue;
437 }
438 strbuf_addf(events, "%s%s",
439 i == 0 || no_group ? "{" : ",",
440 ids[i]);
441 no_group = false;
442 }
443 if (!no_group)
444 strbuf_addf(events, "}:W");
445}
446
Kan Liangab483d82020-02-24 13:59:23 -0800447static void metricgroup__add_metric_non_group(struct strbuf *events,
448 const char **ids,
449 int idnum)
450{
451 int i;
452
453 for (i = 0; i < idnum; i++)
454 strbuf_addf(events, ",%s", ids[i]);
455}
456
457static void metricgroup___watchdog_constraint_hint(const char *name, bool foot)
458{
459 static bool violate_nmi_constraint;
460
461 if (!foot) {
462 pr_warning("Splitting metric group %s into standalone metrics.\n", name);
463 violate_nmi_constraint = true;
464 return;
465 }
466
467 if (!violate_nmi_constraint)
468 return;
469
470 pr_warning("Try disabling the NMI watchdog to comply NO_NMI_WATCHDOG metric constraint:\n"
471 " echo 0 > /proc/sys/kernel/nmi_watchdog\n"
472 " perf stat ...\n"
473 " echo 1 > /proc/sys/kernel/nmi_watchdog\n");
474}
475
476static bool metricgroup__has_constraint(struct pmu_event *pe)
477{
478 if (!pe->metric_constraint)
479 return false;
480
481 if (!strcmp(pe->metric_constraint, "NO_NMI_WATCHDOG") &&
482 sysctl__nmi_watchdog_enabled()) {
483 metricgroup___watchdog_constraint_hint(pe->metric_name, false);
484 return true;
485 }
486
487 return false;
488}
489
Kajol Jain1e1a8732020-04-02 02:03:37 +0530490int __weak arch_get_runtimeparam(void)
491{
492 return 1;
493}
494
Kajol Jain47352ab2020-04-02 02:03:36 +0530495static int __metricgroup__add_metric(struct strbuf *events,
Kajol Jain1e1a8732020-04-02 02:03:37 +0530496 struct list_head *group_list, struct pmu_event *pe, int runtime)
Kajol Jain47352ab2020-04-02 02:03:36 +0530497{
498
499 const char **ids;
500 int idnum;
501 struct egroup *eg;
502
Kajol Jain1e1a8732020-04-02 02:03:37 +0530503 if (expr__find_other(pe->metric_expr, NULL, &ids, &idnum, runtime) < 0)
Kajol Jain47352ab2020-04-02 02:03:36 +0530504 return -EINVAL;
505
506 if (events->len > 0)
507 strbuf_addf(events, ",");
508
509 if (metricgroup__has_constraint(pe))
510 metricgroup__add_metric_non_group(events, ids, idnum);
511 else
512 metricgroup__add_metric_weak_group(events, ids, idnum);
513
514 eg = malloc(sizeof(*eg));
515 if (!eg)
516 return -ENOMEM;
517
518 eg->ids = ids;
519 eg->idnum = idnum;
520 eg->metric_name = pe->metric_name;
521 eg->metric_expr = pe->metric_expr;
522 eg->metric_unit = pe->unit;
Kajol Jain1e1a8732020-04-02 02:03:37 +0530523 eg->runtime = runtime;
Kajol Jain47352ab2020-04-02 02:03:36 +0530524 list_add_tail(&eg->nd, group_list);
525
526 return 0;
527}
528
Andi Kleenb18f3e32017-08-31 12:40:31 -0700529static int metricgroup__add_metric(const char *metric, struct strbuf *events,
530 struct list_head *group_list)
531{
Ganapatrao Kulkarni54e32dc2017-10-17 00:02:18 +0530532 struct pmu_events_map *map = perf_pmu__find_map(NULL);
Andi Kleenb18f3e32017-08-31 12:40:31 -0700533 struct pmu_event *pe;
Kan Liangf7426342020-02-24 13:59:21 -0800534 int i, ret = -EINVAL;
Andi Kleenb18f3e32017-08-31 12:40:31 -0700535
Andi Kleenb18f3e32017-08-31 12:40:31 -0700536 if (!map)
537 return 0;
538
539 for (i = 0; ; i++) {
540 pe = &map->table[i];
541
542 if (!pe->name && !pe->metric_group && !pe->metric_name)
543 break;
544 if (!pe->metric_expr)
545 continue;
546 if (match_metric(pe->metric_group, metric) ||
547 match_metric(pe->metric_name, metric)) {
Andi Kleenb18f3e32017-08-31 12:40:31 -0700548
549 pr_debug("metric expr %s for %s\n", pe->metric_expr, pe->metric_name);
550
Kajol Jain1e1a8732020-04-02 02:03:37 +0530551 if (!strstr(pe->metric_expr, "?")) {
552 ret = __metricgroup__add_metric(events, group_list, pe, 1);
553 } else {
554 int j, count;
555
556 count = arch_get_runtimeparam();
557
558 /* This loop is added to create multiple
559 * events depend on count value and add
560 * those events to group_list.
561 */
562
563 for (j = 0; j < count; j++)
564 ret = __metricgroup__add_metric(events, group_list, pe, j);
565 }
Kajol Jain47352ab2020-04-02 02:03:36 +0530566 if (ret == -ENOMEM)
Andi Kleenb18f3e32017-08-31 12:40:31 -0700567 break;
Andi Kleenb18f3e32017-08-31 12:40:31 -0700568 }
569 }
570 return ret;
571}
572
573static int metricgroup__add_metric_list(const char *list, struct strbuf *events,
574 struct list_head *group_list)
575{
576 char *llist, *nlist, *p;
577 int ret = -EINVAL;
578
579 nlist = strdup(list);
580 if (!nlist)
581 return -ENOMEM;
582 llist = nlist;
Andi Kleen411bc312017-09-14 13:57:35 -0700583
584 strbuf_init(events, 100);
585 strbuf_addf(events, "%s", "");
586
Andi Kleenb18f3e32017-08-31 12:40:31 -0700587 while ((p = strsep(&llist, ",")) != NULL) {
588 ret = metricgroup__add_metric(p, events, group_list);
589 if (ret == -EINVAL) {
590 fprintf(stderr, "Cannot find metric or group `%s'\n",
591 p);
592 break;
593 }
594 }
595 free(nlist);
Kan Liangab483d82020-02-24 13:59:23 -0800596
597 if (!ret)
598 metricgroup___watchdog_constraint_hint(NULL, true);
599
Andi Kleenb18f3e32017-08-31 12:40:31 -0700600 return ret;
601}
602
603static void metricgroup__free_egroups(struct list_head *group_list)
604{
605 struct egroup *eg, *egtmp;
606 int i;
607
608 list_for_each_entry_safe (eg, egtmp, group_list, nd) {
609 for (i = 0; i < eg->idnum; i++)
Arnaldo Carvalho de Melod8f9da22019-07-04 12:06:20 -0300610 zfree(&eg->ids[i]);
611 zfree(&eg->ids);
Arnaldo Carvalho de Meloacc7bfb2019-07-04 12:20:21 -0300612 list_del_init(&eg->nd);
Andi Kleenb18f3e32017-08-31 12:40:31 -0700613 free(eg);
614 }
615}
616
617int metricgroup__parse_groups(const struct option *opt,
618 const char *str,
619 struct rblist *metric_events)
620{
621 struct parse_events_error parse_error;
Jiri Olsa63503db2019-07-21 13:23:52 +0200622 struct evlist *perf_evlist = *(struct evlist **)opt->value;
Andi Kleenb18f3e32017-08-31 12:40:31 -0700623 struct strbuf extra_events;
624 LIST_HEAD(group_list);
625 int ret;
626
627 if (metric_events->nr_entries == 0)
628 metricgroup__rblist_init(metric_events);
629 ret = metricgroup__add_metric_list(str, &extra_events, &group_list);
630 if (ret)
631 return ret;
632 pr_debug("adding %s\n", extra_events.buf);
Ian Rogersa910e462019-11-15 23:46:52 -0800633 bzero(&parse_error, sizeof(parse_error));
Andi Kleenb18f3e32017-08-31 12:40:31 -0700634 ret = parse_events(perf_evlist, extra_events.buf, &parse_error);
635 if (ret) {
Andi Kleen333b5662017-09-13 14:50:06 -0700636 parse_events_print_error(&parse_error, extra_events.buf);
Andi Kleenb18f3e32017-08-31 12:40:31 -0700637 goto out;
638 }
639 strbuf_release(&extra_events);
640 ret = metricgroup__setup_events(&group_list, perf_evlist,
641 metric_events);
642out:
643 metricgroup__free_egroups(&group_list);
644 return ret;
645}
Thomas Richter742d92f2018-06-26 09:17:01 +0200646
647bool metricgroup__has_metric(const char *metric)
648{
649 struct pmu_events_map *map = perf_pmu__find_map(NULL);
650 struct pmu_event *pe;
651 int i;
652
653 if (!map)
654 return false;
655
656 for (i = 0; ; i++) {
657 pe = &map->table[i];
658
659 if (!pe->name && !pe->metric_group && !pe->metric_name)
660 break;
661 if (!pe->metric_expr)
662 continue;
663 if (match_metric(pe->metric_name, metric))
664 return true;
665 }
666 return false;
667}