Linux 4.19.133
[linux/fpc-iii.git] / tools / perf / util / metricgroup.c
blob8b3dafe3fac3a7fa5b70ab7d075491f6c6e3b2c1
1 /*
2 * Copyright (c) 2017, Intel Corporation.
4 * This program is free software; you can redistribute it and/or modify it
5 * under the terms and conditions of the GNU General Public License,
6 * version 2, as published by the Free Software Foundation.
8 * This program is distributed in the hope it will be useful, but WITHOUT
9 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
10 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
11 * more details.
15 /* Manage metrics and groups of metrics from JSON files */
17 #include "metricgroup.h"
18 #include "evlist.h"
19 #include "strbuf.h"
20 #include "pmu.h"
21 #include "expr.h"
22 #include "rblist.h"
23 #include <string.h>
24 #include <stdbool.h>
25 #include <errno.h>
26 #include "pmu-events/pmu-events.h"
27 #include "strlist.h"
28 #include <assert.h>
29 #include <ctype.h>
31 struct metric_event *metricgroup__lookup(struct rblist *metric_events,
32 struct perf_evsel *evsel,
33 bool create)
35 struct rb_node *nd;
36 struct metric_event me = {
37 .evsel = evsel
40 if (!metric_events)
41 return NULL;
43 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);
52 return NULL;
55 static int metric_event_cmp(struct rb_node *rb_node, const void *entry)
57 struct metric_event *a = container_of(rb_node,
58 struct metric_event,
59 nd);
60 const struct metric_event *b = entry;
62 if (a->evsel == b->evsel)
63 return 0;
64 if ((char *)a->evsel < (char *)b->evsel)
65 return -1;
66 return +1;
69 static struct rb_node *metric_event_new(struct rblist *rblist __maybe_unused,
70 const void *entry)
72 struct metric_event *me = malloc(sizeof(struct metric_event));
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;
82 static void metricgroup__rblist_init(struct rblist *metric_events)
84 rblist__init(metric_events);
85 metric_events->node_cmp = metric_event_cmp;
86 metric_events->node_new = metric_event_new;
89 struct egroup {
90 struct list_head nd;
91 int idnum;
92 const char **ids;
93 const char *metric_name;
94 const char *metric_expr;
97 static bool record_evsel(int *ind, struct perf_evsel **start,
98 int idnum,
99 struct perf_evsel **metric_events,
100 struct perf_evsel *ev)
102 metric_events[*ind] = ev;
103 if (*ind == 0)
104 *start = ev;
105 if (++*ind == idnum) {
106 metric_events[*ind] = NULL;
107 return true;
109 return false;
112 static struct perf_evsel *find_evsel_group(struct perf_evlist *perf_evlist,
113 const char **ids,
114 int idnum,
115 struct perf_evsel **metric_events)
117 struct perf_evsel *ev, *start = NULL;
118 int ind = 0;
120 evlist__for_each_entry (perf_evlist, ev) {
121 if (ev->collect_stat)
122 continue;
123 if (!strcmp(ev->name, ids[ind])) {
124 if (record_evsel(&ind, &start, idnum,
125 metric_events, ev))
126 return start;
127 } else {
129 * We saw some other event that is not
130 * in our list of events. Discard
131 * the whole match and start again.
133 ind = 0;
134 start = NULL;
135 if (!strcmp(ev->name, ids[ind])) {
136 if (record_evsel(&ind, &start, idnum,
137 metric_events, ev))
138 return start;
143 * This can happen when an alias expands to multiple
144 * events, like for uncore events.
145 * We don't support this case for now.
147 return NULL;
150 static int metricgroup__setup_events(struct list_head *groups,
151 struct perf_evlist *perf_evlist,
152 struct rblist *metric_events_list)
154 struct metric_event *me;
155 struct metric_expr *expr;
156 int i = 0;
157 int ret = 0;
158 struct egroup *eg;
159 struct perf_evsel *evsel;
161 list_for_each_entry (eg, groups, nd) {
162 struct perf_evsel **metric_events;
164 metric_events = calloc(sizeof(void *), eg->idnum + 1);
165 if (!metric_events) {
166 ret = -ENOMEM;
167 break;
169 evsel = find_evsel_group(perf_evlist, eg->ids, eg->idnum,
170 metric_events);
171 if (!evsel) {
172 pr_debug("Cannot resolve %s: %s\n",
173 eg->metric_name, eg->metric_expr);
174 continue;
176 for (i = 0; i < eg->idnum; i++)
177 metric_events[i]->collect_stat = true;
178 me = metricgroup__lookup(metric_events_list, evsel, true);
179 if (!me) {
180 ret = -ENOMEM;
181 break;
183 expr = malloc(sizeof(struct metric_expr));
184 if (!expr) {
185 ret = -ENOMEM;
186 break;
188 expr->metric_expr = eg->metric_expr;
189 expr->metric_name = eg->metric_name;
190 expr->metric_events = metric_events;
191 list_add(&expr->nd, &me->head);
193 return ret;
196 static bool match_metric(const char *n, const char *list)
198 int len;
199 char *m;
201 if (!list)
202 return false;
203 if (!strcmp(list, "all"))
204 return true;
205 if (!n)
206 return !strcasecmp(list, "No_group");
207 len = strlen(list);
208 m = strcasestr(n, list);
209 if (!m)
210 return false;
211 if ((m == n || m[-1] == ';' || m[-1] == ' ') &&
212 (m[len] == 0 || m[len] == ';'))
213 return true;
214 return false;
217 struct mep {
218 struct rb_node nd;
219 const char *name;
220 struct strlist *metrics;
223 static int mep_cmp(struct rb_node *rb_node, const void *entry)
225 struct mep *a = container_of(rb_node, struct mep, nd);
226 struct mep *b = (struct mep *)entry;
228 return strcmp(a->name, b->name);
231 static struct rb_node *mep_new(struct rblist *rl __maybe_unused,
232 const void *entry)
234 struct mep *me = malloc(sizeof(struct mep));
236 if (!me)
237 return NULL;
238 memcpy(me, entry, sizeof(struct mep));
239 me->name = strdup(me->name);
240 if (!me->name)
241 goto out_me;
242 me->metrics = strlist__new(NULL, NULL);
243 if (!me->metrics)
244 goto out_name;
245 return &me->nd;
246 out_name:
247 free((char *)me->name);
248 out_me:
249 free(me);
250 return NULL;
253 static struct mep *mep_lookup(struct rblist *groups, const char *name)
255 struct rb_node *nd;
256 struct mep me = {
257 .name = name
259 nd = rblist__find(groups, &me);
260 if (nd)
261 return container_of(nd, struct mep, nd);
262 rblist__add_node(groups, &me);
263 nd = rblist__find(groups, &me);
264 if (nd)
265 return container_of(nd, struct mep, nd);
266 return NULL;
269 static void mep_delete(struct rblist *rl __maybe_unused,
270 struct rb_node *nd)
272 struct mep *me = container_of(nd, struct mep, nd);
274 strlist__delete(me->metrics);
275 free((void *)me->name);
276 free(me);
279 static void metricgroup__print_strlist(struct strlist *metrics, bool raw)
281 struct str_node *sn;
282 int n = 0;
284 strlist__for_each_entry (sn, metrics) {
285 if (raw)
286 printf("%s%s", n > 0 ? " " : "", sn->s);
287 else
288 printf(" %s\n", sn->s);
289 n++;
291 if (raw)
292 putchar('\n');
295 void metricgroup__print(bool metrics, bool metricgroups, char *filter,
296 bool raw)
298 struct pmu_events_map *map = perf_pmu__find_map(NULL);
299 struct pmu_event *pe;
300 int i;
301 struct rblist groups;
302 struct rb_node *node, *next;
303 struct strlist *metriclist = NULL;
305 if (!map)
306 return;
308 if (!metricgroups) {
309 metriclist = strlist__new(NULL, NULL);
310 if (!metriclist)
311 return;
314 rblist__init(&groups);
315 groups.node_new = mep_new;
316 groups.node_cmp = mep_cmp;
317 groups.node_delete = mep_delete;
318 for (i = 0; ; i++) {
319 const char *g;
320 pe = &map->table[i];
322 if (!pe->name && !pe->metric_group && !pe->metric_name)
323 break;
324 if (!pe->metric_expr)
325 continue;
326 g = pe->metric_group;
327 if (!g && pe->metric_name) {
328 if (pe->name)
329 continue;
330 g = "No_group";
332 if (g) {
333 char *omg;
334 char *mg = strdup(g);
336 if (!mg)
337 return;
338 omg = mg;
339 while ((g = strsep(&mg, ";")) != NULL) {
340 struct mep *me;
341 char *s;
343 if (*g == 0)
344 g = "No_group";
345 while (isspace(*g))
346 g++;
347 if (filter && !strstr(g, filter))
348 continue;
349 if (raw)
350 s = (char *)pe->metric_name;
351 else {
352 if (asprintf(&s, "%s\n%*s%s]",
353 pe->metric_name, 8, "[", pe->desc) < 0)
354 return;
357 if (!s)
358 continue;
360 if (!metricgroups) {
361 strlist__add(metriclist, s);
362 } else {
363 me = mep_lookup(&groups, g);
364 if (!me)
365 continue;
366 strlist__add(me->metrics, s);
369 free(omg);
373 if (metricgroups && !raw)
374 printf("\nMetric Groups:\n\n");
375 else if (metrics && !raw)
376 printf("\nMetrics:\n\n");
378 for (node = rb_first(&groups.entries); node; node = next) {
379 struct mep *me = container_of(node, struct mep, nd);
381 if (metricgroups)
382 printf("%s%s%s", me->name, metrics ? ":" : "", raw ? " " : "\n");
383 if (metrics)
384 metricgroup__print_strlist(me->metrics, raw);
385 next = rb_next(node);
386 rblist__remove_node(&groups, node);
388 if (!metricgroups)
389 metricgroup__print_strlist(metriclist, raw);
390 strlist__delete(metriclist);
393 static int metricgroup__add_metric(const char *metric, struct strbuf *events,
394 struct list_head *group_list)
396 struct pmu_events_map *map = perf_pmu__find_map(NULL);
397 struct pmu_event *pe;
398 int ret = -EINVAL;
399 int i, j;
401 if (!map)
402 return 0;
404 for (i = 0; ; i++) {
405 pe = &map->table[i];
407 if (!pe->name && !pe->metric_group && !pe->metric_name)
408 break;
409 if (!pe->metric_expr)
410 continue;
411 if (match_metric(pe->metric_group, metric) ||
412 match_metric(pe->metric_name, metric)) {
413 const char **ids;
414 int idnum;
415 struct egroup *eg;
417 pr_debug("metric expr %s for %s\n", pe->metric_expr, pe->metric_name);
419 if (expr__find_other(pe->metric_expr,
420 NULL, &ids, &idnum) < 0)
421 continue;
422 if (events->len > 0)
423 strbuf_addf(events, ",");
424 for (j = 0; j < idnum; j++) {
425 pr_debug("found event %s\n", ids[j]);
426 strbuf_addf(events, "%s%s",
427 j == 0 ? "{" : ",",
428 ids[j]);
430 strbuf_addf(events, "}:W");
432 eg = malloc(sizeof(struct egroup));
433 if (!eg) {
434 ret = -ENOMEM;
435 break;
437 eg->ids = ids;
438 eg->idnum = idnum;
439 eg->metric_name = pe->metric_name;
440 eg->metric_expr = pe->metric_expr;
441 list_add_tail(&eg->nd, group_list);
442 ret = 0;
445 return ret;
448 static int metricgroup__add_metric_list(const char *list, struct strbuf *events,
449 struct list_head *group_list)
451 char *llist, *nlist, *p;
452 int ret = -EINVAL;
454 nlist = strdup(list);
455 if (!nlist)
456 return -ENOMEM;
457 llist = nlist;
459 strbuf_init(events, 100);
460 strbuf_addf(events, "%s", "");
462 while ((p = strsep(&llist, ",")) != NULL) {
463 ret = metricgroup__add_metric(p, events, group_list);
464 if (ret == -EINVAL) {
465 fprintf(stderr, "Cannot find metric or group `%s'\n",
467 break;
470 free(nlist);
471 return ret;
474 static void metricgroup__free_egroups(struct list_head *group_list)
476 struct egroup *eg, *egtmp;
477 int i;
479 list_for_each_entry_safe (eg, egtmp, group_list, nd) {
480 for (i = 0; i < eg->idnum; i++)
481 free((char *)eg->ids[i]);
482 free(eg->ids);
483 free(eg);
487 int metricgroup__parse_groups(const struct option *opt,
488 const char *str,
489 struct rblist *metric_events)
491 struct parse_events_error parse_error;
492 struct perf_evlist *perf_evlist = *(struct perf_evlist **)opt->value;
493 struct strbuf extra_events;
494 LIST_HEAD(group_list);
495 int ret;
497 if (metric_events->nr_entries == 0)
498 metricgroup__rblist_init(metric_events);
499 ret = metricgroup__add_metric_list(str, &extra_events, &group_list);
500 if (ret)
501 return ret;
502 pr_debug("adding %s\n", extra_events.buf);
503 memset(&parse_error, 0, sizeof(struct parse_events_error));
504 ret = parse_events(perf_evlist, extra_events.buf, &parse_error);
505 if (ret) {
506 parse_events_print_error(&parse_error, extra_events.buf);
507 goto out;
509 strbuf_release(&extra_events);
510 ret = metricgroup__setup_events(&group_list, perf_evlist,
511 metric_events);
512 out:
513 metricgroup__free_egroups(&group_list);
514 return ret;
517 bool metricgroup__has_metric(const char *metric)
519 struct pmu_events_map *map = perf_pmu__find_map(NULL);
520 struct pmu_event *pe;
521 int i;
523 if (!map)
524 return false;
526 for (i = 0; ; i++) {
527 pe = &map->table[i];
529 if (!pe->name && !pe->metric_group && !pe->metric_name)
530 break;
531 if (!pe->metric_expr)
532 continue;
533 if (match_metric(pe->metric_name, metric))
534 return true;
536 return false;