1 // SPDX-License-Identifier: GPL-2.0
3 * Support for libpfm4 event encoding.
5 * Copyright 2020 Google LLC.
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"
17 #include <linux/kernel.h>
18 #include <perfmon/pfmlib_perf_event.h>
20 static void libpfm_initialize(void)
24 ret = pfm_initialize();
25 if (ret != PFM_SUCCESS) {
26 ui__warning("libpfm failed to initialize: %s\n",
31 int parse_libpfm_events_option(const struct option *opt, const char *str,
32 int unset __maybe_unused)
34 struct evlist *evlist = *(struct evlist **)opt->value;
35 struct perf_event_attr attr;
37 struct evsel *evsel, *grp_leader = NULL;
45 p_orig = p = strdup(str);
49 * force loading of the PMU list
53 for (q = p; strsep(&p, ",{}"); q = p) {
54 sep = p ? str + (p - p_orig - 1) : "";
58 "nested event groups not supported\n");
68 memset(&attr, 0, sizeof(attr));
69 event_attr_init(&attr);
71 ret = pfm_get_perf_event_encoding(q, PFM_PLM0|PFM_PLM3,
74 if (ret != PFM_SUCCESS) {
75 ui__error("failed to parse event %s : %s\n", str,
80 pmu = perf_pmu__find_by_type((unsigned int)attr.type);
81 evsel = parse_events__add_event(evlist->core.nr_entries,
86 evsel->is_libpfm_event = true;
88 evlist__add(evlist, evsel);
94 evsel->leader = grp_leader;
95 grp_leader->core.nr_members++;
102 "cannot close a non-existing event group\n");
116 static const char *srcs[PFM_ATTR_CTRL_MAX] = {
117 [PFM_ATTR_CTRL_UNKNOWN] = "???",
118 [PFM_ATTR_CTRL_PMU] = "PMU",
119 [PFM_ATTR_CTRL_PERF_EVENT] = "perf_event",
123 print_attr_flags(pfm_event_attr_info_t *info)
128 printf("[default] ");
132 if (info->is_precise) {
133 printf("[precise] ");
142 print_libpfm_events_detailed(pfm_event_info_t *info, bool long_desc)
144 pfm_event_attr_info_t ainfo;
148 ainfo.size = sizeof(ainfo);
150 printf(" %s\n", info->name);
151 printf(" [%s]\n", info->desc);
154 printf(" Equiv: %s\n", info->equiv);
156 printf(" Code : 0x%"PRIx64"\n", info->code);
158 pfm_for_each_event_attr(j, info) {
159 ret = pfm_get_event_attr_info(info->idx, j,
160 PFM_OS_PERF_EVENT_EXT, &ainfo);
161 if (ret != PFM_SUCCESS)
164 if (ainfo.type == PFM_ATTR_UMASK) {
165 printf(" %s:%s\n", info->name, ainfo.name);
166 printf(" [%s]\n", ainfo.desc);
172 if (ainfo.ctrl >= PFM_ATTR_CTRL_MAX)
173 ainfo.ctrl = PFM_ATTR_CTRL_UNKNOWN;
175 src = srcs[ainfo.ctrl];
176 switch (ainfo.type) {
178 printf(" Umask : 0x%02"PRIx64" : %s: ",
180 print_attr_flags(&ainfo);
183 case PFM_ATTR_MOD_BOOL:
184 printf(" Modif : %s: [%s] : %s (boolean)\n", src,
185 ainfo.name, ainfo.desc);
187 case PFM_ATTR_MOD_INTEGER:
188 printf(" Modif : %s: [%s] : %s (integer)\n", src,
189 ainfo.name, ainfo.desc);
192 case PFM_ATTR_RAW_UMASK:
195 printf(" Attr : %s: [%s] : %s\n", src,
196 ainfo.name, ainfo.desc);
202 * list all pmu::event:umask, pmu::event
203 * printed events may not be all valid combinations of umask for an event
206 print_libpfm_events_raw(pfm_pmu_info_t *pinfo, pfm_event_info_t *info)
208 pfm_event_attr_info_t ainfo;
210 bool has_umask = false;
212 ainfo.size = sizeof(ainfo);
214 pfm_for_each_event_attr(j, info) {
215 ret = pfm_get_event_attr_info(info->idx, j,
216 PFM_OS_PERF_EVENT_EXT, &ainfo);
217 if (ret != PFM_SUCCESS)
220 if (ainfo.type != PFM_ATTR_UMASK)
223 printf("%s::%s:%s\n", pinfo->name, info->name, ainfo.name);
227 printf("%s::%s\n", pinfo->name, info->name);
230 void print_libpfm_events(bool name_only, bool long_desc)
232 pfm_event_info_t info;
233 pfm_pmu_info_t pinfo;
238 /* initialize to zero to indicate ABI version */
239 info.size = sizeof(info);
240 pinfo.size = sizeof(pinfo);
243 puts("\nList of pre-defined events (to be used in --pfm-events):\n");
245 pfm_for_all_pmus(p) {
246 bool printed_pmu = false;
248 ret = pfm_get_pmu_info(p, &pinfo);
249 if (ret != PFM_SUCCESS)
252 /* only print events that are supported by host HW */
253 if (!pinfo.is_present)
256 /* handled by perf directly */
257 if (pinfo.pmu == PFM_PMU_PERF_EVENT)
260 for (i = pinfo.first_event; i != -1;
261 i = pfm_get_event_next(i)) {
263 ret = pfm_get_event_info(i, PFM_OS_PERF_EVENT_EXT,
265 if (ret != PFM_SUCCESS)
268 if (!name_only && !printed_pmu) {
269 printf("%s:\n", pinfo.name);
274 print_libpfm_events_detailed(&info, long_desc);
276 print_libpfm_events_raw(&pinfo, &info);
278 if (!name_only && printed_pmu)