72ce4b8fbb0f4656521c5c68bec7f05f88290678
[linux-2.6-microblaze.git] / tools / perf / builtin-mem.c
1 // SPDX-License-Identifier: GPL-2.0
2 #include <inttypes.h>
3 #include <sys/types.h>
4 #include <sys/stat.h>
5 #include <unistd.h>
6 #include "builtin.h"
7 #include "perf.h"
8
9 #include <subcmd/parse-options.h>
10 #include "util/trace-event.h"
11 #include "util/tool.h"
12 #include "util/session.h"
13 #include "util/data.h"
14 #include "util/map_symbol.h"
15 #include "util/mem-events.h"
16 #include "util/debug.h"
17 #include "util/dso.h"
18 #include "util/map.h"
19 #include "util/symbol.h"
20 #include <linux/err.h>
21
22 #define MEM_OPERATION_LOAD      0x1
23 #define MEM_OPERATION_STORE     0x2
24
25 struct perf_mem {
26         struct perf_tool        tool;
27         char const              *input_name;
28         bool                    hide_unresolved;
29         bool                    dump_raw;
30         bool                    force;
31         bool                    phys_addr;
32         int                     operation;
33         const char              *cpu_list;
34         DECLARE_BITMAP(cpu_bitmap, MAX_NR_CPUS);
35 };
36
37 static int parse_record_events(const struct option *opt,
38                                const char *str, int unset __maybe_unused)
39 {
40         struct perf_mem *mem = *(struct perf_mem **)opt->value;
41
42         if (!strcmp(str, "list")) {
43                 perf_mem_events__list();
44                 exit(0);
45         }
46         if (perf_mem_events__parse(str))
47                 exit(-1);
48
49         mem->operation = 0;
50         return 0;
51 }
52
53 static const char * const __usage[] = {
54         "perf mem record [<options>] [<command>]",
55         "perf mem record [<options>] -- <command> [<options>]",
56         NULL
57 };
58
59 static const char * const *record_mem_usage = __usage;
60
61 static int __cmd_record(int argc, const char **argv, struct perf_mem *mem)
62 {
63         int rec_argc, i = 0, j;
64         const char **rec_argv;
65         int ret;
66         bool all_user = false, all_kernel = false;
67         struct perf_mem_event *e;
68         struct option options[] = {
69         OPT_CALLBACK('e', "event", &mem, "event",
70                      "event selector. use 'perf mem record -e list' to list available events",
71                      parse_record_events),
72         OPT_UINTEGER(0, "ldlat", &perf_mem_events__loads_ldlat, "mem-loads latency"),
73         OPT_INCR('v', "verbose", &verbose,
74                  "be more verbose (show counter open errors, etc)"),
75         OPT_BOOLEAN('U', "all-user", &all_user, "collect only user level data"),
76         OPT_BOOLEAN('K', "all-kernel", &all_kernel, "collect only kernel level data"),
77         OPT_END()
78         };
79
80         if (perf_mem_events__init()) {
81                 pr_err("failed: memory events not supported\n");
82                 return -1;
83         }
84
85         argc = parse_options(argc, argv, options, record_mem_usage,
86                              PARSE_OPT_KEEP_UNKNOWN);
87
88         rec_argc = argc + 9; /* max number of arguments */
89         rec_argv = calloc(rec_argc + 1, sizeof(char *));
90         if (!rec_argv)
91                 return -1;
92
93         rec_argv[i++] = "record";
94
95         e = perf_mem_events__ptr(PERF_MEM_EVENTS__LOAD_STORE);
96
97         /*
98          * The load and store operations are required, use the event
99          * PERF_MEM_EVENTS__LOAD_STORE if it is supported.
100          */
101         if (e->tag &&
102             (mem->operation & MEM_OPERATION_LOAD) &&
103             (mem->operation & MEM_OPERATION_STORE)) {
104                 e->record = true;
105         } else {
106                 if (mem->operation & MEM_OPERATION_LOAD) {
107                         e = perf_mem_events__ptr(PERF_MEM_EVENTS__LOAD);
108                         e->record = true;
109                 }
110
111                 if (mem->operation & MEM_OPERATION_STORE) {
112                         e = perf_mem_events__ptr(PERF_MEM_EVENTS__STORE);
113                         e->record = true;
114                 }
115         }
116
117         e = perf_mem_events__ptr(PERF_MEM_EVENTS__LOAD);
118         if (e->record)
119                 rec_argv[i++] = "-W";
120
121         rec_argv[i++] = "-d";
122
123         if (mem->phys_addr)
124                 rec_argv[i++] = "--phys-data";
125
126         for (j = 0; j < PERF_MEM_EVENTS__MAX; j++) {
127                 e = perf_mem_events__ptr(j);
128                 if (!e->record)
129                         continue;
130
131                 if (!e->supported) {
132                         pr_err("failed: event '%s' not supported\n",
133                                perf_mem_events__name(j));
134                         free(rec_argv);
135                         return -1;
136                 }
137
138                 rec_argv[i++] = "-e";
139                 rec_argv[i++] = perf_mem_events__name(j);
140         }
141
142         if (all_user)
143                 rec_argv[i++] = "--all-user";
144
145         if (all_kernel)
146                 rec_argv[i++] = "--all-kernel";
147
148         for (j = 0; j < argc; j++, i++)
149                 rec_argv[i] = argv[j];
150
151         if (verbose > 0) {
152                 pr_debug("calling: record ");
153
154                 while (rec_argv[j]) {
155                         pr_debug("%s ", rec_argv[j]);
156                         j++;
157                 }
158                 pr_debug("\n");
159         }
160
161         ret = cmd_record(i, rec_argv);
162         free(rec_argv);
163         return ret;
164 }
165
166 static int
167 dump_raw_samples(struct perf_tool *tool,
168                  union perf_event *event,
169                  struct perf_sample *sample,
170                  struct machine *machine)
171 {
172         struct perf_mem *mem = container_of(tool, struct perf_mem, tool);
173         struct addr_location al;
174         const char *fmt;
175
176         if (machine__resolve(machine, &al, sample) < 0) {
177                 fprintf(stderr, "problem processing %d event, skipping it.\n",
178                                 event->header.type);
179                 return -1;
180         }
181
182         if (al.filtered || (mem->hide_unresolved && al.sym == NULL))
183                 goto out_put;
184
185         if (al.map != NULL)
186                 al.map->dso->hit = 1;
187
188         if (mem->phys_addr) {
189                 if (symbol_conf.field_sep) {
190                         fmt = "%d%s%d%s0x%"PRIx64"%s0x%"PRIx64"%s0x%016"PRIx64
191                               "%s%"PRIu64"%s0x%"PRIx64"%s%s:%s\n";
192                 } else {
193                         fmt = "%5d%s%5d%s0x%016"PRIx64"%s0x016%"PRIx64
194                               "%s0x%016"PRIx64"%s%5"PRIu64"%s0x%06"PRIx64
195                               "%s%s:%s\n";
196                         symbol_conf.field_sep = " ";
197                 }
198
199                 printf(fmt,
200                         sample->pid,
201                         symbol_conf.field_sep,
202                         sample->tid,
203                         symbol_conf.field_sep,
204                         sample->ip,
205                         symbol_conf.field_sep,
206                         sample->addr,
207                         symbol_conf.field_sep,
208                         sample->phys_addr,
209                         symbol_conf.field_sep,
210                         sample->weight,
211                         symbol_conf.field_sep,
212                         sample->data_src,
213                         symbol_conf.field_sep,
214                         al.map ? (al.map->dso ? al.map->dso->long_name : "???") : "???",
215                         al.sym ? al.sym->name : "???");
216         } else {
217                 if (symbol_conf.field_sep) {
218                         fmt = "%d%s%d%s0x%"PRIx64"%s0x%"PRIx64"%s%"PRIu64
219                               "%s0x%"PRIx64"%s%s:%s\n";
220                 } else {
221                         fmt = "%5d%s%5d%s0x%016"PRIx64"%s0x016%"PRIx64
222                               "%s%5"PRIu64"%s0x%06"PRIx64"%s%s:%s\n";
223                         symbol_conf.field_sep = " ";
224                 }
225
226                 printf(fmt,
227                         sample->pid,
228                         symbol_conf.field_sep,
229                         sample->tid,
230                         symbol_conf.field_sep,
231                         sample->ip,
232                         symbol_conf.field_sep,
233                         sample->addr,
234                         symbol_conf.field_sep,
235                         sample->weight,
236                         symbol_conf.field_sep,
237                         sample->data_src,
238                         symbol_conf.field_sep,
239                         al.map ? (al.map->dso ? al.map->dso->long_name : "???") : "???",
240                         al.sym ? al.sym->name : "???");
241         }
242 out_put:
243         addr_location__put(&al);
244         return 0;
245 }
246
247 static int process_sample_event(struct perf_tool *tool,
248                                 union perf_event *event,
249                                 struct perf_sample *sample,
250                                 struct evsel *evsel __maybe_unused,
251                                 struct machine *machine)
252 {
253         return dump_raw_samples(tool, event, sample, machine);
254 }
255
256 static int report_raw_events(struct perf_mem *mem)
257 {
258         struct perf_data data = {
259                 .path  = input_name,
260                 .mode  = PERF_DATA_MODE_READ,
261                 .force = mem->force,
262         };
263         int ret;
264         struct perf_session *session = perf_session__new(&data, false,
265                                                          &mem->tool);
266
267         if (IS_ERR(session))
268                 return PTR_ERR(session);
269
270         if (mem->cpu_list) {
271                 ret = perf_session__cpu_bitmap(session, mem->cpu_list,
272                                                mem->cpu_bitmap);
273                 if (ret < 0)
274                         goto out_delete;
275         }
276
277         ret = symbol__init(&session->header.env);
278         if (ret < 0)
279                 goto out_delete;
280
281         if (mem->phys_addr)
282                 printf("# PID, TID, IP, ADDR, PHYS ADDR, LOCAL WEIGHT, DSRC, SYMBOL\n");
283         else
284                 printf("# PID, TID, IP, ADDR, LOCAL WEIGHT, DSRC, SYMBOL\n");
285
286         ret = perf_session__process_events(session);
287
288 out_delete:
289         perf_session__delete(session);
290         return ret;
291 }
292
293 static int report_events(int argc, const char **argv, struct perf_mem *mem)
294 {
295         const char **rep_argv;
296         int ret, i = 0, j, rep_argc;
297
298         if (mem->dump_raw)
299                 return report_raw_events(mem);
300
301         rep_argc = argc + 3;
302         rep_argv = calloc(rep_argc + 1, sizeof(char *));
303         if (!rep_argv)
304                 return -1;
305
306         rep_argv[i++] = "report";
307         rep_argv[i++] = "--mem-mode";
308         rep_argv[i++] = "-n"; /* display number of samples */
309
310         /*
311          * there is no weight (cost) associated with stores, so don't print
312          * the column
313          */
314         if (!(mem->operation & MEM_OPERATION_LOAD)) {
315                 if (mem->phys_addr)
316                         rep_argv[i++] = "--sort=mem,sym,dso,symbol_daddr,"
317                                         "dso_daddr,tlb,locked,phys_daddr";
318                 else
319                         rep_argv[i++] = "--sort=mem,sym,dso,symbol_daddr,"
320                                         "dso_daddr,tlb,locked";
321         } else if (mem->phys_addr)
322                 rep_argv[i++] = "--sort=local_weight,mem,sym,dso,symbol_daddr,"
323                                 "dso_daddr,snoop,tlb,locked,phys_daddr";
324
325         for (j = 1; j < argc; j++, i++)
326                 rep_argv[i] = argv[j];
327
328         ret = cmd_report(i, rep_argv);
329         free(rep_argv);
330         return ret;
331 }
332
333 struct mem_mode {
334         const char *name;
335         int mode;
336 };
337
338 #define MEM_OPT(n, m) \
339         { .name = n, .mode = (m) }
340
341 #define MEM_END { .name = NULL }
342
343 static const struct mem_mode mem_modes[]={
344         MEM_OPT("load", MEM_OPERATION_LOAD),
345         MEM_OPT("store", MEM_OPERATION_STORE),
346         MEM_END
347 };
348
349 static int
350 parse_mem_ops(const struct option *opt, const char *str, int unset)
351 {
352         int *mode = (int *)opt->value;
353         const struct mem_mode *m;
354         char *s, *os = NULL, *p;
355         int ret = -1;
356
357         if (unset)
358                 return 0;
359
360         /* str may be NULL in case no arg is passed to -t */
361         if (str) {
362                 /* because str is read-only */
363                 s = os = strdup(str);
364                 if (!s)
365                         return -1;
366
367                 /* reset mode */
368                 *mode = 0;
369
370                 for (;;) {
371                         p = strchr(s, ',');
372                         if (p)
373                                 *p = '\0';
374
375                         for (m = mem_modes; m->name; m++) {
376                                 if (!strcasecmp(s, m->name))
377                                         break;
378                         }
379                         if (!m->name) {
380                                 fprintf(stderr, "unknown sampling op %s,"
381                                             " check man page\n", s);
382                                 goto error;
383                         }
384
385                         *mode |= m->mode;
386
387                         if (!p)
388                                 break;
389
390                         s = p + 1;
391                 }
392         }
393         ret = 0;
394
395         if (*mode == 0)
396                 *mode = MEM_OPERATION_LOAD;
397 error:
398         free(os);
399         return ret;
400 }
401
402 int cmd_mem(int argc, const char **argv)
403 {
404         struct stat st;
405         struct perf_mem mem = {
406                 .tool = {
407                         .sample         = process_sample_event,
408                         .mmap           = perf_event__process_mmap,
409                         .mmap2          = perf_event__process_mmap2,
410                         .comm           = perf_event__process_comm,
411                         .lost           = perf_event__process_lost,
412                         .fork           = perf_event__process_fork,
413                         .build_id       = perf_event__process_build_id,
414                         .namespaces     = perf_event__process_namespaces,
415                         .ordered_events = true,
416                 },
417                 .input_name              = "perf.data",
418                 /*
419                  * default to both load an store sampling
420                  */
421                 .operation               = MEM_OPERATION_LOAD | MEM_OPERATION_STORE,
422         };
423         const struct option mem_options[] = {
424         OPT_CALLBACK('t', "type", &mem.operation,
425                    "type", "memory operations(load,store) Default load,store",
426                     parse_mem_ops),
427         OPT_BOOLEAN('D', "dump-raw-samples", &mem.dump_raw,
428                     "dump raw samples in ASCII"),
429         OPT_BOOLEAN('U', "hide-unresolved", &mem.hide_unresolved,
430                     "Only display entries resolved to a symbol"),
431         OPT_STRING('i', "input", &input_name, "file",
432                    "input file name"),
433         OPT_STRING('C', "cpu", &mem.cpu_list, "cpu",
434                    "list of cpus to profile"),
435         OPT_STRING_NOEMPTY('x', "field-separator", &symbol_conf.field_sep,
436                    "separator",
437                    "separator for columns, no spaces will be added"
438                    " between columns '.' is reserved."),
439         OPT_BOOLEAN('f', "force", &mem.force, "don't complain, do it"),
440         OPT_BOOLEAN('p', "phys-data", &mem.phys_addr, "Record/Report sample physical addresses"),
441         OPT_END()
442         };
443         const char *const mem_subcommands[] = { "record", "report", NULL };
444         const char *mem_usage[] = {
445                 NULL,
446                 NULL
447         };
448
449         argc = parse_options_subcommand(argc, argv, mem_options, mem_subcommands,
450                                         mem_usage, PARSE_OPT_KEEP_UNKNOWN);
451
452         if (!argc || !(strncmp(argv[0], "rec", 3) || mem.operation))
453                 usage_with_options(mem_usage, mem_options);
454
455         if (!mem.input_name || !strlen(mem.input_name)) {
456                 if (!fstat(STDIN_FILENO, &st) && S_ISFIFO(st.st_mode))
457                         mem.input_name = "-";
458                 else
459                         mem.input_name = "perf.data";
460         }
461
462         if (!strncmp(argv[0], "rec", 3))
463                 return __cmd_record(argc, argv, &mem);
464         else if (!strncmp(argv[0], "rep", 3))
465                 return report_events(argc, argv, &mem);
466         else
467                 usage_with_options(mem_usage, mem_options);
468
469         return 0;
470 }