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