perf mem: Remove unused symbol filter member
[platform/adaptation/renesas_rcar/renesas_kernel.git] / tools / perf / builtin-mem.c
1 #include "builtin.h"
2 #include "perf.h"
3
4 #include "util/parse-options.h"
5 #include "util/trace-event.h"
6 #include "util/tool.h"
7 #include "util/session.h"
8
9 #define MEM_OPERATION_LOAD      "load"
10 #define MEM_OPERATION_STORE     "store"
11
12 static const char       *mem_operation          = MEM_OPERATION_LOAD;
13
14 struct perf_mem {
15         struct perf_tool        tool;
16         char const              *input_name;
17         bool                    hide_unresolved;
18         bool                    dump_raw;
19         const char              *cpu_list;
20         DECLARE_BITMAP(cpu_bitmap, MAX_NR_CPUS);
21 };
22
23 static const char * const mem_usage[] = {
24         "perf mem [<options>] {record <command> |report}",
25         NULL
26 };
27
28 static int __cmd_record(int argc, const char **argv)
29 {
30         int rec_argc, i = 0, j;
31         const char **rec_argv;
32         char event[64];
33         int ret;
34
35         rec_argc = argc + 4;
36         rec_argv = calloc(rec_argc + 1, sizeof(char *));
37         if (!rec_argv)
38                 return -1;
39
40         rec_argv[i++] = strdup("record");
41         if (!strcmp(mem_operation, MEM_OPERATION_LOAD))
42                 rec_argv[i++] = strdup("-W");
43         rec_argv[i++] = strdup("-d");
44         rec_argv[i++] = strdup("-e");
45
46         if (strcmp(mem_operation, MEM_OPERATION_LOAD))
47                 sprintf(event, "cpu/mem-stores/pp");
48         else
49                 sprintf(event, "cpu/mem-loads/pp");
50
51         rec_argv[i++] = strdup(event);
52         for (j = 1; j < argc; j++, i++)
53                 rec_argv[i] = argv[j];
54
55         ret = cmd_record(i, rec_argv, NULL);
56         free(rec_argv);
57         return ret;
58 }
59
60 static int
61 dump_raw_samples(struct perf_tool *tool,
62                  union perf_event *event,
63                  struct perf_sample *sample,
64                  struct perf_evsel *evsel __maybe_unused,
65                  struct machine *machine)
66 {
67         struct perf_mem *mem = container_of(tool, struct perf_mem, tool);
68         struct addr_location al;
69         const char *fmt;
70
71         if (perf_event__preprocess_sample(event, machine, &al, sample,
72                                 NULL) < 0) {
73                 fprintf(stderr, "problem processing %d event, skipping it.\n",
74                                 event->header.type);
75                 return -1;
76         }
77
78         if (al.filtered || (mem->hide_unresolved && al.sym == NULL))
79                 return 0;
80
81         if (al.map != NULL)
82                 al.map->dso->hit = 1;
83
84         if (symbol_conf.field_sep) {
85                 fmt = "%d%s%d%s0x%"PRIx64"%s0x%"PRIx64"%s%"PRIu64
86                       "%s0x%"PRIx64"%s%s:%s\n";
87         } else {
88                 fmt = "%5d%s%5d%s0x%016"PRIx64"%s0x016%"PRIx64
89                       "%s%5"PRIu64"%s0x%06"PRIx64"%s%s:%s\n";
90                 symbol_conf.field_sep = " ";
91         }
92
93         printf(fmt,
94                 sample->pid,
95                 symbol_conf.field_sep,
96                 sample->tid,
97                 symbol_conf.field_sep,
98                 event->ip.ip,
99                 symbol_conf.field_sep,
100                 sample->addr,
101                 symbol_conf.field_sep,
102                 sample->weight,
103                 symbol_conf.field_sep,
104                 sample->data_src,
105                 symbol_conf.field_sep,
106                 al.map ? (al.map->dso ? al.map->dso->long_name : "???") : "???",
107                 al.sym ? al.sym->name : "???");
108
109         return 0;
110 }
111
112 static int process_sample_event(struct perf_tool *tool,
113                                 union perf_event *event,
114                                 struct perf_sample *sample,
115                                 struct perf_evsel *evsel,
116                                 struct machine *machine)
117 {
118         return dump_raw_samples(tool, event, sample, evsel, machine);
119 }
120
121 static int report_raw_events(struct perf_mem *mem)
122 {
123         int err = -EINVAL;
124         int ret;
125         struct perf_session *session = perf_session__new(input_name, O_RDONLY,
126                                                          0, false, &mem->tool);
127
128         if (session == NULL)
129                 return -ENOMEM;
130
131         if (mem->cpu_list) {
132                 ret = perf_session__cpu_bitmap(session, mem->cpu_list,
133                                                mem->cpu_bitmap);
134                 if (ret)
135                         goto out_delete;
136         }
137
138         if (symbol__init() < 0)
139                 return -1;
140
141         printf("# PID, TID, IP, ADDR, LOCAL WEIGHT, DSRC, SYMBOL\n");
142
143         err = perf_session__process_events(session, &mem->tool);
144         if (err)
145                 return err;
146
147         return 0;
148
149 out_delete:
150         perf_session__delete(session);
151         return err;
152 }
153
154 static int report_events(int argc, const char **argv, struct perf_mem *mem)
155 {
156         const char **rep_argv;
157         int ret, i = 0, j, rep_argc;
158
159         if (mem->dump_raw)
160                 return report_raw_events(mem);
161
162         rep_argc = argc + 3;
163         rep_argv = calloc(rep_argc + 1, sizeof(char *));
164         if (!rep_argv)
165                 return -1;
166
167         rep_argv[i++] = strdup("report");
168         rep_argv[i++] = strdup("--mem-mode");
169         rep_argv[i++] = strdup("-n"); /* display number of samples */
170
171         /*
172          * there is no weight (cost) associated with stores, so don't print
173          * the column
174          */
175         if (strcmp(mem_operation, MEM_OPERATION_LOAD))
176                 rep_argv[i++] = strdup("--sort=mem,sym,dso,symbol_daddr,"
177                                        "dso_daddr,tlb,locked");
178
179         for (j = 1; j < argc; j++, i++)
180                 rep_argv[i] = argv[j];
181
182         ret = cmd_report(i, rep_argv, NULL);
183         free(rep_argv);
184         return ret;
185 }
186
187 int cmd_mem(int argc, const char **argv, const char *prefix __maybe_unused)
188 {
189         struct stat st;
190         struct perf_mem mem = {
191                 .tool = {
192                         .sample         = process_sample_event,
193                         .mmap           = perf_event__process_mmap,
194                         .comm           = perf_event__process_comm,
195                         .lost           = perf_event__process_lost,
196                         .fork           = perf_event__process_fork,
197                         .build_id       = perf_event__process_build_id,
198                         .ordered_samples = true,
199                 },
200                 .input_name              = "perf.data",
201         };
202         const struct option mem_options[] = {
203         OPT_STRING('t', "type", &mem_operation,
204                    "type", "memory operations(load/store)"),
205         OPT_BOOLEAN('D', "dump-raw-samples", &mem.dump_raw,
206                     "dump raw samples in ASCII"),
207         OPT_BOOLEAN('U', "hide-unresolved", &mem.hide_unresolved,
208                     "Only display entries resolved to a symbol"),
209         OPT_STRING('i', "input", &input_name, "file",
210                    "input file name"),
211         OPT_STRING('C', "cpu", &mem.cpu_list, "cpu",
212                    "list of cpus to profile"),
213         OPT_STRING('x', "field-separator", &symbol_conf.field_sep,
214                    "separator",
215                    "separator for columns, no spaces will be added"
216                    " between columns '.' is reserved."),
217         OPT_END()
218         };
219
220         argc = parse_options(argc, argv, mem_options, mem_usage,
221                              PARSE_OPT_STOP_AT_NON_OPTION);
222
223         if (!argc || !(strncmp(argv[0], "rec", 3) || mem_operation))
224                 usage_with_options(mem_usage, mem_options);
225
226         if (!mem.input_name || !strlen(mem.input_name)) {
227                 if (!fstat(STDIN_FILENO, &st) && S_ISFIFO(st.st_mode))
228                         mem.input_name = "-";
229                 else
230                         mem.input_name = "perf.data";
231         }
232
233         if (!strncmp(argv[0], "rec", 3))
234                 return __cmd_record(argc, argv);
235         else if (!strncmp(argv[0], "rep", 3))
236                 return report_events(argc, argv, &mem);
237         else
238                 usage_with_options(mem_usage, mem_options);
239
240         return 0;
241 }