blob: 0f15634ef82cc003a203065f414e180a04136b26 [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;
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));
Jiri Olsa54fbad52016-02-24 09:46:42 +0100115 return -1;
116 }
117
Stephane Eranian67121f82014-12-17 16:23:55 +0100118 rec_argv[i++] = "-e";
Jiri Olsa2ba7ac52016-02-24 09:46:43 +0100119 rec_argv[i++] = perf_mem_events__name(j);
Jiri Olsaacbe6132016-02-15 09:34:34 +0100120 };
Stephane Eranian67121f82014-12-17 16:23:55 +0100121
Jiri Olsaad165112016-03-24 13:52:16 +0100122 if (all_user)
123 rec_argv[i++] = "--all-user";
124
125 if (all_kernel)
126 rec_argv[i++] = "--all-kernel";
127
Jiri Olsace1e22b2016-02-15 09:34:35 +0100128 for (j = 0; j < argc; j++, i++)
Stephane Eranian028f12e2013-01-24 16:10:38 +0100129 rec_argv[i] = argv[j];
130
Jiri Olsace1e22b2016-02-15 09:34:35 +0100131 if (verbose > 0) {
132 pr_debug("calling: record ");
133
134 while (rec_argv[j]) {
135 pr_debug("%s ", rec_argv[j]);
136 j++;
137 }
138 pr_debug("\n");
139 }
140
Arnaldo Carvalho de Melob0ad8ea2017-03-27 11:47:20 -0300141 ret = cmd_record(i, rec_argv);
Stephane Eranian028f12e2013-01-24 16:10:38 +0100142 free(rec_argv);
143 return ret;
144}
145
146static int
147dump_raw_samples(struct perf_tool *tool,
148 union perf_event *event,
149 struct perf_sample *sample,
Stephane Eranian028f12e2013-01-24 16:10:38 +0100150 struct machine *machine)
151{
152 struct perf_mem *mem = container_of(tool, struct perf_mem, tool);
153 struct addr_location al;
154 const char *fmt;
155
Arnaldo Carvalho de Melobb3eb562016-03-22 18:39:09 -0300156 if (machine__resolve(machine, &al, sample) < 0) {
Stephane Eranian028f12e2013-01-24 16:10:38 +0100157 fprintf(stderr, "problem processing %d event, skipping it.\n",
158 event->header.type);
159 return -1;
160 }
161
162 if (al.filtered || (mem->hide_unresolved && al.sym == NULL))
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -0300163 goto out_put;
Stephane Eranian028f12e2013-01-24 16:10:38 +0100164
165 if (al.map != NULL)
166 al.map->dso->hit = 1;
167
Kan Liangc35aeb92017-08-29 13:11:10 -0400168 if (mem->phys_addr) {
169 if (symbol_conf.field_sep) {
170 fmt = "%d%s%d%s0x%"PRIx64"%s0x%"PRIx64"%s0x%016"PRIx64
171 "%s%"PRIu64"%s0x%"PRIx64"%s%s:%s\n";
172 } else {
173 fmt = "%5d%s%5d%s0x%016"PRIx64"%s0x016%"PRIx64
174 "%s0x%016"PRIx64"%s%5"PRIu64"%s0x%06"PRIx64
175 "%s%s:%s\n";
176 symbol_conf.field_sep = " ";
177 }
Stephane Eranian028f12e2013-01-24 16:10:38 +0100178
Kan Liangc35aeb92017-08-29 13:11:10 -0400179 printf(fmt,
180 sample->pid,
181 symbol_conf.field_sep,
182 sample->tid,
183 symbol_conf.field_sep,
184 sample->ip,
185 symbol_conf.field_sep,
186 sample->addr,
187 symbol_conf.field_sep,
188 sample->phys_addr,
189 symbol_conf.field_sep,
190 sample->weight,
191 symbol_conf.field_sep,
192 sample->data_src,
193 symbol_conf.field_sep,
194 al.map ? (al.map->dso ? al.map->dso->long_name : "???") : "???",
195 al.sym ? al.sym->name : "???");
196 } else {
197 if (symbol_conf.field_sep) {
198 fmt = "%d%s%d%s0x%"PRIx64"%s0x%"PRIx64"%s%"PRIu64
199 "%s0x%"PRIx64"%s%s:%s\n";
200 } else {
201 fmt = "%5d%s%5d%s0x%016"PRIx64"%s0x016%"PRIx64
202 "%s%5"PRIu64"%s0x%06"PRIx64"%s%s:%s\n";
203 symbol_conf.field_sep = " ";
204 }
205
206 printf(fmt,
207 sample->pid,
208 symbol_conf.field_sep,
209 sample->tid,
210 symbol_conf.field_sep,
211 sample->ip,
212 symbol_conf.field_sep,
213 sample->addr,
214 symbol_conf.field_sep,
215 sample->weight,
216 symbol_conf.field_sep,
217 sample->data_src,
218 symbol_conf.field_sep,
219 al.map ? (al.map->dso ? al.map->dso->long_name : "???") : "???",
220 al.sym ? al.sym->name : "???");
221 }
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -0300222out_put:
223 addr_location__put(&al);
Stephane Eranian028f12e2013-01-24 16:10:38 +0100224 return 0;
225}
226
227static int process_sample_event(struct perf_tool *tool,
228 union perf_event *event,
229 struct perf_sample *sample,
Arnaldo Carvalho de Melo8b640cc2013-12-19 17:00:45 -0300230 struct perf_evsel *evsel __maybe_unused,
Stephane Eranian028f12e2013-01-24 16:10:38 +0100231 struct machine *machine)
232{
Arnaldo Carvalho de Melo8b640cc2013-12-19 17:00:45 -0300233 return dump_raw_samples(tool, event, sample, machine);
Stephane Eranian028f12e2013-01-24 16:10:38 +0100234}
235
236static int report_raw_events(struct perf_mem *mem)
237{
Jiri Olsaf5fc1412013-10-15 16:27:32 +0200238 struct perf_data_file file = {
239 .path = input_name,
240 .mode = PERF_DATA_MODE_READ,
Yunlong Song62a1a632015-04-02 21:47:15 +0800241 .force = mem->force,
Jiri Olsaf5fc1412013-10-15 16:27:32 +0200242 };
Stephane Eranian028f12e2013-01-24 16:10:38 +0100243 int ret;
Jiri Olsaf5fc1412013-10-15 16:27:32 +0200244 struct perf_session *session = perf_session__new(&file, false,
245 &mem->tool);
Stephane Eranian028f12e2013-01-24 16:10:38 +0100246
247 if (session == NULL)
Taeung Song52e028342014-09-24 10:33:37 +0900248 return -1;
Stephane Eranian028f12e2013-01-24 16:10:38 +0100249
250 if (mem->cpu_list) {
251 ret = perf_session__cpu_bitmap(session, mem->cpu_list,
252 mem->cpu_bitmap);
Taeung Song1df9fade2015-07-01 21:08:19 +0900253 if (ret < 0)
Stephane Eranian028f12e2013-01-24 16:10:38 +0100254 goto out_delete;
255 }
256
Taeung Song1df9fade2015-07-01 21:08:19 +0900257 ret = symbol__init(&session->header.env);
258 if (ret < 0)
259 goto out_delete;
Stephane Eranian028f12e2013-01-24 16:10:38 +0100260
Kan Liangc35aeb92017-08-29 13:11:10 -0400261 if (mem->phys_addr)
262 printf("# PID, TID, IP, ADDR, PHYS ADDR, LOCAL WEIGHT, DSRC, SYMBOL\n");
263 else
264 printf("# PID, TID, IP, ADDR, LOCAL WEIGHT, DSRC, SYMBOL\n");
Stephane Eranian028f12e2013-01-24 16:10:38 +0100265
Taeung Song1df9fade2015-07-01 21:08:19 +0900266 ret = perf_session__process_events(session);
Stephane Eranian028f12e2013-01-24 16:10:38 +0100267
268out_delete:
269 perf_session__delete(session);
Taeung Song1df9fade2015-07-01 21:08:19 +0900270 return ret;
Stephane Eranian028f12e2013-01-24 16:10:38 +0100271}
272
273static int report_events(int argc, const char **argv, struct perf_mem *mem)
274{
275 const char **rep_argv;
276 int ret, i = 0, j, rep_argc;
277
278 if (mem->dump_raw)
279 return report_raw_events(mem);
280
281 rep_argc = argc + 3;
282 rep_argv = calloc(rep_argc + 1, sizeof(char *));
283 if (!rep_argv)
284 return -1;
285
Stephane Eranian67121f82014-12-17 16:23:55 +0100286 rep_argv[i++] = "report";
287 rep_argv[i++] = "--mem-mode";
288 rep_argv[i++] = "-n"; /* display number of samples */
Stephane Eranian028f12e2013-01-24 16:10:38 +0100289
290 /*
291 * there is no weight (cost) associated with stores, so don't print
292 * the column
293 */
Kan Liangc35aeb92017-08-29 13:11:10 -0400294 if (!(mem->operation & MEM_OPERATION_LOAD)) {
295 if (mem->phys_addr)
296 rep_argv[i++] = "--sort=mem,sym,dso,symbol_daddr,"
297 "dso_daddr,tlb,locked,phys_daddr";
298 else
299 rep_argv[i++] = "--sort=mem,sym,dso,symbol_daddr,"
300 "dso_daddr,tlb,locked";
301 } else if (mem->phys_addr)
302 rep_argv[i++] = "--sort=local_weight,mem,sym,dso,symbol_daddr,"
303 "dso_daddr,snoop,tlb,locked,phys_daddr";
Stephane Eranian028f12e2013-01-24 16:10:38 +0100304
305 for (j = 1; j < argc; j++, i++)
306 rep_argv[i] = argv[j];
307
Arnaldo Carvalho de Melob0ad8ea2017-03-27 11:47:20 -0300308 ret = cmd_report(i, rep_argv);
Stephane Eranian028f12e2013-01-24 16:10:38 +0100309 free(rep_argv);
310 return ret;
311}
312
Stephane Eranian67121f82014-12-17 16:23:55 +0100313struct mem_mode {
314 const char *name;
315 int mode;
316};
317
318#define MEM_OPT(n, m) \
319 { .name = n, .mode = (m) }
320
321#define MEM_END { .name = NULL }
322
323static const struct mem_mode mem_modes[]={
324 MEM_OPT("load", MEM_OPERATION_LOAD),
325 MEM_OPT("store", MEM_OPERATION_STORE),
326 MEM_END
327};
328
329static int
330parse_mem_ops(const struct option *opt, const char *str, int unset)
331{
332 int *mode = (int *)opt->value;
333 const struct mem_mode *m;
334 char *s, *os = NULL, *p;
335 int ret = -1;
336
337 if (unset)
338 return 0;
339
340 /* str may be NULL in case no arg is passed to -t */
341 if (str) {
342 /* because str is read-only */
343 s = os = strdup(str);
344 if (!s)
345 return -1;
346
347 /* reset mode */
348 *mode = 0;
349
350 for (;;) {
351 p = strchr(s, ',');
352 if (p)
353 *p = '\0';
354
355 for (m = mem_modes; m->name; m++) {
356 if (!strcasecmp(s, m->name))
357 break;
358 }
359 if (!m->name) {
360 fprintf(stderr, "unknown sampling op %s,"
361 " check man page\n", s);
362 goto error;
363 }
364
365 *mode |= m->mode;
366
367 if (!p)
368 break;
369
370 s = p + 1;
371 }
372 }
373 ret = 0;
374
375 if (*mode == 0)
376 *mode = MEM_OPERATION_LOAD;
377error:
378 free(os);
379 return ret;
380}
381
Arnaldo Carvalho de Melob0ad8ea2017-03-27 11:47:20 -0300382int cmd_mem(int argc, const char **argv)
Stephane Eranian028f12e2013-01-24 16:10:38 +0100383{
384 struct stat st;
385 struct perf_mem mem = {
386 .tool = {
387 .sample = process_sample_event,
388 .mmap = perf_event__process_mmap,
Stephane Eranian5c5e8542013-08-21 12:10:25 +0200389 .mmap2 = perf_event__process_mmap2,
Stephane Eranian028f12e2013-01-24 16:10:38 +0100390 .comm = perf_event__process_comm,
391 .lost = perf_event__process_lost,
392 .fork = perf_event__process_fork,
393 .build_id = perf_event__process_build_id,
Hari Bathinif3b36142017-03-08 02:11:43 +0530394 .namespaces = perf_event__process_namespaces,
Jiri Olsa0a8cb852014-07-06 14:18:21 +0200395 .ordered_events = true,
Stephane Eranian028f12e2013-01-24 16:10:38 +0100396 },
397 .input_name = "perf.data",
Arnaldo Carvalho de Melo66024122014-12-17 13:53:27 -0300398 /*
399 * default to both load an store sampling
400 */
401 .operation = MEM_OPERATION_LOAD | MEM_OPERATION_STORE,
Stephane Eranian028f12e2013-01-24 16:10:38 +0100402 };
403 const struct option mem_options[] = {
Arnaldo Carvalho de Melo66024122014-12-17 13:53:27 -0300404 OPT_CALLBACK('t', "type", &mem.operation,
Stephane Eranian67121f82014-12-17 16:23:55 +0100405 "type", "memory operations(load,store) Default load,store",
406 parse_mem_ops),
Stephane Eranian028f12e2013-01-24 16:10:38 +0100407 OPT_BOOLEAN('D', "dump-raw-samples", &mem.dump_raw,
408 "dump raw samples in ASCII"),
409 OPT_BOOLEAN('U', "hide-unresolved", &mem.hide_unresolved,
410 "Only display entries resolved to a symbol"),
411 OPT_STRING('i', "input", &input_name, "file",
412 "input file name"),
413 OPT_STRING('C', "cpu", &mem.cpu_list, "cpu",
414 "list of cpus to profile"),
Wang Nan8b8ca6e2015-03-20 02:57:52 +0000415 OPT_STRING_NOEMPTY('x', "field-separator", &symbol_conf.field_sep,
Stephane Eranian028f12e2013-01-24 16:10:38 +0100416 "separator",
417 "separator for columns, no spaces will be added"
418 " between columns '.' is reserved."),
Yunlong Song62a1a632015-04-02 21:47:15 +0800419 OPT_BOOLEAN('f', "force", &mem.force, "don't complain, do it"),
Kan Liangc35aeb92017-08-29 13:11:10 -0400420 OPT_BOOLEAN('p', "phys-data", &mem.phys_addr, "Record/Report sample physical addresses"),
Stephane Eranian028f12e2013-01-24 16:10:38 +0100421 OPT_END()
422 };
Ramkumar Ramachandra8d2a2a12014-03-14 23:17:52 -0400423 const char *const mem_subcommands[] = { "record", "report", NULL };
424 const char *mem_usage[] = {
425 NULL,
426 NULL
427 };
Stephane Eranian028f12e2013-01-24 16:10:38 +0100428
Jiri Olsa54fbad52016-02-24 09:46:42 +0100429 if (perf_mem_events__init()) {
430 pr_err("failed: memory events not supported\n");
431 return -1;
432 }
433
Ramkumar Ramachandra8d2a2a12014-03-14 23:17:52 -0400434 argc = parse_options_subcommand(argc, argv, mem_options, mem_subcommands,
435 mem_usage, PARSE_OPT_STOP_AT_NON_OPTION);
Stephane Eranian028f12e2013-01-24 16:10:38 +0100436
Arnaldo Carvalho de Melo66024122014-12-17 13:53:27 -0300437 if (!argc || !(strncmp(argv[0], "rec", 3) || mem.operation))
Stephane Eranian028f12e2013-01-24 16:10:38 +0100438 usage_with_options(mem_usage, mem_options);
439
440 if (!mem.input_name || !strlen(mem.input_name)) {
441 if (!fstat(STDIN_FILENO, &st) && S_ISFIFO(st.st_mode))
442 mem.input_name = "-";
443 else
444 mem.input_name = "perf.data";
445 }
446
447 if (!strncmp(argv[0], "rec", 3))
Arnaldo Carvalho de Melo66024122014-12-17 13:53:27 -0300448 return __cmd_record(argc, argv, &mem);
Stephane Eranian028f12e2013-01-24 16:10:38 +0100449 else if (!strncmp(argv[0], "rep", 3))
450 return report_events(argc, argv, &mem);
451 else
452 usage_with_options(mem_usage, mem_options);
453
454 return 0;
455}