Stephane Eranian | 028f12e | 2013-01-24 16:10:38 +0100 | [diff] [blame] | 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" |
Jiri Olsa | f5fc1412 | 2013-10-15 16:27:32 +0200 | [diff] [blame] | 8 | #include "util/data.h" |
Stephane Eranian | 028f12e | 2013-01-24 16:10:38 +0100 | [diff] [blame] | 9 | |
Stephane Eranian | 67121f8 | 2014-12-17 16:23:55 +0100 | [diff] [blame^] | 10 | #define MEM_OPERATION_LOAD 0x1 |
| 11 | #define MEM_OPERATION_STORE 0x2 |
Stephane Eranian | 028f12e | 2013-01-24 16:10:38 +0100 | [diff] [blame] | 12 | |
Stephane Eranian | 67121f8 | 2014-12-17 16:23:55 +0100 | [diff] [blame^] | 13 | /* |
| 14 | * default to both load an store sampling |
| 15 | */ |
| 16 | static int mem_operation = MEM_OPERATION_LOAD | MEM_OPERATION_STORE; |
Stephane Eranian | 028f12e | 2013-01-24 16:10:38 +0100 | [diff] [blame] | 17 | |
| 18 | struct perf_mem { |
| 19 | struct perf_tool tool; |
| 20 | char const *input_name; |
Stephane Eranian | 028f12e | 2013-01-24 16:10:38 +0100 | [diff] [blame] | 21 | bool hide_unresolved; |
| 22 | bool dump_raw; |
| 23 | const char *cpu_list; |
| 24 | DECLARE_BITMAP(cpu_bitmap, MAX_NR_CPUS); |
| 25 | }; |
| 26 | |
Stephane Eranian | 028f12e | 2013-01-24 16:10:38 +0100 | [diff] [blame] | 27 | static int __cmd_record(int argc, const char **argv) |
| 28 | { |
| 29 | int rec_argc, i = 0, j; |
| 30 | const char **rec_argv; |
Stephane Eranian | 028f12e | 2013-01-24 16:10:38 +0100 | [diff] [blame] | 31 | int ret; |
| 32 | |
Stephane Eranian | 67121f8 | 2014-12-17 16:23:55 +0100 | [diff] [blame^] | 33 | rec_argc = argc + 7; /* max number of arguments */ |
Stephane Eranian | 028f12e | 2013-01-24 16:10:38 +0100 | [diff] [blame] | 34 | rec_argv = calloc(rec_argc + 1, sizeof(char *)); |
| 35 | if (!rec_argv) |
| 36 | return -1; |
| 37 | |
Stephane Eranian | 67121f8 | 2014-12-17 16:23:55 +0100 | [diff] [blame^] | 38 | rec_argv[i++] = "record"; |
Stephane Eranian | 028f12e | 2013-01-24 16:10:38 +0100 | [diff] [blame] | 39 | |
Stephane Eranian | 67121f8 | 2014-12-17 16:23:55 +0100 | [diff] [blame^] | 40 | if (mem_operation & MEM_OPERATION_LOAD) |
| 41 | rec_argv[i++] = "-W"; |
Stephane Eranian | 028f12e | 2013-01-24 16:10:38 +0100 | [diff] [blame] | 42 | |
Stephane Eranian | 67121f8 | 2014-12-17 16:23:55 +0100 | [diff] [blame^] | 43 | rec_argv[i++] = "-d"; |
| 44 | |
| 45 | if (mem_operation & MEM_OPERATION_LOAD) { |
| 46 | rec_argv[i++] = "-e"; |
| 47 | rec_argv[i++] = "cpu/mem-loads/pp"; |
| 48 | } |
| 49 | |
| 50 | if (mem_operation & MEM_OPERATION_STORE) { |
| 51 | rec_argv[i++] = "-e"; |
| 52 | rec_argv[i++] = "cpu/mem-stores/pp"; |
| 53 | } |
| 54 | |
Stephane Eranian | 028f12e | 2013-01-24 16:10:38 +0100 | [diff] [blame] | 55 | for (j = 1; j < argc; j++, i++) |
| 56 | rec_argv[i] = argv[j]; |
| 57 | |
| 58 | ret = cmd_record(i, rec_argv, NULL); |
| 59 | free(rec_argv); |
| 60 | return ret; |
| 61 | } |
| 62 | |
| 63 | static int |
| 64 | dump_raw_samples(struct perf_tool *tool, |
| 65 | union perf_event *event, |
| 66 | struct perf_sample *sample, |
Stephane Eranian | 028f12e | 2013-01-24 16:10:38 +0100 | [diff] [blame] | 67 | struct machine *machine) |
| 68 | { |
| 69 | struct perf_mem *mem = container_of(tool, struct perf_mem, tool); |
| 70 | struct addr_location al; |
| 71 | const char *fmt; |
| 72 | |
Adrian Hunter | e44baa3 | 2013-08-08 14:32:25 +0300 | [diff] [blame] | 73 | if (perf_event__preprocess_sample(event, machine, &al, sample) < 0) { |
Stephane Eranian | 028f12e | 2013-01-24 16:10:38 +0100 | [diff] [blame] | 74 | fprintf(stderr, "problem processing %d event, skipping it.\n", |
| 75 | event->header.type); |
| 76 | return -1; |
| 77 | } |
| 78 | |
| 79 | if (al.filtered || (mem->hide_unresolved && al.sym == NULL)) |
| 80 | return 0; |
| 81 | |
| 82 | if (al.map != NULL) |
| 83 | al.map->dso->hit = 1; |
| 84 | |
| 85 | if (symbol_conf.field_sep) { |
| 86 | fmt = "%d%s%d%s0x%"PRIx64"%s0x%"PRIx64"%s%"PRIu64 |
| 87 | "%s0x%"PRIx64"%s%s:%s\n"; |
| 88 | } else { |
| 89 | fmt = "%5d%s%5d%s0x%016"PRIx64"%s0x016%"PRIx64 |
| 90 | "%s%5"PRIu64"%s0x%06"PRIx64"%s%s:%s\n"; |
| 91 | symbol_conf.field_sep = " "; |
| 92 | } |
| 93 | |
| 94 | printf(fmt, |
| 95 | sample->pid, |
| 96 | symbol_conf.field_sep, |
| 97 | sample->tid, |
| 98 | symbol_conf.field_sep, |
Adrian Hunter | ef89325 | 2013-08-27 11:23:06 +0300 | [diff] [blame] | 99 | sample->ip, |
Stephane Eranian | 028f12e | 2013-01-24 16:10:38 +0100 | [diff] [blame] | 100 | symbol_conf.field_sep, |
| 101 | sample->addr, |
| 102 | symbol_conf.field_sep, |
| 103 | sample->weight, |
| 104 | symbol_conf.field_sep, |
| 105 | sample->data_src, |
| 106 | symbol_conf.field_sep, |
| 107 | al.map ? (al.map->dso ? al.map->dso->long_name : "???") : "???", |
| 108 | al.sym ? al.sym->name : "???"); |
| 109 | |
| 110 | return 0; |
| 111 | } |
| 112 | |
| 113 | static int process_sample_event(struct perf_tool *tool, |
| 114 | union perf_event *event, |
| 115 | struct perf_sample *sample, |
Arnaldo Carvalho de Melo | 8b640cc | 2013-12-19 17:00:45 -0300 | [diff] [blame] | 116 | struct perf_evsel *evsel __maybe_unused, |
Stephane Eranian | 028f12e | 2013-01-24 16:10:38 +0100 | [diff] [blame] | 117 | struct machine *machine) |
| 118 | { |
Arnaldo Carvalho de Melo | 8b640cc | 2013-12-19 17:00:45 -0300 | [diff] [blame] | 119 | return dump_raw_samples(tool, event, sample, machine); |
Stephane Eranian | 028f12e | 2013-01-24 16:10:38 +0100 | [diff] [blame] | 120 | } |
| 121 | |
| 122 | static int report_raw_events(struct perf_mem *mem) |
| 123 | { |
Jiri Olsa | f5fc1412 | 2013-10-15 16:27:32 +0200 | [diff] [blame] | 124 | struct perf_data_file file = { |
| 125 | .path = input_name, |
| 126 | .mode = PERF_DATA_MODE_READ, |
| 127 | }; |
Stephane Eranian | 028f12e | 2013-01-24 16:10:38 +0100 | [diff] [blame] | 128 | int err = -EINVAL; |
| 129 | int ret; |
Jiri Olsa | f5fc1412 | 2013-10-15 16:27:32 +0200 | [diff] [blame] | 130 | struct perf_session *session = perf_session__new(&file, false, |
| 131 | &mem->tool); |
Stephane Eranian | 028f12e | 2013-01-24 16:10:38 +0100 | [diff] [blame] | 132 | |
| 133 | if (session == NULL) |
Taeung Song | 52e02834 | 2014-09-24 10:33:37 +0900 | [diff] [blame] | 134 | return -1; |
Stephane Eranian | 028f12e | 2013-01-24 16:10:38 +0100 | [diff] [blame] | 135 | |
| 136 | if (mem->cpu_list) { |
| 137 | ret = perf_session__cpu_bitmap(session, mem->cpu_list, |
| 138 | mem->cpu_bitmap); |
| 139 | if (ret) |
| 140 | goto out_delete; |
| 141 | } |
| 142 | |
Namhyung Kim | 0a7e6d1 | 2014-08-12 15:40:45 +0900 | [diff] [blame] | 143 | if (symbol__init(&session->header.env) < 0) |
Stephane Eranian | 028f12e | 2013-01-24 16:10:38 +0100 | [diff] [blame] | 144 | return -1; |
| 145 | |
| 146 | printf("# PID, TID, IP, ADDR, LOCAL WEIGHT, DSRC, SYMBOL\n"); |
| 147 | |
| 148 | err = perf_session__process_events(session, &mem->tool); |
| 149 | if (err) |
| 150 | return err; |
| 151 | |
| 152 | return 0; |
| 153 | |
| 154 | out_delete: |
| 155 | perf_session__delete(session); |
| 156 | return err; |
| 157 | } |
| 158 | |
| 159 | static int report_events(int argc, const char **argv, struct perf_mem *mem) |
| 160 | { |
| 161 | const char **rep_argv; |
| 162 | int ret, i = 0, j, rep_argc; |
| 163 | |
| 164 | if (mem->dump_raw) |
| 165 | return report_raw_events(mem); |
| 166 | |
| 167 | rep_argc = argc + 3; |
| 168 | rep_argv = calloc(rep_argc + 1, sizeof(char *)); |
| 169 | if (!rep_argv) |
| 170 | return -1; |
| 171 | |
Stephane Eranian | 67121f8 | 2014-12-17 16:23:55 +0100 | [diff] [blame^] | 172 | rep_argv[i++] = "report"; |
| 173 | rep_argv[i++] = "--mem-mode"; |
| 174 | rep_argv[i++] = "-n"; /* display number of samples */ |
Stephane Eranian | 028f12e | 2013-01-24 16:10:38 +0100 | [diff] [blame] | 175 | |
| 176 | /* |
| 177 | * there is no weight (cost) associated with stores, so don't print |
| 178 | * the column |
| 179 | */ |
Stephane Eranian | 67121f8 | 2014-12-17 16:23:55 +0100 | [diff] [blame^] | 180 | if (!(mem_operation & MEM_OPERATION_LOAD)) |
| 181 | rep_argv[i++] = "--sort=mem,sym,dso,symbol_daddr," |
| 182 | "dso_daddr,tlb,locked"; |
Stephane Eranian | 028f12e | 2013-01-24 16:10:38 +0100 | [diff] [blame] | 183 | |
| 184 | for (j = 1; j < argc; j++, i++) |
| 185 | rep_argv[i] = argv[j]; |
| 186 | |
| 187 | ret = cmd_report(i, rep_argv, NULL); |
| 188 | free(rep_argv); |
| 189 | return ret; |
| 190 | } |
| 191 | |
Stephane Eranian | 67121f8 | 2014-12-17 16:23:55 +0100 | [diff] [blame^] | 192 | struct mem_mode { |
| 193 | const char *name; |
| 194 | int mode; |
| 195 | }; |
| 196 | |
| 197 | #define MEM_OPT(n, m) \ |
| 198 | { .name = n, .mode = (m) } |
| 199 | |
| 200 | #define MEM_END { .name = NULL } |
| 201 | |
| 202 | static const struct mem_mode mem_modes[]={ |
| 203 | MEM_OPT("load", MEM_OPERATION_LOAD), |
| 204 | MEM_OPT("store", MEM_OPERATION_STORE), |
| 205 | MEM_END |
| 206 | }; |
| 207 | |
| 208 | static int |
| 209 | parse_mem_ops(const struct option *opt, const char *str, int unset) |
| 210 | { |
| 211 | int *mode = (int *)opt->value; |
| 212 | const struct mem_mode *m; |
| 213 | char *s, *os = NULL, *p; |
| 214 | int ret = -1; |
| 215 | |
| 216 | if (unset) |
| 217 | return 0; |
| 218 | |
| 219 | /* str may be NULL in case no arg is passed to -t */ |
| 220 | if (str) { |
| 221 | /* because str is read-only */ |
| 222 | s = os = strdup(str); |
| 223 | if (!s) |
| 224 | return -1; |
| 225 | |
| 226 | /* reset mode */ |
| 227 | *mode = 0; |
| 228 | |
| 229 | for (;;) { |
| 230 | p = strchr(s, ','); |
| 231 | if (p) |
| 232 | *p = '\0'; |
| 233 | |
| 234 | for (m = mem_modes; m->name; m++) { |
| 235 | if (!strcasecmp(s, m->name)) |
| 236 | break; |
| 237 | } |
| 238 | if (!m->name) { |
| 239 | fprintf(stderr, "unknown sampling op %s," |
| 240 | " check man page\n", s); |
| 241 | goto error; |
| 242 | } |
| 243 | |
| 244 | *mode |= m->mode; |
| 245 | |
| 246 | if (!p) |
| 247 | break; |
| 248 | |
| 249 | s = p + 1; |
| 250 | } |
| 251 | } |
| 252 | ret = 0; |
| 253 | |
| 254 | if (*mode == 0) |
| 255 | *mode = MEM_OPERATION_LOAD; |
| 256 | error: |
| 257 | free(os); |
| 258 | return ret; |
| 259 | } |
| 260 | |
Stephane Eranian | 028f12e | 2013-01-24 16:10:38 +0100 | [diff] [blame] | 261 | int cmd_mem(int argc, const char **argv, const char *prefix __maybe_unused) |
| 262 | { |
| 263 | struct stat st; |
| 264 | struct perf_mem mem = { |
| 265 | .tool = { |
| 266 | .sample = process_sample_event, |
| 267 | .mmap = perf_event__process_mmap, |
Stephane Eranian | 5c5e854 | 2013-08-21 12:10:25 +0200 | [diff] [blame] | 268 | .mmap2 = perf_event__process_mmap2, |
Stephane Eranian | 028f12e | 2013-01-24 16:10:38 +0100 | [diff] [blame] | 269 | .comm = perf_event__process_comm, |
| 270 | .lost = perf_event__process_lost, |
| 271 | .fork = perf_event__process_fork, |
| 272 | .build_id = perf_event__process_build_id, |
Jiri Olsa | 0a8cb85 | 2014-07-06 14:18:21 +0200 | [diff] [blame] | 273 | .ordered_events = true, |
Stephane Eranian | 028f12e | 2013-01-24 16:10:38 +0100 | [diff] [blame] | 274 | }, |
| 275 | .input_name = "perf.data", |
| 276 | }; |
| 277 | const struct option mem_options[] = { |
Stephane Eranian | 67121f8 | 2014-12-17 16:23:55 +0100 | [diff] [blame^] | 278 | OPT_CALLBACK('t', "type", &mem_operation, |
| 279 | "type", "memory operations(load,store) Default load,store", |
| 280 | parse_mem_ops), |
Stephane Eranian | 028f12e | 2013-01-24 16:10:38 +0100 | [diff] [blame] | 281 | OPT_BOOLEAN('D', "dump-raw-samples", &mem.dump_raw, |
| 282 | "dump raw samples in ASCII"), |
| 283 | OPT_BOOLEAN('U', "hide-unresolved", &mem.hide_unresolved, |
| 284 | "Only display entries resolved to a symbol"), |
| 285 | OPT_STRING('i', "input", &input_name, "file", |
| 286 | "input file name"), |
| 287 | OPT_STRING('C', "cpu", &mem.cpu_list, "cpu", |
| 288 | "list of cpus to profile"), |
| 289 | OPT_STRING('x', "field-separator", &symbol_conf.field_sep, |
| 290 | "separator", |
| 291 | "separator for columns, no spaces will be added" |
| 292 | " between columns '.' is reserved."), |
| 293 | OPT_END() |
| 294 | }; |
Ramkumar Ramachandra | 8d2a2a1 | 2014-03-14 23:17:52 -0400 | [diff] [blame] | 295 | const char *const mem_subcommands[] = { "record", "report", NULL }; |
| 296 | const char *mem_usage[] = { |
| 297 | NULL, |
| 298 | NULL |
| 299 | }; |
Stephane Eranian | 028f12e | 2013-01-24 16:10:38 +0100 | [diff] [blame] | 300 | |
Ramkumar Ramachandra | 8d2a2a1 | 2014-03-14 23:17:52 -0400 | [diff] [blame] | 301 | |
| 302 | argc = parse_options_subcommand(argc, argv, mem_options, mem_subcommands, |
| 303 | mem_usage, PARSE_OPT_STOP_AT_NON_OPTION); |
Stephane Eranian | 028f12e | 2013-01-24 16:10:38 +0100 | [diff] [blame] | 304 | |
| 305 | if (!argc || !(strncmp(argv[0], "rec", 3) || mem_operation)) |
| 306 | usage_with_options(mem_usage, mem_options); |
| 307 | |
| 308 | if (!mem.input_name || !strlen(mem.input_name)) { |
| 309 | if (!fstat(STDIN_FILENO, &st) && S_ISFIFO(st.st_mode)) |
| 310 | mem.input_name = "-"; |
| 311 | else |
| 312 | mem.input_name = "perf.data"; |
| 313 | } |
| 314 | |
| 315 | if (!strncmp(argv[0], "rec", 3)) |
| 316 | return __cmd_record(argc, argv); |
| 317 | else if (!strncmp(argv[0], "rep", 3)) |
| 318 | return report_events(argc, argv, &mem); |
| 319 | else |
| 320 | usage_with_options(mem_usage, mem_options); |
| 321 | |
| 322 | return 0; |
| 323 | } |