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