GNU Linux-libre 4.19.295-gnu1
[releases.git] / tools / perf / util / metricgroup.c
1 /*
2  * Copyright (c) 2017, Intel Corporation.
3  *
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.
7  *
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.
12  *
13  */
14
15 /* Manage metrics and groups of metrics from JSON files */
16
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>
30
31 struct metric_event *metricgroup__lookup(struct rblist *metric_events,
32                                          struct perf_evsel *evsel,
33                                          bool create)
34 {
35         struct rb_node *nd;
36         struct metric_event me = {
37                 .evsel = evsel
38         };
39
40         if (!metric_events)
41                 return NULL;
42
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);
51         }
52         return NULL;
53 }
54
55 static 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
69 static 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
82 static void metricgroup__rblist_init(struct rblist *metric_events)
83 {
84         rblist__init(metric_events);
85         metric_events->node_cmp = metric_event_cmp;
86         metric_events->node_new = metric_event_new;
87 }
88
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;
95 };
96
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)
101 {
102         metric_events[*ind] = ev;
103         if (*ind == 0)
104                 *start = ev;
105         if (++*ind == idnum) {
106                 metric_events[*ind] = NULL;
107                 return true;
108         }
109         return false;
110 }
111
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)
116 {
117         struct perf_evsel *ev, *start = NULL;
118         int ind = 0;
119
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 {
128                         /*
129                          * We saw some other event that is not
130                          * in our list of events. Discard
131                          * the whole match and start again.
132                          */
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;
139                         }
140                 }
141         }
142         /*
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.
146          */
147         return NULL;
148 }
149
150 static int metricgroup__setup_events(struct list_head *groups,
151                                      struct perf_evlist *perf_evlist,
152                                      struct rblist *metric_events_list)
153 {
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;
160
161         list_for_each_entry (eg, groups, nd) {
162                 struct perf_evsel **metric_events;
163
164                 metric_events = calloc(sizeof(void *), eg->idnum + 1);
165                 if (!metric_events) {
166                         ret = -ENOMEM;
167                         break;
168                 }
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                         free(metric_events);
175                         continue;
176                 }
177                 for (i = 0; i < eg->idnum; i++)
178                         metric_events[i]->collect_stat = true;
179                 me = metricgroup__lookup(metric_events_list, evsel, true);
180                 if (!me) {
181                         ret = -ENOMEM;
182                         free(metric_events);
183                         break;
184                 }
185                 expr = malloc(sizeof(struct metric_expr));
186                 if (!expr) {
187                         ret = -ENOMEM;
188                         free(metric_events);
189                         break;
190                 }
191                 expr->metric_expr = eg->metric_expr;
192                 expr->metric_name = eg->metric_name;
193                 expr->metric_events = metric_events;
194                 list_add(&expr->nd, &me->head);
195         }
196         return ret;
197 }
198
199 static bool match_metric(const char *n, const char *list)
200 {
201         int len;
202         char *m;
203
204         if (!list)
205                 return false;
206         if (!strcmp(list, "all"))
207                 return true;
208         if (!n)
209                 return !strcasecmp(list, "No_group");
210         len = strlen(list);
211         m = strcasestr(n, list);
212         if (!m)
213                 return false;
214         if ((m == n || m[-1] == ';' || m[-1] == ' ') &&
215             (m[len] == 0 || m[len] == ';'))
216                 return true;
217         return false;
218 }
219
220 struct mep {
221         struct rb_node nd;
222         const char *name;
223         struct strlist *metrics;
224 };
225
226 static int mep_cmp(struct rb_node *rb_node, const void *entry)
227 {
228         struct mep *a = container_of(rb_node, struct mep, nd);
229         struct mep *b = (struct mep *)entry;
230
231         return strcmp(a->name, b->name);
232 }
233
234 static struct rb_node *mep_new(struct rblist *rl __maybe_unused,
235                                         const void *entry)
236 {
237         struct mep *me = malloc(sizeof(struct mep));
238
239         if (!me)
240                 return NULL;
241         memcpy(me, entry, sizeof(struct mep));
242         me->name = strdup(me->name);
243         if (!me->name)
244                 goto out_me;
245         me->metrics = strlist__new(NULL, NULL);
246         if (!me->metrics)
247                 goto out_name;
248         return &me->nd;
249 out_name:
250         free((char *)me->name);
251 out_me:
252         free(me);
253         return NULL;
254 }
255
256 static struct mep *mep_lookup(struct rblist *groups, const char *name)
257 {
258         struct rb_node *nd;
259         struct mep me = {
260                 .name = name
261         };
262         nd = rblist__find(groups, &me);
263         if (nd)
264                 return container_of(nd, struct mep, nd);
265         rblist__add_node(groups, &me);
266         nd = rblist__find(groups, &me);
267         if (nd)
268                 return container_of(nd, struct mep, nd);
269         return NULL;
270 }
271
272 static void mep_delete(struct rblist *rl __maybe_unused,
273                        struct rb_node *nd)
274 {
275         struct mep *me = container_of(nd, struct mep, nd);
276
277         strlist__delete(me->metrics);
278         free((void *)me->name);
279         free(me);
280 }
281
282 static void metricgroup__print_strlist(struct strlist *metrics, bool raw)
283 {
284         struct str_node *sn;
285         int n = 0;
286
287         strlist__for_each_entry (sn, metrics) {
288                 if (raw)
289                         printf("%s%s", n > 0 ? " " : "", sn->s);
290                 else
291                         printf("  %s\n", sn->s);
292                 n++;
293         }
294         if (raw)
295                 putchar('\n');
296 }
297
298 void metricgroup__print(bool metrics, bool metricgroups, char *filter,
299                         bool raw)
300 {
301         struct pmu_events_map *map = perf_pmu__find_map(NULL);
302         struct pmu_event *pe;
303         int i;
304         struct rblist groups;
305         struct rb_node *node, *next;
306         struct strlist *metriclist = NULL;
307
308         if (!map)
309                 return;
310
311         if (!metricgroups) {
312                 metriclist = strlist__new(NULL, NULL);
313                 if (!metriclist)
314                         return;
315         }
316
317         rblist__init(&groups);
318         groups.node_new = mep_new;
319         groups.node_cmp = mep_cmp;
320         groups.node_delete = mep_delete;
321         for (i = 0; ; i++) {
322                 const char *g;
323                 pe = &map->table[i];
324
325                 if (!pe->name && !pe->metric_group && !pe->metric_name)
326                         break;
327                 if (!pe->metric_expr)
328                         continue;
329                 g = pe->metric_group;
330                 if (!g && pe->metric_name) {
331                         if (pe->name)
332                                 continue;
333                         g = "No_group";
334                 }
335                 if (g) {
336                         char *omg;
337                         char *mg = strdup(g);
338
339                         if (!mg)
340                                 return;
341                         omg = mg;
342                         while ((g = strsep(&mg, ";")) != NULL) {
343                                 struct mep *me;
344                                 char *s;
345
346                                 if (*g == 0)
347                                         g = "No_group";
348                                 while (isspace(*g))
349                                         g++;
350                                 if (filter && !strstr(g, filter))
351                                         continue;
352                                 if (raw)
353                                         s = (char *)pe->metric_name;
354                                 else {
355                                         if (asprintf(&s, "%s\n%*s%s]",
356                                                      pe->metric_name, 8, "[", pe->desc) < 0)
357                                                 return;
358                                 }
359
360                                 if (!s)
361                                         continue;
362
363                                 if (!metricgroups) {
364                                         strlist__add(metriclist, s);
365                                 } else {
366                                         me = mep_lookup(&groups, g);
367                                         if (!me)
368                                                 continue;
369                                         strlist__add(me->metrics, s);
370                                 }
371                         }
372                         free(omg);
373                 }
374         }
375
376         if (metricgroups && !raw)
377                 printf("\nMetric Groups:\n\n");
378         else if (metrics && !raw)
379                 printf("\nMetrics:\n\n");
380
381         for (node = rb_first(&groups.entries); node; node = next) {
382                 struct mep *me = container_of(node, struct mep, nd);
383
384                 if (metricgroups)
385                         printf("%s%s%s", me->name, metrics ? ":" : "", raw ? " " : "\n");
386                 if (metrics)
387                         metricgroup__print_strlist(me->metrics, raw);
388                 next = rb_next(node);
389                 rblist__remove_node(&groups, node);
390         }
391         if (!metricgroups)
392                 metricgroup__print_strlist(metriclist, raw);
393         strlist__delete(metriclist);
394 }
395
396 static int metricgroup__add_metric(const char *metric, struct strbuf *events,
397                                    struct list_head *group_list)
398 {
399         struct pmu_events_map *map = perf_pmu__find_map(NULL);
400         struct pmu_event *pe;
401         int ret = -EINVAL;
402         int i, j;
403
404         if (!map)
405                 return 0;
406
407         for (i = 0; ; i++) {
408                 pe = &map->table[i];
409
410                 if (!pe->name && !pe->metric_group && !pe->metric_name)
411                         break;
412                 if (!pe->metric_expr)
413                         continue;
414                 if (match_metric(pe->metric_group, metric) ||
415                     match_metric(pe->metric_name, metric)) {
416                         const char **ids;
417                         int idnum;
418                         struct egroup *eg;
419
420                         pr_debug("metric expr %s for %s\n", pe->metric_expr, pe->metric_name);
421
422                         if (expr__find_other(pe->metric_expr,
423                                              NULL, &ids, &idnum) < 0)
424                                 continue;
425                         if (events->len > 0)
426                                 strbuf_addf(events, ",");
427                         for (j = 0; j < idnum; j++) {
428                                 pr_debug("found event %s\n", ids[j]);
429                                 strbuf_addf(events, "%s%s",
430                                         j == 0 ? "{" : ",",
431                                         ids[j]);
432                         }
433                         strbuf_addf(events, "}:W");
434
435                         eg = malloc(sizeof(struct egroup));
436                         if (!eg) {
437                                 ret = -ENOMEM;
438                                 break;
439                         }
440                         eg->ids = ids;
441                         eg->idnum = idnum;
442                         eg->metric_name = pe->metric_name;
443                         eg->metric_expr = pe->metric_expr;
444                         list_add_tail(&eg->nd, group_list);
445                         ret = 0;
446                 }
447         }
448         return ret;
449 }
450
451 static int metricgroup__add_metric_list(const char *list, struct strbuf *events,
452                                         struct list_head *group_list)
453 {
454         char *llist, *nlist, *p;
455         int ret = -EINVAL;
456
457         nlist = strdup(list);
458         if (!nlist)
459                 return -ENOMEM;
460         llist = nlist;
461
462         strbuf_init(events, 100);
463         strbuf_addf(events, "%s", "");
464
465         while ((p = strsep(&llist, ",")) != NULL) {
466                 ret = metricgroup__add_metric(p, events, group_list);
467                 if (ret == -EINVAL) {
468                         fprintf(stderr, "Cannot find metric or group `%s'\n",
469                                         p);
470                         break;
471                 }
472         }
473         free(nlist);
474         return ret;
475 }
476
477 static void metricgroup__free_egroups(struct list_head *group_list)
478 {
479         struct egroup *eg, *egtmp;
480         int i;
481
482         list_for_each_entry_safe (eg, egtmp, group_list, nd) {
483                 for (i = 0; i < eg->idnum; i++)
484                         free((char *)eg->ids[i]);
485                 free(eg->ids);
486                 free(eg);
487         }
488 }
489
490 int metricgroup__parse_groups(const struct option *opt,
491                            const char *str,
492                            struct rblist *metric_events)
493 {
494         struct parse_events_error parse_error;
495         struct perf_evlist *perf_evlist = *(struct perf_evlist **)opt->value;
496         struct strbuf extra_events;
497         LIST_HEAD(group_list);
498         int ret;
499
500         if (metric_events->nr_entries == 0)
501                 metricgroup__rblist_init(metric_events);
502         ret = metricgroup__add_metric_list(str, &extra_events, &group_list);
503         if (ret)
504                 return ret;
505         pr_debug("adding %s\n", extra_events.buf);
506         memset(&parse_error, 0, sizeof(struct parse_events_error));
507         ret = parse_events(perf_evlist, extra_events.buf, &parse_error);
508         if (ret) {
509                 parse_events_print_error(&parse_error, extra_events.buf);
510                 goto out;
511         }
512         strbuf_release(&extra_events);
513         ret = metricgroup__setup_events(&group_list, perf_evlist,
514                                         metric_events);
515 out:
516         metricgroup__free_egroups(&group_list);
517         return ret;
518 }
519
520 bool metricgroup__has_metric(const char *metric)
521 {
522         struct pmu_events_map *map = perf_pmu__find_map(NULL);
523         struct pmu_event *pe;
524         int i;
525
526         if (!map)
527                 return false;
528
529         for (i = 0; ; i++) {
530                 pe = &map->table[i];
531
532                 if (!pe->name && !pe->metric_group && !pe->metric_name)
533                         break;
534                 if (!pe->metric_expr)
535                         continue;
536                 if (match_metric(pe->metric_name, metric))
537                         return true;
538         }
539         return false;
540 }