blob: fffe02aae3ed1fa538da9400753218d2be37c232 [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"
Jin Yao0e0ae872021-09-03 10:52:39 +080014#include "pmu-hybrid.h"
Andi Kleenb18f3e32017-08-31 12:40:31 -070015#include "expr.h"
16#include "rblist.h"
Andi Kleenb18f3e32017-08-31 12:40:31 -070017#include <string.h>
Andi Kleenb18f3e32017-08-31 12:40:31 -070018#include <errno.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>
Ian Rogers80be6432021-10-15 10:21:20 -070022#include <linux/list_sort.h>
Arnaldo Carvalho de Melob4209022019-08-29 15:56:40 -030023#include <linux/string.h>
Arnaldo Carvalho de Melod8f9da22019-07-04 12:06:20 -030024#include <linux/zalloc.h>
Arnaldo Carvalho de Melo0b8026e2019-08-21 10:54:14 -030025#include <subcmd/parse-options.h>
Kan Liangab483d82020-02-24 13:59:23 -080026#include <api/fs/fs.h>
27#include "util.h"
Jiri Olsaf6fb0962020-07-19 20:13:16 +020028#include <asm/bug.h>
Namhyung Kimb214ba82020-09-24 21:44:53 +090029#include "cgroup.h"
Andi Kleenb18f3e32017-08-31 12:40:31 -070030
31struct metric_event *metricgroup__lookup(struct rblist *metric_events,
Jiri Olsa32dcd022019-07-21 13:23:51 +020032 struct evsel *evsel,
Andi Kleenb18f3e32017-08-31 12:40:31 -070033 bool create)
34{
35 struct rb_node *nd;
36 struct metric_event me = {
37 .evsel = evsel
38 };
Andi Kleen4bd1bef2017-11-17 13:43:00 -080039
40 if (!metric_events)
41 return NULL;
42
Andi Kleenb18f3e32017-08-31 12:40:31 -070043 nd = rblist__find(metric_events, &me);
44 if (nd)
45 return container_of(nd, struct metric_event, nd);
46 if (create) {
47 rblist__add_node(metric_events, &me);
48 nd = rblist__find(metric_events, &me);
49 if (nd)
50 return container_of(nd, struct metric_event, nd);
51 }
52 return NULL;
53}
54
55static int metric_event_cmp(struct rb_node *rb_node, const void *entry)
56{
57 struct metric_event *a = container_of(rb_node,
58 struct metric_event,
59 nd);
60 const struct metric_event *b = entry;
61
62 if (a->evsel == b->evsel)
63 return 0;
64 if ((char *)a->evsel < (char *)b->evsel)
65 return -1;
66 return +1;
67}
68
69static struct rb_node *metric_event_new(struct rblist *rblist __maybe_unused,
70 const void *entry)
71{
72 struct metric_event *me = malloc(sizeof(struct metric_event));
73
74 if (!me)
75 return NULL;
76 memcpy(me, entry, sizeof(struct metric_event));
77 me->evsel = ((struct metric_event *)entry)->evsel;
78 INIT_LIST_HEAD(&me->head);
79 return &me->nd;
80}
81
Jiri Olsa9afe5652020-06-02 23:47:38 +020082static void metric_event_delete(struct rblist *rblist __maybe_unused,
83 struct rb_node *rb_node)
84{
85 struct metric_event *me = container_of(rb_node, struct metric_event, nd);
86 struct metric_expr *expr, *tmp;
87
88 list_for_each_entry_safe(expr, tmp, &me->head, nd) {
Ian Rogersb85a4d62021-10-15 10:21:32 -070089 free((char *)expr->metric_name);
Jiri Olsa4ea28962020-07-19 20:13:10 +020090 free(expr->metric_refs);
Namhyung Kimb033ab12020-09-15 12:18:10 +090091 free(expr->metric_events);
Jiri Olsa9afe5652020-06-02 23:47:38 +020092 free(expr);
93 }
94
95 free(me);
96}
97
Andi Kleenb18f3e32017-08-31 12:40:31 -070098static void metricgroup__rblist_init(struct rblist *metric_events)
99{
100 rblist__init(metric_events);
101 metric_events->node_cmp = metric_event_cmp;
102 metric_events->node_new = metric_event_new;
Jiri Olsa9afe5652020-06-02 23:47:38 +0200103 metric_events->node_delete = metric_event_delete;
104}
105
106void metricgroup__rblist_exit(struct rblist *metric_events)
107{
108 rblist__exit(metric_events);
Andi Kleenb18f3e32017-08-31 12:40:31 -0700109}
110
Jiri Olsa83de0b72020-07-19 20:13:09 +0200111/*
112 * A node in the list of referenced metrics. metric_expr
113 * is held as a convenience to avoid a search through the
114 * metric list.
115 */
116struct metric_ref_node {
117 const char *metric_name;
118 const char *metric_expr;
119 struct list_head list;
120};
121
Ian Rogers485fcaed92021-10-15 10:21:22 -0700122/**
123 * The metric under construction. The data held here will be placed in a
124 * metric_expr.
125 */
Jiri Olsaa0c05b32020-07-19 20:13:19 +0200126struct metric {
Andi Kleenb18f3e32017-08-31 12:40:31 -0700127 struct list_head nd;
Ian Rogers485fcaed92021-10-15 10:21:22 -0700128 /**
129 * The expression parse context importantly holding the IDs contained
130 * within the expression.
131 */
Ian Rogerscb94a022021-09-23 00:46:04 -0700132 struct expr_parse_ctx *pctx;
Ian Rogers485fcaed92021-10-15 10:21:22 -0700133 /** The name of the metric such as "IPC". */
Andi Kleenb18f3e32017-08-31 12:40:31 -0700134 const char *metric_name;
Ian Rogersb85a4d62021-10-15 10:21:32 -0700135 /** Modifier on the metric such as "u" or NULL for none. */
136 const char *modifier;
Ian Rogers485fcaed92021-10-15 10:21:22 -0700137 /** The expression to parse, for example, "instructions/cycles". */
Andi Kleenb18f3e32017-08-31 12:40:31 -0700138 const char *metric_expr;
Ian Rogers485fcaed92021-10-15 10:21:22 -0700139 /**
140 * The "ScaleUnit" that scales and adds a unit to the metric during
141 * output.
142 */
Jin Yao287f2642019-08-28 13:59:31 +0800143 const char *metric_unit;
Ian Rogers46bdc0b2021-10-15 10:21:23 -0700144 /** Optional null terminated array of referenced metrics. */
145 struct metric_ref *metric_refs;
Ian Rogers485fcaed92021-10-15 10:21:22 -0700146 /**
147 * Is there a constraint on the group of events? In which case the
148 * events won't be grouped.
149 */
Ian Rogers7f9eca52020-05-20 11:20:07 -0700150 bool has_constraint;
Ian Rogers5ecd5a02021-10-15 10:21:29 -0700151 /**
152 * Parsed events for the metric. Optional as events may be taken from a
153 * different metric whose group contains all the IDs necessary for this
154 * one.
155 */
156 struct evlist *evlist;
Andi Kleenb18f3e32017-08-31 12:40:31 -0700157};
158
Ian Rogers3d81d762021-10-15 10:21:18 -0700159static void metricgroup___watchdog_constraint_hint(const char *name, bool foot)
160{
161 static bool violate_nmi_constraint;
162
163 if (!foot) {
164 pr_warning("Splitting metric group %s into standalone metrics.\n", name);
165 violate_nmi_constraint = true;
166 return;
167 }
168
169 if (!violate_nmi_constraint)
170 return;
171
172 pr_warning("Try disabling the NMI watchdog to comply NO_NMI_WATCHDOG metric constraint:\n"
173 " echo 0 > /proc/sys/kernel/nmi_watchdog\n"
174 " perf stat ...\n"
175 " echo 1 > /proc/sys/kernel/nmi_watchdog\n");
176}
177
178static bool metricgroup__has_constraint(const struct pmu_event *pe)
179{
180 if (!pe->metric_constraint)
181 return false;
182
183 if (!strcmp(pe->metric_constraint, "NO_NMI_WATCHDOG") &&
184 sysctl__nmi_watchdog_enabled()) {
185 metricgroup___watchdog_constraint_hint(pe->metric_name, false);
186 return true;
187 }
188
189 return false;
190}
191
192static struct metric *metric__new(const struct pmu_event *pe,
Ian Rogersb85a4d62021-10-15 10:21:32 -0700193 const char *modifier,
Ian Rogers3d81d762021-10-15 10:21:18 -0700194 bool metric_no_group,
195 int runtime)
196{
197 struct metric *m;
198
199 m = zalloc(sizeof(*m));
200 if (!m)
201 return NULL;
202
203 m->pctx = expr__ctx_new();
204 if (!m->pctx) {
205 free(m);
206 return NULL;
207 }
208
209 m->metric_name = pe->metric_name;
Ian Rogersb85a4d62021-10-15 10:21:32 -0700210 m->modifier = modifier ? strdup(modifier) : NULL;
211 if (modifier && !m->modifier) {
212 free(m);
213 expr__ctx_free(m->pctx);
214 return NULL;
215 }
Ian Rogers3d81d762021-10-15 10:21:18 -0700216 m->metric_expr = pe->metric_expr;
217 m->metric_unit = pe->unit;
218 m->pctx->runtime = runtime;
219 m->has_constraint = metric_no_group || metricgroup__has_constraint(pe);
Ian Rogers46bdc0b2021-10-15 10:21:23 -0700220 m->metric_refs = NULL;
Ian Rogers5ecd5a02021-10-15 10:21:29 -0700221 m->evlist = NULL;
Ian Rogers3d81d762021-10-15 10:21:18 -0700222
223 return m;
224}
225
226static void metric__free(struct metric *m)
227{
Ian Rogers46bdc0b2021-10-15 10:21:23 -0700228 free(m->metric_refs);
Ian Rogers3d81d762021-10-15 10:21:18 -0700229 expr__ctx_free(m->pctx);
Ian Rogersb85a4d62021-10-15 10:21:32 -0700230 free((char *)m->modifier);
Ian Rogersaba8c5e2021-11-07 01:00:02 -0800231 evlist__delete(m->evlist);
Ian Rogers3d81d762021-10-15 10:21:18 -0700232 free(m);
233}
234
Ian Rogersec5c5b32021-10-15 10:21:27 -0700235static bool contains_metric_id(struct evsel **metric_events, int num_events,
236 const char *metric_id)
Ian Rogersdcc81be2020-09-17 13:18:07 -0700237{
238 int i;
239
240 for (i = 0; i < num_events; i++) {
Ian Rogersec5c5b32021-10-15 10:21:27 -0700241 if (!strcmp(evsel__metric_id(metric_events[i]), metric_id))
Ian Rogersdcc81be2020-09-17 13:18:07 -0700242 return true;
243 }
244 return false;
245}
246
Ian Rogers24406892020-05-20 11:20:09 -0700247/**
Ian Rogers5ecd5a02021-10-15 10:21:29 -0700248 * setup_metric_events - Find a group of events in metric_evlist that correspond
249 * to the IDs from a parsed metric expression.
250 * @ids: the metric IDs to match.
251 * @metric_evlist: the list of perf events.
252 * @out_metric_events: holds the created metric events array.
Ian Rogers24406892020-05-20 11:20:09 -0700253 */
Ian Rogers5ecd5a02021-10-15 10:21:29 -0700254static int setup_metric_events(struct hashmap *ids,
255 struct evlist *metric_evlist,
256 struct evsel ***out_metric_events)
Andi Kleenb18f3e32017-08-31 12:40:31 -0700257{
Ian Rogers5ecd5a02021-10-15 10:21:29 -0700258 struct evsel **metric_events;
Ian Rogersec5c5b32021-10-15 10:21:27 -0700259 const char *metric_id;
Ian Rogers5ecd5a02021-10-15 10:21:29 -0700260 struct evsel *ev;
261 size_t ids_size, matched_events, i;
Ian Rogers24406892020-05-20 11:20:09 -0700262
Ian Rogers5ecd5a02021-10-15 10:21:29 -0700263 *out_metric_events = NULL;
264 ids_size = hashmap__size(ids);
265
266 metric_events = calloc(sizeof(void *), ids_size + 1);
267 if (!metric_events)
268 return -ENOMEM;
269
270 matched_events = 0;
271 evlist__for_each_entry(metric_evlist, ev) {
272 struct expr_id_data *val_ptr;
273
Ian Rogers762a05c2021-09-23 00:46:12 -0700274 /*
Ian Rogers5ecd5a02021-10-15 10:21:29 -0700275 * Check for duplicate events with the same name. For
276 * example, uncore_imc/cas_count_read/ will turn into 6
277 * events per socket on skylakex. Only the first such
278 * event is placed in metric_events.
Ian Rogers762a05c2021-09-23 00:46:12 -0700279 */
Ian Rogers5ecd5a02021-10-15 10:21:29 -0700280 metric_id = evsel__metric_id(ev);
281 if (contains_metric_id(metric_events, matched_events, metric_id))
282 continue;
283 /*
284 * Does this event belong to the parse context? For
285 * combined or shared groups, this metric may not care
286 * about this event.
287 */
288 if (hashmap__find(ids, metric_id, (void **)&val_ptr)) {
289 metric_events[matched_events++] = ev;
Andi Kleenb18f3e32017-08-31 12:40:31 -0700290
Ian Rogers5ecd5a02021-10-15 10:21:29 -0700291 if (matched_events >= ids_size)
Ian Rogers762a05c2021-09-23 00:46:12 -0700292 break;
Ian Rogers24406892020-05-20 11:20:09 -0700293 }
Ian Rogers24406892020-05-20 11:20:09 -0700294 }
Ian Rogers5ecd5a02021-10-15 10:21:29 -0700295 if (matched_events < ids_size) {
296 free(metric_events);
297 return -EINVAL;
Andi Kleenb18f3e32017-08-31 12:40:31 -0700298 }
Ian Rogers5ecd5a02021-10-15 10:21:29 -0700299 for (i = 0; i < ids_size; i++) {
Kajol Jain58fc90f2020-02-21 15:41:21 +0530300 ev = metric_events[i];
Ian Rogers5ecd5a02021-10-15 10:21:29 -0700301 ev->collect_stat = true;
302
Ian Rogersdcc81be2020-09-17 13:18:07 -0700303 /*
Ian Rogers5ecd5a02021-10-15 10:21:29 -0700304 * The metric leader points to the identically named
305 * event in metric_events.
Ian Rogersdcc81be2020-09-17 13:18:07 -0700306 */
307 ev->metric_leader = ev;
308 /*
Ian Rogers5ecd5a02021-10-15 10:21:29 -0700309 * Mark two events with identical names in the same
310 * group (or globally) as being in use as uncore events
311 * may be duplicated for each pmu. Set the metric leader
312 * of such events to be the event that appears in
313 * metric_events.
Ian Rogersdcc81be2020-09-17 13:18:07 -0700314 */
Ian Rogersec5c5b32021-10-15 10:21:27 -0700315 metric_id = evsel__metric_id(ev);
Ian Rogers5ecd5a02021-10-15 10:21:29 -0700316 evlist__for_each_entry_continue(metric_evlist, ev) {
317 if (!strcmp(evsel__metric_id(metric_events[i]), metric_id))
Ian Rogersdcc81be2020-09-17 13:18:07 -0700318 ev->metric_leader = metric_events[i];
Ian Rogersdcc81be2020-09-17 13:18:07 -0700319 }
Jin Yaof01642e2019-08-28 13:59:32 +0800320 }
Ian Rogers5ecd5a02021-10-15 10:21:29 -0700321 *out_metric_events = metric_events;
322 return 0;
Andi Kleenb18f3e32017-08-31 12:40:31 -0700323}
324
325static bool match_metric(const char *n, const char *list)
326{
327 int len;
328 char *m;
329
330 if (!list)
331 return false;
332 if (!strcmp(list, "all"))
333 return true;
334 if (!n)
335 return !strcasecmp(list, "No_group");
336 len = strlen(list);
337 m = strcasestr(n, list);
338 if (!m)
339 return false;
340 if ((m == n || m[-1] == ';' || m[-1] == ' ') &&
341 (m[len] == 0 || m[len] == ';'))
342 return true;
343 return false;
344}
345
Ian Rogers47f572a2021-10-15 10:21:15 -0700346static bool match_pe_metric(const struct pmu_event *pe, const char *metric)
John Garrybe335ec2020-12-04 19:10:15 +0800347{
348 return match_metric(pe->metric_group, metric) ||
349 match_metric(pe->metric_name, metric);
350}
351
Andi Kleen71b0acc2017-08-31 12:40:32 -0700352struct mep {
353 struct rb_node nd;
354 const char *name;
355 struct strlist *metrics;
356};
357
358static int mep_cmp(struct rb_node *rb_node, const void *entry)
359{
360 struct mep *a = container_of(rb_node, struct mep, nd);
361 struct mep *b = (struct mep *)entry;
362
363 return strcmp(a->name, b->name);
364}
365
366static struct rb_node *mep_new(struct rblist *rl __maybe_unused,
367 const void *entry)
368{
369 struct mep *me = malloc(sizeof(struct mep));
370
371 if (!me)
372 return NULL;
373 memcpy(me, entry, sizeof(struct mep));
374 me->name = strdup(me->name);
375 if (!me->name)
376 goto out_me;
377 me->metrics = strlist__new(NULL, NULL);
378 if (!me->metrics)
379 goto out_name;
380 return &me->nd;
381out_name:
Arnaldo Carvalho de Melod8f9da22019-07-04 12:06:20 -0300382 zfree(&me->name);
Andi Kleen71b0acc2017-08-31 12:40:32 -0700383out_me:
384 free(me);
385 return NULL;
386}
387
388static struct mep *mep_lookup(struct rblist *groups, const char *name)
389{
390 struct rb_node *nd;
391 struct mep me = {
392 .name = name
393 };
394 nd = rblist__find(groups, &me);
395 if (nd)
396 return container_of(nd, struct mep, nd);
397 rblist__add_node(groups, &me);
398 nd = rblist__find(groups, &me);
399 if (nd)
400 return container_of(nd, struct mep, nd);
401 return NULL;
402}
403
404static void mep_delete(struct rblist *rl __maybe_unused,
405 struct rb_node *nd)
406{
407 struct mep *me = container_of(nd, struct mep, nd);
408
409 strlist__delete(me->metrics);
Arnaldo Carvalho de Melod8f9da22019-07-04 12:06:20 -0300410 zfree(&me->name);
Andi Kleen71b0acc2017-08-31 12:40:32 -0700411 free(me);
412}
413
414static void metricgroup__print_strlist(struct strlist *metrics, bool raw)
415{
416 struct str_node *sn;
417 int n = 0;
418
419 strlist__for_each_entry (sn, metrics) {
420 if (raw)
421 printf("%s%s", n > 0 ? " " : "", sn->s);
422 else
423 printf(" %s\n", sn->s);
424 n++;
425 }
426 if (raw)
427 putchar('\n');
428}
429
Ian Rogers47f572a2021-10-15 10:21:15 -0700430static int metricgroup__print_pmu_event(const struct pmu_event *pe,
John Garryf6fe1e42020-12-04 19:10:13 +0800431 bool metricgroups, char *filter,
432 bool raw, bool details,
433 struct rblist *groups,
434 struct strlist *metriclist)
435{
436 const char *g;
437 char *omg, *mg;
438
439 g = pe->metric_group;
440 if (!g && pe->metric_name) {
441 if (pe->name)
442 return 0;
443 g = "No_group";
444 }
445
446 if (!g)
447 return 0;
448
449 mg = strdup(g);
450
451 if (!mg)
452 return -ENOMEM;
453 omg = mg;
454 while ((g = strsep(&mg, ";")) != NULL) {
455 struct mep *me;
456 char *s;
457
458 g = skip_spaces(g);
459 if (*g == 0)
460 g = "No_group";
461 if (filter && !strstr(g, filter))
462 continue;
463 if (raw)
464 s = (char *)pe->metric_name;
465 else {
466 if (asprintf(&s, "%s\n%*s%s]",
467 pe->metric_name, 8, "[", pe->desc) < 0)
468 return -1;
469 if (details) {
470 if (asprintf(&s, "%s\n%*s%s]",
471 s, 8, "[", pe->metric_expr) < 0)
472 return -1;
473 }
474 }
475
476 if (!s)
477 continue;
478
479 if (!metricgroups) {
480 strlist__add(metriclist, s);
481 } else {
482 me = mep_lookup(groups, g);
483 if (!me)
484 continue;
485 strlist__add(me->metrics, s);
486 }
487
488 if (!raw)
489 free(s);
490 }
491 free(omg);
492
493 return 0;
494}
495
John Garrya36fadb2020-12-04 19:10:14 +0800496struct metricgroup_print_sys_idata {
497 struct strlist *metriclist;
498 char *filter;
499 struct rblist *groups;
500 bool metricgroups;
501 bool raw;
502 bool details;
503};
504
Ian Rogers47f572a2021-10-15 10:21:15 -0700505typedef int (*metricgroup_sys_event_iter_fn)(const struct pmu_event *pe, void *);
John Garrya36fadb2020-12-04 19:10:14 +0800506
507struct metricgroup_iter_data {
508 metricgroup_sys_event_iter_fn fn;
509 void *data;
510};
511
Ian Rogers47f572a2021-10-15 10:21:15 -0700512static int metricgroup__sys_event_iter(const struct pmu_event *pe, void *data)
John Garrya36fadb2020-12-04 19:10:14 +0800513{
514 struct metricgroup_iter_data *d = data;
515 struct perf_pmu *pmu = NULL;
516
517 if (!pe->metric_expr || !pe->compat)
518 return 0;
519
520 while ((pmu = perf_pmu__scan(pmu))) {
521
522 if (!pmu->id || strcmp(pmu->id, pe->compat))
523 continue;
524
525 return d->fn(pe, d->data);
526 }
527
528 return 0;
529}
530
Ian Rogers47f572a2021-10-15 10:21:15 -0700531static int metricgroup__print_sys_event_iter(const struct pmu_event *pe, void *data)
John Garrya36fadb2020-12-04 19:10:14 +0800532{
533 struct metricgroup_print_sys_idata *d = data;
534
535 return metricgroup__print_pmu_event(pe, d->metricgroups, d->filter, d->raw,
536 d->details, d->groups, d->metriclist);
537}
538
Andi Kleen71b0acc2017-08-31 12:40:32 -0700539void metricgroup__print(bool metrics, bool metricgroups, char *filter,
Jin Yao0e0ae872021-09-03 10:52:39 +0800540 bool raw, bool details, const char *pmu_name)
Andi Kleen71b0acc2017-08-31 12:40:32 -0700541{
Ian Rogers0ec43c02021-10-15 10:21:13 -0700542 const struct pmu_events_map *map = pmu_events_map__find();
Ian Rogers47f572a2021-10-15 10:21:15 -0700543 const struct pmu_event *pe;
Andi Kleen71b0acc2017-08-31 12:40:32 -0700544 int i;
545 struct rblist groups;
546 struct rb_node *node, *next;
547 struct strlist *metriclist = NULL;
548
Andi Kleen71b0acc2017-08-31 12:40:32 -0700549 if (!metricgroups) {
550 metriclist = strlist__new(NULL, NULL);
551 if (!metriclist)
552 return;
553 }
554
555 rblist__init(&groups);
556 groups.node_new = mep_new;
557 groups.node_cmp = mep_cmp;
558 groups.node_delete = mep_delete;
John Garrya36fadb2020-12-04 19:10:14 +0800559 for (i = 0; map; i++) {
Andi Kleen71b0acc2017-08-31 12:40:32 -0700560 pe = &map->table[i];
561
562 if (!pe->name && !pe->metric_group && !pe->metric_name)
563 break;
564 if (!pe->metric_expr)
565 continue;
Jin Yao0e0ae872021-09-03 10:52:39 +0800566 if (pmu_name && perf_pmu__is_hybrid(pe->pmu) &&
567 strcmp(pmu_name, pe->pmu)) {
568 continue;
569 }
John Garryf6fe1e42020-12-04 19:10:13 +0800570 if (metricgroup__print_pmu_event(pe, metricgroups, filter,
571 raw, details, &groups,
572 metriclist) < 0)
573 return;
Andi Kleen71b0acc2017-08-31 12:40:32 -0700574 }
575
John Garrya36fadb2020-12-04 19:10:14 +0800576 {
577 struct metricgroup_iter_data data = {
578 .fn = metricgroup__print_sys_event_iter,
579 .data = (void *) &(struct metricgroup_print_sys_idata){
580 .metriclist = metriclist,
581 .metricgroups = metricgroups,
582 .filter = filter,
583 .raw = raw,
584 .details = details,
585 .groups = &groups,
586 },
587 };
588
589 pmu_for_each_sys_event(metricgroup__sys_event_iter, &data);
590 }
591
Namhyung Kimfac49a32020-09-09 14:58:48 +0900592 if (!filter || !rblist__empty(&groups)) {
593 if (metricgroups && !raw)
594 printf("\nMetric Groups:\n\n");
595 else if (metrics && !raw)
596 printf("\nMetrics:\n\n");
597 }
Andi Kleen71b0acc2017-08-31 12:40:32 -0700598
Davidlohr Buesoca227022018-12-06 11:18:16 -0800599 for (node = rb_first_cached(&groups.entries); node; node = next) {
Andi Kleen71b0acc2017-08-31 12:40:32 -0700600 struct mep *me = container_of(node, struct mep, nd);
601
602 if (metricgroups)
Andi Kleen9c344d12019-06-28 15:07:36 -0700603 printf("%s%s%s", me->name, metrics && !raw ? ":" : "", raw ? " " : "\n");
Andi Kleen71b0acc2017-08-31 12:40:32 -0700604 if (metrics)
605 metricgroup__print_strlist(me->metrics, raw);
606 next = rb_next(node);
607 rblist__remove_node(&groups, node);
608 }
609 if (!metricgroups)
610 metricgroup__print_strlist(metriclist, raw);
611 strlist__delete(metriclist);
612}
613
Ian Rogersec5c5b32021-10-15 10:21:27 -0700614static const char *code_characters = ",-=@";
615
616static int encode_metric_id(struct strbuf *sb, const char *x)
617{
618 char *c;
619 int ret = 0;
620
621 for (; *x; x++) {
622 c = strchr(code_characters, *x);
623 if (c) {
624 ret = strbuf_addch(sb, '!');
625 if (ret)
626 break;
627
628 ret = strbuf_addch(sb, '0' + (c - code_characters));
629 if (ret)
630 break;
631 } else {
632 ret = strbuf_addch(sb, *x);
633 if (ret)
634 break;
635 }
636 }
637 return ret;
638}
639
640static int decode_metric_id(struct strbuf *sb, const char *x)
641{
642 const char *orig = x;
643 size_t i;
644 char c;
645 int ret;
646
647 for (; *x; x++) {
648 c = *x;
649 if (*x == '!') {
650 x++;
651 i = *x - '0';
652 if (i > strlen(code_characters)) {
653 pr_err("Bad metric-id encoding in: '%s'", orig);
654 return -1;
655 }
656 c = code_characters[i];
657 }
658 ret = strbuf_addch(sb, c);
659 if (ret)
660 return ret;
661 }
662 return 0;
663}
664
Ian Rogersb85a4d62021-10-15 10:21:32 -0700665static int decode_all_metric_ids(struct evlist *perf_evlist, const char *modifier)
Ian Rogersec5c5b32021-10-15 10:21:27 -0700666{
667 struct evsel *ev;
668 struct strbuf sb = STRBUF_INIT;
669 char *cur;
670 int ret = 0;
671
672 evlist__for_each_entry(perf_evlist, ev) {
673 if (!ev->metric_id)
674 continue;
675
676 ret = strbuf_setlen(&sb, 0);
677 if (ret)
678 break;
679
680 ret = decode_metric_id(&sb, ev->metric_id);
681 if (ret)
682 break;
683
684 free((char *)ev->metric_id);
685 ev->metric_id = strdup(sb.buf);
686 if (!ev->metric_id) {
687 ret = -ENOMEM;
688 break;
689 }
690 /*
691 * If the name is just the parsed event, use the metric-id to
692 * give a more friendly display version.
693 */
694 if (strstr(ev->name, "metric-id=")) {
Ian Rogersb85a4d62021-10-15 10:21:32 -0700695 bool has_slash = false;
Ian Rogersec5c5b32021-10-15 10:21:27 -0700696
Ian Rogersb85a4d62021-10-15 10:21:32 -0700697 free(ev->name);
698 for (cur = strchr(sb.buf, '@') ; cur; cur = strchr(++cur, '@')) {
699 *cur = '/';
700 has_slash = true;
701 }
702
703 if (modifier) {
704 if (!has_slash && !strchr(sb.buf, ':')) {
705 ret = strbuf_addch(&sb, ':');
706 if (ret)
707 break;
708 }
709 ret = strbuf_addstr(&sb, modifier);
710 if (ret)
711 break;
712 }
Ian Rogersec5c5b32021-10-15 10:21:27 -0700713 ev->name = strdup(sb.buf);
714 if (!ev->name) {
715 ret = -ENOMEM;
716 break;
717 }
718 }
719 }
720 strbuf_release(&sb);
721 return ret;
722}
723
724static int metricgroup__build_event_string(struct strbuf *events,
725 const struct expr_parse_ctx *ctx,
Ian Rogersb85a4d62021-10-15 10:21:32 -0700726 const char *modifier,
Ian Rogersec5c5b32021-10-15 10:21:27 -0700727 bool has_constraint)
Kan Liangf7426342020-02-24 13:59:21 -0800728{
Ian Rogersded80bd2020-05-15 15:17:32 -0700729 struct hashmap_entry *cur;
Ian Rogers4e21c132020-05-20 11:20:05 -0700730 size_t bkt;
731 bool no_group = true, has_duration = false;
Ian Rogersec5c5b32021-10-15 10:21:27 -0700732 int ret = 0;
733
734#define RETURN_IF_NON_ZERO(x) do { if (x) return x; } while (0)
Kan Liangf7426342020-02-24 13:59:21 -0800735
Ian Rogerscb94a022021-09-23 00:46:04 -0700736 hashmap__for_each_entry(ctx->ids, cur, bkt) {
Ian Rogersec5c5b32021-10-15 10:21:27 -0700737 const char *sep, *rsep, *id = cur->key;
738
739 pr_debug("found event %s\n", id);
Kan Liangf7426342020-02-24 13:59:21 -0800740 /*
741 * Duration time maps to a software event and can make
742 * groups not count. Always use it outside a
743 * group.
744 */
Ian Rogersec5c5b32021-10-15 10:21:27 -0700745 if (!strcmp(id, "duration_time")) {
Ian Rogers4e21c132020-05-20 11:20:05 -0700746 has_duration = true;
Kan Liangf7426342020-02-24 13:59:21 -0800747 continue;
748 }
Ian Rogersec5c5b32021-10-15 10:21:27 -0700749 /* Separate events with commas and open the group if necessary. */
750 if (no_group) {
751 if (!has_constraint) {
752 ret = strbuf_addch(events, '{');
753 RETURN_IF_NON_ZERO(ret);
754 }
Kan Liangf7426342020-02-24 13:59:21 -0800755
Ian Rogersec5c5b32021-10-15 10:21:27 -0700756 no_group = false;
757 } else {
758 ret = strbuf_addch(events, ',');
759 RETURN_IF_NON_ZERO(ret);
760 }
761 /*
762 * Encode the ID as an event string. Add a qualifier for
763 * metric_id that is the original name except with characters
764 * that parse-events can't parse replaced. For example,
765 * 'msr@tsc@' gets added as msr/tsc,metric-id=msr!3tsc!3/
766 */
767 sep = strchr(id, '@');
768 if (sep != NULL) {
769 ret = strbuf_add(events, id, sep - id);
770 RETURN_IF_NON_ZERO(ret);
771 ret = strbuf_addch(events, '/');
772 RETURN_IF_NON_ZERO(ret);
773 rsep = strrchr(sep, '@');
774 ret = strbuf_add(events, sep + 1, rsep - sep - 1);
775 RETURN_IF_NON_ZERO(ret);
776 ret = strbuf_addstr(events, ",metric-id=");
777 RETURN_IF_NON_ZERO(ret);
778 sep = rsep;
779 } else {
780 sep = strchr(id, ':');
781 if (sep != NULL) {
782 ret = strbuf_add(events, id, sep - id);
783 RETURN_IF_NON_ZERO(ret);
784 } else {
785 ret = strbuf_addstr(events, id);
786 RETURN_IF_NON_ZERO(ret);
787 }
788 ret = strbuf_addstr(events, "/metric-id=");
789 RETURN_IF_NON_ZERO(ret);
790 }
791 ret = encode_metric_id(events, id);
792 RETURN_IF_NON_ZERO(ret);
793 ret = strbuf_addstr(events, "/");
794 RETURN_IF_NON_ZERO(ret);
Kan Liangab483d82020-02-24 13:59:23 -0800795
Ian Rogersec5c5b32021-10-15 10:21:27 -0700796 if (sep != NULL) {
797 ret = strbuf_addstr(events, sep + 1);
798 RETURN_IF_NON_ZERO(ret);
799 }
Ian Rogersb85a4d62021-10-15 10:21:32 -0700800 if (modifier) {
801 ret = strbuf_addstr(events, modifier);
802 RETURN_IF_NON_ZERO(ret);
803 }
Ian Rogerse2ce1052020-05-20 11:20:11 -0700804 }
Ian Rogersec5c5b32021-10-15 10:21:27 -0700805 if (has_duration) {
806 if (no_group) {
807 /* Strange case of a metric of just duration_time. */
808 ret = strbuf_addf(events, "duration_time");
809 } else if (!has_constraint)
810 ret = strbuf_addf(events, "}:W,duration_time");
811 else
812 ret = strbuf_addf(events, ",duration_time");
813 } else if (!no_group && !has_constraint)
814 ret = strbuf_addf(events, "}:W");
815
816 return ret;
817#undef RETURN_IF_NON_ZERO
Kan Liangab483d82020-02-24 13:59:23 -0800818}
819
Ian Rogers47f572a2021-10-15 10:21:15 -0700820int __weak arch_get_runtimeparam(const struct pmu_event *pe __maybe_unused)
Kajol Jain1e1a8732020-04-02 02:03:37 +0530821{
822 return 1;
823}
824
Ian Rogers80be6432021-10-15 10:21:20 -0700825/*
826 * A singly linked list on the stack of the names of metrics being
827 * processed. Used to identify recursion.
828 */
829struct visited_metric {
830 const char *name;
831 const struct visited_metric *parent;
832};
833
John Garrybe335ec2020-12-04 19:10:15 +0800834struct metricgroup_add_iter_data {
835 struct list_head *metric_list;
Ian Rogers68074812021-10-15 10:21:17 -0700836 const char *metric_name;
Ian Rogersb85a4d62021-10-15 10:21:32 -0700837 const char *modifier;
John Garrybe335ec2020-12-04 19:10:15 +0800838 int *ret;
839 bool *has_match;
840 bool metric_no_group;
Ian Rogers80be6432021-10-15 10:21:20 -0700841 struct metric *root_metric;
842 const struct visited_metric *visited;
843 const struct pmu_events_map *map;
John Garrybe335ec2020-12-04 19:10:15 +0800844};
845
Ian Rogers80be6432021-10-15 10:21:20 -0700846static int add_metric(struct list_head *metric_list,
847 const struct pmu_event *pe,
Ian Rogersb85a4d62021-10-15 10:21:32 -0700848 const char *modifier,
Ian Rogers80be6432021-10-15 10:21:20 -0700849 bool metric_no_group,
850 struct metric *root_metric,
851 const struct visited_metric *visited,
852 const struct pmu_events_map *map);
853
854/**
855 * resolve_metric - Locate metrics within the root metric and recursively add
856 * references to them.
857 * @metric_list: The list the metric is added to.
Ian Rogersb85a4d62021-10-15 10:21:32 -0700858 * @modifier: if non-null event modifiers like "u".
Ian Rogers80be6432021-10-15 10:21:20 -0700859 * @metric_no_group: Should events written to events be grouped "{}" or
860 * global. Grouping is the default but due to multiplexing the
861 * user may override.
862 * @root_metric: Metrics may reference other metrics to form a tree. In this
863 * case the root_metric holds all the IDs and a list of referenced
864 * metrics. When adding a root this argument is NULL.
865 * @visited: A singly linked list of metric names being added that is used to
866 * detect recursion.
867 * @map: The map that is searched for metrics, most commonly the table for the
868 * architecture perf is running upon.
869 */
870static int resolve_metric(struct list_head *metric_list,
Ian Rogersb85a4d62021-10-15 10:21:32 -0700871 const char *modifier,
Ian Rogers80be6432021-10-15 10:21:20 -0700872 bool metric_no_group,
873 struct metric *root_metric,
874 const struct visited_metric *visited,
875 const struct pmu_events_map *map)
876{
877 struct hashmap_entry *cur;
878 size_t bkt;
879 struct to_resolve {
880 /* The metric to resolve. */
881 const struct pmu_event *pe;
882 /*
883 * The key in the IDs map, this may differ from in case,
884 * etc. from pe->metric_name.
885 */
886 const char *key;
887 } *pending = NULL;
888 int i, ret = 0, pending_cnt = 0;
889
890 /*
891 * Iterate all the parsed IDs and if there's a matching metric and it to
892 * the pending array.
893 */
894 hashmap__for_each_entry(root_metric->pctx->ids, cur, bkt) {
895 const struct pmu_event *pe;
896
897 pe = metricgroup__find_metric(cur->key, map);
898 if (pe) {
899 pending = realloc(pending,
900 (pending_cnt + 1) * sizeof(struct to_resolve));
901 if (!pending)
902 return -ENOMEM;
903
904 pending[pending_cnt].pe = pe;
905 pending[pending_cnt].key = cur->key;
906 pending_cnt++;
907 }
908 }
909
910 /* Remove the metric IDs from the context. */
911 for (i = 0; i < pending_cnt; i++)
912 expr__del_id(root_metric->pctx, pending[i].key);
913
914 /*
915 * Recursively add all the metrics, IDs are added to the root metric's
916 * context.
917 */
918 for (i = 0; i < pending_cnt; i++) {
Ian Rogersb85a4d62021-10-15 10:21:32 -0700919 ret = add_metric(metric_list, pending[i].pe, modifier, metric_no_group,
Ian Rogers80be6432021-10-15 10:21:20 -0700920 root_metric, visited, map);
921 if (ret)
922 break;
923 }
924
925 free(pending);
926 return ret;
927}
928
Ian Rogers68074812021-10-15 10:21:17 -0700929/**
930 * __add_metric - Add a metric to metric_list.
931 * @metric_list: The list the metric is added to.
932 * @pe: The pmu_event containing the metric to be added.
Ian Rogersb85a4d62021-10-15 10:21:32 -0700933 * @modifier: if non-null event modifiers like "u".
Ian Rogers68074812021-10-15 10:21:17 -0700934 * @metric_no_group: Should events written to events be grouped "{}" or
935 * global. Grouping is the default but due to multiplexing the
936 * user may override.
937 * @runtime: A special argument for the parser only known at runtime.
Ian Rogers80be6432021-10-15 10:21:20 -0700938 * @root_metric: Metrics may reference other metrics to form a tree. In this
939 * case the root_metric holds all the IDs and a list of referenced
940 * metrics. When adding a root this argument is NULL.
941 * @visited: A singly linked list of metric names being added that is used to
942 * detect recursion.
943 * @map: The map that is searched for metrics, most commonly the table for the
944 * architecture perf is running upon.
Ian Rogers68074812021-10-15 10:21:17 -0700945 */
Jiri Olsa119e5212020-07-19 20:13:20 +0200946static int __add_metric(struct list_head *metric_list,
Ian Rogers47f572a2021-10-15 10:21:15 -0700947 const struct pmu_event *pe,
Ian Rogersb85a4d62021-10-15 10:21:32 -0700948 const char *modifier,
Jiri Olsae7e1badd2020-07-19 20:13:08 +0200949 bool metric_no_group,
Jiri Olsa83de0b72020-07-19 20:13:09 +0200950 int runtime,
Ian Rogers80be6432021-10-15 10:21:20 -0700951 struct metric *root_metric,
952 const struct visited_metric *visited,
953 const struct pmu_events_map *map)
Kajol Jain47352ab2020-04-02 02:03:36 +0530954{
Ian Rogers80be6432021-10-15 10:21:20 -0700955 const struct visited_metric *vm;
956 int ret;
957 bool is_root = !root_metric;
958 struct visited_metric visited_node = {
959 .name = pe->metric_name,
960 .parent = visited,
961 };
Kajol Jain47352ab2020-04-02 02:03:36 +0530962
Ian Rogers80be6432021-10-15 10:21:20 -0700963 for (vm = visited; vm; vm = vm->parent) {
964 if (!strcmp(pe->metric_name, vm->name)) {
965 pr_err("failed: recursion detected for %s\n", pe->metric_name);
966 return -1;
967 }
968 }
969
970 if (is_root) {
Jiri Olsa83de0b72020-07-19 20:13:09 +0200971 /*
Ian Rogers80be6432021-10-15 10:21:20 -0700972 * This metric is the root of a tree and may reference other
973 * metrics that are added recursively.
Jiri Olsa83de0b72020-07-19 20:13:09 +0200974 */
Ian Rogersb85a4d62021-10-15 10:21:32 -0700975 root_metric = metric__new(pe, modifier, metric_no_group, runtime);
Ian Rogers80be6432021-10-15 10:21:20 -0700976 if (!root_metric)
Jiri Olsa83de0b72020-07-19 20:13:09 +0200977 return -ENOMEM;
Kajol Jain47352ab2020-04-02 02:03:36 +0530978
Jiri Olsa83de0b72020-07-19 20:13:09 +0200979 } else {
Ian Rogers46bdc0b2021-10-15 10:21:23 -0700980 int cnt = 0;
981
Jiri Olsa83de0b72020-07-19 20:13:09 +0200982 /*
Ian Rogersa3de7692021-10-15 10:21:19 -0700983 * This metric was referenced in a metric higher in the
984 * tree. Check if the same metric is already resolved in the
985 * metric_refs list.
Jiri Olsa83de0b72020-07-19 20:13:09 +0200986 */
Ian Rogers46bdc0b2021-10-15 10:21:23 -0700987 if (root_metric->metric_refs) {
988 for (; root_metric->metric_refs[cnt].metric_name; cnt++) {
989 if (!strcmp(pe->metric_name,
990 root_metric->metric_refs[cnt].metric_name))
991 return 0;
992 }
Ian Rogersa3de7692021-10-15 10:21:19 -0700993 }
994
Ian Rogers46bdc0b2021-10-15 10:21:23 -0700995 /* Create reference. Need space for the entry and the terminator. */
996 root_metric->metric_refs = realloc(root_metric->metric_refs,
997 (cnt + 2) * sizeof(struct metric_ref));
998 if (!root_metric->metric_refs)
Jiri Olsa83de0b72020-07-19 20:13:09 +0200999 return -ENOMEM;
1000
1001 /*
1002 * Intentionally passing just const char pointers,
1003 * from 'pe' object, so they never go away. We don't
1004 * need to change them, so there's no need to create
1005 * our own copy.
1006 */
Ian Rogers46bdc0b2021-10-15 10:21:23 -07001007 root_metric->metric_refs[cnt].metric_name = pe->metric_name;
1008 root_metric->metric_refs[cnt].metric_expr = pe->metric_expr;
Jiri Olsa83de0b72020-07-19 20:13:09 +02001009
Ian Rogers46bdc0b2021-10-15 10:21:23 -07001010 /* Null terminate array. */
1011 root_metric->metric_refs[cnt+1].metric_name = NULL;
1012 root_metric->metric_refs[cnt+1].metric_expr = NULL;
Jiri Olsa83de0b72020-07-19 20:13:09 +02001013 }
1014
1015 /*
1016 * For both the parent and referenced metrics, we parse
Ian Rogers80be6432021-10-15 10:21:20 -07001017 * all the metric's IDs and add it to the root context.
Jiri Olsa83de0b72020-07-19 20:13:09 +02001018 */
Ian Rogers80be6432021-10-15 10:21:20 -07001019 if (expr__find_ids(pe->metric_expr, NULL, root_metric->pctx) < 0) {
1020 /* Broken metric. */
1021 ret = -EINVAL;
1022 } else {
1023 /* Resolve referenced metrics. */
Ian Rogersb85a4d62021-10-15 10:21:32 -07001024 ret = resolve_metric(metric_list, modifier, metric_no_group, root_metric,
Ian Rogers80be6432021-10-15 10:21:20 -07001025 &visited_node, map);
Ian Rogersded80bd2020-05-15 15:17:32 -07001026 }
1027
Ian Rogers80be6432021-10-15 10:21:20 -07001028 if (ret) {
1029 if (is_root)
1030 metric__free(root_metric);
Jiri Olsa83de0b72020-07-19 20:13:09 +02001031
Ian Rogers80be6432021-10-15 10:21:20 -07001032 } else if (is_root)
1033 list_add(&root_metric->nd, metric_list);
Ian Rogers6bf21022020-05-20 11:20:08 -07001034
Ian Rogers80be6432021-10-15 10:21:20 -07001035 return ret;
Kajol Jain47352ab2020-04-02 02:03:36 +05301036}
1037
John Garrybe335ec2020-12-04 19:10:15 +08001038#define map_for_each_event(__pe, __idx, __map) \
1039 if (__map) \
1040 for (__idx = 0, __pe = &__map->table[__idx]; \
1041 __pe->name || __pe->metric_group || __pe->metric_name; \
1042 __pe = &__map->table[++__idx])
Jiri Olsace391942020-07-19 20:13:06 +02001043
1044#define map_for_each_metric(__pe, __idx, __map, __metric) \
1045 map_for_each_event(__pe, __idx, __map) \
1046 if (__pe->metric_expr && \
1047 (match_metric(__pe->metric_group, __metric) || \
1048 match_metric(__pe->metric_name, __metric)))
1049
Ian Rogers47f572a2021-10-15 10:21:15 -07001050const struct pmu_event *metricgroup__find_metric(const char *metric,
1051 const struct pmu_events_map *map)
Jiri Olsa83de0b72020-07-19 20:13:09 +02001052{
Ian Rogers47f572a2021-10-15 10:21:15 -07001053 const struct pmu_event *pe;
Jiri Olsa83de0b72020-07-19 20:13:09 +02001054 int i;
1055
1056 map_for_each_event(pe, i, map) {
1057 if (match_metric(pe->metric_name, metric))
1058 return pe;
1059 }
1060
1061 return NULL;
1062}
1063
Jiri Olsa119e5212020-07-19 20:13:20 +02001064static int add_metric(struct list_head *metric_list,
Ian Rogers47f572a2021-10-15 10:21:15 -07001065 const struct pmu_event *pe,
Ian Rogersb85a4d62021-10-15 10:21:32 -07001066 const char *modifier,
Jiri Olsa83de0b72020-07-19 20:13:09 +02001067 bool metric_no_group,
Ian Rogers80be6432021-10-15 10:21:20 -07001068 struct metric *root_metric,
1069 const struct visited_metric *visited,
1070 const struct pmu_events_map *map)
Jiri Olsaa29c1642020-07-19 20:13:07 +02001071{
1072 int ret = 0;
1073
1074 pr_debug("metric expr %s for %s\n", pe->metric_expr, pe->metric_name);
1075
1076 if (!strstr(pe->metric_expr, "?")) {
Ian Rogersb85a4d62021-10-15 10:21:32 -07001077 ret = __add_metric(metric_list, pe, modifier, metric_no_group, 0,
Ian Rogers80be6432021-10-15 10:21:20 -07001078 root_metric, visited, map);
Jiri Olsaa29c1642020-07-19 20:13:07 +02001079 } else {
1080 int j, count;
1081
Kajol Jainf5a489d2020-09-07 12:11:32 +05301082 count = arch_get_runtimeparam(pe);
Jiri Olsaa29c1642020-07-19 20:13:07 +02001083
1084 /* This loop is added to create multiple
1085 * events depend on count value and add
Jiri Olsa119e5212020-07-19 20:13:20 +02001086 * those events to metric_list.
Jiri Olsaa29c1642020-07-19 20:13:07 +02001087 */
1088
Ian Rogers80be6432021-10-15 10:21:20 -07001089 for (j = 0; j < count && !ret; j++)
Ian Rogersb85a4d62021-10-15 10:21:32 -07001090 ret = __add_metric(metric_list, pe, modifier, metric_no_group, j,
Ian Rogers80be6432021-10-15 10:21:20 -07001091 root_metric, visited, map);
Jiri Olsaa29c1642020-07-19 20:13:07 +02001092 }
1093
1094 return ret;
1095}
1096
Ian Rogers47f572a2021-10-15 10:21:15 -07001097static int metricgroup__add_metric_sys_event_iter(const struct pmu_event *pe,
John Garrybe335ec2020-12-04 19:10:15 +08001098 void *data)
1099{
1100 struct metricgroup_add_iter_data *d = data;
1101 int ret;
1102
Ian Rogers68074812021-10-15 10:21:17 -07001103 if (!match_pe_metric(pe, d->metric_name))
John Garrybe335ec2020-12-04 19:10:15 +08001104 return 0;
1105
Ian Rogersb85a4d62021-10-15 10:21:32 -07001106 ret = add_metric(d->metric_list, pe, d->modifier, d->metric_no_group,
Ian Rogers80be6432021-10-15 10:21:20 -07001107 d->root_metric, d->visited, d->map);
John Garrybe335ec2020-12-04 19:10:15 +08001108 if (ret)
John Garryfe7a98b2021-06-10 22:33:00 +08001109 goto out;
John Garrybe335ec2020-12-04 19:10:15 +08001110
1111 *(d->has_match) = true;
1112
John Garryfe7a98b2021-06-10 22:33:00 +08001113out:
1114 *(d->ret) = ret;
1115 return ret;
John Garrybe335ec2020-12-04 19:10:15 +08001116}
1117
Ian Rogers80be6432021-10-15 10:21:20 -07001118static int metric_list_cmp(void *priv __maybe_unused, const struct list_head *l,
1119 const struct list_head *r)
1120{
1121 const struct metric *left = container_of(l, struct metric, nd);
1122 const struct metric *right = container_of(r, struct metric, nd);
1123
1124 return hashmap__size(right->pctx->ids) - hashmap__size(left->pctx->ids);
1125}
1126
Ian Rogers68074812021-10-15 10:21:17 -07001127/**
1128 * metricgroup__add_metric - Find and add a metric, or a metric group.
1129 * @metric_name: The name of the metric or metric group. For example, "IPC"
1130 * could be the name of a metric and "TopDownL1" the name of a
1131 * metric group.
Ian Rogersb85a4d62021-10-15 10:21:32 -07001132 * @modifier: if non-null event modifiers like "u".
Ian Rogers68074812021-10-15 10:21:17 -07001133 * @metric_no_group: Should events written to events be grouped "{}" or
1134 * global. Grouping is the default but due to multiplexing the
1135 * user may override.
Ian Rogers68074812021-10-15 10:21:17 -07001136 * @metric_list: The list that the metric or metric group are added to.
1137 * @map: The map that is searched for metrics, most commonly the table for the
1138 * architecture perf is running upon.
1139 */
Ian Rogersb85a4d62021-10-15 10:21:32 -07001140static int metricgroup__add_metric(const char *metric_name, const char *modifier,
1141 bool metric_no_group,
Jiri Olsa119e5212020-07-19 20:13:20 +02001142 struct list_head *metric_list,
Ian Rogers0ec43c02021-10-15 10:21:13 -07001143 const struct pmu_events_map *map)
Andi Kleenb18f3e32017-08-31 12:40:31 -07001144{
Ian Rogers47f572a2021-10-15 10:21:15 -07001145 const struct pmu_event *pe;
Jiri Olsa98461d92020-07-19 20:13:13 +02001146 LIST_HEAD(list);
Ian Rogers90810392020-05-20 11:20:06 -07001147 int i, ret;
1148 bool has_match = false;
Andi Kleenb18f3e32017-08-31 12:40:31 -07001149
Ian Rogers68074812021-10-15 10:21:17 -07001150 /*
1151 * Iterate over all metrics seeing if metric matches either the name or
1152 * group. When it does add the metric to the list.
1153 */
1154 map_for_each_metric(pe, i, map, metric_name) {
Jiri Olsace391942020-07-19 20:13:06 +02001155 has_match = true;
Ian Rogersb85a4d62021-10-15 10:21:32 -07001156 ret = add_metric(&list, pe, modifier, metric_no_group,
Ian Rogers80be6432021-10-15 10:21:20 -07001157 /*root_metric=*/NULL,
1158 /*visited_metrics=*/NULL, map);
Jiri Olsaa29c1642020-07-19 20:13:07 +02001159 if (ret)
Namhyung Kim27adafc2020-09-15 12:18:16 +09001160 goto out;
Andi Kleenb18f3e32017-08-31 12:40:31 -07001161 }
Jiri Olsace391942020-07-19 20:13:06 +02001162
John Garrybe335ec2020-12-04 19:10:15 +08001163 {
1164 struct metricgroup_iter_data data = {
1165 .fn = metricgroup__add_metric_sys_event_iter,
1166 .data = (void *) &(struct metricgroup_add_iter_data) {
1167 .metric_list = &list,
Ian Rogers68074812021-10-15 10:21:17 -07001168 .metric_name = metric_name,
Ian Rogersb85a4d62021-10-15 10:21:32 -07001169 .modifier = modifier,
John Garrybe335ec2020-12-04 19:10:15 +08001170 .metric_no_group = metric_no_group,
John Garrybe335ec2020-12-04 19:10:15 +08001171 .has_match = &has_match,
1172 .ret = &ret,
Ian Rogers80be6432021-10-15 10:21:20 -07001173 .map = map,
John Garrybe335ec2020-12-04 19:10:15 +08001174 },
1175 };
1176
1177 pmu_for_each_sys_event(metricgroup__sys_event_iter, &data);
1178 }
Jiri Olsace391942020-07-19 20:13:06 +02001179 /* End of pmu events. */
Ian Rogers5ecd5a02021-10-15 10:21:29 -07001180 if (!has_match)
Namhyung Kim27adafc2020-09-15 12:18:16 +09001181 ret = -EINVAL;
Jiri Olsa98461d92020-07-19 20:13:13 +02001182
Namhyung Kim27adafc2020-09-15 12:18:16 +09001183out:
1184 /*
1185 * add to metric_list so that they can be released
1186 * even if it's failed
1187 */
Jiri Olsa119e5212020-07-19 20:13:20 +02001188 list_splice(&list, metric_list);
Namhyung Kim27adafc2020-09-15 12:18:16 +09001189 return ret;
Andi Kleenb18f3e32017-08-31 12:40:31 -07001190}
1191
Ian Rogers68074812021-10-15 10:21:17 -07001192/**
1193 * metricgroup__add_metric_list - Find and add metrics, or metric groups,
1194 * specified in a list.
1195 * @list: the list of metrics or metric groups. For example, "IPC,CPI,TopDownL1"
1196 * would match the IPC and CPI metrics, and TopDownL1 would match all
1197 * the metrics in the TopDownL1 group.
1198 * @metric_no_group: Should events written to events be grouped "{}" or
1199 * global. Grouping is the default but due to multiplexing the
1200 * user may override.
Ian Rogers68074812021-10-15 10:21:17 -07001201 * @metric_list: The list that metrics are added to.
1202 * @map: The map that is searched for metrics, most commonly the table for the
1203 * architecture perf is running upon.
1204 */
Ian Rogers05530a72020-05-20 11:20:10 -07001205static int metricgroup__add_metric_list(const char *list, bool metric_no_group,
Jiri Olsa119e5212020-07-19 20:13:20 +02001206 struct list_head *metric_list,
Ian Rogers0ec43c02021-10-15 10:21:13 -07001207 const struct pmu_events_map *map)
Andi Kleenb18f3e32017-08-31 12:40:31 -07001208{
Ian Rogersb85a4d62021-10-15 10:21:32 -07001209 char *list_itr, *list_copy, *metric_name, *modifier;
Ian Rogersec5c5b32021-10-15 10:21:27 -07001210 int ret, count = 0;
Andi Kleenb18f3e32017-08-31 12:40:31 -07001211
Ian Rogersb85a4d62021-10-15 10:21:32 -07001212 list_copy = strdup(list);
1213 if (!list_copy)
Andi Kleenb18f3e32017-08-31 12:40:31 -07001214 return -ENOMEM;
Ian Rogersb85a4d62021-10-15 10:21:32 -07001215 list_itr = list_copy;
Andi Kleen411bc312017-09-14 13:57:35 -07001216
Ian Rogersb85a4d62021-10-15 10:21:32 -07001217 while ((metric_name = strsep(&list_itr, ",")) != NULL) {
1218 modifier = strchr(metric_name, ':');
1219 if (modifier)
1220 *modifier++ = '\0';
1221
1222 ret = metricgroup__add_metric(metric_name, modifier,
1223 metric_no_group, metric_list,
Ian Rogers5ecd5a02021-10-15 10:21:29 -07001224 map);
Ian Rogersec5c5b32021-10-15 10:21:27 -07001225 if (ret == -EINVAL)
Ian Rogersb85a4d62021-10-15 10:21:32 -07001226 pr_err("Cannot find metric or group `%s'\n", metric_name);
Ian Rogersec5c5b32021-10-15 10:21:27 -07001227
1228 if (ret)
Andi Kleenb18f3e32017-08-31 12:40:31 -07001229 break;
Ian Rogersec5c5b32021-10-15 10:21:27 -07001230
1231 count++;
Andi Kleenb18f3e32017-08-31 12:40:31 -07001232 }
Ian Rogersb85a4d62021-10-15 10:21:32 -07001233 free(list_copy);
Kan Liangab483d82020-02-24 13:59:23 -08001234
Ian Rogersec5c5b32021-10-15 10:21:27 -07001235 if (!ret) {
1236 /*
1237 * Warn about nmi_watchdog if any parsed metrics had the
1238 * NO_NMI_WATCHDOG constraint.
1239 */
Kan Liangab483d82020-02-24 13:59:23 -08001240 metricgroup___watchdog_constraint_hint(NULL, true);
Ian Rogersec5c5b32021-10-15 10:21:27 -07001241 /* No metrics. */
1242 if (count == 0)
1243 return -EINVAL;
1244 }
Andi Kleenb18f3e32017-08-31 12:40:31 -07001245 return ret;
1246}
1247
Jiri Olsa119e5212020-07-19 20:13:20 +02001248static void metricgroup__free_metrics(struct list_head *metric_list)
Andi Kleenb18f3e32017-08-31 12:40:31 -07001249{
Jiri Olsaa0c05b32020-07-19 20:13:19 +02001250 struct metric *m, *tmp;
Andi Kleenb18f3e32017-08-31 12:40:31 -07001251
Jiri Olsa119e5212020-07-19 20:13:20 +02001252 list_for_each_entry_safe (m, tmp, metric_list, nd) {
Jiri Olsaa0c05b32020-07-19 20:13:19 +02001253 list_del_init(&m->nd);
Ian Rogers3d81d762021-10-15 10:21:18 -07001254 metric__free(m);
Andi Kleenb18f3e32017-08-31 12:40:31 -07001255 }
1256}
1257
Ian Rogers5ecd5a02021-10-15 10:21:29 -07001258/**
1259 * build_combined_expr_ctx - Make an expr_parse_ctx with all has_constraint
1260 * metric IDs, as the IDs are held in a set,
1261 * duplicates will be removed.
1262 * @metric_list: List to take metrics from.
1263 * @combined: Out argument for result.
1264 */
1265static int build_combined_expr_ctx(const struct list_head *metric_list,
1266 struct expr_parse_ctx **combined)
1267{
1268 struct hashmap_entry *cur;
1269 size_t bkt;
1270 struct metric *m;
1271 char *dup;
1272 int ret;
1273
1274 *combined = expr__ctx_new();
1275 if (!*combined)
1276 return -ENOMEM;
1277
1278 list_for_each_entry(m, metric_list, nd) {
Ian Rogersb85a4d62021-10-15 10:21:32 -07001279 if (m->has_constraint && !m->modifier) {
Ian Rogers5ecd5a02021-10-15 10:21:29 -07001280 hashmap__for_each_entry(m->pctx->ids, cur, bkt) {
1281 dup = strdup(cur->key);
1282 if (!dup) {
1283 ret = -ENOMEM;
1284 goto err_out;
1285 }
1286 ret = expr__add_id(*combined, dup);
1287 if (ret)
1288 goto err_out;
1289 }
1290 }
1291 }
1292 return 0;
1293err_out:
1294 expr__ctx_free(*combined);
1295 *combined = NULL;
1296 return ret;
1297}
1298
1299/**
1300 * parse_ids - Build the event string for the ids and parse them creating an
1301 * evlist. The encoded metric_ids are decoded.
1302 * @fake_pmu: used when testing metrics not supported by the current CPU.
1303 * @ids: the event identifiers parsed from a metric.
Ian Rogersb85a4d62021-10-15 10:21:32 -07001304 * @modifier: any modifiers added to the events.
Ian Rogers5ecd5a02021-10-15 10:21:29 -07001305 * @has_constraint: false if events should be placed in a weak group.
1306 * @out_evlist: the created list of events.
1307 */
1308static int parse_ids(struct perf_pmu *fake_pmu, struct expr_parse_ctx *ids,
Ian Rogersb85a4d62021-10-15 10:21:32 -07001309 const char *modifier, bool has_constraint, struct evlist **out_evlist)
Ian Rogers5ecd5a02021-10-15 10:21:29 -07001310{
1311 struct parse_events_error parse_error;
1312 struct evlist *parsed_evlist;
1313 struct strbuf events = STRBUF_INIT;
1314 int ret;
1315
1316 *out_evlist = NULL;
1317 if (hashmap__size(ids->ids) == 0) {
1318 char *tmp;
1319 /*
1320 * No ids/events in the expression parsing context. Events may
1321 * have been removed because of constant evaluation, e.g.:
1322 * event1 if #smt_on else 0
1323 * Add a duration_time event to avoid a parse error on an empty
1324 * string.
1325 */
1326 tmp = strdup("duration_time");
1327 if (!tmp)
1328 return -ENOMEM;
1329
1330 ids__insert(ids->ids, tmp);
1331 }
Ian Rogersb85a4d62021-10-15 10:21:32 -07001332 ret = metricgroup__build_event_string(&events, ids, modifier,
1333 has_constraint);
Ian Rogers5ecd5a02021-10-15 10:21:29 -07001334 if (ret)
1335 return ret;
1336
1337 parsed_evlist = evlist__new();
1338 if (!parsed_evlist) {
1339 ret = -ENOMEM;
1340 goto err_out;
1341 }
1342 pr_debug("Parsing metric events '%s'\n", events.buf);
Ian Rogers07eafd42021-11-07 01:00:01 -08001343 parse_events_error__init(&parse_error);
Ian Rogers5ecd5a02021-10-15 10:21:29 -07001344 ret = __parse_events(parsed_evlist, events.buf, &parse_error, fake_pmu);
1345 if (ret) {
Ian Rogers6c191282021-11-07 01:00:00 -08001346 parse_events_error__print(&parse_error, events.buf);
Ian Rogers5ecd5a02021-10-15 10:21:29 -07001347 goto err_out;
1348 }
Ian Rogersb85a4d62021-10-15 10:21:32 -07001349 ret = decode_all_metric_ids(parsed_evlist, modifier);
Ian Rogers5ecd5a02021-10-15 10:21:29 -07001350 if (ret)
1351 goto err_out;
1352
1353 *out_evlist = parsed_evlist;
1354 parsed_evlist = NULL;
1355err_out:
Ian Rogers07eafd42021-11-07 01:00:01 -08001356 parse_events_error__exit(&parse_error);
Ian Rogers5ecd5a02021-10-15 10:21:29 -07001357 evlist__delete(parsed_evlist);
1358 strbuf_release(&events);
1359 return ret;
1360}
1361
Jiri Olsa8b4468a2020-06-02 23:47:33 +02001362static int parse_groups(struct evlist *perf_evlist, const char *str,
1363 bool metric_no_group,
1364 bool metric_no_merge,
Jiri Olsa68173bd2020-06-09 12:50:42 -03001365 struct perf_pmu *fake_pmu,
Ian Rogers5ecd5a02021-10-15 10:21:29 -07001366 struct rblist *metric_events_list,
Ian Rogers0ec43c02021-10-15 10:21:13 -07001367 const struct pmu_events_map *map)
Andi Kleenb18f3e32017-08-31 12:40:31 -07001368{
Ian Rogers5ecd5a02021-10-15 10:21:29 -07001369 struct evlist *combined_evlist = NULL;
Jiri Olsa119e5212020-07-19 20:13:20 +02001370 LIST_HEAD(metric_list);
Ian Rogers5ecd5a02021-10-15 10:21:29 -07001371 struct metric *m;
Andi Kleenb18f3e32017-08-31 12:40:31 -07001372 int ret;
1373
Ian Rogers5ecd5a02021-10-15 10:21:29 -07001374 if (metric_events_list->nr_entries == 0)
1375 metricgroup__rblist_init(metric_events_list);
Ian Rogers05530a72020-05-20 11:20:10 -07001376 ret = metricgroup__add_metric_list(str, metric_no_group,
Ian Rogers5ecd5a02021-10-15 10:21:29 -07001377 &metric_list, map);
Ian Rogersec5c5b32021-10-15 10:21:27 -07001378 if (ret)
1379 goto out;
1380
Ian Rogers5ecd5a02021-10-15 10:21:29 -07001381 /* Sort metrics from largest to smallest. */
1382 list_sort(NULL, &metric_list, metric_list_cmp);
1383
1384 if (!metric_no_merge) {
1385 struct expr_parse_ctx *combined = NULL;
1386
1387 ret = build_combined_expr_ctx(&metric_list, &combined);
1388
1389 if (!ret && combined && hashmap__size(combined->ids)) {
Ian Rogersb85a4d62021-10-15 10:21:32 -07001390 ret = parse_ids(fake_pmu, combined, /*modifier=*/NULL,
1391 /*has_constraint=*/true,
Ian Rogers5ecd5a02021-10-15 10:21:29 -07001392 &combined_evlist);
1393 }
1394 if (combined)
1395 expr__ctx_free(combined);
1396
1397 if (ret)
1398 goto out;
1399 }
1400
1401 list_for_each_entry(m, &metric_list, nd) {
1402 struct metric_event *me;
1403 struct evsel **metric_events;
1404 struct evlist *metric_evlist = NULL;
1405 struct metric *n;
1406 struct metric_expr *expr;
1407
1408 if (combined_evlist && m->has_constraint) {
1409 metric_evlist = combined_evlist;
1410 } else if (!metric_no_merge) {
1411 /*
1412 * See if the IDs for this metric are a subset of an
1413 * earlier metric.
1414 */
1415 list_for_each_entry(n, &metric_list, nd) {
1416 if (m == n)
1417 break;
1418
1419 if (n->evlist == NULL)
1420 continue;
1421
Ian Rogersb85a4d62021-10-15 10:21:32 -07001422 if ((!m->modifier && n->modifier) ||
1423 (m->modifier && !n->modifier) ||
1424 (m->modifier && n->modifier &&
1425 strcmp(m->modifier, n->modifier)))
1426 continue;
1427
Ian Rogers5ecd5a02021-10-15 10:21:29 -07001428 if (expr__subset_of_ids(n->pctx, m->pctx)) {
1429 pr_debug("Events in '%s' fully contained within '%s'\n",
1430 m->metric_name, n->metric_name);
1431 metric_evlist = n->evlist;
1432 break;
1433 }
1434
1435 }
1436 }
1437 if (!metric_evlist) {
Ian Rogersb85a4d62021-10-15 10:21:32 -07001438 ret = parse_ids(fake_pmu, m->pctx, m->modifier,
1439 m->has_constraint, &m->evlist);
Ian Rogers5ecd5a02021-10-15 10:21:29 -07001440 if (ret)
1441 goto out;
1442
1443 metric_evlist = m->evlist;
1444 }
1445 ret = setup_metric_events(m->pctx->ids, metric_evlist, &metric_events);
1446 if (ret) {
1447 pr_debug("Cannot resolve IDs for %s: %s\n",
1448 m->metric_name, m->metric_expr);
1449 goto out;
1450 }
1451
1452 me = metricgroup__lookup(metric_events_list, metric_events[0], true);
1453
1454 expr = malloc(sizeof(struct metric_expr));
1455 if (!expr) {
1456 ret = -ENOMEM;
1457 free(metric_events);
1458 goto out;
1459 }
1460
1461 expr->metric_refs = m->metric_refs;
1462 m->metric_refs = NULL;
1463 expr->metric_expr = m->metric_expr;
Ian Rogersb85a4d62021-10-15 10:21:32 -07001464 if (m->modifier) {
1465 char *tmp;
1466
1467 if (asprintf(&tmp, "%s:%s", m->metric_name, m->modifier) < 0)
1468 expr->metric_name = NULL;
1469 else
1470 expr->metric_name = tmp;
1471 } else
1472 expr->metric_name = strdup(m->metric_name);
1473
1474 if (!expr->metric_name) {
1475 ret = -ENOMEM;
1476 free(metric_events);
1477 goto out;
1478 }
Ian Rogers5ecd5a02021-10-15 10:21:29 -07001479 expr->metric_unit = m->metric_unit;
1480 expr->metric_events = metric_events;
1481 expr->runtime = m->pctx->runtime;
1482 list_add(&expr->nd, &me->head);
1483 }
1484
1485
Ian Rogersaba8c5e2021-11-07 01:00:02 -08001486 if (combined_evlist) {
Ian Rogers5ecd5a02021-10-15 10:21:29 -07001487 evlist__splice_list_tail(perf_evlist, &combined_evlist->core.entries);
Ian Rogersaba8c5e2021-11-07 01:00:02 -08001488 evlist__delete(combined_evlist);
1489 }
Ian Rogers5ecd5a02021-10-15 10:21:29 -07001490
1491 list_for_each_entry(m, &metric_list, nd) {
1492 if (m->evlist)
1493 evlist__splice_list_tail(perf_evlist, &m->evlist->core.entries);
1494 }
1495
Andi Kleenb18f3e32017-08-31 12:40:31 -07001496out:
Jiri Olsa119e5212020-07-19 20:13:20 +02001497 metricgroup__free_metrics(&metric_list);
Andi Kleenb18f3e32017-08-31 12:40:31 -07001498 return ret;
1499}
Thomas Richter742d92f2018-06-26 09:17:01 +02001500
Jiri Olsa8b4468a2020-06-02 23:47:33 +02001501int metricgroup__parse_groups(const struct option *opt,
1502 const char *str,
1503 bool metric_no_group,
1504 bool metric_no_merge,
1505 struct rblist *metric_events)
1506{
1507 struct evlist *perf_evlist = *(struct evlist **)opt->value;
Ian Rogers0ec43c02021-10-15 10:21:13 -07001508 const struct pmu_events_map *map = pmu_events_map__find();
Jiri Olsa8b4468a2020-06-02 23:47:33 +02001509
1510 return parse_groups(perf_evlist, str, metric_no_group,
Jiri Olsa13813962020-06-09 12:57:47 -03001511 metric_no_merge, NULL, metric_events, map);
Jiri Olsa8b4468a2020-06-02 23:47:33 +02001512}
1513
Jiri Olsaf78ac002020-06-02 23:47:36 +02001514int metricgroup__parse_groups_test(struct evlist *evlist,
Ian Rogers0ec43c02021-10-15 10:21:13 -07001515 const struct pmu_events_map *map,
Jiri Olsaf78ac002020-06-02 23:47:36 +02001516 const char *str,
1517 bool metric_no_group,
1518 bool metric_no_merge,
1519 struct rblist *metric_events)
1520{
1521 return parse_groups(evlist, str, metric_no_group,
1522 metric_no_merge, &perf_pmu__fake, metric_events, map);
1523}
1524
Thomas Richter742d92f2018-06-26 09:17:01 +02001525bool metricgroup__has_metric(const char *metric)
1526{
Ian Rogers0ec43c02021-10-15 10:21:13 -07001527 const struct pmu_events_map *map = pmu_events_map__find();
Ian Rogers47f572a2021-10-15 10:21:15 -07001528 const struct pmu_event *pe;
Thomas Richter742d92f2018-06-26 09:17:01 +02001529 int i;
1530
1531 if (!map)
1532 return false;
1533
1534 for (i = 0; ; i++) {
1535 pe = &map->table[i];
1536
1537 if (!pe->name && !pe->metric_group && !pe->metric_name)
1538 break;
1539 if (!pe->metric_expr)
1540 continue;
1541 if (match_metric(pe->metric_name, metric))
1542 return true;
1543 }
1544 return false;
1545}
Namhyung Kimb214ba82020-09-24 21:44:53 +09001546
1547int metricgroup__copy_metric_events(struct evlist *evlist, struct cgroup *cgrp,
1548 struct rblist *new_metric_events,
1549 struct rblist *old_metric_events)
1550{
1551 unsigned i;
1552
1553 for (i = 0; i < rblist__nr_entries(old_metric_events); i++) {
1554 struct rb_node *nd;
1555 struct metric_event *old_me, *new_me;
1556 struct metric_expr *old_expr, *new_expr;
1557 struct evsel *evsel;
1558 size_t alloc_size;
1559 int idx, nr;
1560
1561 nd = rblist__entry(old_metric_events, i);
1562 old_me = container_of(nd, struct metric_event, nd);
1563
Jiri Olsa38fe0e02021-07-06 17:16:59 +02001564 evsel = evlist__find_evsel(evlist, old_me->evsel->core.idx);
Namhyung Kimb214ba82020-09-24 21:44:53 +09001565 if (!evsel)
1566 return -EINVAL;
1567 new_me = metricgroup__lookup(new_metric_events, evsel, true);
1568 if (!new_me)
1569 return -ENOMEM;
1570
1571 pr_debug("copying metric event for cgroup '%s': %s (idx=%d)\n",
Jiri Olsa38fe0e02021-07-06 17:16:59 +02001572 cgrp ? cgrp->name : "root", evsel->name, evsel->core.idx);
Namhyung Kimb214ba82020-09-24 21:44:53 +09001573
1574 list_for_each_entry(old_expr, &old_me->head, nd) {
1575 new_expr = malloc(sizeof(*new_expr));
1576 if (!new_expr)
1577 return -ENOMEM;
1578
1579 new_expr->metric_expr = old_expr->metric_expr;
Ian Rogersb85a4d62021-10-15 10:21:32 -07001580 new_expr->metric_name = strdup(old_expr->metric_name);
1581 if (!new_expr->metric_name)
1582 return -ENOMEM;
1583
Namhyung Kimb214ba82020-09-24 21:44:53 +09001584 new_expr->metric_unit = old_expr->metric_unit;
1585 new_expr->runtime = old_expr->runtime;
1586
1587 if (old_expr->metric_refs) {
1588 /* calculate number of metric_events */
1589 for (nr = 0; old_expr->metric_refs[nr].metric_name; nr++)
1590 continue;
1591 alloc_size = sizeof(*new_expr->metric_refs);
1592 new_expr->metric_refs = calloc(nr + 1, alloc_size);
1593 if (!new_expr->metric_refs) {
1594 free(new_expr);
1595 return -ENOMEM;
1596 }
1597
1598 memcpy(new_expr->metric_refs, old_expr->metric_refs,
1599 nr * alloc_size);
1600 } else {
1601 new_expr->metric_refs = NULL;
1602 }
1603
1604 /* calculate number of metric_events */
1605 for (nr = 0; old_expr->metric_events[nr]; nr++)
1606 continue;
1607 alloc_size = sizeof(*new_expr->metric_events);
1608 new_expr->metric_events = calloc(nr + 1, alloc_size);
1609 if (!new_expr->metric_events) {
1610 free(new_expr->metric_refs);
1611 free(new_expr);
1612 return -ENOMEM;
1613 }
1614
1615 /* copy evsel in the same position */
1616 for (idx = 0; idx < nr; idx++) {
1617 evsel = old_expr->metric_events[idx];
Jiri Olsa38fe0e02021-07-06 17:16:59 +02001618 evsel = evlist__find_evsel(evlist, evsel->core.idx);
Namhyung Kimb214ba82020-09-24 21:44:53 +09001619 if (evsel == NULL) {
1620 free(new_expr->metric_events);
1621 free(new_expr->metric_refs);
1622 free(new_expr);
1623 return -EINVAL;
1624 }
1625 new_expr->metric_events[idx] = evsel;
1626 }
1627
1628 list_add(&new_expr->nd, &new_me->head);
1629 }
1630 }
1631 return 0;
1632}