1028f12eeSStephane Eranian #include "builtin.h" 2028f12eeSStephane Eranian #include "perf.h" 3028f12eeSStephane Eranian 4028f12eeSStephane Eranian #include "util/parse-options.h" 5028f12eeSStephane Eranian #include "util/trace-event.h" 6028f12eeSStephane Eranian #include "util/tool.h" 7028f12eeSStephane Eranian #include "util/session.h" 8f5fc1412SJiri Olsa #include "util/data.h" 9028f12eeSStephane Eranian 10028f12eeSStephane Eranian #define MEM_OPERATION_LOAD "load" 11028f12eeSStephane Eranian #define MEM_OPERATION_STORE "store" 12028f12eeSStephane Eranian 13028f12eeSStephane Eranian static const char *mem_operation = MEM_OPERATION_LOAD; 14028f12eeSStephane Eranian 15028f12eeSStephane Eranian struct perf_mem { 16028f12eeSStephane Eranian struct perf_tool tool; 17028f12eeSStephane Eranian char const *input_name; 18028f12eeSStephane Eranian bool hide_unresolved; 19028f12eeSStephane Eranian bool dump_raw; 20028f12eeSStephane Eranian const char *cpu_list; 21028f12eeSStephane Eranian DECLARE_BITMAP(cpu_bitmap, MAX_NR_CPUS); 22028f12eeSStephane Eranian }; 23028f12eeSStephane Eranian 24028f12eeSStephane Eranian static const char * const mem_usage[] = { 25028f12eeSStephane Eranian "perf mem [<options>] {record <command> |report}", 26028f12eeSStephane Eranian NULL 27028f12eeSStephane Eranian }; 28028f12eeSStephane Eranian 29028f12eeSStephane Eranian static int __cmd_record(int argc, const char **argv) 30028f12eeSStephane Eranian { 31028f12eeSStephane Eranian int rec_argc, i = 0, j; 32028f12eeSStephane Eranian const char **rec_argv; 33028f12eeSStephane Eranian char event[64]; 34028f12eeSStephane Eranian int ret; 35028f12eeSStephane Eranian 36028f12eeSStephane Eranian rec_argc = argc + 4; 37028f12eeSStephane Eranian rec_argv = calloc(rec_argc + 1, sizeof(char *)); 38028f12eeSStephane Eranian if (!rec_argv) 39028f12eeSStephane Eranian return -1; 40028f12eeSStephane Eranian 41028f12eeSStephane Eranian rec_argv[i++] = strdup("record"); 42028f12eeSStephane Eranian if (!strcmp(mem_operation, MEM_OPERATION_LOAD)) 43028f12eeSStephane Eranian rec_argv[i++] = strdup("-W"); 44028f12eeSStephane Eranian rec_argv[i++] = strdup("-d"); 45028f12eeSStephane Eranian rec_argv[i++] = strdup("-e"); 46028f12eeSStephane Eranian 47028f12eeSStephane Eranian if (strcmp(mem_operation, MEM_OPERATION_LOAD)) 48028f12eeSStephane Eranian sprintf(event, "cpu/mem-stores/pp"); 49028f12eeSStephane Eranian else 50028f12eeSStephane Eranian sprintf(event, "cpu/mem-loads/pp"); 51028f12eeSStephane Eranian 52028f12eeSStephane Eranian rec_argv[i++] = strdup(event); 53028f12eeSStephane Eranian for (j = 1; j < argc; j++, i++) 54028f12eeSStephane Eranian rec_argv[i] = argv[j]; 55028f12eeSStephane Eranian 56028f12eeSStephane Eranian ret = cmd_record(i, rec_argv, NULL); 57028f12eeSStephane Eranian free(rec_argv); 58028f12eeSStephane Eranian return ret; 59028f12eeSStephane Eranian } 60028f12eeSStephane Eranian 61028f12eeSStephane Eranian static int 62028f12eeSStephane Eranian dump_raw_samples(struct perf_tool *tool, 63028f12eeSStephane Eranian union perf_event *event, 64028f12eeSStephane Eranian struct perf_sample *sample, 65028f12eeSStephane Eranian struct machine *machine) 66028f12eeSStephane Eranian { 67028f12eeSStephane Eranian struct perf_mem *mem = container_of(tool, struct perf_mem, tool); 68028f12eeSStephane Eranian struct addr_location al; 69028f12eeSStephane Eranian const char *fmt; 70028f12eeSStephane Eranian 71e44baa3eSAdrian Hunter if (perf_event__preprocess_sample(event, machine, &al, sample) < 0) { 72028f12eeSStephane Eranian fprintf(stderr, "problem processing %d event, skipping it.\n", 73028f12eeSStephane Eranian event->header.type); 74028f12eeSStephane Eranian return -1; 75028f12eeSStephane Eranian } 76028f12eeSStephane Eranian 77028f12eeSStephane Eranian if (al.filtered || (mem->hide_unresolved && al.sym == NULL)) 78028f12eeSStephane Eranian return 0; 79028f12eeSStephane Eranian 80028f12eeSStephane Eranian if (al.map != NULL) 81028f12eeSStephane Eranian al.map->dso->hit = 1; 82028f12eeSStephane Eranian 83028f12eeSStephane Eranian if (symbol_conf.field_sep) { 84028f12eeSStephane Eranian fmt = "%d%s%d%s0x%"PRIx64"%s0x%"PRIx64"%s%"PRIu64 85028f12eeSStephane Eranian "%s0x%"PRIx64"%s%s:%s\n"; 86028f12eeSStephane Eranian } else { 87028f12eeSStephane Eranian fmt = "%5d%s%5d%s0x%016"PRIx64"%s0x016%"PRIx64 88028f12eeSStephane Eranian "%s%5"PRIu64"%s0x%06"PRIx64"%s%s:%s\n"; 89028f12eeSStephane Eranian symbol_conf.field_sep = " "; 90028f12eeSStephane Eranian } 91028f12eeSStephane Eranian 92028f12eeSStephane Eranian printf(fmt, 93028f12eeSStephane Eranian sample->pid, 94028f12eeSStephane Eranian symbol_conf.field_sep, 95028f12eeSStephane Eranian sample->tid, 96028f12eeSStephane Eranian symbol_conf.field_sep, 97ef89325fSAdrian Hunter sample->ip, 98028f12eeSStephane Eranian symbol_conf.field_sep, 99028f12eeSStephane Eranian sample->addr, 100028f12eeSStephane Eranian symbol_conf.field_sep, 101028f12eeSStephane Eranian sample->weight, 102028f12eeSStephane Eranian symbol_conf.field_sep, 103028f12eeSStephane Eranian sample->data_src, 104028f12eeSStephane Eranian symbol_conf.field_sep, 105028f12eeSStephane Eranian al.map ? (al.map->dso ? al.map->dso->long_name : "???") : "???", 106028f12eeSStephane Eranian al.sym ? al.sym->name : "???"); 107028f12eeSStephane Eranian 108028f12eeSStephane Eranian return 0; 109028f12eeSStephane Eranian } 110028f12eeSStephane Eranian 111028f12eeSStephane Eranian static int process_sample_event(struct perf_tool *tool, 112028f12eeSStephane Eranian union perf_event *event, 113028f12eeSStephane Eranian struct perf_sample *sample, 1148b640cc4SArnaldo Carvalho de Melo struct perf_evsel *evsel __maybe_unused, 115028f12eeSStephane Eranian struct machine *machine) 116028f12eeSStephane Eranian { 1178b640cc4SArnaldo Carvalho de Melo return dump_raw_samples(tool, event, sample, machine); 118028f12eeSStephane Eranian } 119028f12eeSStephane Eranian 120028f12eeSStephane Eranian static int report_raw_events(struct perf_mem *mem) 121028f12eeSStephane Eranian { 122f5fc1412SJiri Olsa struct perf_data_file file = { 123f5fc1412SJiri Olsa .path = input_name, 124f5fc1412SJiri Olsa .mode = PERF_DATA_MODE_READ, 125f5fc1412SJiri Olsa }; 126028f12eeSStephane Eranian int err = -EINVAL; 127028f12eeSStephane Eranian int ret; 128f5fc1412SJiri Olsa struct perf_session *session = perf_session__new(&file, false, 129f5fc1412SJiri Olsa &mem->tool); 130028f12eeSStephane Eranian 131028f12eeSStephane Eranian if (session == NULL) 132028f12eeSStephane Eranian return -ENOMEM; 133028f12eeSStephane Eranian 134028f12eeSStephane Eranian if (mem->cpu_list) { 135028f12eeSStephane Eranian ret = perf_session__cpu_bitmap(session, mem->cpu_list, 136028f12eeSStephane Eranian mem->cpu_bitmap); 137028f12eeSStephane Eranian if (ret) 138028f12eeSStephane Eranian goto out_delete; 139028f12eeSStephane Eranian } 140028f12eeSStephane Eranian 141028f12eeSStephane Eranian if (symbol__init() < 0) 142028f12eeSStephane Eranian return -1; 143028f12eeSStephane Eranian 144028f12eeSStephane Eranian printf("# PID, TID, IP, ADDR, LOCAL WEIGHT, DSRC, SYMBOL\n"); 145028f12eeSStephane Eranian 146028f12eeSStephane Eranian err = perf_session__process_events(session, &mem->tool); 147028f12eeSStephane Eranian if (err) 148028f12eeSStephane Eranian return err; 149028f12eeSStephane Eranian 150028f12eeSStephane Eranian return 0; 151028f12eeSStephane Eranian 152028f12eeSStephane Eranian out_delete: 153028f12eeSStephane Eranian perf_session__delete(session); 154028f12eeSStephane Eranian return err; 155028f12eeSStephane Eranian } 156028f12eeSStephane Eranian 157028f12eeSStephane Eranian static int report_events(int argc, const char **argv, struct perf_mem *mem) 158028f12eeSStephane Eranian { 159028f12eeSStephane Eranian const char **rep_argv; 160028f12eeSStephane Eranian int ret, i = 0, j, rep_argc; 161028f12eeSStephane Eranian 162028f12eeSStephane Eranian if (mem->dump_raw) 163028f12eeSStephane Eranian return report_raw_events(mem); 164028f12eeSStephane Eranian 165028f12eeSStephane Eranian rep_argc = argc + 3; 166028f12eeSStephane Eranian rep_argv = calloc(rep_argc + 1, sizeof(char *)); 167028f12eeSStephane Eranian if (!rep_argv) 168028f12eeSStephane Eranian return -1; 169028f12eeSStephane Eranian 170028f12eeSStephane Eranian rep_argv[i++] = strdup("report"); 171028f12eeSStephane Eranian rep_argv[i++] = strdup("--mem-mode"); 172028f12eeSStephane Eranian rep_argv[i++] = strdup("-n"); /* display number of samples */ 173028f12eeSStephane Eranian 174028f12eeSStephane Eranian /* 175028f12eeSStephane Eranian * there is no weight (cost) associated with stores, so don't print 176028f12eeSStephane Eranian * the column 177028f12eeSStephane Eranian */ 178028f12eeSStephane Eranian if (strcmp(mem_operation, MEM_OPERATION_LOAD)) 179028f12eeSStephane Eranian rep_argv[i++] = strdup("--sort=mem,sym,dso,symbol_daddr," 180028f12eeSStephane Eranian "dso_daddr,tlb,locked"); 181028f12eeSStephane Eranian 182028f12eeSStephane Eranian for (j = 1; j < argc; j++, i++) 183028f12eeSStephane Eranian rep_argv[i] = argv[j]; 184028f12eeSStephane Eranian 185028f12eeSStephane Eranian ret = cmd_report(i, rep_argv, NULL); 186028f12eeSStephane Eranian free(rep_argv); 187028f12eeSStephane Eranian return ret; 188028f12eeSStephane Eranian } 189028f12eeSStephane Eranian 190028f12eeSStephane Eranian int cmd_mem(int argc, const char **argv, const char *prefix __maybe_unused) 191028f12eeSStephane Eranian { 192028f12eeSStephane Eranian struct stat st; 193028f12eeSStephane Eranian struct perf_mem mem = { 194028f12eeSStephane Eranian .tool = { 195028f12eeSStephane Eranian .sample = process_sample_event, 196028f12eeSStephane Eranian .mmap = perf_event__process_mmap, 1975c5e854bSStephane Eranian .mmap2 = perf_event__process_mmap2, 198028f12eeSStephane Eranian .comm = perf_event__process_comm, 199028f12eeSStephane Eranian .lost = perf_event__process_lost, 200028f12eeSStephane Eranian .fork = perf_event__process_fork, 201028f12eeSStephane Eranian .build_id = perf_event__process_build_id, 202028f12eeSStephane Eranian .ordered_samples = true, 203028f12eeSStephane Eranian }, 204028f12eeSStephane Eranian .input_name = "perf.data", 205028f12eeSStephane Eranian }; 206028f12eeSStephane Eranian const struct option mem_options[] = { 207028f12eeSStephane Eranian OPT_STRING('t', "type", &mem_operation, 208028f12eeSStephane Eranian "type", "memory operations(load/store)"), 209028f12eeSStephane Eranian OPT_BOOLEAN('D', "dump-raw-samples", &mem.dump_raw, 210028f12eeSStephane Eranian "dump raw samples in ASCII"), 211028f12eeSStephane Eranian OPT_BOOLEAN('U', "hide-unresolved", &mem.hide_unresolved, 212028f12eeSStephane Eranian "Only display entries resolved to a symbol"), 213028f12eeSStephane Eranian OPT_STRING('i', "input", &input_name, "file", 214028f12eeSStephane Eranian "input file name"), 215028f12eeSStephane Eranian OPT_STRING('C', "cpu", &mem.cpu_list, "cpu", 216028f12eeSStephane Eranian "list of cpus to profile"), 217028f12eeSStephane Eranian OPT_STRING('x', "field-separator", &symbol_conf.field_sep, 218028f12eeSStephane Eranian "separator", 219028f12eeSStephane Eranian "separator for columns, no spaces will be added" 220028f12eeSStephane Eranian " between columns '.' is reserved."), 221028f12eeSStephane Eranian OPT_END() 222028f12eeSStephane Eranian }; 223028f12eeSStephane Eranian 224028f12eeSStephane Eranian argc = parse_options(argc, argv, mem_options, mem_usage, 225028f12eeSStephane Eranian PARSE_OPT_STOP_AT_NON_OPTION); 226028f12eeSStephane Eranian 227028f12eeSStephane Eranian if (!argc || !(strncmp(argv[0], "rec", 3) || mem_operation)) 228028f12eeSStephane Eranian usage_with_options(mem_usage, mem_options); 229028f12eeSStephane Eranian 230028f12eeSStephane Eranian if (!mem.input_name || !strlen(mem.input_name)) { 231028f12eeSStephane Eranian if (!fstat(STDIN_FILENO, &st) && S_ISFIFO(st.st_mode)) 232028f12eeSStephane Eranian mem.input_name = "-"; 233028f12eeSStephane Eranian else 234028f12eeSStephane Eranian mem.input_name = "perf.data"; 235028f12eeSStephane Eranian } 236028f12eeSStephane Eranian 237028f12eeSStephane Eranian if (!strncmp(argv[0], "rec", 3)) 238028f12eeSStephane Eranian return __cmd_record(argc, argv); 239028f12eeSStephane Eranian else if (!strncmp(argv[0], "rep", 3)) 240028f12eeSStephane Eranian return report_events(argc, argv, &mem); 241028f12eeSStephane Eranian else 242028f12eeSStephane Eranian usage_with_options(mem_usage, mem_options); 243028f12eeSStephane Eranian 244028f12eeSStephane Eranian return 0; 245028f12eeSStephane Eranian } 246