Merge tag 'scsi-misc' of git://git.kernel.org/pub/scm/linux/kernel/git/jejb/scsi
[deliverable/linux.git] / tools / perf / builtin-mem.c
CommitLineData
028f12ee
SE
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"
f5fc1412 8#include "util/data.h"
028f12ee 9
67121f85
SE
10#define MEM_OPERATION_LOAD 0x1
11#define MEM_OPERATION_STORE 0x2
028f12ee 12
028f12ee
SE
13struct perf_mem {
14 struct perf_tool tool;
15 char const *input_name;
028f12ee
SE
16 bool hide_unresolved;
17 bool dump_raw;
62a1a63a 18 bool force;
66024122 19 int operation;
028f12ee
SE
20 const char *cpu_list;
21 DECLARE_BITMAP(cpu_bitmap, MAX_NR_CPUS);
22};
23
66024122 24static int __cmd_record(int argc, const char **argv, struct perf_mem *mem)
028f12ee
SE
25{
26 int rec_argc, i = 0, j;
27 const char **rec_argv;
028f12ee
SE
28 int ret;
29
67121f85 30 rec_argc = argc + 7; /* max number of arguments */
028f12ee
SE
31 rec_argv = calloc(rec_argc + 1, sizeof(char *));
32 if (!rec_argv)
33 return -1;
34
67121f85 35 rec_argv[i++] = "record";
028f12ee 36
66024122 37 if (mem->operation & MEM_OPERATION_LOAD)
67121f85
SE
38 rec_argv[i++] = "-W";
39
40 rec_argv[i++] = "-d";
41
66024122 42 if (mem->operation & MEM_OPERATION_LOAD) {
67121f85
SE
43 rec_argv[i++] = "-e";
44 rec_argv[i++] = "cpu/mem-loads/pp";
45 }
46
66024122 47 if (mem->operation & MEM_OPERATION_STORE) {
67121f85
SE
48 rec_argv[i++] = "-e";
49 rec_argv[i++] = "cpu/mem-stores/pp";
50 }
028f12ee 51
028f12ee
SE
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
60static int
61dump_raw_samples(struct perf_tool *tool,
62 union perf_event *event,
63 struct perf_sample *sample,
028f12ee
SE
64 struct machine *machine)
65{
66 struct perf_mem *mem = container_of(tool, struct perf_mem, tool);
67 struct addr_location al;
68 const char *fmt;
69
e44baa3e 70 if (perf_event__preprocess_sample(event, machine, &al, sample) < 0) {
028f12ee
SE
71 fprintf(stderr, "problem processing %d event, skipping it.\n",
72 event->header.type);
73 return -1;
74 }
75
76 if (al.filtered || (mem->hide_unresolved && al.sym == NULL))
b91fc39f 77 goto out_put;
028f12ee
SE
78
79 if (al.map != NULL)
80 al.map->dso->hit = 1;
81
82 if (symbol_conf.field_sep) {
83 fmt = "%d%s%d%s0x%"PRIx64"%s0x%"PRIx64"%s%"PRIu64
84 "%s0x%"PRIx64"%s%s:%s\n";
85 } else {
86 fmt = "%5d%s%5d%s0x%016"PRIx64"%s0x016%"PRIx64
87 "%s%5"PRIu64"%s0x%06"PRIx64"%s%s:%s\n";
88 symbol_conf.field_sep = " ";
89 }
90
91 printf(fmt,
92 sample->pid,
93 symbol_conf.field_sep,
94 sample->tid,
95 symbol_conf.field_sep,
ef89325f 96 sample->ip,
028f12ee
SE
97 symbol_conf.field_sep,
98 sample->addr,
99 symbol_conf.field_sep,
100 sample->weight,
101 symbol_conf.field_sep,
102 sample->data_src,
103 symbol_conf.field_sep,
104 al.map ? (al.map->dso ? al.map->dso->long_name : "???") : "???",
105 al.sym ? al.sym->name : "???");
b91fc39f
ACM
106out_put:
107 addr_location__put(&al);
028f12ee
SE
108 return 0;
109}
110
111static int process_sample_event(struct perf_tool *tool,
112 union perf_event *event,
113 struct perf_sample *sample,
8b640cc4 114 struct perf_evsel *evsel __maybe_unused,
028f12ee
SE
115 struct machine *machine)
116{
8b640cc4 117 return dump_raw_samples(tool, event, sample, machine);
028f12ee
SE
118}
119
120static int report_raw_events(struct perf_mem *mem)
121{
f5fc1412
JO
122 struct perf_data_file file = {
123 .path = input_name,
124 .mode = PERF_DATA_MODE_READ,
62a1a63a 125 .force = mem->force,
f5fc1412 126 };
028f12ee 127 int ret;
f5fc1412
JO
128 struct perf_session *session = perf_session__new(&file, false,
129 &mem->tool);
028f12ee
SE
130
131 if (session == NULL)
52e02834 132 return -1;
028f12ee
SE
133
134 if (mem->cpu_list) {
135 ret = perf_session__cpu_bitmap(session, mem->cpu_list,
136 mem->cpu_bitmap);
1df9fade 137 if (ret < 0)
028f12ee
SE
138 goto out_delete;
139 }
140
1df9fade
TS
141 ret = symbol__init(&session->header.env);
142 if (ret < 0)
143 goto out_delete;
028f12ee
SE
144
145 printf("# PID, TID, IP, ADDR, LOCAL WEIGHT, DSRC, SYMBOL\n");
146
1df9fade 147 ret = perf_session__process_events(session);
028f12ee
SE
148
149out_delete:
150 perf_session__delete(session);
1df9fade 151 return ret;
028f12ee
SE
152}
153
154static 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
67121f85
SE
167 rep_argv[i++] = "report";
168 rep_argv[i++] = "--mem-mode";
169 rep_argv[i++] = "-n"; /* display number of samples */
028f12ee
SE
170
171 /*
172 * there is no weight (cost) associated with stores, so don't print
173 * the column
174 */
66024122 175 if (!(mem->operation & MEM_OPERATION_LOAD))
67121f85
SE
176 rep_argv[i++] = "--sort=mem,sym,dso,symbol_daddr,"
177 "dso_daddr,tlb,locked";
028f12ee
SE
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
67121f85
SE
187struct mem_mode {
188 const char *name;
189 int mode;
190};
191
192#define MEM_OPT(n, m) \
193 { .name = n, .mode = (m) }
194
195#define MEM_END { .name = NULL }
196
197static const struct mem_mode mem_modes[]={
198 MEM_OPT("load", MEM_OPERATION_LOAD),
199 MEM_OPT("store", MEM_OPERATION_STORE),
200 MEM_END
201};
202
203static int
204parse_mem_ops(const struct option *opt, const char *str, int unset)
205{
206 int *mode = (int *)opt->value;
207 const struct mem_mode *m;
208 char *s, *os = NULL, *p;
209 int ret = -1;
210
211 if (unset)
212 return 0;
213
214 /* str may be NULL in case no arg is passed to -t */
215 if (str) {
216 /* because str is read-only */
217 s = os = strdup(str);
218 if (!s)
219 return -1;
220
221 /* reset mode */
222 *mode = 0;
223
224 for (;;) {
225 p = strchr(s, ',');
226 if (p)
227 *p = '\0';
228
229 for (m = mem_modes; m->name; m++) {
230 if (!strcasecmp(s, m->name))
231 break;
232 }
233 if (!m->name) {
234 fprintf(stderr, "unknown sampling op %s,"
235 " check man page\n", s);
236 goto error;
237 }
238
239 *mode |= m->mode;
240
241 if (!p)
242 break;
243
244 s = p + 1;
245 }
246 }
247 ret = 0;
248
249 if (*mode == 0)
250 *mode = MEM_OPERATION_LOAD;
251error:
252 free(os);
253 return ret;
254}
255
028f12ee
SE
256int cmd_mem(int argc, const char **argv, const char *prefix __maybe_unused)
257{
258 struct stat st;
259 struct perf_mem mem = {
260 .tool = {
261 .sample = process_sample_event,
262 .mmap = perf_event__process_mmap,
5c5e854b 263 .mmap2 = perf_event__process_mmap2,
028f12ee
SE
264 .comm = perf_event__process_comm,
265 .lost = perf_event__process_lost,
266 .fork = perf_event__process_fork,
267 .build_id = perf_event__process_build_id,
0a8cb85c 268 .ordered_events = true,
028f12ee
SE
269 },
270 .input_name = "perf.data",
66024122
ACM
271 /*
272 * default to both load an store sampling
273 */
274 .operation = MEM_OPERATION_LOAD | MEM_OPERATION_STORE,
028f12ee
SE
275 };
276 const struct option mem_options[] = {
66024122 277 OPT_CALLBACK('t', "type", &mem.operation,
67121f85
SE
278 "type", "memory operations(load,store) Default load,store",
279 parse_mem_ops),
028f12ee
SE
280 OPT_BOOLEAN('D', "dump-raw-samples", &mem.dump_raw,
281 "dump raw samples in ASCII"),
282 OPT_BOOLEAN('U', "hide-unresolved", &mem.hide_unresolved,
283 "Only display entries resolved to a symbol"),
284 OPT_STRING('i', "input", &input_name, "file",
285 "input file name"),
286 OPT_STRING('C', "cpu", &mem.cpu_list, "cpu",
287 "list of cpus to profile"),
8b8ca6e1 288 OPT_STRING_NOEMPTY('x', "field-separator", &symbol_conf.field_sep,
028f12ee
SE
289 "separator",
290 "separator for columns, no spaces will be added"
291 " between columns '.' is reserved."),
62a1a63a 292 OPT_BOOLEAN('f', "force", &mem.force, "don't complain, do it"),
028f12ee
SE
293 OPT_END()
294 };
8d2a2a1d
RR
295 const char *const mem_subcommands[] = { "record", "report", NULL };
296 const char *mem_usage[] = {
297 NULL,
298 NULL
299 };
300
028f12ee 301
8d2a2a1d
RR
302 argc = parse_options_subcommand(argc, argv, mem_options, mem_subcommands,
303 mem_usage, PARSE_OPT_STOP_AT_NON_OPTION);
028f12ee 304
66024122 305 if (!argc || !(strncmp(argv[0], "rec", 3) || mem.operation))
028f12ee
SE
306 usage_with_options(mem_usage, mem_options);
307
308 if (!mem.input_name || !strlen(mem.input_name)) {
309 if (!fstat(STDIN_FILENO, &st) && S_ISFIFO(st.st_mode))
310 mem.input_name = "-";
311 else
312 mem.input_name = "perf.data";
313 }
314
315 if (!strncmp(argv[0], "rec", 3))
66024122 316 return __cmd_record(argc, argv, &mem);
028f12ee
SE
317 else if (!strncmp(argv[0], "rep", 3))
318 return report_events(argc, argv, &mem);
319 else
320 usage_with_options(mem_usage, mem_options);
321
322 return 0;
323}
This page took 0.101949 seconds and 5 git commands to generate.