blob: d608a2c9e48cd219e82697bdfa9331a477e9eeed [file] [log] [blame]
Stephane Eranian028f12e2013-01-24 16:10:38 +01001#include "builtin.h"
2#include "perf.h"
3
Josh Poimboeuf4b6ab942015-12-15 09:39:39 -06004#include <subcmd/parse-options.h>
Stephane Eranian028f12e2013-01-24 16:10:38 +01005#include "util/trace-event.h"
6#include "util/tool.h"
7#include "util/session.h"
Jiri Olsaf5fc1412013-10-15 16:27:32 +02008#include "util/data.h"
Jiri Olsaacbe6132016-02-15 09:34:34 +01009#include "util/mem-events.h"
Jiri Olsace1e22b2016-02-15 09:34:35 +010010#include "util/debug.h"
Stephane Eranian028f12e2013-01-24 16:10:38 +010011
Stephane Eranian67121f82014-12-17 16:23:55 +010012#define MEM_OPERATION_LOAD 0x1
13#define MEM_OPERATION_STORE 0x2
Stephane Eranian028f12e2013-01-24 16:10:38 +010014
Stephane Eranian028f12e2013-01-24 16:10:38 +010015struct perf_mem {
16 struct perf_tool tool;
17 char const *input_name;
Stephane Eranian028f12e2013-01-24 16:10:38 +010018 bool hide_unresolved;
19 bool dump_raw;
Yunlong Song62a1a632015-04-02 21:47:15 +080020 bool force;
Arnaldo Carvalho de Melo66024122014-12-17 13:53:27 -030021 int operation;
Stephane Eranian028f12e2013-01-24 16:10:38 +010022 const char *cpu_list;
23 DECLARE_BITMAP(cpu_bitmap, MAX_NR_CPUS);
24};
25
Jiri Olsace1e22b2016-02-15 09:34:35 +010026static 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
Jiri Olsa54fbad52016-02-24 09:46:42 +010043 fprintf(stderr, "%-13s%-*s%s\n",
44 e->tag,
45 verbose ? 25 : 0,
Jiri Olsa2ba7ac52016-02-24 09:46:43 +010046 verbose ? perf_mem_events__name(j) : "",
Jiri Olsa54fbad52016-02-24 09:46:42 +010047 e->supported ? ": available" : "");
Jiri Olsace1e22b2016-02-15 09:34:35 +010048 }
49 exit(0);
50}
51
52static const char * const __usage[] = {
53 "perf mem record [<options>] [<command>]",
54 "perf mem record [<options>] -- <command> [<options>]",
55 NULL
56};
57
58static const char * const *record_mem_usage = __usage;
59
Arnaldo Carvalho de Melo66024122014-12-17 13:53:27 -030060static int __cmd_record(int argc, const char **argv, struct perf_mem *mem)
Stephane Eranian028f12e2013-01-24 16:10:38 +010061{
62 int rec_argc, i = 0, j;
63 const char **rec_argv;
Stephane Eranian028f12e2013-01-24 16:10:38 +010064 int ret;
Jiri Olsaad165112016-03-24 13:52:16 +010065 bool all_user = false, all_kernel = false;
Jiri Olsace1e22b2016-02-15 09:34:35 +010066 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),
Jiri Olsab0d745b2016-06-14 20:19:11 +020070 OPT_UINTEGER(0, "ldlat", &perf_mem_events__loads_ldlat, "mem-loads latency"),
Jiri Olsace1e22b2016-02-15 09:34:35 +010071 OPT_INCR('v', "verbose", &verbose,
72 "be more verbose (show counter open errors, etc)"),
Jiri Olsaad165112016-03-24 13:52:16 +010073 OPT_BOOLEAN('U', "--all-user", &all_user, "collect only user level data"),
74 OPT_BOOLEAN('K', "--all-kernel", &all_kernel, "collect only kernel level data"),
Jiri Olsace1e22b2016-02-15 09:34:35 +010075 OPT_END()
76 };
77
78 argc = parse_options(argc, argv, options, record_mem_usage,
79 PARSE_OPT_STOP_AT_NON_OPTION);
Stephane Eranian028f12e2013-01-24 16:10:38 +010080
Jiri Olsaad165112016-03-24 13:52:16 +010081 rec_argc = argc + 9; /* max number of arguments */
Stephane Eranian028f12e2013-01-24 16:10:38 +010082 rec_argv = calloc(rec_argc + 1, sizeof(char *));
83 if (!rec_argv)
84 return -1;
85
Stephane Eranian67121f82014-12-17 16:23:55 +010086 rec_argv[i++] = "record";
Stephane Eranian028f12e2013-01-24 16:10:38 +010087
Jiri Olsace1e22b2016-02-15 09:34:35 +010088 if (mem->operation & MEM_OPERATION_LOAD)
Jiri Olsaacbe6132016-02-15 09:34:34 +010089 perf_mem_events[PERF_MEM_EVENTS__LOAD].record = true;
Jiri Olsace1e22b2016-02-15 09:34:35 +010090
91 if (perf_mem_events[PERF_MEM_EVENTS__LOAD].record)
Stephane Eranian67121f82014-12-17 16:23:55 +010092 rec_argv[i++] = "-W";
Stephane Eranian028f12e2013-01-24 16:10:38 +010093
Stephane Eranian67121f82014-12-17 16:23:55 +010094 rec_argv[i++] = "-d";
95
Jiri Olsaacbe6132016-02-15 09:34:34 +010096 for (j = 0; j < PERF_MEM_EVENTS__MAX; j++) {
97 if (!perf_mem_events[j].record)
98 continue;
Stephane Eranian67121f82014-12-17 16:23:55 +010099
Jiri Olsa54fbad52016-02-24 09:46:42 +0100100 if (!perf_mem_events[j].supported) {
101 pr_err("failed: event '%s' not supported\n",
Jiri Olsa2ba7ac52016-02-24 09:46:43 +0100102 perf_mem_events__name(j));
Jiri Olsa54fbad52016-02-24 09:46:42 +0100103 return -1;
104 }
105
Stephane Eranian67121f82014-12-17 16:23:55 +0100106 rec_argv[i++] = "-e";
Jiri Olsa2ba7ac52016-02-24 09:46:43 +0100107 rec_argv[i++] = perf_mem_events__name(j);
Jiri Olsaacbe6132016-02-15 09:34:34 +0100108 };
Stephane Eranian67121f82014-12-17 16:23:55 +0100109
Jiri Olsaad165112016-03-24 13:52:16 +0100110 if (all_user)
111 rec_argv[i++] = "--all-user";
112
113 if (all_kernel)
114 rec_argv[i++] = "--all-kernel";
115
Jiri Olsace1e22b2016-02-15 09:34:35 +0100116 for (j = 0; j < argc; j++, i++)
Stephane Eranian028f12e2013-01-24 16:10:38 +0100117 rec_argv[i] = argv[j];
118
Jiri Olsace1e22b2016-02-15 09:34:35 +0100119 if (verbose > 0) {
120 pr_debug("calling: record ");
121
122 while (rec_argv[j]) {
123 pr_debug("%s ", rec_argv[j]);
124 j++;
125 }
126 pr_debug("\n");
127 }
128
Stephane Eranian028f12e2013-01-24 16:10:38 +0100129 ret = cmd_record(i, rec_argv, NULL);
130 free(rec_argv);
131 return ret;
132}
133
134static int
135dump_raw_samples(struct perf_tool *tool,
136 union perf_event *event,
137 struct perf_sample *sample,
Stephane Eranian028f12e2013-01-24 16:10:38 +0100138 struct machine *machine)
139{
140 struct perf_mem *mem = container_of(tool, struct perf_mem, tool);
141 struct addr_location al;
142 const char *fmt;
143
Arnaldo Carvalho de Melobb3eb562016-03-22 18:39:09 -0300144 if (machine__resolve(machine, &al, sample) < 0) {
Stephane Eranian028f12e2013-01-24 16:10:38 +0100145 fprintf(stderr, "problem processing %d event, skipping it.\n",
146 event->header.type);
147 return -1;
148 }
149
150 if (al.filtered || (mem->hide_unresolved && al.sym == NULL))
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -0300151 goto out_put;
Stephane Eranian028f12e2013-01-24 16:10:38 +0100152
153 if (al.map != NULL)
154 al.map->dso->hit = 1;
155
156 if (symbol_conf.field_sep) {
157 fmt = "%d%s%d%s0x%"PRIx64"%s0x%"PRIx64"%s%"PRIu64
158 "%s0x%"PRIx64"%s%s:%s\n";
159 } else {
160 fmt = "%5d%s%5d%s0x%016"PRIx64"%s0x016%"PRIx64
161 "%s%5"PRIu64"%s0x%06"PRIx64"%s%s:%s\n";
162 symbol_conf.field_sep = " ";
163 }
164
165 printf(fmt,
166 sample->pid,
167 symbol_conf.field_sep,
168 sample->tid,
169 symbol_conf.field_sep,
Adrian Hunteref893252013-08-27 11:23:06 +0300170 sample->ip,
Stephane Eranian028f12e2013-01-24 16:10:38 +0100171 symbol_conf.field_sep,
172 sample->addr,
173 symbol_conf.field_sep,
174 sample->weight,
175 symbol_conf.field_sep,
176 sample->data_src,
177 symbol_conf.field_sep,
178 al.map ? (al.map->dso ? al.map->dso->long_name : "???") : "???",
179 al.sym ? al.sym->name : "???");
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -0300180out_put:
181 addr_location__put(&al);
Stephane Eranian028f12e2013-01-24 16:10:38 +0100182 return 0;
183}
184
185static int process_sample_event(struct perf_tool *tool,
186 union perf_event *event,
187 struct perf_sample *sample,
Arnaldo Carvalho de Melo8b640cc2013-12-19 17:00:45 -0300188 struct perf_evsel *evsel __maybe_unused,
Stephane Eranian028f12e2013-01-24 16:10:38 +0100189 struct machine *machine)
190{
Arnaldo Carvalho de Melo8b640cc2013-12-19 17:00:45 -0300191 return dump_raw_samples(tool, event, sample, machine);
Stephane Eranian028f12e2013-01-24 16:10:38 +0100192}
193
194static int report_raw_events(struct perf_mem *mem)
195{
Jiri Olsaf5fc1412013-10-15 16:27:32 +0200196 struct perf_data_file file = {
197 .path = input_name,
198 .mode = PERF_DATA_MODE_READ,
Yunlong Song62a1a632015-04-02 21:47:15 +0800199 .force = mem->force,
Jiri Olsaf5fc1412013-10-15 16:27:32 +0200200 };
Stephane Eranian028f12e2013-01-24 16:10:38 +0100201 int ret;
Jiri Olsaf5fc1412013-10-15 16:27:32 +0200202 struct perf_session *session = perf_session__new(&file, false,
203 &mem->tool);
Stephane Eranian028f12e2013-01-24 16:10:38 +0100204
205 if (session == NULL)
Taeung Song52e028342014-09-24 10:33:37 +0900206 return -1;
Stephane Eranian028f12e2013-01-24 16:10:38 +0100207
208 if (mem->cpu_list) {
209 ret = perf_session__cpu_bitmap(session, mem->cpu_list,
210 mem->cpu_bitmap);
Taeung Song1df9fade2015-07-01 21:08:19 +0900211 if (ret < 0)
Stephane Eranian028f12e2013-01-24 16:10:38 +0100212 goto out_delete;
213 }
214
Taeung Song1df9fade2015-07-01 21:08:19 +0900215 ret = symbol__init(&session->header.env);
216 if (ret < 0)
217 goto out_delete;
Stephane Eranian028f12e2013-01-24 16:10:38 +0100218
219 printf("# PID, TID, IP, ADDR, LOCAL WEIGHT, DSRC, SYMBOL\n");
220
Taeung Song1df9fade2015-07-01 21:08:19 +0900221 ret = perf_session__process_events(session);
Stephane Eranian028f12e2013-01-24 16:10:38 +0100222
223out_delete:
224 perf_session__delete(session);
Taeung Song1df9fade2015-07-01 21:08:19 +0900225 return ret;
Stephane Eranian028f12e2013-01-24 16:10:38 +0100226}
227
228static int report_events(int argc, const char **argv, struct perf_mem *mem)
229{
230 const char **rep_argv;
231 int ret, i = 0, j, rep_argc;
232
233 if (mem->dump_raw)
234 return report_raw_events(mem);
235
236 rep_argc = argc + 3;
237 rep_argv = calloc(rep_argc + 1, sizeof(char *));
238 if (!rep_argv)
239 return -1;
240
Stephane Eranian67121f82014-12-17 16:23:55 +0100241 rep_argv[i++] = "report";
242 rep_argv[i++] = "--mem-mode";
243 rep_argv[i++] = "-n"; /* display number of samples */
Stephane Eranian028f12e2013-01-24 16:10:38 +0100244
245 /*
246 * there is no weight (cost) associated with stores, so don't print
247 * the column
248 */
Arnaldo Carvalho de Melo66024122014-12-17 13:53:27 -0300249 if (!(mem->operation & MEM_OPERATION_LOAD))
Stephane Eranian67121f82014-12-17 16:23:55 +0100250 rep_argv[i++] = "--sort=mem,sym,dso,symbol_daddr,"
251 "dso_daddr,tlb,locked";
Stephane Eranian028f12e2013-01-24 16:10:38 +0100252
253 for (j = 1; j < argc; j++, i++)
254 rep_argv[i] = argv[j];
255
256 ret = cmd_report(i, rep_argv, NULL);
257 free(rep_argv);
258 return ret;
259}
260
Stephane Eranian67121f82014-12-17 16:23:55 +0100261struct mem_mode {
262 const char *name;
263 int mode;
264};
265
266#define MEM_OPT(n, m) \
267 { .name = n, .mode = (m) }
268
269#define MEM_END { .name = NULL }
270
271static const struct mem_mode mem_modes[]={
272 MEM_OPT("load", MEM_OPERATION_LOAD),
273 MEM_OPT("store", MEM_OPERATION_STORE),
274 MEM_END
275};
276
277static int
278parse_mem_ops(const struct option *opt, const char *str, int unset)
279{
280 int *mode = (int *)opt->value;
281 const struct mem_mode *m;
282 char *s, *os = NULL, *p;
283 int ret = -1;
284
285 if (unset)
286 return 0;
287
288 /* str may be NULL in case no arg is passed to -t */
289 if (str) {
290 /* because str is read-only */
291 s = os = strdup(str);
292 if (!s)
293 return -1;
294
295 /* reset mode */
296 *mode = 0;
297
298 for (;;) {
299 p = strchr(s, ',');
300 if (p)
301 *p = '\0';
302
303 for (m = mem_modes; m->name; m++) {
304 if (!strcasecmp(s, m->name))
305 break;
306 }
307 if (!m->name) {
308 fprintf(stderr, "unknown sampling op %s,"
309 " check man page\n", s);
310 goto error;
311 }
312
313 *mode |= m->mode;
314
315 if (!p)
316 break;
317
318 s = p + 1;
319 }
320 }
321 ret = 0;
322
323 if (*mode == 0)
324 *mode = MEM_OPERATION_LOAD;
325error:
326 free(os);
327 return ret;
328}
329
Stephane Eranian028f12e2013-01-24 16:10:38 +0100330int cmd_mem(int argc, const char **argv, const char *prefix __maybe_unused)
331{
332 struct stat st;
333 struct perf_mem mem = {
334 .tool = {
335 .sample = process_sample_event,
336 .mmap = perf_event__process_mmap,
Stephane Eranian5c5e8542013-08-21 12:10:25 +0200337 .mmap2 = perf_event__process_mmap2,
Stephane Eranian028f12e2013-01-24 16:10:38 +0100338 .comm = perf_event__process_comm,
339 .lost = perf_event__process_lost,
340 .fork = perf_event__process_fork,
341 .build_id = perf_event__process_build_id,
Jiri Olsa0a8cb852014-07-06 14:18:21 +0200342 .ordered_events = true,
Stephane Eranian028f12e2013-01-24 16:10:38 +0100343 },
344 .input_name = "perf.data",
Arnaldo Carvalho de Melo66024122014-12-17 13:53:27 -0300345 /*
346 * default to both load an store sampling
347 */
348 .operation = MEM_OPERATION_LOAD | MEM_OPERATION_STORE,
Stephane Eranian028f12e2013-01-24 16:10:38 +0100349 };
350 const struct option mem_options[] = {
Arnaldo Carvalho de Melo66024122014-12-17 13:53:27 -0300351 OPT_CALLBACK('t', "type", &mem.operation,
Stephane Eranian67121f82014-12-17 16:23:55 +0100352 "type", "memory operations(load,store) Default load,store",
353 parse_mem_ops),
Stephane Eranian028f12e2013-01-24 16:10:38 +0100354 OPT_BOOLEAN('D', "dump-raw-samples", &mem.dump_raw,
355 "dump raw samples in ASCII"),
356 OPT_BOOLEAN('U', "hide-unresolved", &mem.hide_unresolved,
357 "Only display entries resolved to a symbol"),
358 OPT_STRING('i', "input", &input_name, "file",
359 "input file name"),
360 OPT_STRING('C', "cpu", &mem.cpu_list, "cpu",
361 "list of cpus to profile"),
Wang Nan8b8ca6e2015-03-20 02:57:52 +0000362 OPT_STRING_NOEMPTY('x', "field-separator", &symbol_conf.field_sep,
Stephane Eranian028f12e2013-01-24 16:10:38 +0100363 "separator",
364 "separator for columns, no spaces will be added"
365 " between columns '.' is reserved."),
Yunlong Song62a1a632015-04-02 21:47:15 +0800366 OPT_BOOLEAN('f', "force", &mem.force, "don't complain, do it"),
Stephane Eranian028f12e2013-01-24 16:10:38 +0100367 OPT_END()
368 };
Ramkumar Ramachandra8d2a2a12014-03-14 23:17:52 -0400369 const char *const mem_subcommands[] = { "record", "report", NULL };
370 const char *mem_usage[] = {
371 NULL,
372 NULL
373 };
Stephane Eranian028f12e2013-01-24 16:10:38 +0100374
Jiri Olsa54fbad52016-02-24 09:46:42 +0100375 if (perf_mem_events__init()) {
376 pr_err("failed: memory events not supported\n");
377 return -1;
378 }
379
Ramkumar Ramachandra8d2a2a12014-03-14 23:17:52 -0400380 argc = parse_options_subcommand(argc, argv, mem_options, mem_subcommands,
381 mem_usage, PARSE_OPT_STOP_AT_NON_OPTION);
Stephane Eranian028f12e2013-01-24 16:10:38 +0100382
Arnaldo Carvalho de Melo66024122014-12-17 13:53:27 -0300383 if (!argc || !(strncmp(argv[0], "rec", 3) || mem.operation))
Stephane Eranian028f12e2013-01-24 16:10:38 +0100384 usage_with_options(mem_usage, mem_options);
385
386 if (!mem.input_name || !strlen(mem.input_name)) {
387 if (!fstat(STDIN_FILENO, &st) && S_ISFIFO(st.st_mode))
388 mem.input_name = "-";
389 else
390 mem.input_name = "perf.data";
391 }
392
393 if (!strncmp(argv[0], "rec", 3))
Arnaldo Carvalho de Melo66024122014-12-17 13:53:27 -0300394 return __cmd_record(argc, argv, &mem);
Stephane Eranian028f12e2013-01-24 16:10:38 +0100395 else if (!strncmp(argv[0], "rep", 3))
396 return report_events(argc, argv, &mem);
397 else
398 usage_with_options(mem_usage, mem_options);
399
400 return 0;
401}