1 // SPDX-License-Identifier: GPL-2.0
2 /*
3 * Support for libpfm4 event encoding.
4 *
5 * Copyright 2020 Google LLC.
6 */
7 #include "util/cpumap.h"
8 #include "util/debug.h"
9 #include "util/event.h"
10 #include "util/evlist.h"
11 #include "util/evsel.h"
12 #include "util/parse-events.h"
13 #include "util/pmus.h"
14 #include "util/pfm.h"
15 #include "util/strbuf.h"
16 #include "util/thread_map.h"
17
18 #include <string.h>
19 #include <linux/kernel.h>
20 #include <perfmon/pfmlib_perf_event.h>
21
libpfm_initialize(void)22 static void libpfm_initialize(void)
23 {
24 int ret;
25
26 ret = pfm_initialize();
27 if (ret != PFM_SUCCESS) {
28 ui__warning("libpfm failed to initialize: %s\n",
29 pfm_strerror(ret));
30 }
31 }
32
parse_libpfm_events_option(const struct option * opt,const char * str,int unset __maybe_unused)33 int parse_libpfm_events_option(const struct option *opt, const char *str,
34 int unset __maybe_unused)
35 {
36 struct evlist *evlist = *(struct evlist **)opt->value;
37 struct perf_event_attr attr;
38 struct perf_pmu *pmu;
39 struct evsel *evsel, *grp_leader = NULL;
40 char *p, *q, *p_orig;
41 const char *sep;
42 int grp_evt = -1;
43 int ret;
44
45 libpfm_initialize();
46
47 p_orig = p = strdup(str);
48 if (!p)
49 return -1;
50
51 for (q = p; strsep(&p, ",{}"); q = p) {
52 sep = p ? str + (p - p_orig - 1) : "";
53 if (*sep == '{') {
54 if (grp_evt > -1) {
55 ui__error(
56 "nested event groups not supported\n");
57 goto error;
58 }
59 grp_evt++;
60 }
61
62 /* no event */
63 if (*q == '\0') {
64 if (*sep == '}') {
65 if (grp_evt < 0) {
66 ui__error("cannot close a non-existing event group\n");
67 goto error;
68 }
69 grp_evt--;
70 }
71 continue;
72 }
73
74 memset(&attr, 0, sizeof(attr));
75 event_attr_init(&attr);
76
77 ret = pfm_get_perf_event_encoding(q, PFM_PLM0|PFM_PLM3,
78 &attr, NULL, NULL);
79
80 if (ret != PFM_SUCCESS) {
81 ui__error("failed to parse event %s : %s\n", str,
82 pfm_strerror(ret));
83 goto error;
84 }
85
86 pmu = perf_pmus__find_by_type((unsigned int)attr.type);
87 evsel = parse_events__add_event(evlist->core.nr_entries,
88 &attr, q, /*metric_id=*/NULL,
89 pmu);
90 if (evsel == NULL)
91 goto error;
92
93 evsel->is_libpfm_event = true;
94
95 evlist__add(evlist, evsel);
96
97 if (grp_evt == 0)
98 grp_leader = evsel;
99
100 if (grp_evt > -1) {
101 evsel__set_leader(evsel, grp_leader);
102 grp_leader->core.nr_members++;
103 grp_evt++;
104 }
105
106 if (*sep == '}') {
107 if (grp_evt < 0) {
108 ui__error(
109 "cannot close a non-existing event group\n");
110 goto error;
111 }
112 grp_leader = NULL;
113 grp_evt = -1;
114 }
115 }
116 free(p_orig);
117 return 0;
118 error:
119 free(p_orig);
120 return -1;
121 }
122
is_libpfm_event_supported(const char * name,struct perf_cpu_map * cpus,struct perf_thread_map * threads)123 static bool is_libpfm_event_supported(const char *name, struct perf_cpu_map *cpus,
124 struct perf_thread_map *threads)
125 {
126 struct perf_pmu *pmu;
127 struct evsel *evsel;
128 struct perf_event_attr attr = {};
129 bool result = true;
130 int ret;
131
132 ret = pfm_get_perf_event_encoding(name, PFM_PLM0|PFM_PLM3,
133 &attr, NULL, NULL);
134 if (ret != PFM_SUCCESS)
135 return false;
136
137 pmu = perf_pmus__find_by_type((unsigned int)attr.type);
138 evsel = parse_events__add_event(0, &attr, name, /*metric_id=*/NULL, pmu);
139 if (evsel == NULL)
140 return false;
141
142 evsel->is_libpfm_event = true;
143
144 ret = evsel__open(evsel, cpus, threads);
145 if (ret == -EACCES) {
146 /*
147 * This happens if the paranoid value
148 * /proc/sys/kernel/perf_event_paranoid is set to 2
149 * Re-run with exclude_kernel set; we don't do that
150 * by default as some ARM machines do not support it.
151 *
152 */
153 evsel->core.attr.exclude_kernel = 1;
154 ret = evsel__open(evsel, cpus, threads);
155
156 }
157 if (ret < 0)
158 result = false;
159
160 evsel__close(evsel);
161 evsel__delete(evsel);
162
163 return result;
164 }
165
166 static const char *srcs[PFM_ATTR_CTRL_MAX] = {
167 [PFM_ATTR_CTRL_UNKNOWN] = "???",
168 [PFM_ATTR_CTRL_PMU] = "PMU",
169 [PFM_ATTR_CTRL_PERF_EVENT] = "perf_event",
170 };
171
172 static void
print_attr_flags(struct strbuf * buf,const pfm_event_attr_info_t * info)173 print_attr_flags(struct strbuf *buf, const pfm_event_attr_info_t *info)
174 {
175 if (info->is_dfl)
176 strbuf_addf(buf, "[default] ");
177
178 if (info->is_precise)
179 strbuf_addf(buf, "[precise] ");
180 }
181
182 static void
print_libpfm_event(const struct print_callbacks * print_cb,void * print_state,const pfm_pmu_info_t * pinfo,const pfm_event_info_t * info,struct strbuf * buf)183 print_libpfm_event(const struct print_callbacks *print_cb, void *print_state,
184 const pfm_pmu_info_t *pinfo, const pfm_event_info_t *info,
185 struct strbuf *buf)
186 {
187 int j, ret;
188 char topic[80], name[80];
189 struct perf_cpu_map *cpus = perf_cpu_map__empty_new(1);
190 struct perf_thread_map *threads = thread_map__new_by_tid(0);
191
192 strbuf_setlen(buf, 0);
193 snprintf(topic, sizeof(topic), "pfm %s", pinfo->name);
194
195 snprintf(name, sizeof(name), "%s::%s", pinfo->name, info->name);
196 strbuf_addf(buf, "Code: 0x%"PRIx64"\n", info->code);
197
198 pfm_for_each_event_attr(j, info) {
199 pfm_event_attr_info_t ainfo;
200 const char *src;
201
202 ainfo.size = sizeof(ainfo);
203 ret = pfm_get_event_attr_info(info->idx, j, PFM_OS_PERF_EVENT_EXT, &ainfo);
204 if (ret != PFM_SUCCESS)
205 continue;
206
207 if (ainfo.ctrl >= PFM_ATTR_CTRL_MAX)
208 ainfo.ctrl = PFM_ATTR_CTRL_UNKNOWN;
209
210 src = srcs[ainfo.ctrl];
211 switch (ainfo.type) {
212 case PFM_ATTR_UMASK: /* Ignore for now */
213 break;
214 case PFM_ATTR_MOD_BOOL:
215 strbuf_addf(buf, " Modif: %s: [%s] : %s (boolean)\n", src,
216 ainfo.name, ainfo.desc);
217 break;
218 case PFM_ATTR_MOD_INTEGER:
219 strbuf_addf(buf, " Modif: %s: [%s] : %s (integer)\n", src,
220 ainfo.name, ainfo.desc);
221 break;
222 case PFM_ATTR_NONE:
223 case PFM_ATTR_RAW_UMASK:
224 case PFM_ATTR_MAX:
225 default:
226 strbuf_addf(buf, " Attr: %s: [%s] : %s\n", src,
227 ainfo.name, ainfo.desc);
228 }
229 }
230
231 if (is_libpfm_event_supported(name, cpus, threads)) {
232 print_cb->print_event(print_state, topic, pinfo->name,
233 /*pmu_type=*/PERF_TYPE_RAW,
234 name, info->equiv,
235 /*scale_unit=*/NULL,
236 /*deprecated=*/NULL, "PFM event",
237 info->desc, /*long_desc=*/NULL,
238 /*encoding_desc=*/buf->buf);
239 }
240
241 pfm_for_each_event_attr(j, info) {
242 pfm_event_attr_info_t ainfo;
243 const char *src;
244
245 strbuf_setlen(buf, 0);
246
247 ainfo.size = sizeof(ainfo);
248 ret = pfm_get_event_attr_info(info->idx, j, PFM_OS_PERF_EVENT_EXT, &ainfo);
249 if (ret != PFM_SUCCESS)
250 continue;
251
252 if (ainfo.ctrl >= PFM_ATTR_CTRL_MAX)
253 ainfo.ctrl = PFM_ATTR_CTRL_UNKNOWN;
254
255 src = srcs[ainfo.ctrl];
256 if (ainfo.type == PFM_ATTR_UMASK) {
257 strbuf_addf(buf, "Umask: 0x%02"PRIx64" : %s: ",
258 ainfo.code, src);
259 print_attr_flags(buf, &ainfo);
260 snprintf(name, sizeof(name), "%s::%s:%s",
261 pinfo->name, info->name, ainfo.name);
262
263 if (!is_libpfm_event_supported(name, cpus, threads))
264 continue;
265
266 print_cb->print_event(print_state,
267 topic,
268 pinfo->name,
269 /*pmu_type=*/PERF_TYPE_RAW,
270 name, /*alias=*/NULL,
271 /*scale_unit=*/NULL,
272 /*deprecated=*/NULL, "PFM event",
273 ainfo.desc, /*long_desc=*/NULL,
274 /*encoding_desc=*/buf->buf);
275 }
276 }
277
278 perf_cpu_map__put(cpus);
279 perf_thread_map__put(threads);
280 }
281
print_libpfm_events(const struct print_callbacks * print_cb,void * print_state)282 void print_libpfm_events(const struct print_callbacks *print_cb, void *print_state)
283 {
284 pfm_event_info_t info;
285 pfm_pmu_info_t pinfo;
286 int p, ret;
287 struct strbuf storage;
288
289 libpfm_initialize();
290
291 /* initialize to zero to indicate ABI version */
292 info.size = sizeof(info);
293 pinfo.size = sizeof(pinfo);
294
295 strbuf_init(&storage, 2048);
296
297 pfm_for_all_pmus(p) {
298 ret = pfm_get_pmu_info(p, &pinfo);
299 if (ret != PFM_SUCCESS)
300 continue;
301
302 /* only print events that are supported by host HW */
303 if (!pinfo.is_present)
304 continue;
305
306 /* handled by perf directly */
307 if (pinfo.pmu == PFM_PMU_PERF_EVENT)
308 continue;
309
310 for (int i = pinfo.first_event; i != -1; i = pfm_get_event_next(i)) {
311 ret = pfm_get_event_info(i, PFM_OS_PERF_EVENT_EXT,
312 &info);
313 if (ret != PFM_SUCCESS)
314 continue;
315
316 print_libpfm_event(print_cb, print_state, &pinfo, &info, &storage);
317 }
318 }
319 strbuf_release(&storage);
320 }
321