]>
Commit | Line | Data |
---|---|---|
fd20e811 | 1 | #include <inttypes.h> |
7a8ef4c4 ACM |
2 | #include <sys/types.h> |
3 | #include <sys/stat.h> | |
4 | #include <unistd.h> | |
028f12ee SE |
5 | #include "builtin.h" |
6 | #include "perf.h" | |
7 | ||
4b6ab94e | 8 | #include <subcmd/parse-options.h> |
028f12ee SE |
9 | #include "util/trace-event.h" |
10 | #include "util/tool.h" | |
11 | #include "util/session.h" | |
f5fc1412 | 12 | #include "util/data.h" |
acbe613e | 13 | #include "util/mem-events.h" |
ce1e22b0 | 14 | #include "util/debug.h" |
e7ff8920 | 15 | #include "util/symbol.h" |
028f12ee | 16 | |
67121f85 SE |
17 | #define MEM_OPERATION_LOAD 0x1 |
18 | #define MEM_OPERATION_STORE 0x2 | |
028f12ee | 19 | |
028f12ee SE |
20 | struct perf_mem { |
21 | struct perf_tool tool; | |
22 | char const *input_name; | |
028f12ee SE |
23 | bool hide_unresolved; |
24 | bool dump_raw; | |
62a1a63a | 25 | bool force; |
66024122 | 26 | int operation; |
028f12ee SE |
27 | const char *cpu_list; |
28 | DECLARE_BITMAP(cpu_bitmap, MAX_NR_CPUS); | |
29 | }; | |
30 | ||
ce1e22b0 JO |
31 | static int parse_record_events(const struct option *opt, |
32 | const char *str, int unset __maybe_unused) | |
33 | { | |
34 | struct perf_mem *mem = *(struct perf_mem **)opt->value; | |
35 | int j; | |
36 | ||
37 | if (strcmp(str, "list")) { | |
38 | if (!perf_mem_events__parse(str)) { | |
39 | mem->operation = 0; | |
40 | return 0; | |
41 | } | |
42 | exit(-1); | |
43 | } | |
44 | ||
45 | for (j = 0; j < PERF_MEM_EVENTS__MAX; j++) { | |
46 | struct perf_mem_event *e = &perf_mem_events[j]; | |
47 | ||
54fbad54 JO |
48 | fprintf(stderr, "%-13s%-*s%s\n", |
49 | e->tag, | |
bb963e16 NK |
50 | verbose > 0 ? 25 : 0, |
51 | verbose > 0 ? perf_mem_events__name(j) : "", | |
54fbad54 | 52 | e->supported ? ": available" : ""); |
ce1e22b0 JO |
53 | } |
54 | exit(0); | |
55 | } | |
56 | ||
57 | static const char * const __usage[] = { | |
58 | "perf mem record [<options>] [<command>]", | |
59 | "perf mem record [<options>] -- <command> [<options>]", | |
60 | NULL | |
61 | }; | |
62 | ||
63 | static const char * const *record_mem_usage = __usage; | |
64 | ||
66024122 | 65 | static int __cmd_record(int argc, const char **argv, struct perf_mem *mem) |
028f12ee SE |
66 | { |
67 | int rec_argc, i = 0, j; | |
68 | const char **rec_argv; | |
028f12ee | 69 | int ret; |
ad16511b | 70 | bool all_user = false, all_kernel = false; |
ce1e22b0 JO |
71 | struct option options[] = { |
72 | OPT_CALLBACK('e', "event", &mem, "event", | |
73 | "event selector. use 'perf mem record -e list' to list available events", | |
74 | parse_record_events), | |
b0d745b3 | 75 | OPT_UINTEGER(0, "ldlat", &perf_mem_events__loads_ldlat, "mem-loads latency"), |
ce1e22b0 JO |
76 | OPT_INCR('v', "verbose", &verbose, |
77 | "be more verbose (show counter open errors, etc)"), | |
631ac41b JO |
78 | OPT_BOOLEAN('U', "all-user", &all_user, "collect only user level data"), |
79 | OPT_BOOLEAN('K', "all-kernel", &all_kernel, "collect only kernel level data"), | |
ce1e22b0 JO |
80 | OPT_END() |
81 | }; | |
82 | ||
83 | argc = parse_options(argc, argv, options, record_mem_usage, | |
84 | PARSE_OPT_STOP_AT_NON_OPTION); | |
028f12ee | 85 | |
ad16511b | 86 | rec_argc = argc + 9; /* max number of arguments */ |
028f12ee SE |
87 | rec_argv = calloc(rec_argc + 1, sizeof(char *)); |
88 | if (!rec_argv) | |
89 | return -1; | |
90 | ||
67121f85 | 91 | rec_argv[i++] = "record"; |
028f12ee | 92 | |
ce1e22b0 | 93 | if (mem->operation & MEM_OPERATION_LOAD) |
acbe613e | 94 | perf_mem_events[PERF_MEM_EVENTS__LOAD].record = true; |
ce1e22b0 | 95 | |
33da54fa JO |
96 | if (mem->operation & MEM_OPERATION_STORE) |
97 | perf_mem_events[PERF_MEM_EVENTS__STORE].record = true; | |
98 | ||
ce1e22b0 | 99 | if (perf_mem_events[PERF_MEM_EVENTS__LOAD].record) |
67121f85 SE |
100 | rec_argv[i++] = "-W"; |
101 | ||
102 | rec_argv[i++] = "-d"; | |
103 | ||
acbe613e JO |
104 | for (j = 0; j < PERF_MEM_EVENTS__MAX; j++) { |
105 | if (!perf_mem_events[j].record) | |
106 | continue; | |
67121f85 | 107 | |
54fbad54 JO |
108 | if (!perf_mem_events[j].supported) { |
109 | pr_err("failed: event '%s' not supported\n", | |
2ba7ac58 | 110 | perf_mem_events__name(j)); |
54fbad54 JO |
111 | return -1; |
112 | } | |
113 | ||
67121f85 | 114 | rec_argv[i++] = "-e"; |
2ba7ac58 | 115 | rec_argv[i++] = perf_mem_events__name(j); |
acbe613e | 116 | }; |
028f12ee | 117 | |
ad16511b JO |
118 | if (all_user) |
119 | rec_argv[i++] = "--all-user"; | |
120 | ||
121 | if (all_kernel) | |
122 | rec_argv[i++] = "--all-kernel"; | |
123 | ||
ce1e22b0 | 124 | for (j = 0; j < argc; j++, i++) |
028f12ee SE |
125 | rec_argv[i] = argv[j]; |
126 | ||
ce1e22b0 JO |
127 | if (verbose > 0) { |
128 | pr_debug("calling: record "); | |
129 | ||
130 | while (rec_argv[j]) { | |
131 | pr_debug("%s ", rec_argv[j]); | |
132 | j++; | |
133 | } | |
134 | pr_debug("\n"); | |
135 | } | |
136 | ||
b0ad8ea6 | 137 | ret = cmd_record(i, rec_argv); |
028f12ee SE |
138 | free(rec_argv); |
139 | return ret; | |
140 | } | |
141 | ||
142 | static int | |
143 | dump_raw_samples(struct perf_tool *tool, | |
144 | union perf_event *event, | |
145 | struct perf_sample *sample, | |
028f12ee SE |
146 | struct machine *machine) |
147 | { | |
148 | struct perf_mem *mem = container_of(tool, struct perf_mem, tool); | |
149 | struct addr_location al; | |
150 | const char *fmt; | |
151 | ||
bb3eb566 | 152 | if (machine__resolve(machine, &al, sample) < 0) { |
028f12ee SE |
153 | fprintf(stderr, "problem processing %d event, skipping it.\n", |
154 | event->header.type); | |
155 | return -1; | |
156 | } | |
157 | ||
158 | if (al.filtered || (mem->hide_unresolved && al.sym == NULL)) | |
b91fc39f | 159 | goto out_put; |
028f12ee SE |
160 | |
161 | if (al.map != NULL) | |
162 | al.map->dso->hit = 1; | |
163 | ||
164 | if (symbol_conf.field_sep) { | |
165 | fmt = "%d%s%d%s0x%"PRIx64"%s0x%"PRIx64"%s%"PRIu64 | |
166 | "%s0x%"PRIx64"%s%s:%s\n"; | |
167 | } else { | |
168 | fmt = "%5d%s%5d%s0x%016"PRIx64"%s0x016%"PRIx64 | |
169 | "%s%5"PRIu64"%s0x%06"PRIx64"%s%s:%s\n"; | |
170 | symbol_conf.field_sep = " "; | |
171 | } | |
172 | ||
173 | printf(fmt, | |
174 | sample->pid, | |
175 | symbol_conf.field_sep, | |
176 | sample->tid, | |
177 | symbol_conf.field_sep, | |
ef89325f | 178 | sample->ip, |
028f12ee SE |
179 | symbol_conf.field_sep, |
180 | sample->addr, | |
181 | symbol_conf.field_sep, | |
182 | sample->weight, | |
183 | symbol_conf.field_sep, | |
184 | sample->data_src, | |
185 | symbol_conf.field_sep, | |
186 | al.map ? (al.map->dso ? al.map->dso->long_name : "???") : "???", | |
187 | al.sym ? al.sym->name : "???"); | |
b91fc39f ACM |
188 | out_put: |
189 | addr_location__put(&al); | |
028f12ee SE |
190 | return 0; |
191 | } | |
192 | ||
193 | static int process_sample_event(struct perf_tool *tool, | |
194 | union perf_event *event, | |
195 | struct perf_sample *sample, | |
8b640cc4 | 196 | struct perf_evsel *evsel __maybe_unused, |
028f12ee SE |
197 | struct machine *machine) |
198 | { | |
8b640cc4 | 199 | return dump_raw_samples(tool, event, sample, machine); |
028f12ee SE |
200 | } |
201 | ||
202 | static int report_raw_events(struct perf_mem *mem) | |
203 | { | |
f5fc1412 JO |
204 | struct perf_data_file file = { |
205 | .path = input_name, | |
206 | .mode = PERF_DATA_MODE_READ, | |
62a1a63a | 207 | .force = mem->force, |
f5fc1412 | 208 | }; |
028f12ee | 209 | int ret; |
f5fc1412 JO |
210 | struct perf_session *session = perf_session__new(&file, false, |
211 | &mem->tool); | |
028f12ee SE |
212 | |
213 | if (session == NULL) | |
52e02834 | 214 | return -1; |
028f12ee SE |
215 | |
216 | if (mem->cpu_list) { | |
217 | ret = perf_session__cpu_bitmap(session, mem->cpu_list, | |
218 | mem->cpu_bitmap); | |
1df9fade | 219 | if (ret < 0) |
028f12ee SE |
220 | goto out_delete; |
221 | } | |
222 | ||
1df9fade TS |
223 | ret = symbol__init(&session->header.env); |
224 | if (ret < 0) | |
225 | goto out_delete; | |
028f12ee SE |
226 | |
227 | printf("# PID, TID, IP, ADDR, LOCAL WEIGHT, DSRC, SYMBOL\n"); | |
228 | ||
1df9fade | 229 | ret = perf_session__process_events(session); |
028f12ee SE |
230 | |
231 | out_delete: | |
232 | perf_session__delete(session); | |
1df9fade | 233 | return ret; |
028f12ee SE |
234 | } |
235 | ||
236 | static int report_events(int argc, const char **argv, struct perf_mem *mem) | |
237 | { | |
238 | const char **rep_argv; | |
239 | int ret, i = 0, j, rep_argc; | |
240 | ||
241 | if (mem->dump_raw) | |
242 | return report_raw_events(mem); | |
243 | ||
244 | rep_argc = argc + 3; | |
245 | rep_argv = calloc(rep_argc + 1, sizeof(char *)); | |
246 | if (!rep_argv) | |
247 | return -1; | |
248 | ||
67121f85 SE |
249 | rep_argv[i++] = "report"; |
250 | rep_argv[i++] = "--mem-mode"; | |
251 | rep_argv[i++] = "-n"; /* display number of samples */ | |
028f12ee SE |
252 | |
253 | /* | |
254 | * there is no weight (cost) associated with stores, so don't print | |
255 | * the column | |
256 | */ | |
66024122 | 257 | if (!(mem->operation & MEM_OPERATION_LOAD)) |
67121f85 SE |
258 | rep_argv[i++] = "--sort=mem,sym,dso,symbol_daddr," |
259 | "dso_daddr,tlb,locked"; | |
028f12ee SE |
260 | |
261 | for (j = 1; j < argc; j++, i++) | |
262 | rep_argv[i] = argv[j]; | |
263 | ||
b0ad8ea6 | 264 | ret = cmd_report(i, rep_argv); |
028f12ee SE |
265 | free(rep_argv); |
266 | return ret; | |
267 | } | |
268 | ||
67121f85 SE |
269 | struct mem_mode { |
270 | const char *name; | |
271 | int mode; | |
272 | }; | |
273 | ||
274 | #define MEM_OPT(n, m) \ | |
275 | { .name = n, .mode = (m) } | |
276 | ||
277 | #define MEM_END { .name = NULL } | |
278 | ||
279 | static const struct mem_mode mem_modes[]={ | |
280 | MEM_OPT("load", MEM_OPERATION_LOAD), | |
281 | MEM_OPT("store", MEM_OPERATION_STORE), | |
282 | MEM_END | |
283 | }; | |
284 | ||
285 | static int | |
286 | parse_mem_ops(const struct option *opt, const char *str, int unset) | |
287 | { | |
288 | int *mode = (int *)opt->value; | |
289 | const struct mem_mode *m; | |
290 | char *s, *os = NULL, *p; | |
291 | int ret = -1; | |
292 | ||
293 | if (unset) | |
294 | return 0; | |
295 | ||
296 | /* str may be NULL in case no arg is passed to -t */ | |
297 | if (str) { | |
298 | /* because str is read-only */ | |
299 | s = os = strdup(str); | |
300 | if (!s) | |
301 | return -1; | |
302 | ||
303 | /* reset mode */ | |
304 | *mode = 0; | |
305 | ||
306 | for (;;) { | |
307 | p = strchr(s, ','); | |
308 | if (p) | |
309 | *p = '\0'; | |
310 | ||
311 | for (m = mem_modes; m->name; m++) { | |
312 | if (!strcasecmp(s, m->name)) | |
313 | break; | |
314 | } | |
315 | if (!m->name) { | |
316 | fprintf(stderr, "unknown sampling op %s," | |
317 | " check man page\n", s); | |
318 | goto error; | |
319 | } | |
320 | ||
321 | *mode |= m->mode; | |
322 | ||
323 | if (!p) | |
324 | break; | |
325 | ||
326 | s = p + 1; | |
327 | } | |
328 | } | |
329 | ret = 0; | |
330 | ||
331 | if (*mode == 0) | |
332 | *mode = MEM_OPERATION_LOAD; | |
333 | error: | |
334 | free(os); | |
335 | return ret; | |
336 | } | |
337 | ||
b0ad8ea6 | 338 | int cmd_mem(int argc, const char **argv) |
028f12ee SE |
339 | { |
340 | struct stat st; | |
341 | struct perf_mem mem = { | |
342 | .tool = { | |
343 | .sample = process_sample_event, | |
344 | .mmap = perf_event__process_mmap, | |
5c5e854b | 345 | .mmap2 = perf_event__process_mmap2, |
028f12ee SE |
346 | .comm = perf_event__process_comm, |
347 | .lost = perf_event__process_lost, | |
348 | .fork = perf_event__process_fork, | |
349 | .build_id = perf_event__process_build_id, | |
f3b3614a | 350 | .namespaces = perf_event__process_namespaces, |
0a8cb85c | 351 | .ordered_events = true, |
028f12ee SE |
352 | }, |
353 | .input_name = "perf.data", | |
66024122 ACM |
354 | /* |
355 | * default to both load an store sampling | |
356 | */ | |
357 | .operation = MEM_OPERATION_LOAD | MEM_OPERATION_STORE, | |
028f12ee SE |
358 | }; |
359 | const struct option mem_options[] = { | |
66024122 | 360 | OPT_CALLBACK('t', "type", &mem.operation, |
67121f85 SE |
361 | "type", "memory operations(load,store) Default load,store", |
362 | parse_mem_ops), | |
028f12ee SE |
363 | OPT_BOOLEAN('D', "dump-raw-samples", &mem.dump_raw, |
364 | "dump raw samples in ASCII"), | |
365 | OPT_BOOLEAN('U', "hide-unresolved", &mem.hide_unresolved, | |
366 | "Only display entries resolved to a symbol"), | |
367 | OPT_STRING('i', "input", &input_name, "file", | |
368 | "input file name"), | |
369 | OPT_STRING('C', "cpu", &mem.cpu_list, "cpu", | |
370 | "list of cpus to profile"), | |
8b8ca6e1 | 371 | OPT_STRING_NOEMPTY('x', "field-separator", &symbol_conf.field_sep, |
028f12ee SE |
372 | "separator", |
373 | "separator for columns, no spaces will be added" | |
374 | " between columns '.' is reserved."), | |
62a1a63a | 375 | OPT_BOOLEAN('f', "force", &mem.force, "don't complain, do it"), |
028f12ee SE |
376 | OPT_END() |
377 | }; | |
8d2a2a1d RR |
378 | const char *const mem_subcommands[] = { "record", "report", NULL }; |
379 | const char *mem_usage[] = { | |
380 | NULL, | |
381 | NULL | |
382 | }; | |
383 | ||
54fbad54 JO |
384 | if (perf_mem_events__init()) { |
385 | pr_err("failed: memory events not supported\n"); | |
386 | return -1; | |
387 | } | |
388 | ||
8d2a2a1d RR |
389 | argc = parse_options_subcommand(argc, argv, mem_options, mem_subcommands, |
390 | mem_usage, PARSE_OPT_STOP_AT_NON_OPTION); | |
028f12ee | 391 | |
66024122 | 392 | if (!argc || !(strncmp(argv[0], "rec", 3) || mem.operation)) |
028f12ee SE |
393 | usage_with_options(mem_usage, mem_options); |
394 | ||
395 | if (!mem.input_name || !strlen(mem.input_name)) { | |
396 | if (!fstat(STDIN_FILENO, &st) && S_ISFIFO(st.st_mode)) | |
397 | mem.input_name = "-"; | |
398 | else | |
399 | mem.input_name = "perf.data"; | |
400 | } | |
401 | ||
402 | if (!strncmp(argv[0], "rec", 3)) | |
66024122 | 403 | return __cmd_record(argc, argv, &mem); |
028f12ee SE |
404 | else if (!strncmp(argv[0], "rep", 3)) |
405 | return report_events(argc, argv, &mem); | |
406 | else | |
407 | usage_with_options(mem_usage, mem_options); | |
408 | ||
409 | return 0; | |
410 | } |