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