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