blob: e001c02907937792d373f5245be0e6489b3e5210 [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"
Arnaldo Carvalho de Meloe7ff8922017-04-19 21:34:35 -030015#include "util/symbol.h"
Stephane Eranian028f12e2013-01-24 16:10:38 +010016
Stephane Eranian67121f82014-12-17 16:23:55 +010017#define MEM_OPERATION_LOAD 0x1
18#define MEM_OPERATION_STORE 0x2
Stephane Eranian028f12e2013-01-24 16:10:38 +010019
Stephane Eranian028f12e2013-01-24 16:10:38 +010020struct perf_mem {
21 struct perf_tool tool;
22 char const *input_name;
Stephane Eranian028f12e2013-01-24 16:10:38 +010023 bool hide_unresolved;
24 bool dump_raw;
Yunlong Song62a1a632015-04-02 21:47:15 +080025 bool force;
Arnaldo Carvalho de Melo66024122014-12-17 13:53:27 -030026 int operation;
Stephane Eranian028f12e2013-01-24 16:10:38 +010027 const char *cpu_list;
28 DECLARE_BITMAP(cpu_bitmap, MAX_NR_CPUS);
29};
30
Jiri Olsace1e22b2016-02-15 09:34:35 +010031static 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
Jiri Olsa54fbad52016-02-24 09:46:42 +010048 fprintf(stderr, "%-13s%-*s%s\n",
49 e->tag,
Namhyung Kimbb963e12017-02-17 17:17:38 +090050 verbose > 0 ? 25 : 0,
51 verbose > 0 ? perf_mem_events__name(j) : "",
Jiri Olsa54fbad52016-02-24 09:46:42 +010052 e->supported ? ": available" : "");
Jiri Olsace1e22b2016-02-15 09:34:35 +010053 }
54 exit(0);
55}
56
57static const char * const __usage[] = {
58 "perf mem record [<options>] [<command>]",
59 "perf mem record [<options>] -- <command> [<options>]",
60 NULL
61};
62
63static const char * const *record_mem_usage = __usage;
64
Arnaldo Carvalho de Melo66024122014-12-17 13:53:27 -030065static int __cmd_record(int argc, const char **argv, struct perf_mem *mem)
Stephane Eranian028f12e2013-01-24 16:10:38 +010066{
67 int rec_argc, i = 0, j;
68 const char **rec_argv;
Stephane Eranian028f12e2013-01-24 16:10:38 +010069 int ret;
Jiri Olsaad165112016-03-24 13:52:16 +010070 bool all_user = false, all_kernel = false;
Jiri Olsace1e22b2016-02-15 09:34:35 +010071 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),
Jiri Olsab0d745b2016-06-14 20:19:11 +020075 OPT_UINTEGER(0, "ldlat", &perf_mem_events__loads_ldlat, "mem-loads latency"),
Jiri Olsace1e22b2016-02-15 09:34:35 +010076 OPT_INCR('v', "verbose", &verbose,
77 "be more verbose (show counter open errors, etc)"),
Jiri Olsa631ac412016-12-12 11:35:39 +010078 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"),
Jiri Olsace1e22b2016-02-15 09:34:35 +010080 OPT_END()
81 };
82
83 argc = parse_options(argc, argv, options, record_mem_usage,
84 PARSE_OPT_STOP_AT_NON_OPTION);
Stephane Eranian028f12e2013-01-24 16:10:38 +010085
Jiri Olsaad165112016-03-24 13:52:16 +010086 rec_argc = argc + 9; /* max number of arguments */
Stephane Eranian028f12e2013-01-24 16:10:38 +010087 rec_argv = calloc(rec_argc + 1, sizeof(char *));
88 if (!rec_argv)
89 return -1;
90
Stephane Eranian67121f82014-12-17 16:23:55 +010091 rec_argv[i++] = "record";
Stephane Eranian028f12e2013-01-24 16:10:38 +010092
Jiri Olsace1e22b2016-02-15 09:34:35 +010093 if (mem->operation & MEM_OPERATION_LOAD)
Jiri Olsaacbe6132016-02-15 09:34:34 +010094 perf_mem_events[PERF_MEM_EVENTS__LOAD].record = true;
Jiri Olsace1e22b2016-02-15 09:34:35 +010095
Jiri Olsa33da54f2016-08-11 10:50:57 +020096 if (mem->operation & MEM_OPERATION_STORE)
97 perf_mem_events[PERF_MEM_EVENTS__STORE].record = true;
98
Jiri Olsace1e22b2016-02-15 09:34:35 +010099 if (perf_mem_events[PERF_MEM_EVENTS__LOAD].record)
Stephane Eranian67121f82014-12-17 16:23:55 +0100100 rec_argv[i++] = "-W";
Stephane Eranian028f12e2013-01-24 16:10:38 +0100101
Stephane Eranian67121f82014-12-17 16:23:55 +0100102 rec_argv[i++] = "-d";
103
Jiri Olsaacbe6132016-02-15 09:34:34 +0100104 for (j = 0; j < PERF_MEM_EVENTS__MAX; j++) {
105 if (!perf_mem_events[j].record)
106 continue;
Stephane Eranian67121f82014-12-17 16:23:55 +0100107
Jiri Olsa54fbad52016-02-24 09:46:42 +0100108 if (!perf_mem_events[j].supported) {
109 pr_err("failed: event '%s' not supported\n",
Jiri Olsa2ba7ac52016-02-24 09:46:43 +0100110 perf_mem_events__name(j));
Jiri Olsa54fbad52016-02-24 09:46:42 +0100111 return -1;
112 }
113
Stephane Eranian67121f82014-12-17 16:23:55 +0100114 rec_argv[i++] = "-e";
Jiri Olsa2ba7ac52016-02-24 09:46:43 +0100115 rec_argv[i++] = perf_mem_events__name(j);
Jiri Olsaacbe6132016-02-15 09:34:34 +0100116 };
Stephane Eranian67121f82014-12-17 16:23:55 +0100117
Jiri Olsaad165112016-03-24 13:52:16 +0100118 if (all_user)
119 rec_argv[i++] = "--all-user";
120
121 if (all_kernel)
122 rec_argv[i++] = "--all-kernel";
123
Jiri Olsace1e22b2016-02-15 09:34:35 +0100124 for (j = 0; j < argc; j++, i++)
Stephane Eranian028f12e2013-01-24 16:10:38 +0100125 rec_argv[i] = argv[j];
126
Jiri Olsace1e22b2016-02-15 09:34:35 +0100127 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
Arnaldo Carvalho de Melob0ad8ea2017-03-27 11:47:20 -0300137 ret = cmd_record(i, rec_argv);
Stephane Eranian028f12e2013-01-24 16:10:38 +0100138 free(rec_argv);
139 return ret;
140}
141
142static int
143dump_raw_samples(struct perf_tool *tool,
144 union perf_event *event,
145 struct perf_sample *sample,
Stephane Eranian028f12e2013-01-24 16:10:38 +0100146 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
Arnaldo Carvalho de Melobb3eb562016-03-22 18:39:09 -0300152 if (machine__resolve(machine, &al, sample) < 0) {
Stephane Eranian028f12e2013-01-24 16:10:38 +0100153 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))
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -0300159 goto out_put;
Stephane Eranian028f12e2013-01-24 16:10:38 +0100160
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,
Adrian Hunteref893252013-08-27 11:23:06 +0300178 sample->ip,
Stephane Eranian028f12e2013-01-24 16:10:38 +0100179 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 : "???");
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -0300188out_put:
189 addr_location__put(&al);
Stephane Eranian028f12e2013-01-24 16:10:38 +0100190 return 0;
191}
192
193static int process_sample_event(struct perf_tool *tool,
194 union perf_event *event,
195 struct perf_sample *sample,
Arnaldo Carvalho de Melo8b640cc2013-12-19 17:00:45 -0300196 struct perf_evsel *evsel __maybe_unused,
Stephane Eranian028f12e2013-01-24 16:10:38 +0100197 struct machine *machine)
198{
Arnaldo Carvalho de Melo8b640cc2013-12-19 17:00:45 -0300199 return dump_raw_samples(tool, event, sample, machine);
Stephane Eranian028f12e2013-01-24 16:10:38 +0100200}
201
202static int report_raw_events(struct perf_mem *mem)
203{
Jiri Olsaf5fc1412013-10-15 16:27:32 +0200204 struct perf_data_file file = {
205 .path = input_name,
206 .mode = PERF_DATA_MODE_READ,
Yunlong Song62a1a632015-04-02 21:47:15 +0800207 .force = mem->force,
Jiri Olsaf5fc1412013-10-15 16:27:32 +0200208 };
Stephane Eranian028f12e2013-01-24 16:10:38 +0100209 int ret;
Jiri Olsaf5fc1412013-10-15 16:27:32 +0200210 struct perf_session *session = perf_session__new(&file, false,
211 &mem->tool);
Stephane Eranian028f12e2013-01-24 16:10:38 +0100212
213 if (session == NULL)
Taeung Song52e028342014-09-24 10:33:37 +0900214 return -1;
Stephane Eranian028f12e2013-01-24 16:10:38 +0100215
216 if (mem->cpu_list) {
217 ret = perf_session__cpu_bitmap(session, mem->cpu_list,
218 mem->cpu_bitmap);
Taeung Song1df9fade2015-07-01 21:08:19 +0900219 if (ret < 0)
Stephane Eranian028f12e2013-01-24 16:10:38 +0100220 goto out_delete;
221 }
222
Taeung Song1df9fade2015-07-01 21:08:19 +0900223 ret = symbol__init(&session->header.env);
224 if (ret < 0)
225 goto out_delete;
Stephane Eranian028f12e2013-01-24 16:10:38 +0100226
227 printf("# PID, TID, IP, ADDR, LOCAL WEIGHT, DSRC, SYMBOL\n");
228
Taeung Song1df9fade2015-07-01 21:08:19 +0900229 ret = perf_session__process_events(session);
Stephane Eranian028f12e2013-01-24 16:10:38 +0100230
231out_delete:
232 perf_session__delete(session);
Taeung Song1df9fade2015-07-01 21:08:19 +0900233 return ret;
Stephane Eranian028f12e2013-01-24 16:10:38 +0100234}
235
236static 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
Stephane Eranian67121f82014-12-17 16:23:55 +0100249 rep_argv[i++] = "report";
250 rep_argv[i++] = "--mem-mode";
251 rep_argv[i++] = "-n"; /* display number of samples */
Stephane Eranian028f12e2013-01-24 16:10:38 +0100252
253 /*
254 * there is no weight (cost) associated with stores, so don't print
255 * the column
256 */
Arnaldo Carvalho de Melo66024122014-12-17 13:53:27 -0300257 if (!(mem->operation & MEM_OPERATION_LOAD))
Stephane Eranian67121f82014-12-17 16:23:55 +0100258 rep_argv[i++] = "--sort=mem,sym,dso,symbol_daddr,"
259 "dso_daddr,tlb,locked";
Stephane Eranian028f12e2013-01-24 16:10:38 +0100260
261 for (j = 1; j < argc; j++, i++)
262 rep_argv[i] = argv[j];
263
Arnaldo Carvalho de Melob0ad8ea2017-03-27 11:47:20 -0300264 ret = cmd_report(i, rep_argv);
Stephane Eranian028f12e2013-01-24 16:10:38 +0100265 free(rep_argv);
266 return ret;
267}
268
Stephane Eranian67121f82014-12-17 16:23:55 +0100269struct 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
279static 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
285static int
286parse_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;
333error:
334 free(os);
335 return ret;
336}
337
Arnaldo Carvalho de Melob0ad8ea2017-03-27 11:47:20 -0300338int cmd_mem(int argc, const char **argv)
Stephane Eranian028f12e2013-01-24 16:10:38 +0100339{
340 struct stat st;
341 struct perf_mem mem = {
342 .tool = {
343 .sample = process_sample_event,
344 .mmap = perf_event__process_mmap,
Stephane Eranian5c5e8542013-08-21 12:10:25 +0200345 .mmap2 = perf_event__process_mmap2,
Stephane Eranian028f12e2013-01-24 16:10:38 +0100346 .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,
Hari Bathinif3b36142017-03-08 02:11:43 +0530350 .namespaces = perf_event__process_namespaces,
Jiri Olsa0a8cb852014-07-06 14:18:21 +0200351 .ordered_events = true,
Stephane Eranian028f12e2013-01-24 16:10:38 +0100352 },
353 .input_name = "perf.data",
Arnaldo Carvalho de Melo66024122014-12-17 13:53:27 -0300354 /*
355 * default to both load an store sampling
356 */
357 .operation = MEM_OPERATION_LOAD | MEM_OPERATION_STORE,
Stephane Eranian028f12e2013-01-24 16:10:38 +0100358 };
359 const struct option mem_options[] = {
Arnaldo Carvalho de Melo66024122014-12-17 13:53:27 -0300360 OPT_CALLBACK('t', "type", &mem.operation,
Stephane Eranian67121f82014-12-17 16:23:55 +0100361 "type", "memory operations(load,store) Default load,store",
362 parse_mem_ops),
Stephane Eranian028f12e2013-01-24 16:10:38 +0100363 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"),
Wang Nan8b8ca6e2015-03-20 02:57:52 +0000371 OPT_STRING_NOEMPTY('x', "field-separator", &symbol_conf.field_sep,
Stephane Eranian028f12e2013-01-24 16:10:38 +0100372 "separator",
373 "separator for columns, no spaces will be added"
374 " between columns '.' is reserved."),
Yunlong Song62a1a632015-04-02 21:47:15 +0800375 OPT_BOOLEAN('f', "force", &mem.force, "don't complain, do it"),
Stephane Eranian028f12e2013-01-24 16:10:38 +0100376 OPT_END()
377 };
Ramkumar Ramachandra8d2a2a12014-03-14 23:17:52 -0400378 const char *const mem_subcommands[] = { "record", "report", NULL };
379 const char *mem_usage[] = {
380 NULL,
381 NULL
382 };
Stephane Eranian028f12e2013-01-24 16:10:38 +0100383
Jiri Olsa54fbad52016-02-24 09:46:42 +0100384 if (perf_mem_events__init()) {
385 pr_err("failed: memory events not supported\n");
386 return -1;
387 }
388
Ramkumar Ramachandra8d2a2a12014-03-14 23:17:52 -0400389 argc = parse_options_subcommand(argc, argv, mem_options, mem_subcommands,
390 mem_usage, PARSE_OPT_STOP_AT_NON_OPTION);
Stephane Eranian028f12e2013-01-24 16:10:38 +0100391
Arnaldo Carvalho de Melo66024122014-12-17 13:53:27 -0300392 if (!argc || !(strncmp(argv[0], "rec", 3) || mem.operation))
Stephane Eranian028f12e2013-01-24 16:10:38 +0100393 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))
Arnaldo Carvalho de Melo66024122014-12-17 13:53:27 -0300403 return __cmd_record(argc, argv, &mem);
Stephane Eranian028f12e2013-01-24 16:10:38 +0100404 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}