blob: 2e5be1d63af6e7eefc5f5ec0ff05dc4802ac52cf [file] [log] [blame]
Arnaldo Carvalho de Melofd20e812017-04-17 15:23:08 -03001#include <inttypes.h>
Arnaldo Carvalho de Melo7a8ef4c2017-04-19 20:57:47 -03002#include <sys/types.h>
3#include <sys/stat.h>
4#include <unistd.h>
Stephane Eranian028f12e2013-01-24 16:10:38 +01005#include "builtin.h"
6#include "perf.h"
7
Josh Poimboeuf4b6ab942015-12-15 09:39:39 -06008#include <subcmd/parse-options.h>
Stephane Eranian028f12e2013-01-24 16:10:38 +01009#include "util/trace-event.h"
10#include "util/tool.h"
11#include "util/session.h"
Jiri Olsaf5fc1412013-10-15 16:27:32 +020012#include "util/data.h"
Jiri Olsaacbe6132016-02-15 09:34:34 +010013#include "util/mem-events.h"
Jiri Olsace1e22b2016-02-15 09:34:35 +010014#include "util/debug.h"
Stephane Eranian028f12e2013-01-24 16:10:38 +010015
Stephane Eranian67121f82014-12-17 16:23:55 +010016#define MEM_OPERATION_LOAD 0x1
17#define MEM_OPERATION_STORE 0x2
Stephane Eranian028f12e2013-01-24 16:10:38 +010018
Stephane Eranian028f12e2013-01-24 16:10:38 +010019struct perf_mem {
20 struct perf_tool tool;
21 char const *input_name;
Stephane Eranian028f12e2013-01-24 16:10:38 +010022 bool hide_unresolved;
23 bool dump_raw;
Yunlong Song62a1a632015-04-02 21:47:15 +080024 bool force;
Arnaldo Carvalho de Melo66024122014-12-17 13:53:27 -030025 int operation;
Stephane Eranian028f12e2013-01-24 16:10:38 +010026 const char *cpu_list;
27 DECLARE_BITMAP(cpu_bitmap, MAX_NR_CPUS);
28};
29
Jiri Olsace1e22b2016-02-15 09:34:35 +010030static int parse_record_events(const struct option *opt,
31 const char *str, int unset __maybe_unused)
32{
33 struct perf_mem *mem = *(struct perf_mem **)opt->value;
34 int j;
35
36 if (strcmp(str, "list")) {
37 if (!perf_mem_events__parse(str)) {
38 mem->operation = 0;
39 return 0;
40 }
41 exit(-1);
42 }
43
44 for (j = 0; j < PERF_MEM_EVENTS__MAX; j++) {
45 struct perf_mem_event *e = &perf_mem_events[j];
46
Jiri Olsa54fbad52016-02-24 09:46:42 +010047 fprintf(stderr, "%-13s%-*s%s\n",
48 e->tag,
Namhyung Kimbb963e12017-02-17 17:17:38 +090049 verbose > 0 ? 25 : 0,
50 verbose > 0 ? perf_mem_events__name(j) : "",
Jiri Olsa54fbad52016-02-24 09:46:42 +010051 e->supported ? ": available" : "");
Jiri Olsace1e22b2016-02-15 09:34:35 +010052 }
53 exit(0);
54}
55
56static const char * const __usage[] = {
57 "perf mem record [<options>] [<command>]",
58 "perf mem record [<options>] -- <command> [<options>]",
59 NULL
60};
61
62static const char * const *record_mem_usage = __usage;
63
Arnaldo Carvalho de Melo66024122014-12-17 13:53:27 -030064static int __cmd_record(int argc, const char **argv, struct perf_mem *mem)
Stephane Eranian028f12e2013-01-24 16:10:38 +010065{
66 int rec_argc, i = 0, j;
67 const char **rec_argv;
Stephane Eranian028f12e2013-01-24 16:10:38 +010068 int ret;
Jiri Olsaad165112016-03-24 13:52:16 +010069 bool all_user = false, all_kernel = false;
Jiri Olsace1e22b2016-02-15 09:34:35 +010070 struct option options[] = {
71 OPT_CALLBACK('e', "event", &mem, "event",
72 "event selector. use 'perf mem record -e list' to list available events",
73 parse_record_events),
Jiri Olsab0d745b2016-06-14 20:19:11 +020074 OPT_UINTEGER(0, "ldlat", &perf_mem_events__loads_ldlat, "mem-loads latency"),
Jiri Olsace1e22b2016-02-15 09:34:35 +010075 OPT_INCR('v', "verbose", &verbose,
76 "be more verbose (show counter open errors, etc)"),
Jiri Olsa631ac412016-12-12 11:35:39 +010077 OPT_BOOLEAN('U', "all-user", &all_user, "collect only user level data"),
78 OPT_BOOLEAN('K', "all-kernel", &all_kernel, "collect only kernel level data"),
Jiri Olsace1e22b2016-02-15 09:34:35 +010079 OPT_END()
80 };
81
82 argc = parse_options(argc, argv, options, record_mem_usage,
83 PARSE_OPT_STOP_AT_NON_OPTION);
Stephane Eranian028f12e2013-01-24 16:10:38 +010084
Jiri Olsaad165112016-03-24 13:52:16 +010085 rec_argc = argc + 9; /* max number of arguments */
Stephane Eranian028f12e2013-01-24 16:10:38 +010086 rec_argv = calloc(rec_argc + 1, sizeof(char *));
87 if (!rec_argv)
88 return -1;
89
Stephane Eranian67121f82014-12-17 16:23:55 +010090 rec_argv[i++] = "record";
Stephane Eranian028f12e2013-01-24 16:10:38 +010091
Jiri Olsace1e22b2016-02-15 09:34:35 +010092 if (mem->operation & MEM_OPERATION_LOAD)
Jiri Olsaacbe6132016-02-15 09:34:34 +010093 perf_mem_events[PERF_MEM_EVENTS__LOAD].record = true;
Jiri Olsace1e22b2016-02-15 09:34:35 +010094
Jiri Olsa33da54f2016-08-11 10:50:57 +020095 if (mem->operation & MEM_OPERATION_STORE)
96 perf_mem_events[PERF_MEM_EVENTS__STORE].record = true;
97
Jiri Olsace1e22b2016-02-15 09:34:35 +010098 if (perf_mem_events[PERF_MEM_EVENTS__LOAD].record)
Stephane Eranian67121f82014-12-17 16:23:55 +010099 rec_argv[i++] = "-W";
Stephane Eranian028f12e2013-01-24 16:10:38 +0100100
Stephane Eranian67121f82014-12-17 16:23:55 +0100101 rec_argv[i++] = "-d";
102
Jiri Olsaacbe6132016-02-15 09:34:34 +0100103 for (j = 0; j < PERF_MEM_EVENTS__MAX; j++) {
104 if (!perf_mem_events[j].record)
105 continue;
Stephane Eranian67121f82014-12-17 16:23:55 +0100106
Jiri Olsa54fbad52016-02-24 09:46:42 +0100107 if (!perf_mem_events[j].supported) {
108 pr_err("failed: event '%s' not supported\n",
Jiri Olsa2ba7ac52016-02-24 09:46:43 +0100109 perf_mem_events__name(j));
Jiri Olsa54fbad52016-02-24 09:46:42 +0100110 return -1;
111 }
112
Stephane Eranian67121f82014-12-17 16:23:55 +0100113 rec_argv[i++] = "-e";
Jiri Olsa2ba7ac52016-02-24 09:46:43 +0100114 rec_argv[i++] = perf_mem_events__name(j);
Jiri Olsaacbe6132016-02-15 09:34:34 +0100115 };
Stephane Eranian67121f82014-12-17 16:23:55 +0100116
Jiri Olsaad165112016-03-24 13:52:16 +0100117 if (all_user)
118 rec_argv[i++] = "--all-user";
119
120 if (all_kernel)
121 rec_argv[i++] = "--all-kernel";
122
Jiri Olsace1e22b2016-02-15 09:34:35 +0100123 for (j = 0; j < argc; j++, i++)
Stephane Eranian028f12e2013-01-24 16:10:38 +0100124 rec_argv[i] = argv[j];
125
Jiri Olsace1e22b2016-02-15 09:34:35 +0100126 if (verbose > 0) {
127 pr_debug("calling: record ");
128
129 while (rec_argv[j]) {
130 pr_debug("%s ", rec_argv[j]);
131 j++;
132 }
133 pr_debug("\n");
134 }
135
Arnaldo Carvalho de Melob0ad8ea2017-03-27 11:47:20 -0300136 ret = cmd_record(i, rec_argv);
Stephane Eranian028f12e2013-01-24 16:10:38 +0100137 free(rec_argv);
138 return ret;
139}
140
141static int
142dump_raw_samples(struct perf_tool *tool,
143 union perf_event *event,
144 struct perf_sample *sample,
Stephane Eranian028f12e2013-01-24 16:10:38 +0100145 struct machine *machine)
146{
147 struct perf_mem *mem = container_of(tool, struct perf_mem, tool);
148 struct addr_location al;
149 const char *fmt;
150
Arnaldo Carvalho de Melobb3eb562016-03-22 18:39:09 -0300151 if (machine__resolve(machine, &al, sample) < 0) {
Stephane Eranian028f12e2013-01-24 16:10:38 +0100152 fprintf(stderr, "problem processing %d event, skipping it.\n",
153 event->header.type);
154 return -1;
155 }
156
157 if (al.filtered || (mem->hide_unresolved && al.sym == NULL))
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -0300158 goto out_put;
Stephane Eranian028f12e2013-01-24 16:10:38 +0100159
160 if (al.map != NULL)
161 al.map->dso->hit = 1;
162
163 if (symbol_conf.field_sep) {
164 fmt = "%d%s%d%s0x%"PRIx64"%s0x%"PRIx64"%s%"PRIu64
165 "%s0x%"PRIx64"%s%s:%s\n";
166 } else {
167 fmt = "%5d%s%5d%s0x%016"PRIx64"%s0x016%"PRIx64
168 "%s%5"PRIu64"%s0x%06"PRIx64"%s%s:%s\n";
169 symbol_conf.field_sep = " ";
170 }
171
172 printf(fmt,
173 sample->pid,
174 symbol_conf.field_sep,
175 sample->tid,
176 symbol_conf.field_sep,
Adrian Hunteref893252013-08-27 11:23:06 +0300177 sample->ip,
Stephane Eranian028f12e2013-01-24 16:10:38 +0100178 symbol_conf.field_sep,
179 sample->addr,
180 symbol_conf.field_sep,
181 sample->weight,
182 symbol_conf.field_sep,
183 sample->data_src,
184 symbol_conf.field_sep,
185 al.map ? (al.map->dso ? al.map->dso->long_name : "???") : "???",
186 al.sym ? al.sym->name : "???");
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -0300187out_put:
188 addr_location__put(&al);
Stephane Eranian028f12e2013-01-24 16:10:38 +0100189 return 0;
190}
191
192static int process_sample_event(struct perf_tool *tool,
193 union perf_event *event,
194 struct perf_sample *sample,
Arnaldo Carvalho de Melo8b640cc2013-12-19 17:00:45 -0300195 struct perf_evsel *evsel __maybe_unused,
Stephane Eranian028f12e2013-01-24 16:10:38 +0100196 struct machine *machine)
197{
Arnaldo Carvalho de Melo8b640cc2013-12-19 17:00:45 -0300198 return dump_raw_samples(tool, event, sample, machine);
Stephane Eranian028f12e2013-01-24 16:10:38 +0100199}
200
201static int report_raw_events(struct perf_mem *mem)
202{
Jiri Olsaf5fc1412013-10-15 16:27:32 +0200203 struct perf_data_file file = {
204 .path = input_name,
205 .mode = PERF_DATA_MODE_READ,
Yunlong Song62a1a632015-04-02 21:47:15 +0800206 .force = mem->force,
Jiri Olsaf5fc1412013-10-15 16:27:32 +0200207 };
Stephane Eranian028f12e2013-01-24 16:10:38 +0100208 int ret;
Jiri Olsaf5fc1412013-10-15 16:27:32 +0200209 struct perf_session *session = perf_session__new(&file, false,
210 &mem->tool);
Stephane Eranian028f12e2013-01-24 16:10:38 +0100211
212 if (session == NULL)
Taeung Song52e028342014-09-24 10:33:37 +0900213 return -1;
Stephane Eranian028f12e2013-01-24 16:10:38 +0100214
215 if (mem->cpu_list) {
216 ret = perf_session__cpu_bitmap(session, mem->cpu_list,
217 mem->cpu_bitmap);
Taeung Song1df9fade2015-07-01 21:08:19 +0900218 if (ret < 0)
Stephane Eranian028f12e2013-01-24 16:10:38 +0100219 goto out_delete;
220 }
221
Taeung Song1df9fade2015-07-01 21:08:19 +0900222 ret = symbol__init(&session->header.env);
223 if (ret < 0)
224 goto out_delete;
Stephane Eranian028f12e2013-01-24 16:10:38 +0100225
226 printf("# PID, TID, IP, ADDR, LOCAL WEIGHT, DSRC, SYMBOL\n");
227
Taeung Song1df9fade2015-07-01 21:08:19 +0900228 ret = perf_session__process_events(session);
Stephane Eranian028f12e2013-01-24 16:10:38 +0100229
230out_delete:
231 perf_session__delete(session);
Taeung Song1df9fade2015-07-01 21:08:19 +0900232 return ret;
Stephane Eranian028f12e2013-01-24 16:10:38 +0100233}
234
235static int report_events(int argc, const char **argv, struct perf_mem *mem)
236{
237 const char **rep_argv;
238 int ret, i = 0, j, rep_argc;
239
240 if (mem->dump_raw)
241 return report_raw_events(mem);
242
243 rep_argc = argc + 3;
244 rep_argv = calloc(rep_argc + 1, sizeof(char *));
245 if (!rep_argv)
246 return -1;
247
Stephane Eranian67121f82014-12-17 16:23:55 +0100248 rep_argv[i++] = "report";
249 rep_argv[i++] = "--mem-mode";
250 rep_argv[i++] = "-n"; /* display number of samples */
Stephane Eranian028f12e2013-01-24 16:10:38 +0100251
252 /*
253 * there is no weight (cost) associated with stores, so don't print
254 * the column
255 */
Arnaldo Carvalho de Melo66024122014-12-17 13:53:27 -0300256 if (!(mem->operation & MEM_OPERATION_LOAD))
Stephane Eranian67121f82014-12-17 16:23:55 +0100257 rep_argv[i++] = "--sort=mem,sym,dso,symbol_daddr,"
258 "dso_daddr,tlb,locked";
Stephane Eranian028f12e2013-01-24 16:10:38 +0100259
260 for (j = 1; j < argc; j++, i++)
261 rep_argv[i] = argv[j];
262
Arnaldo Carvalho de Melob0ad8ea2017-03-27 11:47:20 -0300263 ret = cmd_report(i, rep_argv);
Stephane Eranian028f12e2013-01-24 16:10:38 +0100264 free(rep_argv);
265 return ret;
266}
267
Stephane Eranian67121f82014-12-17 16:23:55 +0100268struct mem_mode {
269 const char *name;
270 int mode;
271};
272
273#define MEM_OPT(n, m) \
274 { .name = n, .mode = (m) }
275
276#define MEM_END { .name = NULL }
277
278static const struct mem_mode mem_modes[]={
279 MEM_OPT("load", MEM_OPERATION_LOAD),
280 MEM_OPT("store", MEM_OPERATION_STORE),
281 MEM_END
282};
283
284static int
285parse_mem_ops(const struct option *opt, const char *str, int unset)
286{
287 int *mode = (int *)opt->value;
288 const struct mem_mode *m;
289 char *s, *os = NULL, *p;
290 int ret = -1;
291
292 if (unset)
293 return 0;
294
295 /* str may be NULL in case no arg is passed to -t */
296 if (str) {
297 /* because str is read-only */
298 s = os = strdup(str);
299 if (!s)
300 return -1;
301
302 /* reset mode */
303 *mode = 0;
304
305 for (;;) {
306 p = strchr(s, ',');
307 if (p)
308 *p = '\0';
309
310 for (m = mem_modes; m->name; m++) {
311 if (!strcasecmp(s, m->name))
312 break;
313 }
314 if (!m->name) {
315 fprintf(stderr, "unknown sampling op %s,"
316 " check man page\n", s);
317 goto error;
318 }
319
320 *mode |= m->mode;
321
322 if (!p)
323 break;
324
325 s = p + 1;
326 }
327 }
328 ret = 0;
329
330 if (*mode == 0)
331 *mode = MEM_OPERATION_LOAD;
332error:
333 free(os);
334 return ret;
335}
336
Arnaldo Carvalho de Melob0ad8ea2017-03-27 11:47:20 -0300337int cmd_mem(int argc, const char **argv)
Stephane Eranian028f12e2013-01-24 16:10:38 +0100338{
339 struct stat st;
340 struct perf_mem mem = {
341 .tool = {
342 .sample = process_sample_event,
343 .mmap = perf_event__process_mmap,
Stephane Eranian5c5e8542013-08-21 12:10:25 +0200344 .mmap2 = perf_event__process_mmap2,
Stephane Eranian028f12e2013-01-24 16:10:38 +0100345 .comm = perf_event__process_comm,
346 .lost = perf_event__process_lost,
347 .fork = perf_event__process_fork,
348 .build_id = perf_event__process_build_id,
Hari Bathinif3b36142017-03-08 02:11:43 +0530349 .namespaces = perf_event__process_namespaces,
Jiri Olsa0a8cb852014-07-06 14:18:21 +0200350 .ordered_events = true,
Stephane Eranian028f12e2013-01-24 16:10:38 +0100351 },
352 .input_name = "perf.data",
Arnaldo Carvalho de Melo66024122014-12-17 13:53:27 -0300353 /*
354 * default to both load an store sampling
355 */
356 .operation = MEM_OPERATION_LOAD | MEM_OPERATION_STORE,
Stephane Eranian028f12e2013-01-24 16:10:38 +0100357 };
358 const struct option mem_options[] = {
Arnaldo Carvalho de Melo66024122014-12-17 13:53:27 -0300359 OPT_CALLBACK('t', "type", &mem.operation,
Stephane Eranian67121f82014-12-17 16:23:55 +0100360 "type", "memory operations(load,store) Default load,store",
361 parse_mem_ops),
Stephane Eranian028f12e2013-01-24 16:10:38 +0100362 OPT_BOOLEAN('D', "dump-raw-samples", &mem.dump_raw,
363 "dump raw samples in ASCII"),
364 OPT_BOOLEAN('U', "hide-unresolved", &mem.hide_unresolved,
365 "Only display entries resolved to a symbol"),
366 OPT_STRING('i', "input", &input_name, "file",
367 "input file name"),
368 OPT_STRING('C', "cpu", &mem.cpu_list, "cpu",
369 "list of cpus to profile"),
Wang Nan8b8ca6e2015-03-20 02:57:52 +0000370 OPT_STRING_NOEMPTY('x', "field-separator", &symbol_conf.field_sep,
Stephane Eranian028f12e2013-01-24 16:10:38 +0100371 "separator",
372 "separator for columns, no spaces will be added"
373 " between columns '.' is reserved."),
Yunlong Song62a1a632015-04-02 21:47:15 +0800374 OPT_BOOLEAN('f', "force", &mem.force, "don't complain, do it"),
Stephane Eranian028f12e2013-01-24 16:10:38 +0100375 OPT_END()
376 };
Ramkumar Ramachandra8d2a2a12014-03-14 23:17:52 -0400377 const char *const mem_subcommands[] = { "record", "report", NULL };
378 const char *mem_usage[] = {
379 NULL,
380 NULL
381 };
Stephane Eranian028f12e2013-01-24 16:10:38 +0100382
Jiri Olsa54fbad52016-02-24 09:46:42 +0100383 if (perf_mem_events__init()) {
384 pr_err("failed: memory events not supported\n");
385 return -1;
386 }
387
Ramkumar Ramachandra8d2a2a12014-03-14 23:17:52 -0400388 argc = parse_options_subcommand(argc, argv, mem_options, mem_subcommands,
389 mem_usage, PARSE_OPT_STOP_AT_NON_OPTION);
Stephane Eranian028f12e2013-01-24 16:10:38 +0100390
Arnaldo Carvalho de Melo66024122014-12-17 13:53:27 -0300391 if (!argc || !(strncmp(argv[0], "rec", 3) || mem.operation))
Stephane Eranian028f12e2013-01-24 16:10:38 +0100392 usage_with_options(mem_usage, mem_options);
393
394 if (!mem.input_name || !strlen(mem.input_name)) {
395 if (!fstat(STDIN_FILENO, &st) && S_ISFIFO(st.st_mode))
396 mem.input_name = "-";
397 else
398 mem.input_name = "perf.data";
399 }
400
401 if (!strncmp(argv[0], "rec", 3))
Arnaldo Carvalho de Melo66024122014-12-17 13:53:27 -0300402 return __cmd_record(argc, argv, &mem);
Stephane Eranian028f12e2013-01-24 16:10:38 +0100403 else if (!strncmp(argv[0], "rep", 3))
404 return report_events(argc, argv, &mem);
405 else
406 usage_with_options(mem_usage, mem_options);
407
408 return 0;
409}