xref: /openbmc/linux/tools/perf/util/pfm.c (revision d47a97bd)
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/pmu.h"
14 #include "util/pfm.h"
15 #include "util/strbuf.h"
16 
17 #include <string.h>
18 #include <linux/kernel.h>
19 #include <perfmon/pfmlib_perf_event.h>
20 
21 static void libpfm_initialize(void)
22 {
23 	int ret;
24 
25 	ret = pfm_initialize();
26 	if (ret != PFM_SUCCESS) {
27 		ui__warning("libpfm failed to initialize: %s\n",
28 			pfm_strerror(ret));
29 	}
30 }
31 
32 int parse_libpfm_events_option(const struct option *opt, const char *str,
33 			int unset __maybe_unused)
34 {
35 	struct evlist *evlist = *(struct evlist **)opt->value;
36 	struct perf_event_attr attr;
37 	struct perf_pmu *pmu;
38 	struct evsel *evsel, *grp_leader = NULL;
39 	char *p, *q, *p_orig;
40 	const char *sep;
41 	int grp_evt = -1;
42 	int ret;
43 
44 	libpfm_initialize();
45 
46 	p_orig = p = strdup(str);
47 	if (!p)
48 		return -1;
49 	/*
50 	 * force loading of the PMU list
51 	 */
52 	perf_pmu__scan(NULL);
53 
54 	for (q = p; strsep(&p, ",{}"); q = p) {
55 		sep = p ? str + (p - p_orig - 1) : "";
56 		if (*sep == '{') {
57 			if (grp_evt > -1) {
58 				ui__error(
59 					"nested event groups not supported\n");
60 				goto error;
61 			}
62 			grp_evt++;
63 		}
64 
65 		/* no event */
66 		if (*q == '\0') {
67 			if (*sep == '}') {
68 				if (grp_evt < 0) {
69 					ui__error("cannot close a non-existing event group\n");
70 					goto error;
71 				}
72 				grp_evt--;
73 			}
74 			continue;
75 		}
76 
77 		memset(&attr, 0, sizeof(attr));
78 		event_attr_init(&attr);
79 
80 		ret = pfm_get_perf_event_encoding(q, PFM_PLM0|PFM_PLM3,
81 						&attr, NULL, NULL);
82 
83 		if (ret != PFM_SUCCESS) {
84 			ui__error("failed to parse event %s : %s\n", str,
85 				  pfm_strerror(ret));
86 			goto error;
87 		}
88 
89 		pmu = perf_pmu__find_by_type((unsigned int)attr.type);
90 		evsel = parse_events__add_event(evlist->core.nr_entries,
91 						&attr, q, /*metric_id=*/NULL,
92 						pmu);
93 		if (evsel == NULL)
94 			goto error;
95 
96 		evsel->is_libpfm_event = true;
97 
98 		evlist__add(evlist, evsel);
99 
100 		if (grp_evt == 0)
101 			grp_leader = evsel;
102 
103 		if (grp_evt > -1) {
104 			evsel__set_leader(evsel, grp_leader);
105 			grp_leader->core.nr_members++;
106 			grp_evt++;
107 		}
108 
109 		if (*sep == '}') {
110 			if (grp_evt < 0) {
111 				ui__error(
112 				   "cannot close a non-existing event group\n");
113 				goto error;
114 			}
115 			evlist->core.nr_groups++;
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 const char *srcs[PFM_ATTR_CTRL_MAX] = {
128 	[PFM_ATTR_CTRL_UNKNOWN] = "???",
129 	[PFM_ATTR_CTRL_PMU] = "PMU",
130 	[PFM_ATTR_CTRL_PERF_EVENT] = "perf_event",
131 };
132 
133 static void
134 print_attr_flags(struct strbuf *buf, const pfm_event_attr_info_t *info)
135 {
136 	if (info->is_dfl)
137 		strbuf_addf(buf, "[default] ");
138 
139 	if (info->is_precise)
140 		strbuf_addf(buf, "[precise] ");
141 }
142 
143 static void
144 print_libpfm_event(const struct print_callbacks *print_cb, void *print_state,
145 		const pfm_pmu_info_t *pinfo, const pfm_event_info_t *info,
146 		struct strbuf *buf)
147 {
148 	int j, ret;
149 	char topic[80], name[80];
150 
151 	strbuf_setlen(buf, 0);
152 	snprintf(topic, sizeof(topic), "pfm %s", pinfo->name);
153 
154 	snprintf(name, sizeof(name), "%s::%s", pinfo->name, info->name);
155 	strbuf_addf(buf, "Code: 0x%"PRIx64"\n", info->code);
156 
157 	pfm_for_each_event_attr(j, info) {
158 		pfm_event_attr_info_t ainfo;
159 		const char *src;
160 
161 		ainfo.size = sizeof(ainfo);
162 		ret = pfm_get_event_attr_info(info->idx, j, PFM_OS_PERF_EVENT_EXT, &ainfo);
163 		if (ret != PFM_SUCCESS)
164 			continue;
165 
166 		if (ainfo.ctrl >= PFM_ATTR_CTRL_MAX)
167 			ainfo.ctrl = PFM_ATTR_CTRL_UNKNOWN;
168 
169 		src = srcs[ainfo.ctrl];
170 		switch (ainfo.type) {
171 		case PFM_ATTR_UMASK: /* Ignore for now */
172 			break;
173 		case PFM_ATTR_MOD_BOOL:
174 			strbuf_addf(buf, " Modif: %s: [%s] : %s (boolean)\n", src,
175 				    ainfo.name, ainfo.desc);
176 			break;
177 		case PFM_ATTR_MOD_INTEGER:
178 			strbuf_addf(buf, " Modif: %s: [%s] : %s (integer)\n", src,
179 				    ainfo.name, ainfo.desc);
180 			break;
181 		case PFM_ATTR_NONE:
182 		case PFM_ATTR_RAW_UMASK:
183 		case PFM_ATTR_MAX:
184 		default:
185 			strbuf_addf(buf, " Attr: %s: [%s] : %s\n", src,
186 				    ainfo.name, ainfo.desc);
187 		}
188 	}
189 	print_cb->print_event(print_state,
190 			pinfo->name,
191 			topic,
192 			name, info->equiv,
193 			/*scale_unit=*/NULL,
194 			/*deprecated=*/NULL, "PFM event",
195 			info->desc, /*long_desc=*/NULL,
196 			/*encoding_desc=*/buf->buf);
197 
198 	pfm_for_each_event_attr(j, info) {
199 		pfm_event_attr_info_t ainfo;
200 		const char *src;
201 
202 		strbuf_setlen(buf, 0);
203 
204 		ainfo.size = sizeof(ainfo);
205 		ret = pfm_get_event_attr_info(info->idx, j, PFM_OS_PERF_EVENT_EXT, &ainfo);
206 		if (ret != PFM_SUCCESS)
207 			continue;
208 
209 		if (ainfo.ctrl >= PFM_ATTR_CTRL_MAX)
210 			ainfo.ctrl = PFM_ATTR_CTRL_UNKNOWN;
211 
212 		src = srcs[ainfo.ctrl];
213 		if (ainfo.type == PFM_ATTR_UMASK) {
214 			strbuf_addf(buf, "Umask: 0x%02"PRIx64" : %s: ",
215 				ainfo.code, src);
216 			print_attr_flags(buf, &ainfo);
217 			snprintf(name, sizeof(name), "%s::%s:%s",
218 				 pinfo->name, info->name, ainfo.name);
219 			print_cb->print_event(print_state,
220 					pinfo->name,
221 					topic,
222 					name, /*alias=*/NULL,
223 					/*scale_unit=*/NULL,
224 					/*deprecated=*/NULL, "PFM event",
225 					ainfo.desc, /*long_desc=*/NULL,
226 					/*encoding_desc=*/buf->buf);
227 		}
228 	}
229 }
230 
231 void print_libpfm_events(const struct print_callbacks *print_cb, void *print_state)
232 {
233 	pfm_event_info_t info;
234 	pfm_pmu_info_t pinfo;
235 	int p, ret;
236 	struct strbuf storage;
237 
238 	libpfm_initialize();
239 
240 	/* initialize to zero to indicate ABI version */
241 	info.size  = sizeof(info);
242 	pinfo.size = sizeof(pinfo);
243 
244 	strbuf_init(&storage, 2048);
245 
246 	pfm_for_all_pmus(p) {
247 		ret = pfm_get_pmu_info(p, &pinfo);
248 		if (ret != PFM_SUCCESS)
249 			continue;
250 
251 		/* only print events that are supported by host HW */
252 		if (!pinfo.is_present)
253 			continue;
254 
255 		/* handled by perf directly */
256 		if (pinfo.pmu == PFM_PMU_PERF_EVENT)
257 			continue;
258 
259 		for (int i = pinfo.first_event; i != -1; i = pfm_get_event_next(i)) {
260 			ret = pfm_get_event_info(i, PFM_OS_PERF_EVENT_EXT,
261 						&info);
262 			if (ret != PFM_SUCCESS)
263 				continue;
264 
265 			print_libpfm_event(print_cb, print_state, &pinfo, &info, &storage);
266 		}
267 	}
268 	strbuf_release(&storage);
269 }
270