blob: 6940490bc3f96bea7486dfa587eca015819b9aa6 [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 Olsaf5fc14122013-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;
Kan Liangc35aeb92017-08-29 13:11:10 -040026 bool phys_addr;
Arnaldo Carvalho de Melo66024122014-12-17 13:53:27 -030027 int operation;
Stephane Eranian028f12e2013-01-24 16:10:38 +010028 const char *cpu_list;
29 DECLARE_BITMAP(cpu_bitmap, MAX_NR_CPUS);
30};
31
Jiri Olsace1e22b2016-02-15 09:34:35 +010032static int parse_record_events(const struct option *opt,
33 const char *str, int unset __maybe_unused)
34{
35 struct perf_mem *mem = *(struct perf_mem **)opt->value;
36 int j;
37
38 if (strcmp(str, "list")) {
39 if (!perf_mem_events__parse(str)) {
40 mem->operation = 0;
41 return 0;
42 }
43 exit(-1);
44 }
45
46 for (j = 0; j < PERF_MEM_EVENTS__MAX; j++) {
47 struct perf_mem_event *e = &perf_mem_events[j];
48
Jiri Olsa54fbad52016-02-24 09:46:42 +010049 fprintf(stderr, "%-13s%-*s%s\n",
50 e->tag,
Namhyung Kimbb963e12017-02-17 17:17:38 +090051 verbose > 0 ? 25 : 0,
52 verbose > 0 ? perf_mem_events__name(j) : "",
Jiri Olsa54fbad52016-02-24 09:46:42 +010053 e->supported ? ": available" : "");
Jiri Olsace1e22b2016-02-15 09:34:35 +010054 }
55 exit(0);
56}
57
58static const char * const __usage[] = {
59 "perf mem record [<options>] [<command>]",
60 "perf mem record [<options>] -- <command> [<options>]",
61 NULL
62};
63
64static const char * const *record_mem_usage = __usage;
65
Arnaldo Carvalho de Melo66024122014-12-17 13:53:27 -030066static int __cmd_record(int argc, const char **argv, struct perf_mem *mem)
Stephane Eranian028f12e2013-01-24 16:10:38 +010067{
68 int rec_argc, i = 0, j;
69 const char **rec_argv;
Stephane Eranian028f12e2013-01-24 16:10:38 +010070 int ret;
Jiri Olsaad165112016-03-24 13:52:16 +010071 bool all_user = false, all_kernel = false;
Jiri Olsace1e22b2016-02-15 09:34:35 +010072 struct option options[] = {
73 OPT_CALLBACK('e', "event", &mem, "event",
74 "event selector. use 'perf mem record -e list' to list available events",
75 parse_record_events),
Jiri Olsab0d745b2016-06-14 20:19:11 +020076 OPT_UINTEGER(0, "ldlat", &perf_mem_events__loads_ldlat, "mem-loads latency"),
Jiri Olsace1e22b2016-02-15 09:34:35 +010077 OPT_INCR('v', "verbose", &verbose,
78 "be more verbose (show counter open errors, etc)"),
Jiri Olsa631ac412016-12-12 11:35:39 +010079 OPT_BOOLEAN('U', "all-user", &all_user, "collect only user level data"),
80 OPT_BOOLEAN('K', "all-kernel", &all_kernel, "collect only kernel level data"),
Jiri Olsace1e22b2016-02-15 09:34:35 +010081 OPT_END()
82 };
83
84 argc = parse_options(argc, argv, options, record_mem_usage,
85 PARSE_OPT_STOP_AT_NON_OPTION);
Stephane Eranian028f12e2013-01-24 16:10:38 +010086
Jiri Olsaad165112016-03-24 13:52:16 +010087 rec_argc = argc + 9; /* max number of arguments */
Stephane Eranian028f12e2013-01-24 16:10:38 +010088 rec_argv = calloc(rec_argc + 1, sizeof(char *));
89 if (!rec_argv)
90 return -1;
91
Stephane Eranian67121f82014-12-17 16:23:55 +010092 rec_argv[i++] = "record";
Stephane Eranian028f12e2013-01-24 16:10:38 +010093
Jiri Olsace1e22b2016-02-15 09:34:35 +010094 if (mem->operation & MEM_OPERATION_LOAD)
Jiri Olsaacbe6132016-02-15 09:34:34 +010095 perf_mem_events[PERF_MEM_EVENTS__LOAD].record = true;
Jiri Olsace1e22b2016-02-15 09:34:35 +010096
Jiri Olsa33da54f2016-08-11 10:50:57 +020097 if (mem->operation & MEM_OPERATION_STORE)
98 perf_mem_events[PERF_MEM_EVENTS__STORE].record = true;
99
Jiri Olsace1e22b2016-02-15 09:34:35 +0100100 if (perf_mem_events[PERF_MEM_EVENTS__LOAD].record)
Stephane Eranian67121f82014-12-17 16:23:55 +0100101 rec_argv[i++] = "-W";
Stephane Eranian028f12e2013-01-24 16:10:38 +0100102
Stephane Eranian67121f82014-12-17 16:23:55 +0100103 rec_argv[i++] = "-d";
104
Kan Liangc35aeb92017-08-29 13:11:10 -0400105 if (mem->phys_addr)
106 rec_argv[i++] = "--phys-data";
107
Jiri Olsaacbe6132016-02-15 09:34:34 +0100108 for (j = 0; j < PERF_MEM_EVENTS__MAX; j++) {
109 if (!perf_mem_events[j].record)
110 continue;
Stephane Eranian67121f82014-12-17 16:23:55 +0100111
Jiri Olsa54fbad52016-02-24 09:46:42 +0100112 if (!perf_mem_events[j].supported) {
113 pr_err("failed: event '%s' not supported\n",
Jiri Olsa2ba7ac52016-02-24 09:46:43 +0100114 perf_mem_events__name(j));
Martin Kepplingerc896f852017-09-13 21:14:19 +0200115 free(rec_argv);
Jiri Olsa54fbad52016-02-24 09:46:42 +0100116 return -1;
117 }
118
Stephane Eranian67121f82014-12-17 16:23:55 +0100119 rec_argv[i++] = "-e";
Jiri Olsa2ba7ac52016-02-24 09:46:43 +0100120 rec_argv[i++] = perf_mem_events__name(j);
Jiri Olsaacbe6132016-02-15 09:34:34 +0100121 };
Stephane Eranian67121f82014-12-17 16:23:55 +0100122
Jiri Olsaad165112016-03-24 13:52:16 +0100123 if (all_user)
124 rec_argv[i++] = "--all-user";
125
126 if (all_kernel)
127 rec_argv[i++] = "--all-kernel";
128
Jiri Olsace1e22b2016-02-15 09:34:35 +0100129 for (j = 0; j < argc; j++, i++)
Stephane Eranian028f12e2013-01-24 16:10:38 +0100130 rec_argv[i] = argv[j];
131
Jiri Olsace1e22b2016-02-15 09:34:35 +0100132 if (verbose > 0) {
133 pr_debug("calling: record ");
134
135 while (rec_argv[j]) {
136 pr_debug("%s ", rec_argv[j]);
137 j++;
138 }
139 pr_debug("\n");
140 }
141
Arnaldo Carvalho de Melob0ad8ea2017-03-27 11:47:20 -0300142 ret = cmd_record(i, rec_argv);
Stephane Eranian028f12e2013-01-24 16:10:38 +0100143 free(rec_argv);
144 return ret;
145}
146
147static int
148dump_raw_samples(struct perf_tool *tool,
149 union perf_event *event,
150 struct perf_sample *sample,
Stephane Eranian028f12e2013-01-24 16:10:38 +0100151 struct machine *machine)
152{
153 struct perf_mem *mem = container_of(tool, struct perf_mem, tool);
154 struct addr_location al;
155 const char *fmt;
156
Arnaldo Carvalho de Melobb3eb562016-03-22 18:39:09 -0300157 if (machine__resolve(machine, &al, sample) < 0) {
Stephane Eranian028f12e2013-01-24 16:10:38 +0100158 fprintf(stderr, "problem processing %d event, skipping it.\n",
159 event->header.type);
160 return -1;
161 }
162
163 if (al.filtered || (mem->hide_unresolved && al.sym == NULL))
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -0300164 goto out_put;
Stephane Eranian028f12e2013-01-24 16:10:38 +0100165
166 if (al.map != NULL)
167 al.map->dso->hit = 1;
168
Kan Liangc35aeb92017-08-29 13:11:10 -0400169 if (mem->phys_addr) {
170 if (symbol_conf.field_sep) {
171 fmt = "%d%s%d%s0x%"PRIx64"%s0x%"PRIx64"%s0x%016"PRIx64
172 "%s%"PRIu64"%s0x%"PRIx64"%s%s:%s\n";
173 } else {
174 fmt = "%5d%s%5d%s0x%016"PRIx64"%s0x016%"PRIx64
175 "%s0x%016"PRIx64"%s%5"PRIu64"%s0x%06"PRIx64
176 "%s%s:%s\n";
177 symbol_conf.field_sep = " ";
178 }
Stephane Eranian028f12e2013-01-24 16:10:38 +0100179
Kan Liangc35aeb92017-08-29 13:11:10 -0400180 printf(fmt,
181 sample->pid,
182 symbol_conf.field_sep,
183 sample->tid,
184 symbol_conf.field_sep,
185 sample->ip,
186 symbol_conf.field_sep,
187 sample->addr,
188 symbol_conf.field_sep,
189 sample->phys_addr,
190 symbol_conf.field_sep,
191 sample->weight,
192 symbol_conf.field_sep,
193 sample->data_src,
194 symbol_conf.field_sep,
195 al.map ? (al.map->dso ? al.map->dso->long_name : "???") : "???",
196 al.sym ? al.sym->name : "???");
197 } else {
198 if (symbol_conf.field_sep) {
199 fmt = "%d%s%d%s0x%"PRIx64"%s0x%"PRIx64"%s%"PRIu64
200 "%s0x%"PRIx64"%s%s:%s\n";
201 } else {
202 fmt = "%5d%s%5d%s0x%016"PRIx64"%s0x016%"PRIx64
203 "%s%5"PRIu64"%s0x%06"PRIx64"%s%s:%s\n";
204 symbol_conf.field_sep = " ";
205 }
206
207 printf(fmt,
208 sample->pid,
209 symbol_conf.field_sep,
210 sample->tid,
211 symbol_conf.field_sep,
212 sample->ip,
213 symbol_conf.field_sep,
214 sample->addr,
215 symbol_conf.field_sep,
216 sample->weight,
217 symbol_conf.field_sep,
218 sample->data_src,
219 symbol_conf.field_sep,
220 al.map ? (al.map->dso ? al.map->dso->long_name : "???") : "???",
221 al.sym ? al.sym->name : "???");
222 }
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -0300223out_put:
224 addr_location__put(&al);
Stephane Eranian028f12e2013-01-24 16:10:38 +0100225 return 0;
226}
227
228static int process_sample_event(struct perf_tool *tool,
229 union perf_event *event,
230 struct perf_sample *sample,
Arnaldo Carvalho de Melo8b640cc2013-12-19 17:00:45 -0300231 struct perf_evsel *evsel __maybe_unused,
Stephane Eranian028f12e2013-01-24 16:10:38 +0100232 struct machine *machine)
233{
Arnaldo Carvalho de Melo8b640cc2013-12-19 17:00:45 -0300234 return dump_raw_samples(tool, event, sample, machine);
Stephane Eranian028f12e2013-01-24 16:10:38 +0100235}
236
237static int report_raw_events(struct perf_mem *mem)
238{
Jiri Olsaf5fc14122013-10-15 16:27:32 +0200239 struct perf_data_file file = {
240 .path = input_name,
241 .mode = PERF_DATA_MODE_READ,
Yunlong Song62a1a632015-04-02 21:47:15 +0800242 .force = mem->force,
Jiri Olsaf5fc14122013-10-15 16:27:32 +0200243 };
Stephane Eranian028f12e2013-01-24 16:10:38 +0100244 int ret;
Jiri Olsaf5fc14122013-10-15 16:27:32 +0200245 struct perf_session *session = perf_session__new(&file, false,
246 &mem->tool);
Stephane Eranian028f12e2013-01-24 16:10:38 +0100247
248 if (session == NULL)
Taeung Song52e028342014-09-24 10:33:37 +0900249 return -1;
Stephane Eranian028f12e2013-01-24 16:10:38 +0100250
251 if (mem->cpu_list) {
252 ret = perf_session__cpu_bitmap(session, mem->cpu_list,
253 mem->cpu_bitmap);
Taeung Song1df9fade2015-07-01 21:08:19 +0900254 if (ret < 0)
Stephane Eranian028f12e2013-01-24 16:10:38 +0100255 goto out_delete;
256 }
257
Taeung Song1df9fade2015-07-01 21:08:19 +0900258 ret = symbol__init(&session->header.env);
259 if (ret < 0)
260 goto out_delete;
Stephane Eranian028f12e2013-01-24 16:10:38 +0100261
Kan Liangc35aeb92017-08-29 13:11:10 -0400262 if (mem->phys_addr)
263 printf("# PID, TID, IP, ADDR, PHYS ADDR, LOCAL WEIGHT, DSRC, SYMBOL\n");
264 else
265 printf("# PID, TID, IP, ADDR, LOCAL WEIGHT, DSRC, SYMBOL\n");
Stephane Eranian028f12e2013-01-24 16:10:38 +0100266
Taeung Song1df9fade2015-07-01 21:08:19 +0900267 ret = perf_session__process_events(session);
Stephane Eranian028f12e2013-01-24 16:10:38 +0100268
269out_delete:
270 perf_session__delete(session);
Taeung Song1df9fade2015-07-01 21:08:19 +0900271 return ret;
Stephane Eranian028f12e2013-01-24 16:10:38 +0100272}
273
274static int report_events(int argc, const char **argv, struct perf_mem *mem)
275{
276 const char **rep_argv;
277 int ret, i = 0, j, rep_argc;
278
279 if (mem->dump_raw)
280 return report_raw_events(mem);
281
282 rep_argc = argc + 3;
283 rep_argv = calloc(rep_argc + 1, sizeof(char *));
284 if (!rep_argv)
285 return -1;
286
Stephane Eranian67121f82014-12-17 16:23:55 +0100287 rep_argv[i++] = "report";
288 rep_argv[i++] = "--mem-mode";
289 rep_argv[i++] = "-n"; /* display number of samples */
Stephane Eranian028f12e2013-01-24 16:10:38 +0100290
291 /*
292 * there is no weight (cost) associated with stores, so don't print
293 * the column
294 */
Kan Liangc35aeb92017-08-29 13:11:10 -0400295 if (!(mem->operation & MEM_OPERATION_LOAD)) {
296 if (mem->phys_addr)
297 rep_argv[i++] = "--sort=mem,sym,dso,symbol_daddr,"
298 "dso_daddr,tlb,locked,phys_daddr";
299 else
300 rep_argv[i++] = "--sort=mem,sym,dso,symbol_daddr,"
301 "dso_daddr,tlb,locked";
302 } else if (mem->phys_addr)
303 rep_argv[i++] = "--sort=local_weight,mem,sym,dso,symbol_daddr,"
304 "dso_daddr,snoop,tlb,locked,phys_daddr";
Stephane Eranian028f12e2013-01-24 16:10:38 +0100305
306 for (j = 1; j < argc; j++, i++)
307 rep_argv[i] = argv[j];
308
Arnaldo Carvalho de Melob0ad8ea2017-03-27 11:47:20 -0300309 ret = cmd_report(i, rep_argv);
Stephane Eranian028f12e2013-01-24 16:10:38 +0100310 free(rep_argv);
311 return ret;
312}
313
Stephane Eranian67121f82014-12-17 16:23:55 +0100314struct mem_mode {
315 const char *name;
316 int mode;
317};
318
319#define MEM_OPT(n, m) \
320 { .name = n, .mode = (m) }
321
322#define MEM_END { .name = NULL }
323
324static const struct mem_mode mem_modes[]={
325 MEM_OPT("load", MEM_OPERATION_LOAD),
326 MEM_OPT("store", MEM_OPERATION_STORE),
327 MEM_END
328};
329
330static int
331parse_mem_ops(const struct option *opt, const char *str, int unset)
332{
333 int *mode = (int *)opt->value;
334 const struct mem_mode *m;
335 char *s, *os = NULL, *p;
336 int ret = -1;
337
338 if (unset)
339 return 0;
340
341 /* str may be NULL in case no arg is passed to -t */
342 if (str) {
343 /* because str is read-only */
344 s = os = strdup(str);
345 if (!s)
346 return -1;
347
348 /* reset mode */
349 *mode = 0;
350
351 for (;;) {
352 p = strchr(s, ',');
353 if (p)
354 *p = '\0';
355
356 for (m = mem_modes; m->name; m++) {
357 if (!strcasecmp(s, m->name))
358 break;
359 }
360 if (!m->name) {
361 fprintf(stderr, "unknown sampling op %s,"
362 " check man page\n", s);
363 goto error;
364 }
365
366 *mode |= m->mode;
367
368 if (!p)
369 break;
370
371 s = p + 1;
372 }
373 }
374 ret = 0;
375
376 if (*mode == 0)
377 *mode = MEM_OPERATION_LOAD;
378error:
379 free(os);
380 return ret;
381}
382
Arnaldo Carvalho de Melob0ad8ea2017-03-27 11:47:20 -0300383int cmd_mem(int argc, const char **argv)
Stephane Eranian028f12e2013-01-24 16:10:38 +0100384{
385 struct stat st;
386 struct perf_mem mem = {
387 .tool = {
388 .sample = process_sample_event,
389 .mmap = perf_event__process_mmap,
Stephane Eranian5c5e8542013-08-21 12:10:25 +0200390 .mmap2 = perf_event__process_mmap2,
Stephane Eranian028f12e2013-01-24 16:10:38 +0100391 .comm = perf_event__process_comm,
392 .lost = perf_event__process_lost,
393 .fork = perf_event__process_fork,
394 .build_id = perf_event__process_build_id,
Hari Bathinif3b36142017-03-08 02:11:43 +0530395 .namespaces = perf_event__process_namespaces,
Jiri Olsa0a8cb852014-07-06 14:18:21 +0200396 .ordered_events = true,
Stephane Eranian028f12e2013-01-24 16:10:38 +0100397 },
398 .input_name = "perf.data",
Arnaldo Carvalho de Melo66024122014-12-17 13:53:27 -0300399 /*
400 * default to both load an store sampling
401 */
402 .operation = MEM_OPERATION_LOAD | MEM_OPERATION_STORE,
Stephane Eranian028f12e2013-01-24 16:10:38 +0100403 };
404 const struct option mem_options[] = {
Arnaldo Carvalho de Melo66024122014-12-17 13:53:27 -0300405 OPT_CALLBACK('t', "type", &mem.operation,
Stephane Eranian67121f82014-12-17 16:23:55 +0100406 "type", "memory operations(load,store) Default load,store",
407 parse_mem_ops),
Stephane Eranian028f12e2013-01-24 16:10:38 +0100408 OPT_BOOLEAN('D', "dump-raw-samples", &mem.dump_raw,
409 "dump raw samples in ASCII"),
410 OPT_BOOLEAN('U', "hide-unresolved", &mem.hide_unresolved,
411 "Only display entries resolved to a symbol"),
412 OPT_STRING('i', "input", &input_name, "file",
413 "input file name"),
414 OPT_STRING('C', "cpu", &mem.cpu_list, "cpu",
415 "list of cpus to profile"),
Wang Nan8b8ca6e2015-03-20 02:57:52 +0000416 OPT_STRING_NOEMPTY('x', "field-separator", &symbol_conf.field_sep,
Stephane Eranian028f12e2013-01-24 16:10:38 +0100417 "separator",
418 "separator for columns, no spaces will be added"
419 " between columns '.' is reserved."),
Yunlong Song62a1a632015-04-02 21:47:15 +0800420 OPT_BOOLEAN('f', "force", &mem.force, "don't complain, do it"),
Kan Liangc35aeb92017-08-29 13:11:10 -0400421 OPT_BOOLEAN('p', "phys-data", &mem.phys_addr, "Record/Report sample physical addresses"),
Stephane Eranian028f12e2013-01-24 16:10:38 +0100422 OPT_END()
423 };
Ramkumar Ramachandra8d2a2a12014-03-14 23:17:52 -0400424 const char *const mem_subcommands[] = { "record", "report", NULL };
425 const char *mem_usage[] = {
426 NULL,
427 NULL
428 };
Stephane Eranian028f12e2013-01-24 16:10:38 +0100429
Jiri Olsa54fbad52016-02-24 09:46:42 +0100430 if (perf_mem_events__init()) {
431 pr_err("failed: memory events not supported\n");
432 return -1;
433 }
434
Ramkumar Ramachandra8d2a2a12014-03-14 23:17:52 -0400435 argc = parse_options_subcommand(argc, argv, mem_options, mem_subcommands,
436 mem_usage, PARSE_OPT_STOP_AT_NON_OPTION);
Stephane Eranian028f12e2013-01-24 16:10:38 +0100437
Arnaldo Carvalho de Melo66024122014-12-17 13:53:27 -0300438 if (!argc || !(strncmp(argv[0], "rec", 3) || mem.operation))
Stephane Eranian028f12e2013-01-24 16:10:38 +0100439 usage_with_options(mem_usage, mem_options);
440
441 if (!mem.input_name || !strlen(mem.input_name)) {
442 if (!fstat(STDIN_FILENO, &st) && S_ISFIFO(st.st_mode))
443 mem.input_name = "-";
444 else
445 mem.input_name = "perf.data";
446 }
447
448 if (!strncmp(argv[0], "rec", 3))
Arnaldo Carvalho de Melo66024122014-12-17 13:53:27 -0300449 return __cmd_record(argc, argv, &mem);
Stephane Eranian028f12e2013-01-24 16:10:38 +0100450 else if (!strncmp(argv[0], "rep", 3))
451 return report_events(argc, argv, &mem);
452 else
453 usage_with_options(mem_usage, mem_options);
454
455 return 0;
456}