perf stat: Support JSON metrics in perf stat
[linux-block.git] / tools / perf / util / metricgroup.c
CommitLineData
b18f3e36
AK
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 "pmu.h"
24#include <string.h>
25#include <stdbool.h>
26#include <errno.h>
27#include "pmu-events/pmu-events.h"
28#include "strbuf.h"
29#include "strlist.h"
30#include <assert.h>
31#include <ctype.h>
32
33struct metric_event *metricgroup__lookup(struct rblist *metric_events,
34 struct perf_evsel *evsel,
35 bool create)
36{
37 struct rb_node *nd;
38 struct metric_event me = {
39 .evsel = evsel
40 };
41 nd = rblist__find(metric_events, &me);
42 if (nd)
43 return container_of(nd, struct metric_event, nd);
44 if (create) {
45 rblist__add_node(metric_events, &me);
46 nd = rblist__find(metric_events, &me);
47 if (nd)
48 return container_of(nd, struct metric_event, nd);
49 }
50 return NULL;
51}
52
53static int metric_event_cmp(struct rb_node *rb_node, const void *entry)
54{
55 struct metric_event *a = container_of(rb_node,
56 struct metric_event,
57 nd);
58 const struct metric_event *b = entry;
59
60 if (a->evsel == b->evsel)
61 return 0;
62 if ((char *)a->evsel < (char *)b->evsel)
63 return -1;
64 return +1;
65}
66
67static struct rb_node *metric_event_new(struct rblist *rblist __maybe_unused,
68 const void *entry)
69{
70 struct metric_event *me = malloc(sizeof(struct metric_event));
71
72 if (!me)
73 return NULL;
74 memcpy(me, entry, sizeof(struct metric_event));
75 me->evsel = ((struct metric_event *)entry)->evsel;
76 INIT_LIST_HEAD(&me->head);
77 return &me->nd;
78}
79
80static void metricgroup__rblist_init(struct rblist *metric_events)
81{
82 rblist__init(metric_events);
83 metric_events->node_cmp = metric_event_cmp;
84 metric_events->node_new = metric_event_new;
85}
86
87struct egroup {
88 struct list_head nd;
89 int idnum;
90 const char **ids;
91 const char *metric_name;
92 const char *metric_expr;
93};
94
95static struct perf_evsel *find_evsel(struct perf_evlist *perf_evlist,
96 const char **ids,
97 int idnum,
98 struct perf_evsel **metric_events)
99{
100 struct perf_evsel *ev, *start = NULL;
101 int ind = 0;
102
103 evlist__for_each_entry (perf_evlist, ev) {
104 if (!strcmp(ev->name, ids[ind])) {
105 metric_events[ind] = ev;
106 if (ind == 0)
107 start = ev;
108 if (++ind == idnum) {
109 metric_events[ind] = NULL;
110 return start;
111 }
112 } else {
113 ind = 0;
114 start = NULL;
115 }
116 }
117 /*
118 * This can happen when an alias expands to multiple
119 * events, like for uncore events.
120 * We don't support this case for now.
121 */
122 return NULL;
123}
124
125static int metricgroup__setup_events(struct list_head *groups,
126 struct perf_evlist *perf_evlist,
127 struct rblist *metric_events_list)
128{
129 struct metric_event *me;
130 struct metric_expr *expr;
131 int i = 0;
132 int ret = 0;
133 struct egroup *eg;
134 struct perf_evsel *evsel;
135
136 list_for_each_entry (eg, groups, nd) {
137 struct perf_evsel **metric_events;
138
139 metric_events = calloc(sizeof(void *), eg->idnum + 1);
140 if (!metric_events) {
141 ret = -ENOMEM;
142 break;
143 }
144 evsel = find_evsel(perf_evlist, eg->ids, eg->idnum,
145 metric_events);
146 if (!evsel) {
147 pr_debug("Cannot resolve %s: %s\n",
148 eg->metric_name, eg->metric_expr);
149 continue;
150 }
151 for (i = 0; i < eg->idnum; i++)
152 metric_events[i]->collect_stat = true;
153 me = metricgroup__lookup(metric_events_list, evsel, true);
154 if (!me) {
155 ret = -ENOMEM;
156 break;
157 }
158 expr = malloc(sizeof(struct metric_expr));
159 if (!expr) {
160 ret = -ENOMEM;
161 break;
162 }
163 expr->metric_expr = eg->metric_expr;
164 expr->metric_name = eg->metric_name;
165 expr->metric_events = metric_events;
166 list_add(&expr->nd, &me->head);
167 }
168 return ret;
169}
170
171static bool match_metric(const char *n, const char *list)
172{
173 int len;
174 char *m;
175
176 if (!list)
177 return false;
178 if (!strcmp(list, "all"))
179 return true;
180 if (!n)
181 return !strcasecmp(list, "No_group");
182 len = strlen(list);
183 m = strcasestr(n, list);
184 if (!m)
185 return false;
186 if ((m == n || m[-1] == ';' || m[-1] == ' ') &&
187 (m[len] == 0 || m[len] == ';'))
188 return true;
189 return false;
190}
191
192static int metricgroup__add_metric(const char *metric, struct strbuf *events,
193 struct list_head *group_list)
194{
195 struct pmu_events_map *map = perf_pmu__find_map();
196 struct pmu_event *pe;
197 int ret = -EINVAL;
198 int i, j;
199
200 strbuf_init(events, 100);
201 strbuf_addf(events, "%s", "");
202
203 if (!map)
204 return 0;
205
206 for (i = 0; ; i++) {
207 pe = &map->table[i];
208
209 if (!pe->name && !pe->metric_group && !pe->metric_name)
210 break;
211 if (!pe->metric_expr)
212 continue;
213 if (match_metric(pe->metric_group, metric) ||
214 match_metric(pe->metric_name, metric)) {
215 const char **ids;
216 int idnum;
217 struct egroup *eg;
218
219 pr_debug("metric expr %s for %s\n", pe->metric_expr, pe->metric_name);
220
221 if (expr__find_other(pe->metric_expr,
222 NULL, &ids, &idnum) < 0)
223 continue;
224 if (events->len > 0)
225 strbuf_addf(events, ",");
226 for (j = 0; j < idnum; j++) {
227 pr_debug("found event %s\n", ids[j]);
228 strbuf_addf(events, "%s%s",
229 j == 0 ? "{" : ",",
230 ids[j]);
231 }
232 strbuf_addf(events, "}:W");
233
234 eg = malloc(sizeof(struct egroup));
235 if (!eg) {
236 ret = -ENOMEM;
237 break;
238 }
239 eg->ids = ids;
240 eg->idnum = idnum;
241 eg->metric_name = pe->metric_name;
242 eg->metric_expr = pe->metric_expr;
243 list_add_tail(&eg->nd, group_list);
244 ret = 0;
245 }
246 }
247 return ret;
248}
249
250static int metricgroup__add_metric_list(const char *list, struct strbuf *events,
251 struct list_head *group_list)
252{
253 char *llist, *nlist, *p;
254 int ret = -EINVAL;
255
256 nlist = strdup(list);
257 if (!nlist)
258 return -ENOMEM;
259 llist = nlist;
260 while ((p = strsep(&llist, ",")) != NULL) {
261 ret = metricgroup__add_metric(p, events, group_list);
262 if (ret == -EINVAL) {
263 fprintf(stderr, "Cannot find metric or group `%s'\n",
264 p);
265 break;
266 }
267 }
268 free(nlist);
269 return ret;
270}
271
272static void metricgroup__free_egroups(struct list_head *group_list)
273{
274 struct egroup *eg, *egtmp;
275 int i;
276
277 list_for_each_entry_safe (eg, egtmp, group_list, nd) {
278 for (i = 0; i < eg->idnum; i++)
279 free((char *)eg->ids[i]);
280 free(eg->ids);
281 free(eg);
282 }
283}
284
285int metricgroup__parse_groups(const struct option *opt,
286 const char *str,
287 struct rblist *metric_events)
288{
289 struct parse_events_error parse_error;
290 struct perf_evlist *perf_evlist = *(struct perf_evlist **)opt->value;
291 struct strbuf extra_events;
292 LIST_HEAD(group_list);
293 int ret;
294
295 if (metric_events->nr_entries == 0)
296 metricgroup__rblist_init(metric_events);
297 ret = metricgroup__add_metric_list(str, &extra_events, &group_list);
298 if (ret)
299 return ret;
300 pr_debug("adding %s\n", extra_events.buf);
301 memset(&parse_error, 0, sizeof(struct parse_events_error));
302 ret = parse_events(perf_evlist, extra_events.buf, &parse_error);
303 if (ret) {
304 pr_err("Cannot set up events %s\n", extra_events.buf);
305 goto out;
306 }
307 strbuf_release(&extra_events);
308 ret = metricgroup__setup_events(&group_list, perf_evlist,
309 metric_events);
310out:
311 metricgroup__free_egroups(&group_list);
312 return ret;
313}