blob: 7753c3091478a4cd9284b5109e69079a81a036c5 [file] [log] [blame]
b.liue9582032025-04-17 19:18:16 +08001// SPDX-License-Identifier: GPL-2.0-only
2/*
3 * Copyright (c) 2017, Intel Corporation.
4 */
5
6/* Manage metrics and groups of metrics from JSON files */
7
8#include "metricgroup.h"
9#include "debug.h"
10#include "evlist.h"
11#include "evsel.h"
12#include "strbuf.h"
13#include "pmu.h"
14#include "expr.h"
15#include "rblist.h"
16#include <string.h>
17#include <errno.h>
18#include "pmu-events/pmu-events.h"
19#include "strlist.h"
20#include <assert.h>
21#include <linux/ctype.h>
22#include <linux/string.h>
23#include <linux/zalloc.h>
24#include <subcmd/parse-options.h>
25
26struct metric_event *metricgroup__lookup(struct rblist *metric_events,
27 struct evsel *evsel,
28 bool create)
29{
30 struct rb_node *nd;
31 struct metric_event me = {
32 .evsel = evsel
33 };
34
35 if (!metric_events)
36 return NULL;
37
38 nd = rblist__find(metric_events, &me);
39 if (nd)
40 return container_of(nd, struct metric_event, nd);
41 if (create) {
42 rblist__add_node(metric_events, &me);
43 nd = rblist__find(metric_events, &me);
44 if (nd)
45 return container_of(nd, struct metric_event, nd);
46 }
47 return NULL;
48}
49
50static int metric_event_cmp(struct rb_node *rb_node, const void *entry)
51{
52 struct metric_event *a = container_of(rb_node,
53 struct metric_event,
54 nd);
55 const struct metric_event *b = entry;
56
57 if (a->evsel == b->evsel)
58 return 0;
59 if ((char *)a->evsel < (char *)b->evsel)
60 return -1;
61 return +1;
62}
63
64static struct rb_node *metric_event_new(struct rblist *rblist __maybe_unused,
65 const void *entry)
66{
67 struct metric_event *me = malloc(sizeof(struct metric_event));
68
69 if (!me)
70 return NULL;
71 memcpy(me, entry, sizeof(struct metric_event));
72 me->evsel = ((struct metric_event *)entry)->evsel;
73 INIT_LIST_HEAD(&me->head);
74 return &me->nd;
75}
76
77static void metricgroup__rblist_init(struct rblist *metric_events)
78{
79 rblist__init(metric_events);
80 metric_events->node_cmp = metric_event_cmp;
81 metric_events->node_new = metric_event_new;
82}
83
84struct egroup {
85 struct list_head nd;
86 int idnum;
87 const char **ids;
88 const char *metric_name;
89 const char *metric_expr;
90 const char *metric_unit;
91};
92
93static struct evsel *find_evsel_group(struct evlist *perf_evlist,
94 const char **ids,
95 int idnum,
96 struct evsel **metric_events)
97{
98 struct evsel *ev;
99 int i = 0;
100 bool leader_found;
101
102 evlist__for_each_entry (perf_evlist, ev) {
103 if (!strcmp(ev->name, ids[i])) {
104 if (!metric_events[i])
105 metric_events[i] = ev;
106 i++;
107 if (i == idnum)
108 break;
109 } else {
110 if (i + 1 == idnum) {
111 /* Discard the whole match and start again */
112 i = 0;
113 memset(metric_events, 0,
114 sizeof(struct evsel *) * idnum);
115 continue;
116 }
117
118 if (!strcmp(ev->name, ids[i]))
119 metric_events[i] = ev;
120 else {
121 /* Discard the whole match and start again */
122 i = 0;
123 memset(metric_events, 0,
124 sizeof(struct evsel *) * idnum);
125 continue;
126 }
127 }
128 }
129
130 if (i != idnum) {
131 /* Not whole match */
132 return NULL;
133 }
134
135 metric_events[idnum] = NULL;
136
137 for (i = 0; i < idnum; i++) {
138 leader_found = false;
139 evlist__for_each_entry(perf_evlist, ev) {
140 if (!leader_found && (ev == metric_events[i]))
141 leader_found = true;
142
143 if (leader_found &&
144 !strcmp(ev->name, metric_events[i]->name)) {
145 ev->metric_leader = metric_events[i];
146 }
147 }
148 }
149
150 return metric_events[0];
151}
152
153static int metricgroup__setup_events(struct list_head *groups,
154 struct evlist *perf_evlist,
155 struct rblist *metric_events_list)
156{
157 struct metric_event *me;
158 struct metric_expr *expr;
159 int i = 0;
160 int ret = 0;
161 struct egroup *eg;
162 struct evsel *evsel;
163
164 list_for_each_entry (eg, groups, nd) {
165 struct evsel **metric_events;
166
167 metric_events = calloc(sizeof(void *), eg->idnum + 1);
168 if (!metric_events) {
169 ret = -ENOMEM;
170 break;
171 }
172 evsel = find_evsel_group(perf_evlist, eg->ids, eg->idnum,
173 metric_events);
174 if (!evsel) {
175 pr_debug("Cannot resolve %s: %s\n",
176 eg->metric_name, eg->metric_expr);
177 free(metric_events);
178 continue;
179 }
180 for (i = 0; i < eg->idnum; i++)
181 metric_events[i]->collect_stat = true;
182 me = metricgroup__lookup(metric_events_list, evsel, true);
183 if (!me) {
184 ret = -ENOMEM;
185 free(metric_events);
186 break;
187 }
188 expr = malloc(sizeof(struct metric_expr));
189 if (!expr) {
190 ret = -ENOMEM;
191 free(metric_events);
192 break;
193 }
194 expr->metric_expr = eg->metric_expr;
195 expr->metric_name = eg->metric_name;
196 expr->metric_unit = eg->metric_unit;
197 expr->metric_events = metric_events;
198 list_add(&expr->nd, &me->head);
199 }
200 return ret;
201}
202
203static bool match_metric(const char *n, const char *list)
204{
205 int len;
206 char *m;
207
208 if (!list)
209 return false;
210 if (!strcmp(list, "all"))
211 return true;
212 if (!n)
213 return !strcasecmp(list, "No_group");
214 len = strlen(list);
215 m = strcasestr(n, list);
216 if (!m)
217 return false;
218 if ((m == n || m[-1] == ';' || m[-1] == ' ') &&
219 (m[len] == 0 || m[len] == ';'))
220 return true;
221 return false;
222}
223
224struct mep {
225 struct rb_node nd;
226 const char *name;
227 struct strlist *metrics;
228};
229
230static int mep_cmp(struct rb_node *rb_node, const void *entry)
231{
232 struct mep *a = container_of(rb_node, struct mep, nd);
233 struct mep *b = (struct mep *)entry;
234
235 return strcmp(a->name, b->name);
236}
237
238static struct rb_node *mep_new(struct rblist *rl __maybe_unused,
239 const void *entry)
240{
241 struct mep *me = malloc(sizeof(struct mep));
242
243 if (!me)
244 return NULL;
245 memcpy(me, entry, sizeof(struct mep));
246 me->name = strdup(me->name);
247 if (!me->name)
248 goto out_me;
249 me->metrics = strlist__new(NULL, NULL);
250 if (!me->metrics)
251 goto out_name;
252 return &me->nd;
253out_name:
254 zfree(&me->name);
255out_me:
256 free(me);
257 return NULL;
258}
259
260static struct mep *mep_lookup(struct rblist *groups, const char *name)
261{
262 struct rb_node *nd;
263 struct mep me = {
264 .name = name
265 };
266 nd = rblist__find(groups, &me);
267 if (nd)
268 return container_of(nd, struct mep, nd);
269 rblist__add_node(groups, &me);
270 nd = rblist__find(groups, &me);
271 if (nd)
272 return container_of(nd, struct mep, nd);
273 return NULL;
274}
275
276static void mep_delete(struct rblist *rl __maybe_unused,
277 struct rb_node *nd)
278{
279 struct mep *me = container_of(nd, struct mep, nd);
280
281 strlist__delete(me->metrics);
282 zfree(&me->name);
283 free(me);
284}
285
286static void metricgroup__print_strlist(struct strlist *metrics, bool raw)
287{
288 struct str_node *sn;
289 int n = 0;
290
291 strlist__for_each_entry (sn, metrics) {
292 if (raw)
293 printf("%s%s", n > 0 ? " " : "", sn->s);
294 else
295 printf(" %s\n", sn->s);
296 n++;
297 }
298 if (raw)
299 putchar('\n');
300}
301
302void metricgroup__print(bool metrics, bool metricgroups, char *filter,
303 bool raw, bool details)
304{
305 struct pmu_events_map *map = perf_pmu__find_map(NULL);
306 struct pmu_event *pe;
307 int i;
308 struct rblist groups;
309 struct rb_node *node, *next;
310 struct strlist *metriclist = NULL;
311
312 if (!map)
313 return;
314
315 if (!metricgroups) {
316 metriclist = strlist__new(NULL, NULL);
317 if (!metriclist)
318 return;
319 }
320
321 rblist__init(&groups);
322 groups.node_new = mep_new;
323 groups.node_cmp = mep_cmp;
324 groups.node_delete = mep_delete;
325 for (i = 0; ; i++) {
326 const char *g;
327 pe = &map->table[i];
328
329 if (!pe->name && !pe->metric_group && !pe->metric_name)
330 break;
331 if (!pe->metric_expr)
332 continue;
333 g = pe->metric_group;
334 if (!g && pe->metric_name) {
335 if (pe->name)
336 continue;
337 g = "No_group";
338 }
339 if (g) {
340 char *omg;
341 char *mg = strdup(g);
342
343 if (!mg)
344 return;
345 omg = mg;
346 while ((g = strsep(&mg, ";")) != NULL) {
347 struct mep *me;
348 char *s;
349
350 g = skip_spaces(g);
351 if (*g == 0)
352 g = "No_group";
353 if (filter && !strstr(g, filter))
354 continue;
355 if (raw)
356 s = (char *)pe->metric_name;
357 else {
358 if (asprintf(&s, "%s\n%*s%s]",
359 pe->metric_name, 8, "[", pe->desc) < 0)
360 return;
361
362 if (details) {
363 if (asprintf(&s, "%s\n%*s%s]",
364 s, 8, "[", pe->metric_expr) < 0)
365 return;
366 }
367 }
368
369 if (!s)
370 continue;
371
372 if (!metricgroups) {
373 strlist__add(metriclist, s);
374 } else {
375 me = mep_lookup(&groups, g);
376 if (!me)
377 continue;
378 strlist__add(me->metrics, s);
379 }
380 }
381 free(omg);
382 }
383 }
384
385 if (metricgroups && !raw)
386 printf("\nMetric Groups:\n\n");
387 else if (metrics && !raw)
388 printf("\nMetrics:\n\n");
389
390 for (node = rb_first_cached(&groups.entries); node; node = next) {
391 struct mep *me = container_of(node, struct mep, nd);
392
393 if (metricgroups)
394 printf("%s%s%s", me->name, metrics && !raw ? ":" : "", raw ? " " : "\n");
395 if (metrics)
396 metricgroup__print_strlist(me->metrics, raw);
397 next = rb_next(node);
398 rblist__remove_node(&groups, node);
399 }
400 if (!metricgroups)
401 metricgroup__print_strlist(metriclist, raw);
402 strlist__delete(metriclist);
403}
404
405static int metricgroup__add_metric(const char *metric, struct strbuf *events,
406 struct list_head *group_list)
407{
408 struct pmu_events_map *map = perf_pmu__find_map(NULL);
409 struct pmu_event *pe;
410 int ret = -EINVAL;
411 int i, j;
412
413 if (!map)
414 return 0;
415
416 for (i = 0; ; i++) {
417 pe = &map->table[i];
418
419 if (!pe->name && !pe->metric_group && !pe->metric_name)
420 break;
421 if (!pe->metric_expr)
422 continue;
423 if (match_metric(pe->metric_group, metric) ||
424 match_metric(pe->metric_name, metric)) {
425 const char **ids;
426 int idnum;
427 struct egroup *eg;
428 bool no_group = false;
429
430 pr_debug("metric expr %s for %s\n", pe->metric_expr, pe->metric_name);
431
432 if (expr__find_other(pe->metric_expr,
433 NULL, &ids, &idnum) < 0)
434 continue;
435 if (events->len > 0)
436 strbuf_addf(events, ",");
437 for (j = 0; j < idnum; j++) {
438 pr_debug("found event %s\n", ids[j]);
439 /*
440 * Duration time maps to a software event and can make
441 * groups not count. Always use it outside a
442 * group.
443 */
444 if (!strcmp(ids[j], "duration_time")) {
445 if (j > 0)
446 strbuf_addf(events, "}:W,");
447 strbuf_addf(events, "duration_time");
448 no_group = true;
449 continue;
450 }
451 strbuf_addf(events, "%s%s",
452 j == 0 || no_group ? "{" : ",",
453 ids[j]);
454 no_group = false;
455 }
456 if (!no_group)
457 strbuf_addf(events, "}:W");
458
459 eg = malloc(sizeof(struct egroup));
460 if (!eg) {
461 ret = -ENOMEM;
462 break;
463 }
464 eg->ids = ids;
465 eg->idnum = idnum;
466 eg->metric_name = pe->metric_name;
467 eg->metric_expr = pe->metric_expr;
468 eg->metric_unit = pe->unit;
469 list_add_tail(&eg->nd, group_list);
470 ret = 0;
471 }
472 }
473 return ret;
474}
475
476static int metricgroup__add_metric_list(const char *list, struct strbuf *events,
477 struct list_head *group_list)
478{
479 char *llist, *nlist, *p;
480 int ret = -EINVAL;
481
482 nlist = strdup(list);
483 if (!nlist)
484 return -ENOMEM;
485 llist = nlist;
486
487 strbuf_init(events, 100);
488 strbuf_addf(events, "%s", "");
489
490 while ((p = strsep(&llist, ",")) != NULL) {
491 ret = metricgroup__add_metric(p, events, group_list);
492 if (ret == -EINVAL) {
493 fprintf(stderr, "Cannot find metric or group `%s'\n",
494 p);
495 break;
496 }
497 }
498 free(nlist);
499 return ret;
500}
501
502static void metricgroup__free_egroups(struct list_head *group_list)
503{
504 struct egroup *eg, *egtmp;
505 int i;
506
507 list_for_each_entry_safe (eg, egtmp, group_list, nd) {
508 for (i = 0; i < eg->idnum; i++)
509 zfree(&eg->ids[i]);
510 zfree(&eg->ids);
511 list_del_init(&eg->nd);
512 free(eg);
513 }
514}
515
516int metricgroup__parse_groups(const struct option *opt,
517 const char *str,
518 struct rblist *metric_events)
519{
520 struct parse_events_error parse_error;
521 struct evlist *perf_evlist = *(struct evlist **)opt->value;
522 struct strbuf extra_events;
523 LIST_HEAD(group_list);
524 int ret;
525
526 if (metric_events->nr_entries == 0)
527 metricgroup__rblist_init(metric_events);
528 ret = metricgroup__add_metric_list(str, &extra_events, &group_list);
529 if (ret)
530 return ret;
531 pr_debug("adding %s\n", extra_events.buf);
532 memset(&parse_error, 0, sizeof(struct parse_events_error));
533 ret = parse_events(perf_evlist, extra_events.buf, &parse_error);
534 if (ret) {
535 parse_events_print_error(&parse_error, extra_events.buf);
536 goto out;
537 }
538 strbuf_release(&extra_events);
539 ret = metricgroup__setup_events(&group_list, perf_evlist,
540 metric_events);
541out:
542 metricgroup__free_egroups(&group_list);
543 return ret;
544}
545
546bool metricgroup__has_metric(const char *metric)
547{
548 struct pmu_events_map *map = perf_pmu__find_map(NULL);
549 struct pmu_event *pe;
550 int i;
551
552 if (!map)
553 return false;
554
555 for (i = 0; ; i++) {
556 pe = &map->table[i];
557
558 if (!pe->name && !pe->metric_group && !pe->metric_name)
559 break;
560 if (!pe->metric_expr)
561 continue;
562 if (match_metric(pe->metric_name, metric))
563 return true;
564 }
565 return false;
566}