1 // SPDX-License-Identifier: GPL-2.0 2 #include <inttypes.h> 3 #include <sys/types.h> 4 #include <sys/stat.h> 5 #include <unistd.h> 6 #include "builtin.h" 7 #include "perf.h" 8 9 #include <subcmd/parse-options.h> 10 #include "util/auxtrace.h" 11 #include "util/trace-event.h" 12 #include "util/tool.h" 13 #include "util/session.h" 14 #include "util/data.h" 15 #include "util/map_symbol.h" 16 #include "util/mem-events.h" 17 #include "util/debug.h" 18 #include "util/dso.h" 19 #include "util/map.h" 20 #include "util/symbol.h" 21 #include "util/pmu.h" 22 #include "util/pmu-hybrid.h" 23 #include "util/string2.h" 24 #include <linux/err.h> 25 26 #define MEM_OPERATION_LOAD 0x1 27 #define MEM_OPERATION_STORE 0x2 28 29 struct perf_mem { 30 struct perf_tool tool; 31 char const *input_name; 32 bool hide_unresolved; 33 bool dump_raw; 34 bool force; 35 bool phys_addr; 36 bool data_page_size; 37 int operation; 38 const char *cpu_list; 39 DECLARE_BITMAP(cpu_bitmap, MAX_NR_CPUS); 40 }; 41 42 static int parse_record_events(const struct option *opt, 43 const char *str, int unset __maybe_unused) 44 { 45 struct perf_mem *mem = *(struct perf_mem **)opt->value; 46 47 if (!strcmp(str, "list")) { 48 perf_mem_events__list(); 49 exit(0); 50 } 51 if (perf_mem_events__parse(str)) 52 exit(-1); 53 54 mem->operation = 0; 55 return 0; 56 } 57 58 static const char * const __usage[] = { 59 "perf mem record [<options>] [<command>]", 60 "perf mem record [<options>] -- <command> [<options>]", 61 NULL 62 }; 63 64 static const char * const *record_mem_usage = __usage; 65 66 static int __cmd_record(int argc, const char **argv, struct perf_mem *mem) 67 { 68 int rec_argc, i = 0, j, tmp_nr = 0; 69 int start, end; 70 const char **rec_argv; 71 char **rec_tmp; 72 int ret; 73 bool all_user = false, all_kernel = false; 74 struct perf_mem_event *e; 75 struct option options[] = { 76 OPT_CALLBACK('e', "event", &mem, "event", 77 "event selector. use 'perf mem record -e list' to list available events", 78 parse_record_events), 79 OPT_UINTEGER(0, "ldlat", &perf_mem_events__loads_ldlat, "mem-loads latency"), 80 OPT_INCR('v', "verbose", &verbose, 81 "be more verbose (show counter open errors, etc)"), 82 OPT_BOOLEAN('U', "all-user", &all_user, "collect only user level data"), 83 OPT_BOOLEAN('K', "all-kernel", &all_kernel, "collect only kernel level data"), 84 OPT_END() 85 }; 86 87 if (perf_mem_events__init()) { 88 pr_err("failed: memory events not supported\n"); 89 return -1; 90 } 91 92 argc = parse_options(argc, argv, options, record_mem_usage, 93 PARSE_OPT_KEEP_UNKNOWN); 94 95 if (!perf_pmu__has_hybrid()) 96 rec_argc = argc + 9; /* max number of arguments */ 97 else 98 rec_argc = argc + 9 * perf_pmu__hybrid_pmu_num(); 99 100 if (mem->cpu_list) 101 rec_argc += 2; 102 103 rec_argv = calloc(rec_argc + 1, sizeof(char *)); 104 if (!rec_argv) 105 return -1; 106 107 /* 108 * Save the allocated event name strings. 109 */ 110 rec_tmp = calloc(rec_argc + 1, sizeof(char *)); 111 if (!rec_tmp) { 112 free(rec_argv); 113 return -1; 114 } 115 116 rec_argv[i++] = "record"; 117 118 e = perf_mem_events__ptr(PERF_MEM_EVENTS__LOAD_STORE); 119 120 /* 121 * The load and store operations are required, use the event 122 * PERF_MEM_EVENTS__LOAD_STORE if it is supported. 123 */ 124 if (e->tag && 125 (mem->operation & MEM_OPERATION_LOAD) && 126 (mem->operation & MEM_OPERATION_STORE)) { 127 e->record = true; 128 rec_argv[i++] = "-W"; 129 } else { 130 if (mem->operation & MEM_OPERATION_LOAD) { 131 e = perf_mem_events__ptr(PERF_MEM_EVENTS__LOAD); 132 e->record = true; 133 } 134 135 if (mem->operation & MEM_OPERATION_STORE) { 136 e = perf_mem_events__ptr(PERF_MEM_EVENTS__STORE); 137 e->record = true; 138 } 139 } 140 141 e = perf_mem_events__ptr(PERF_MEM_EVENTS__LOAD); 142 if (e->record) 143 rec_argv[i++] = "-W"; 144 145 rec_argv[i++] = "-d"; 146 147 if (mem->phys_addr) 148 rec_argv[i++] = "--phys-data"; 149 150 if (mem->data_page_size) 151 rec_argv[i++] = "--data-page-size"; 152 153 start = i; 154 ret = perf_mem_events__record_args(rec_argv, &i, rec_tmp, &tmp_nr); 155 if (ret) 156 goto out; 157 end = i; 158 159 if (all_user) 160 rec_argv[i++] = "--all-user"; 161 162 if (all_kernel) 163 rec_argv[i++] = "--all-kernel"; 164 165 if (mem->cpu_list) { 166 rec_argv[i++] = "-C"; 167 rec_argv[i++] = mem->cpu_list; 168 } 169 170 for (j = 0; j < argc; j++, i++) 171 rec_argv[i] = argv[j]; 172 173 if (verbose > 0) { 174 pr_debug("calling: record "); 175 176 for (j = start; j < end; j++) 177 pr_debug("%s ", rec_argv[j]); 178 179 pr_debug("\n"); 180 } 181 182 ret = cmd_record(i, rec_argv); 183 out: 184 for (i = 0; i < tmp_nr; i++) 185 free(rec_tmp[i]); 186 187 free(rec_tmp); 188 free(rec_argv); 189 return ret; 190 } 191 192 static int 193 dump_raw_samples(struct perf_tool *tool, 194 union perf_event *event, 195 struct perf_sample *sample, 196 struct machine *machine) 197 { 198 struct perf_mem *mem = container_of(tool, struct perf_mem, tool); 199 struct addr_location al; 200 const char *fmt, *field_sep; 201 char str[PAGE_SIZE_NAME_LEN]; 202 203 if (machine__resolve(machine, &al, sample) < 0) { 204 fprintf(stderr, "problem processing %d event, skipping it.\n", 205 event->header.type); 206 return -1; 207 } 208 209 if (al.filtered || (mem->hide_unresolved && al.sym == NULL)) 210 goto out_put; 211 212 if (al.map != NULL) 213 al.map->dso->hit = 1; 214 215 field_sep = symbol_conf.field_sep; 216 if (field_sep) { 217 fmt = "%d%s%d%s0x%"PRIx64"%s0x%"PRIx64"%s"; 218 } else { 219 fmt = "%5d%s%5d%s0x%016"PRIx64"%s0x016%"PRIx64"%s"; 220 symbol_conf.field_sep = " "; 221 } 222 printf(fmt, 223 sample->pid, 224 symbol_conf.field_sep, 225 sample->tid, 226 symbol_conf.field_sep, 227 sample->ip, 228 symbol_conf.field_sep, 229 sample->addr, 230 symbol_conf.field_sep); 231 232 if (mem->phys_addr) { 233 printf("0x%016"PRIx64"%s", 234 sample->phys_addr, 235 symbol_conf.field_sep); 236 } 237 238 if (mem->data_page_size) { 239 printf("%s%s", 240 get_page_size_name(sample->data_page_size, str), 241 symbol_conf.field_sep); 242 } 243 244 if (field_sep) 245 fmt = "%"PRIu64"%s0x%"PRIx64"%s%s:%s\n"; 246 else 247 fmt = "%5"PRIu64"%s0x%06"PRIx64"%s%s:%s\n"; 248 249 printf(fmt, 250 sample->weight, 251 symbol_conf.field_sep, 252 sample->data_src, 253 symbol_conf.field_sep, 254 al.map ? (al.map->dso ? al.map->dso->long_name : "???") : "???", 255 al.sym ? al.sym->name : "???"); 256 out_put: 257 addr_location__put(&al); 258 return 0; 259 } 260 261 static int process_sample_event(struct perf_tool *tool, 262 union perf_event *event, 263 struct perf_sample *sample, 264 struct evsel *evsel __maybe_unused, 265 struct machine *machine) 266 { 267 return dump_raw_samples(tool, event, sample, machine); 268 } 269 270 static int report_raw_events(struct perf_mem *mem) 271 { 272 struct itrace_synth_opts itrace_synth_opts = { 273 .set = true, 274 .mem = true, /* Only enable memory event */ 275 .default_no_sample = true, 276 }; 277 278 struct perf_data data = { 279 .path = input_name, 280 .mode = PERF_DATA_MODE_READ, 281 .force = mem->force, 282 }; 283 int ret; 284 struct perf_session *session = perf_session__new(&data, &mem->tool); 285 286 if (IS_ERR(session)) 287 return PTR_ERR(session); 288 289 session->itrace_synth_opts = &itrace_synth_opts; 290 291 if (mem->cpu_list) { 292 ret = perf_session__cpu_bitmap(session, mem->cpu_list, 293 mem->cpu_bitmap); 294 if (ret < 0) 295 goto out_delete; 296 } 297 298 ret = symbol__init(&session->header.env); 299 if (ret < 0) 300 goto out_delete; 301 302 printf("# PID, TID, IP, ADDR, "); 303 304 if (mem->phys_addr) 305 printf("PHYS ADDR, "); 306 307 if (mem->data_page_size) 308 printf("DATA PAGE SIZE, "); 309 310 printf("LOCAL WEIGHT, DSRC, SYMBOL\n"); 311 312 ret = perf_session__process_events(session); 313 314 out_delete: 315 perf_session__delete(session); 316 return ret; 317 } 318 static char *get_sort_order(struct perf_mem *mem) 319 { 320 bool has_extra_options = (mem->phys_addr | mem->data_page_size) ? true : false; 321 char sort[128]; 322 323 /* 324 * there is no weight (cost) associated with stores, so don't print 325 * the column 326 */ 327 if (!(mem->operation & MEM_OPERATION_LOAD)) { 328 strcpy(sort, "--sort=mem,sym,dso,symbol_daddr," 329 "dso_daddr,tlb,locked"); 330 } else if (has_extra_options) { 331 strcpy(sort, "--sort=local_weight,mem,sym,dso,symbol_daddr," 332 "dso_daddr,snoop,tlb,locked,blocked"); 333 } else 334 return NULL; 335 336 if (mem->phys_addr) 337 strcat(sort, ",phys_daddr"); 338 339 if (mem->data_page_size) 340 strcat(sort, ",data_page_size"); 341 342 return strdup(sort); 343 } 344 345 static int report_events(int argc, const char **argv, struct perf_mem *mem) 346 { 347 const char **rep_argv; 348 int ret, i = 0, j, rep_argc; 349 char *new_sort_order; 350 351 if (mem->dump_raw) 352 return report_raw_events(mem); 353 354 rep_argc = argc + 3; 355 rep_argv = calloc(rep_argc + 1, sizeof(char *)); 356 if (!rep_argv) 357 return -1; 358 359 rep_argv[i++] = "report"; 360 rep_argv[i++] = "--mem-mode"; 361 rep_argv[i++] = "-n"; /* display number of samples */ 362 363 new_sort_order = get_sort_order(mem); 364 if (new_sort_order) 365 rep_argv[i++] = new_sort_order; 366 367 for (j = 1; j < argc; j++, i++) 368 rep_argv[i] = argv[j]; 369 370 ret = cmd_report(i, rep_argv); 371 free(rep_argv); 372 return ret; 373 } 374 375 struct mem_mode { 376 const char *name; 377 int mode; 378 }; 379 380 #define MEM_OPT(n, m) \ 381 { .name = n, .mode = (m) } 382 383 #define MEM_END { .name = NULL } 384 385 static const struct mem_mode mem_modes[]={ 386 MEM_OPT("load", MEM_OPERATION_LOAD), 387 MEM_OPT("store", MEM_OPERATION_STORE), 388 MEM_END 389 }; 390 391 static int 392 parse_mem_ops(const struct option *opt, const char *str, int unset) 393 { 394 int *mode = (int *)opt->value; 395 const struct mem_mode *m; 396 char *s, *os = NULL, *p; 397 int ret = -1; 398 399 if (unset) 400 return 0; 401 402 /* str may be NULL in case no arg is passed to -t */ 403 if (str) { 404 /* because str is read-only */ 405 s = os = strdup(str); 406 if (!s) 407 return -1; 408 409 /* reset mode */ 410 *mode = 0; 411 412 for (;;) { 413 p = strchr(s, ','); 414 if (p) 415 *p = '\0'; 416 417 for (m = mem_modes; m->name; m++) { 418 if (!strcasecmp(s, m->name)) 419 break; 420 } 421 if (!m->name) { 422 fprintf(stderr, "unknown sampling op %s," 423 " check man page\n", s); 424 goto error; 425 } 426 427 *mode |= m->mode; 428 429 if (!p) 430 break; 431 432 s = p + 1; 433 } 434 } 435 ret = 0; 436 437 if (*mode == 0) 438 *mode = MEM_OPERATION_LOAD; 439 error: 440 free(os); 441 return ret; 442 } 443 444 int cmd_mem(int argc, const char **argv) 445 { 446 struct stat st; 447 struct perf_mem mem = { 448 .tool = { 449 .sample = process_sample_event, 450 .mmap = perf_event__process_mmap, 451 .mmap2 = perf_event__process_mmap2, 452 .comm = perf_event__process_comm, 453 .lost = perf_event__process_lost, 454 .fork = perf_event__process_fork, 455 .attr = perf_event__process_attr, 456 .build_id = perf_event__process_build_id, 457 .namespaces = perf_event__process_namespaces, 458 .auxtrace_info = perf_event__process_auxtrace_info, 459 .auxtrace = perf_event__process_auxtrace, 460 .auxtrace_error = perf_event__process_auxtrace_error, 461 .ordered_events = true, 462 }, 463 .input_name = "perf.data", 464 /* 465 * default to both load an store sampling 466 */ 467 .operation = MEM_OPERATION_LOAD | MEM_OPERATION_STORE, 468 }; 469 const struct option mem_options[] = { 470 OPT_CALLBACK('t', "type", &mem.operation, 471 "type", "memory operations(load,store) Default load,store", 472 parse_mem_ops), 473 OPT_BOOLEAN('D', "dump-raw-samples", &mem.dump_raw, 474 "dump raw samples in ASCII"), 475 OPT_BOOLEAN('U', "hide-unresolved", &mem.hide_unresolved, 476 "Only display entries resolved to a symbol"), 477 OPT_STRING('i', "input", &input_name, "file", 478 "input file name"), 479 OPT_STRING('C', "cpu", &mem.cpu_list, "cpu", 480 "list of cpus to profile"), 481 OPT_STRING_NOEMPTY('x', "field-separator", &symbol_conf.field_sep, 482 "separator", 483 "separator for columns, no spaces will be added" 484 " between columns '.' is reserved."), 485 OPT_BOOLEAN('f', "force", &mem.force, "don't complain, do it"), 486 OPT_BOOLEAN('p', "phys-data", &mem.phys_addr, "Record/Report sample physical addresses"), 487 OPT_BOOLEAN(0, "data-page-size", &mem.data_page_size, "Record/Report sample data address page size"), 488 OPT_END() 489 }; 490 const char *const mem_subcommands[] = { "record", "report", NULL }; 491 const char *mem_usage[] = { 492 NULL, 493 NULL 494 }; 495 496 argc = parse_options_subcommand(argc, argv, mem_options, mem_subcommands, 497 mem_usage, PARSE_OPT_KEEP_UNKNOWN); 498 499 if (!argc || !(strncmp(argv[0], "rec", 3) || mem.operation)) 500 usage_with_options(mem_usage, mem_options); 501 502 if (!mem.input_name || !strlen(mem.input_name)) { 503 if (!fstat(STDIN_FILENO, &st) && S_ISFIFO(st.st_mode)) 504 mem.input_name = "-"; 505 else 506 mem.input_name = "perf.data"; 507 } 508 509 if (strlen(argv[0]) > 2 && strstarts("record", argv[0])) 510 return __cmd_record(argc, argv, &mem); 511 else if (strlen(argv[0]) > 2 && strstarts("report", argv[0])) 512 return report_events(argc, argv, &mem); 513 else 514 usage_with_options(mem_usage, mem_options); 515 516 return 0; 517 } 518