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