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