1 // SPDX-License-Identifier: GPL-2.0 2 #include <dirent.h> 3 #include <errno.h> 4 #include <stdio.h> 5 #include <stdlib.h> 6 #include <string.h> 7 #include <fcntl.h> 8 #include <sys/param.h> 9 #include <unistd.h> 10 11 #include <api/fs/tracing_path.h> 12 #include <api/io.h> 13 #include <linux/stddef.h> 14 #include <linux/perf_event.h> 15 #include <linux/zalloc.h> 16 #include <subcmd/pager.h> 17 18 #include "build-id.h" 19 #include "debug.h" 20 #include "evsel.h" 21 #include "metricgroup.h" 22 #include "parse-events.h" 23 #include "pmu.h" 24 #include "pmus.h" 25 #include "print-events.h" 26 #include "probe-file.h" 27 #include "string2.h" 28 #include "strlist.h" 29 #include "tracepoint.h" 30 #include "pfm.h" 31 #include "thread_map.h" 32 #include "tool_pmu.h" 33 #include "util.h" 34 35 #define MAX_NAME_LEN 100 36 37 /** Strings corresponding to enum perf_type_id. */ 38 static const char * const event_type_descriptors[] = { 39 "Hardware event", 40 "Software event", 41 "Tracepoint event", 42 "Hardware cache event", 43 "Raw event descriptor", 44 "Hardware breakpoint", 45 }; 46 47 void print_sdt_events(const struct print_callbacks *print_cb, void *print_state) 48 { 49 struct strlist *bidlist, *sdtlist; 50 struct str_node *bid_nd, *sdt_name, *next_sdt_name; 51 const char *last_sdt_name = NULL; 52 53 /* 54 * The implicitly sorted sdtlist will hold the tracepoint name followed 55 * by @<buildid>. If the tracepoint name is unique (determined by 56 * looking at the adjacent nodes) the @<buildid> is dropped otherwise 57 * the executable path and buildid are added to the name. 58 */ 59 sdtlist = strlist__new(NULL, NULL); 60 if (!sdtlist) { 61 pr_debug("Failed to allocate new strlist for SDT\n"); 62 return; 63 } 64 bidlist = build_id_cache__list_all(true); 65 if (!bidlist) { 66 pr_debug("Failed to get buildids: %d\n", errno); 67 return; 68 } 69 strlist__for_each_entry(bid_nd, bidlist) { 70 struct probe_cache *pcache; 71 struct probe_cache_entry *ent; 72 73 pcache = probe_cache__new(bid_nd->s, NULL); 74 if (!pcache) 75 continue; 76 list_for_each_entry(ent, &pcache->entries, node) { 77 char buf[1024]; 78 79 snprintf(buf, sizeof(buf), "%s:%s@%s", 80 ent->pev.group, ent->pev.event, bid_nd->s); 81 strlist__add(sdtlist, buf); 82 } 83 probe_cache__delete(pcache); 84 } 85 strlist__delete(bidlist); 86 87 strlist__for_each_entry(sdt_name, sdtlist) { 88 bool show_detail = false; 89 char *bid = (char *)strchr(sdt_name->s, '@'); 90 char *evt_name = NULL; 91 92 if (bid) 93 *(bid++) = '\0'; 94 95 if (last_sdt_name && !strcmp(last_sdt_name, sdt_name->s)) { 96 show_detail = true; 97 } else { 98 next_sdt_name = strlist__next(sdt_name); 99 if (next_sdt_name) { 100 const char *bid2 = strchrnul(next_sdt_name->s, '@'); 101 102 show_detail = strncmp(sdt_name->s, next_sdt_name->s, bid2 - next_sdt_name->s) == 0; 103 } 104 } 105 last_sdt_name = sdt_name->s; 106 107 if (show_detail) { 108 char *path = build_id_cache__origname(bid); 109 110 if (path) { 111 if (asprintf(&evt_name, "%s@%s(%.12s)", sdt_name->s, path, bid) < 0) 112 evt_name = NULL; 113 free(path); 114 } 115 } 116 print_cb->print_event(print_state, 117 /*topic=*/NULL, 118 /*pmu_name=*/NULL, 119 PERF_TYPE_TRACEPOINT, 120 evt_name ?: sdt_name->s, 121 /*event_alias=*/NULL, 122 /*deprecated=*/false, 123 /*scale_unit=*/NULL, 124 "SDT event", 125 /*desc=*/NULL, 126 /*long_desc=*/NULL, 127 /*encoding_desc=*/NULL); 128 129 free(evt_name); 130 } 131 strlist__delete(sdtlist); 132 } 133 134 bool is_event_supported(u8 type, u64 config) 135 { 136 bool ret = true; 137 struct evsel *evsel; 138 struct perf_event_attr attr = { 139 .type = type, 140 .config = config, 141 .disabled = 1, 142 }; 143 struct perf_thread_map *tmap = thread_map__new_by_tid(0); 144 145 if (tmap == NULL) 146 return false; 147 148 evsel = evsel__new(&attr); 149 if (evsel) { 150 ret = evsel__open(evsel, NULL, tmap) >= 0; 151 152 if (!ret) { 153 /* 154 * The event may fail to open if the paranoid value 155 * /proc/sys/kernel/perf_event_paranoid is set to 2 156 * Re-run with exclude_kernel set; we don't do that by 157 * default as some ARM machines do not support it. 158 */ 159 evsel->core.attr.exclude_kernel = 1; 160 ret = evsel__open(evsel, NULL, tmap) >= 0; 161 } 162 163 if (!ret) { 164 /* 165 * The event may fail to open if the PMU requires 166 * exclude_guest to be set (e.g. as the Apple M1 PMU 167 * requires). 168 * Re-run with exclude_guest set; we don't do that by 169 * default as it's equally legitimate for another PMU 170 * driver to require that exclude_guest is clear. 171 */ 172 evsel->core.attr.exclude_guest = 1; 173 ret = evsel__open(evsel, NULL, tmap) >= 0; 174 } 175 176 evsel__close(evsel); 177 evsel__delete(evsel); 178 } 179 180 perf_thread_map__put(tmap); 181 return ret; 182 } 183 184 /** struct mep - RB-tree node for building printing information. */ 185 struct mep { 186 /** nd - RB-tree element. */ 187 struct rb_node nd; 188 /** @metric_group: Owned metric group name, separated others with ';'. */ 189 char *metric_group; 190 const char *metric_name; 191 const char *metric_desc; 192 const char *metric_long_desc; 193 const char *metric_expr; 194 const char *metric_threshold; 195 const char *metric_unit; 196 const char *pmu_name; 197 }; 198 199 static int mep_cmp(struct rb_node *rb_node, const void *entry) 200 { 201 struct mep *a = container_of(rb_node, struct mep, nd); 202 struct mep *b = (struct mep *)entry; 203 int ret; 204 205 ret = strcmp(a->metric_group, b->metric_group); 206 if (ret) 207 return ret; 208 209 return strcmp(a->metric_name, b->metric_name); 210 } 211 212 static struct rb_node *mep_new(struct rblist *rl __maybe_unused, const void *entry) 213 { 214 struct mep *me = malloc(sizeof(struct mep)); 215 216 if (!me) 217 return NULL; 218 219 memcpy(me, entry, sizeof(struct mep)); 220 return &me->nd; 221 } 222 223 static void mep_delete(struct rblist *rl __maybe_unused, 224 struct rb_node *nd) 225 { 226 struct mep *me = container_of(nd, struct mep, nd); 227 228 zfree(&me->metric_group); 229 free(me); 230 } 231 232 static struct mep *mep_lookup(struct rblist *groups, const char *metric_group, 233 const char *metric_name) 234 { 235 struct rb_node *nd; 236 struct mep me = { 237 .metric_group = strdup(metric_group), 238 .metric_name = metric_name, 239 }; 240 nd = rblist__find(groups, &me); 241 if (nd) { 242 free(me.metric_group); 243 return container_of(nd, struct mep, nd); 244 } 245 rblist__add_node(groups, &me); 246 nd = rblist__find(groups, &me); 247 if (nd) 248 return container_of(nd, struct mep, nd); 249 return NULL; 250 } 251 252 static int metricgroup__add_to_mep_groups_callback(const struct pmu_metric *pm, 253 const struct pmu_metrics_table *table __maybe_unused, 254 void *vdata) 255 { 256 struct rblist *groups = vdata; 257 const char *g; 258 char *omg, *mg; 259 260 mg = strdup(pm->metric_group ?: pm->metric_name); 261 if (!mg) 262 return -ENOMEM; 263 omg = mg; 264 while ((g = strsep(&mg, ";")) != NULL) { 265 struct mep *me; 266 267 g = skip_spaces(g); 268 if (strlen(g)) 269 me = mep_lookup(groups, g, pm->metric_name); 270 else 271 me = mep_lookup(groups, pm->metric_name, pm->metric_name); 272 273 if (me) { 274 me->metric_desc = pm->desc; 275 me->metric_long_desc = pm->long_desc; 276 me->metric_expr = pm->metric_expr; 277 me->metric_threshold = pm->metric_threshold; 278 me->metric_unit = pm->unit; 279 me->pmu_name = pm->pmu; 280 } 281 } 282 free(omg); 283 284 return 0; 285 } 286 287 void metricgroup__print(const struct print_callbacks *print_cb, void *print_state) 288 { 289 struct rblist groups; 290 struct rb_node *node, *next; 291 const struct pmu_metrics_table *table = pmu_metrics_table__find(); 292 293 rblist__init(&groups); 294 groups.node_new = mep_new; 295 groups.node_cmp = mep_cmp; 296 groups.node_delete = mep_delete; 297 298 metricgroup__for_each_metric(table, metricgroup__add_to_mep_groups_callback, &groups); 299 300 for (node = rb_first_cached(&groups.entries); node; node = next) { 301 struct mep *me = container_of(node, struct mep, nd); 302 303 print_cb->print_metric(print_state, 304 me->metric_group, 305 me->metric_name, 306 me->metric_desc, 307 me->metric_long_desc, 308 me->metric_expr, 309 me->metric_threshold, 310 me->metric_unit, 311 me->pmu_name); 312 next = rb_next(node); 313 rblist__remove_node(&groups, node); 314 } 315 } 316 317 /* 318 * Print the help text for the event symbols: 319 */ 320 void print_events(const struct print_callbacks *print_cb, void *print_state) 321 { 322 perf_pmus__print_pmu_events(print_cb, print_state); 323 324 print_cb->print_event(print_state, 325 /*topic=*/NULL, 326 /*pmu_name=*/NULL, 327 PERF_TYPE_RAW, 328 "rNNN", 329 /*event_alias=*/NULL, 330 /*scale_unit=*/NULL, 331 /*deprecated=*/false, 332 event_type_descriptors[PERF_TYPE_RAW], 333 /*desc=*/NULL, 334 /*long_desc=*/NULL, 335 /*encoding_desc=*/NULL); 336 337 perf_pmus__print_raw_pmu_events(print_cb, print_state); 338 339 print_cb->print_event(print_state, 340 /*topic=*/NULL, 341 /*pmu_name=*/NULL, 342 PERF_TYPE_BREAKPOINT, 343 "mem:<addr>[/len][:access]", 344 /*scale_unit=*/NULL, 345 /*event_alias=*/NULL, 346 /*deprecated=*/false, 347 event_type_descriptors[PERF_TYPE_BREAKPOINT], 348 /*desc=*/NULL, 349 /*long_desc=*/NULL, 350 /*encoding_desc=*/NULL); 351 352 print_sdt_events(print_cb, print_state); 353 354 metricgroup__print(print_cb, print_state); 355 356 print_libpfm_events(print_cb, print_state); 357 } 358