1 #include "builtin.h" 2 #include "perf.h" 3 4 #include "util/parse-options.h" 5 #include "util/trace-event.h" 6 #include "util/tool.h" 7 #include "util/session.h" 8 #include "util/data.h" 9 10 #define MEM_OPERATION_LOAD "load" 11 #define MEM_OPERATION_STORE "store" 12 13 static const char *mem_operation = MEM_OPERATION_LOAD; 14 15 struct perf_mem { 16 struct perf_tool tool; 17 char const *input_name; 18 bool hide_unresolved; 19 bool dump_raw; 20 const char *cpu_list; 21 DECLARE_BITMAP(cpu_bitmap, MAX_NR_CPUS); 22 }; 23 24 static const char * const mem_usage[] = { 25 "perf mem [<options>] {record <command> |report}", 26 NULL 27 }; 28 29 static int __cmd_record(int argc, const char **argv) 30 { 31 int rec_argc, i = 0, j; 32 const char **rec_argv; 33 char event[64]; 34 int ret; 35 36 rec_argc = argc + 4; 37 rec_argv = calloc(rec_argc + 1, sizeof(char *)); 38 if (!rec_argv) 39 return -1; 40 41 rec_argv[i++] = strdup("record"); 42 if (!strcmp(mem_operation, MEM_OPERATION_LOAD)) 43 rec_argv[i++] = strdup("-W"); 44 rec_argv[i++] = strdup("-d"); 45 rec_argv[i++] = strdup("-e"); 46 47 if (strcmp(mem_operation, MEM_OPERATION_LOAD)) 48 sprintf(event, "cpu/mem-stores/pp"); 49 else 50 sprintf(event, "cpu/mem-loads/pp"); 51 52 rec_argv[i++] = strdup(event); 53 for (j = 1; j < argc; j++, i++) 54 rec_argv[i] = argv[j]; 55 56 ret = cmd_record(i, rec_argv, NULL); 57 free(rec_argv); 58 return ret; 59 } 60 61 static int 62 dump_raw_samples(struct perf_tool *tool, 63 union perf_event *event, 64 struct perf_sample *sample, 65 struct perf_evsel *evsel __maybe_unused, 66 struct machine *machine) 67 { 68 struct perf_mem *mem = container_of(tool, struct perf_mem, tool); 69 struct addr_location al; 70 const char *fmt; 71 72 if (perf_event__preprocess_sample(event, machine, &al, sample) < 0) { 73 fprintf(stderr, "problem processing %d event, skipping it.\n", 74 event->header.type); 75 return -1; 76 } 77 78 if (al.filtered || (mem->hide_unresolved && al.sym == NULL)) 79 return 0; 80 81 if (al.map != NULL) 82 al.map->dso->hit = 1; 83 84 if (symbol_conf.field_sep) { 85 fmt = "%d%s%d%s0x%"PRIx64"%s0x%"PRIx64"%s%"PRIu64 86 "%s0x%"PRIx64"%s%s:%s\n"; 87 } else { 88 fmt = "%5d%s%5d%s0x%016"PRIx64"%s0x016%"PRIx64 89 "%s%5"PRIu64"%s0x%06"PRIx64"%s%s:%s\n"; 90 symbol_conf.field_sep = " "; 91 } 92 93 printf(fmt, 94 sample->pid, 95 symbol_conf.field_sep, 96 sample->tid, 97 symbol_conf.field_sep, 98 sample->ip, 99 symbol_conf.field_sep, 100 sample->addr, 101 symbol_conf.field_sep, 102 sample->weight, 103 symbol_conf.field_sep, 104 sample->data_src, 105 symbol_conf.field_sep, 106 al.map ? (al.map->dso ? al.map->dso->long_name : "???") : "???", 107 al.sym ? al.sym->name : "???"); 108 109 return 0; 110 } 111 112 static int process_sample_event(struct perf_tool *tool, 113 union perf_event *event, 114 struct perf_sample *sample, 115 struct perf_evsel *evsel, 116 struct machine *machine) 117 { 118 return dump_raw_samples(tool, event, sample, evsel, machine); 119 } 120 121 static int report_raw_events(struct perf_mem *mem) 122 { 123 struct perf_data_file file = { 124 .path = input_name, 125 .mode = PERF_DATA_MODE_READ, 126 }; 127 int err = -EINVAL; 128 int ret; 129 struct perf_session *session = perf_session__new(&file, false, 130 &mem->tool); 131 132 if (session == NULL) 133 return -ENOMEM; 134 135 if (mem->cpu_list) { 136 ret = perf_session__cpu_bitmap(session, mem->cpu_list, 137 mem->cpu_bitmap); 138 if (ret) 139 goto out_delete; 140 } 141 142 if (symbol__init() < 0) 143 return -1; 144 145 printf("# PID, TID, IP, ADDR, LOCAL WEIGHT, DSRC, SYMBOL\n"); 146 147 err = perf_session__process_events(session, &mem->tool); 148 if (err) 149 return err; 150 151 return 0; 152 153 out_delete: 154 perf_session__delete(session); 155 return err; 156 } 157 158 static int report_events(int argc, const char **argv, struct perf_mem *mem) 159 { 160 const char **rep_argv; 161 int ret, i = 0, j, rep_argc; 162 163 if (mem->dump_raw) 164 return report_raw_events(mem); 165 166 rep_argc = argc + 3; 167 rep_argv = calloc(rep_argc + 1, sizeof(char *)); 168 if (!rep_argv) 169 return -1; 170 171 rep_argv[i++] = strdup("report"); 172 rep_argv[i++] = strdup("--mem-mode"); 173 rep_argv[i++] = strdup("-n"); /* display number of samples */ 174 175 /* 176 * there is no weight (cost) associated with stores, so don't print 177 * the column 178 */ 179 if (strcmp(mem_operation, MEM_OPERATION_LOAD)) 180 rep_argv[i++] = strdup("--sort=mem,sym,dso,symbol_daddr," 181 "dso_daddr,tlb,locked"); 182 183 for (j = 1; j < argc; j++, i++) 184 rep_argv[i] = argv[j]; 185 186 ret = cmd_report(i, rep_argv, NULL); 187 free(rep_argv); 188 return ret; 189 } 190 191 int cmd_mem(int argc, const char **argv, const char *prefix __maybe_unused) 192 { 193 struct stat st; 194 struct perf_mem mem = { 195 .tool = { 196 .sample = process_sample_event, 197 .mmap = perf_event__process_mmap, 198 .mmap2 = perf_event__process_mmap2, 199 .comm = perf_event__process_comm, 200 .lost = perf_event__process_lost, 201 .fork = perf_event__process_fork, 202 .build_id = perf_event__process_build_id, 203 .ordered_samples = true, 204 }, 205 .input_name = "perf.data", 206 }; 207 const struct option mem_options[] = { 208 OPT_STRING('t', "type", &mem_operation, 209 "type", "memory operations(load/store)"), 210 OPT_BOOLEAN('D', "dump-raw-samples", &mem.dump_raw, 211 "dump raw samples in ASCII"), 212 OPT_BOOLEAN('U', "hide-unresolved", &mem.hide_unresolved, 213 "Only display entries resolved to a symbol"), 214 OPT_STRING('i', "input", &input_name, "file", 215 "input file name"), 216 OPT_STRING('C', "cpu", &mem.cpu_list, "cpu", 217 "list of cpus to profile"), 218 OPT_STRING('x', "field-separator", &symbol_conf.field_sep, 219 "separator", 220 "separator for columns, no spaces will be added" 221 " between columns '.' is reserved."), 222 OPT_END() 223 }; 224 225 argc = parse_options(argc, argv, mem_options, mem_usage, 226 PARSE_OPT_STOP_AT_NON_OPTION); 227 228 if (!argc || !(strncmp(argv[0], "rec", 3) || mem_operation)) 229 usage_with_options(mem_usage, mem_options); 230 231 if (!mem.input_name || !strlen(mem.input_name)) { 232 if (!fstat(STDIN_FILENO, &st) && S_ISFIFO(st.st_mode)) 233 mem.input_name = "-"; 234 else 235 mem.input_name = "perf.data"; 236 } 237 238 if (!strncmp(argv[0], "rec", 3)) 239 return __cmd_record(argc, argv); 240 else if (!strncmp(argv[0], "rep", 3)) 241 return report_events(argc, argv, &mem); 242 else 243 usage_with_options(mem_usage, mem_options); 244 245 return 0; 246 } 247