blob: b3f8a89ede40af9e07ac12724f853af2095aa2b7 [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 Olsaf5fc14122013-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
43 fprintf(stderr, "%-20s%s",
44 e->tag, verbose ? "" : "\n");
45 if (verbose)
46 fprintf(stderr, " [%s]\n", e->name);
47 }
48 exit(0);
49}
50
51static const char * const __usage[] = {
52 "perf mem record [<options>] [<command>]",
53 "perf mem record [<options>] -- <command> [<options>]",
54 NULL
55};
56
57static const char * const *record_mem_usage = __usage;
58
Arnaldo Carvalho de Melo66024122014-12-17 13:53:27 -030059static int __cmd_record(int argc, const char **argv, struct perf_mem *mem)
Stephane Eranian028f12e2013-01-24 16:10:38 +010060{
61 int rec_argc, i = 0, j;
62 const char **rec_argv;
Stephane Eranian028f12e2013-01-24 16:10:38 +010063 int ret;
Jiri Olsace1e22b2016-02-15 09:34:35 +010064 struct option options[] = {
65 OPT_CALLBACK('e', "event", &mem, "event",
66 "event selector. use 'perf mem record -e list' to list available events",
67 parse_record_events),
68 OPT_INCR('v', "verbose", &verbose,
69 "be more verbose (show counter open errors, etc)"),
70 OPT_END()
71 };
72
73 argc = parse_options(argc, argv, options, record_mem_usage,
74 PARSE_OPT_STOP_AT_NON_OPTION);
Stephane Eranian028f12e2013-01-24 16:10:38 +010075
Stephane Eranian67121f82014-12-17 16:23:55 +010076 rec_argc = argc + 7; /* max number of arguments */
Stephane Eranian028f12e2013-01-24 16:10:38 +010077 rec_argv = calloc(rec_argc + 1, sizeof(char *));
78 if (!rec_argv)
79 return -1;
80
Stephane Eranian67121f82014-12-17 16:23:55 +010081 rec_argv[i++] = "record";
Stephane Eranian028f12e2013-01-24 16:10:38 +010082
Jiri Olsace1e22b2016-02-15 09:34:35 +010083 if (mem->operation & MEM_OPERATION_LOAD)
Jiri Olsaacbe6132016-02-15 09:34:34 +010084 perf_mem_events[PERF_MEM_EVENTS__LOAD].record = true;
Jiri Olsace1e22b2016-02-15 09:34:35 +010085
86 if (perf_mem_events[PERF_MEM_EVENTS__LOAD].record)
Stephane Eranian67121f82014-12-17 16:23:55 +010087 rec_argv[i++] = "-W";
Stephane Eranian028f12e2013-01-24 16:10:38 +010088
Stephane Eranian67121f82014-12-17 16:23:55 +010089 rec_argv[i++] = "-d";
90
Jiri Olsaacbe6132016-02-15 09:34:34 +010091 for (j = 0; j < PERF_MEM_EVENTS__MAX; j++) {
92 if (!perf_mem_events[j].record)
93 continue;
Stephane Eranian67121f82014-12-17 16:23:55 +010094
Stephane Eranian67121f82014-12-17 16:23:55 +010095 rec_argv[i++] = "-e";
Jiri Olsaacbe6132016-02-15 09:34:34 +010096 rec_argv[i++] = perf_mem_events[j].name;
97 };
Stephane Eranian67121f82014-12-17 16:23:55 +010098
Jiri Olsace1e22b2016-02-15 09:34:35 +010099 for (j = 0; j < argc; j++, i++)
Stephane Eranian028f12e2013-01-24 16:10:38 +0100100 rec_argv[i] = argv[j];
101
Jiri Olsace1e22b2016-02-15 09:34:35 +0100102 if (verbose > 0) {
103 pr_debug("calling: record ");
104
105 while (rec_argv[j]) {
106 pr_debug("%s ", rec_argv[j]);
107 j++;
108 }
109 pr_debug("\n");
110 }
111
Stephane Eranian028f12e2013-01-24 16:10:38 +0100112 ret = cmd_record(i, rec_argv, NULL);
113 free(rec_argv);
114 return ret;
115}
116
117static int
118dump_raw_samples(struct perf_tool *tool,
119 union perf_event *event,
120 struct perf_sample *sample,
Stephane Eranian028f12e2013-01-24 16:10:38 +0100121 struct machine *machine)
122{
123 struct perf_mem *mem = container_of(tool, struct perf_mem, tool);
124 struct addr_location al;
125 const char *fmt;
126
Adrian Huntere44baa32013-08-08 14:32:25 +0300127 if (perf_event__preprocess_sample(event, machine, &al, sample) < 0) {
Stephane Eranian028f12e2013-01-24 16:10:38 +0100128 fprintf(stderr, "problem processing %d event, skipping it.\n",
129 event->header.type);
130 return -1;
131 }
132
133 if (al.filtered || (mem->hide_unresolved && al.sym == NULL))
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -0300134 goto out_put;
Stephane Eranian028f12e2013-01-24 16:10:38 +0100135
136 if (al.map != NULL)
137 al.map->dso->hit = 1;
138
139 if (symbol_conf.field_sep) {
140 fmt = "%d%s%d%s0x%"PRIx64"%s0x%"PRIx64"%s%"PRIu64
141 "%s0x%"PRIx64"%s%s:%s\n";
142 } else {
143 fmt = "%5d%s%5d%s0x%016"PRIx64"%s0x016%"PRIx64
144 "%s%5"PRIu64"%s0x%06"PRIx64"%s%s:%s\n";
145 symbol_conf.field_sep = " ";
146 }
147
148 printf(fmt,
149 sample->pid,
150 symbol_conf.field_sep,
151 sample->tid,
152 symbol_conf.field_sep,
Adrian Hunteref893252013-08-27 11:23:06 +0300153 sample->ip,
Stephane Eranian028f12e2013-01-24 16:10:38 +0100154 symbol_conf.field_sep,
155 sample->addr,
156 symbol_conf.field_sep,
157 sample->weight,
158 symbol_conf.field_sep,
159 sample->data_src,
160 symbol_conf.field_sep,
161 al.map ? (al.map->dso ? al.map->dso->long_name : "???") : "???",
162 al.sym ? al.sym->name : "???");
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -0300163out_put:
164 addr_location__put(&al);
Stephane Eranian028f12e2013-01-24 16:10:38 +0100165 return 0;
166}
167
168static int process_sample_event(struct perf_tool *tool,
169 union perf_event *event,
170 struct perf_sample *sample,
Arnaldo Carvalho de Melo8b640cc2013-12-19 17:00:45 -0300171 struct perf_evsel *evsel __maybe_unused,
Stephane Eranian028f12e2013-01-24 16:10:38 +0100172 struct machine *machine)
173{
Arnaldo Carvalho de Melo8b640cc2013-12-19 17:00:45 -0300174 return dump_raw_samples(tool, event, sample, machine);
Stephane Eranian028f12e2013-01-24 16:10:38 +0100175}
176
177static int report_raw_events(struct perf_mem *mem)
178{
Jiri Olsaf5fc14122013-10-15 16:27:32 +0200179 struct perf_data_file file = {
180 .path = input_name,
181 .mode = PERF_DATA_MODE_READ,
Yunlong Song62a1a632015-04-02 21:47:15 +0800182 .force = mem->force,
Jiri Olsaf5fc14122013-10-15 16:27:32 +0200183 };
Stephane Eranian028f12e2013-01-24 16:10:38 +0100184 int ret;
Jiri Olsaf5fc14122013-10-15 16:27:32 +0200185 struct perf_session *session = perf_session__new(&file, false,
186 &mem->tool);
Stephane Eranian028f12e2013-01-24 16:10:38 +0100187
188 if (session == NULL)
Taeung Song52e028342014-09-24 10:33:37 +0900189 return -1;
Stephane Eranian028f12e2013-01-24 16:10:38 +0100190
191 if (mem->cpu_list) {
192 ret = perf_session__cpu_bitmap(session, mem->cpu_list,
193 mem->cpu_bitmap);
Taeung Song1df9fade2015-07-01 21:08:19 +0900194 if (ret < 0)
Stephane Eranian028f12e2013-01-24 16:10:38 +0100195 goto out_delete;
196 }
197
Taeung Song1df9fade2015-07-01 21:08:19 +0900198 ret = symbol__init(&session->header.env);
199 if (ret < 0)
200 goto out_delete;
Stephane Eranian028f12e2013-01-24 16:10:38 +0100201
202 printf("# PID, TID, IP, ADDR, LOCAL WEIGHT, DSRC, SYMBOL\n");
203
Taeung Song1df9fade2015-07-01 21:08:19 +0900204 ret = perf_session__process_events(session);
Stephane Eranian028f12e2013-01-24 16:10:38 +0100205
206out_delete:
207 perf_session__delete(session);
Taeung Song1df9fade2015-07-01 21:08:19 +0900208 return ret;
Stephane Eranian028f12e2013-01-24 16:10:38 +0100209}
210
211static int report_events(int argc, const char **argv, struct perf_mem *mem)
212{
213 const char **rep_argv;
214 int ret, i = 0, j, rep_argc;
215
216 if (mem->dump_raw)
217 return report_raw_events(mem);
218
219 rep_argc = argc + 3;
220 rep_argv = calloc(rep_argc + 1, sizeof(char *));
221 if (!rep_argv)
222 return -1;
223
Stephane Eranian67121f82014-12-17 16:23:55 +0100224 rep_argv[i++] = "report";
225 rep_argv[i++] = "--mem-mode";
226 rep_argv[i++] = "-n"; /* display number of samples */
Stephane Eranian028f12e2013-01-24 16:10:38 +0100227
228 /*
229 * there is no weight (cost) associated with stores, so don't print
230 * the column
231 */
Arnaldo Carvalho de Melo66024122014-12-17 13:53:27 -0300232 if (!(mem->operation & MEM_OPERATION_LOAD))
Stephane Eranian67121f82014-12-17 16:23:55 +0100233 rep_argv[i++] = "--sort=mem,sym,dso,symbol_daddr,"
234 "dso_daddr,tlb,locked";
Stephane Eranian028f12e2013-01-24 16:10:38 +0100235
236 for (j = 1; j < argc; j++, i++)
237 rep_argv[i] = argv[j];
238
239 ret = cmd_report(i, rep_argv, NULL);
240 free(rep_argv);
241 return ret;
242}
243
Stephane Eranian67121f82014-12-17 16:23:55 +0100244struct mem_mode {
245 const char *name;
246 int mode;
247};
248
249#define MEM_OPT(n, m) \
250 { .name = n, .mode = (m) }
251
252#define MEM_END { .name = NULL }
253
254static const struct mem_mode mem_modes[]={
255 MEM_OPT("load", MEM_OPERATION_LOAD),
256 MEM_OPT("store", MEM_OPERATION_STORE),
257 MEM_END
258};
259
260static int
261parse_mem_ops(const struct option *opt, const char *str, int unset)
262{
263 int *mode = (int *)opt->value;
264 const struct mem_mode *m;
265 char *s, *os = NULL, *p;
266 int ret = -1;
267
268 if (unset)
269 return 0;
270
271 /* str may be NULL in case no arg is passed to -t */
272 if (str) {
273 /* because str is read-only */
274 s = os = strdup(str);
275 if (!s)
276 return -1;
277
278 /* reset mode */
279 *mode = 0;
280
281 for (;;) {
282 p = strchr(s, ',');
283 if (p)
284 *p = '\0';
285
286 for (m = mem_modes; m->name; m++) {
287 if (!strcasecmp(s, m->name))
288 break;
289 }
290 if (!m->name) {
291 fprintf(stderr, "unknown sampling op %s,"
292 " check man page\n", s);
293 goto error;
294 }
295
296 *mode |= m->mode;
297
298 if (!p)
299 break;
300
301 s = p + 1;
302 }
303 }
304 ret = 0;
305
306 if (*mode == 0)
307 *mode = MEM_OPERATION_LOAD;
308error:
309 free(os);
310 return ret;
311}
312
Stephane Eranian028f12e2013-01-24 16:10:38 +0100313int cmd_mem(int argc, const char **argv, const char *prefix __maybe_unused)
314{
315 struct stat st;
316 struct perf_mem mem = {
317 .tool = {
318 .sample = process_sample_event,
319 .mmap = perf_event__process_mmap,
Stephane Eranian5c5e8542013-08-21 12:10:25 +0200320 .mmap2 = perf_event__process_mmap2,
Stephane Eranian028f12e2013-01-24 16:10:38 +0100321 .comm = perf_event__process_comm,
322 .lost = perf_event__process_lost,
323 .fork = perf_event__process_fork,
324 .build_id = perf_event__process_build_id,
Jiri Olsa0a8cb852014-07-06 14:18:21 +0200325 .ordered_events = true,
Stephane Eranian028f12e2013-01-24 16:10:38 +0100326 },
327 .input_name = "perf.data",
Arnaldo Carvalho de Melo66024122014-12-17 13:53:27 -0300328 /*
329 * default to both load an store sampling
330 */
331 .operation = MEM_OPERATION_LOAD | MEM_OPERATION_STORE,
Stephane Eranian028f12e2013-01-24 16:10:38 +0100332 };
333 const struct option mem_options[] = {
Arnaldo Carvalho de Melo66024122014-12-17 13:53:27 -0300334 OPT_CALLBACK('t', "type", &mem.operation,
Stephane Eranian67121f82014-12-17 16:23:55 +0100335 "type", "memory operations(load,store) Default load,store",
336 parse_mem_ops),
Stephane Eranian028f12e2013-01-24 16:10:38 +0100337 OPT_BOOLEAN('D', "dump-raw-samples", &mem.dump_raw,
338 "dump raw samples in ASCII"),
339 OPT_BOOLEAN('U', "hide-unresolved", &mem.hide_unresolved,
340 "Only display entries resolved to a symbol"),
341 OPT_STRING('i', "input", &input_name, "file",
342 "input file name"),
343 OPT_STRING('C', "cpu", &mem.cpu_list, "cpu",
344 "list of cpus to profile"),
Wang Nan8b8ca6e2015-03-20 02:57:52 +0000345 OPT_STRING_NOEMPTY('x', "field-separator", &symbol_conf.field_sep,
Stephane Eranian028f12e2013-01-24 16:10:38 +0100346 "separator",
347 "separator for columns, no spaces will be added"
348 " between columns '.' is reserved."),
Yunlong Song62a1a632015-04-02 21:47:15 +0800349 OPT_BOOLEAN('f', "force", &mem.force, "don't complain, do it"),
Stephane Eranian028f12e2013-01-24 16:10:38 +0100350 OPT_END()
351 };
Ramkumar Ramachandra8d2a2a12014-03-14 23:17:52 -0400352 const char *const mem_subcommands[] = { "record", "report", NULL };
353 const char *mem_usage[] = {
354 NULL,
355 NULL
356 };
Stephane Eranian028f12e2013-01-24 16:10:38 +0100357
Ramkumar Ramachandra8d2a2a12014-03-14 23:17:52 -0400358 argc = parse_options_subcommand(argc, argv, mem_options, mem_subcommands,
359 mem_usage, PARSE_OPT_STOP_AT_NON_OPTION);
Stephane Eranian028f12e2013-01-24 16:10:38 +0100360
Arnaldo Carvalho de Melo66024122014-12-17 13:53:27 -0300361 if (!argc || !(strncmp(argv[0], "rec", 3) || mem.operation))
Stephane Eranian028f12e2013-01-24 16:10:38 +0100362 usage_with_options(mem_usage, mem_options);
363
364 if (!mem.input_name || !strlen(mem.input_name)) {
365 if (!fstat(STDIN_FILENO, &st) && S_ISFIFO(st.st_mode))
366 mem.input_name = "-";
367 else
368 mem.input_name = "perf.data";
369 }
370
371 if (!strncmp(argv[0], "rec", 3))
Arnaldo Carvalho de Melo66024122014-12-17 13:53:27 -0300372 return __cmd_record(argc, argv, &mem);
Stephane Eranian028f12e2013-01-24 16:10:38 +0100373 else if (!strncmp(argv[0], "rep", 3))
374 return report_events(argc, argv, &mem);
375 else
376 usage_with_options(mem_usage, mem_options);
377
378 return 0;
379}