blob: da2ec06f0742dc6acf98c1c9b74d7cf45ff0fcb2 [file] [log] [blame]
Stephane Eranian028f12e2013-01-24 16:10:38 +01001#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"
Jiri Olsaf5fc1412013-10-15 16:27:32 +02008#include "util/data.h"
Stephane Eranian028f12e2013-01-24 16:10:38 +01009
Stephane Eranian67121f82014-12-17 16:23:55 +010010#define MEM_OPERATION_LOAD 0x1
11#define MEM_OPERATION_STORE 0x2
Stephane Eranian028f12e2013-01-24 16:10:38 +010012
Stephane Eranian028f12e2013-01-24 16:10:38 +010013struct perf_mem {
14 struct perf_tool tool;
15 char const *input_name;
Stephane Eranian028f12e2013-01-24 16:10:38 +010016 bool hide_unresolved;
17 bool dump_raw;
Yunlong Song62a1a632015-04-02 21:47:15 +080018 bool force;
Arnaldo Carvalho de Melo66024122014-12-17 13:53:27 -030019 int operation;
Stephane Eranian028f12e2013-01-24 16:10:38 +010020 const char *cpu_list;
21 DECLARE_BITMAP(cpu_bitmap, MAX_NR_CPUS);
22};
23
Arnaldo Carvalho de Melo66024122014-12-17 13:53:27 -030024static int __cmd_record(int argc, const char **argv, struct perf_mem *mem)
Stephane Eranian028f12e2013-01-24 16:10:38 +010025{
26 int rec_argc, i = 0, j;
27 const char **rec_argv;
Stephane Eranian028f12e2013-01-24 16:10:38 +010028 int ret;
29
Stephane Eranian67121f82014-12-17 16:23:55 +010030 rec_argc = argc + 7; /* max number of arguments */
Stephane Eranian028f12e2013-01-24 16:10:38 +010031 rec_argv = calloc(rec_argc + 1, sizeof(char *));
32 if (!rec_argv)
33 return -1;
34
Stephane Eranian67121f82014-12-17 16:23:55 +010035 rec_argv[i++] = "record";
Stephane Eranian028f12e2013-01-24 16:10:38 +010036
Arnaldo Carvalho de Melo66024122014-12-17 13:53:27 -030037 if (mem->operation & MEM_OPERATION_LOAD)
Stephane Eranian67121f82014-12-17 16:23:55 +010038 rec_argv[i++] = "-W";
Stephane Eranian028f12e2013-01-24 16:10:38 +010039
Stephane Eranian67121f82014-12-17 16:23:55 +010040 rec_argv[i++] = "-d";
41
Arnaldo Carvalho de Melo66024122014-12-17 13:53:27 -030042 if (mem->operation & MEM_OPERATION_LOAD) {
Stephane Eranian67121f82014-12-17 16:23:55 +010043 rec_argv[i++] = "-e";
44 rec_argv[i++] = "cpu/mem-loads/pp";
45 }
46
Arnaldo Carvalho de Melo66024122014-12-17 13:53:27 -030047 if (mem->operation & MEM_OPERATION_STORE) {
Stephane Eranian67121f82014-12-17 16:23:55 +010048 rec_argv[i++] = "-e";
49 rec_argv[i++] = "cpu/mem-stores/pp";
50 }
51
Stephane Eranian028f12e2013-01-24 16:10:38 +010052 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,
Stephane Eranian028f12e2013-01-24 16:10:38 +010064 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
Adrian Huntere44baa32013-08-08 14:32:25 +030070 if (perf_event__preprocess_sample(event, machine, &al, sample) < 0) {
Stephane Eranian028f12e2013-01-24 16:10:38 +010071 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))
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -030077 goto out_put;
Stephane Eranian028f12e2013-01-24 16:10:38 +010078
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,
Adrian Hunteref893252013-08-27 11:23:06 +030096 sample->ip,
Stephane Eranian028f12e2013-01-24 16:10:38 +010097 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 : "???");
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -0300106out_put:
107 addr_location__put(&al);
Stephane Eranian028f12e2013-01-24 16:10:38 +0100108 return 0;
109}
110
111static int process_sample_event(struct perf_tool *tool,
112 union perf_event *event,
113 struct perf_sample *sample,
Arnaldo Carvalho de Melo8b640cc2013-12-19 17:00:45 -0300114 struct perf_evsel *evsel __maybe_unused,
Stephane Eranian028f12e2013-01-24 16:10:38 +0100115 struct machine *machine)
116{
Arnaldo Carvalho de Melo8b640cc2013-12-19 17:00:45 -0300117 return dump_raw_samples(tool, event, sample, machine);
Stephane Eranian028f12e2013-01-24 16:10:38 +0100118}
119
120static int report_raw_events(struct perf_mem *mem)
121{
Jiri Olsaf5fc1412013-10-15 16:27:32 +0200122 struct perf_data_file file = {
123 .path = input_name,
124 .mode = PERF_DATA_MODE_READ,
Yunlong Song62a1a632015-04-02 21:47:15 +0800125 .force = mem->force,
Jiri Olsaf5fc1412013-10-15 16:27:32 +0200126 };
Stephane Eranian028f12e2013-01-24 16:10:38 +0100127 int err = -EINVAL;
128 int ret;
Jiri Olsaf5fc1412013-10-15 16:27:32 +0200129 struct perf_session *session = perf_session__new(&file, false,
130 &mem->tool);
Stephane Eranian028f12e2013-01-24 16:10:38 +0100131
132 if (session == NULL)
Taeung Song52e028342014-09-24 10:33:37 +0900133 return -1;
Stephane Eranian028f12e2013-01-24 16:10:38 +0100134
135 if (mem->cpu_list) {
136 ret = perf_session__cpu_bitmap(session, mem->cpu_list,
137 mem->cpu_bitmap);
138 if (ret)
139 goto out_delete;
140 }
141
Namhyung Kim0a7e6d12014-08-12 15:40:45 +0900142 if (symbol__init(&session->header.env) < 0)
Stephane Eranian028f12e2013-01-24 16:10:38 +0100143 return -1;
144
145 printf("# PID, TID, IP, ADDR, LOCAL WEIGHT, DSRC, SYMBOL\n");
146
Arnaldo Carvalho de Melob7b61cb2015-03-03 11:58:45 -0300147 err = perf_session__process_events(session);
Stephane Eranian028f12e2013-01-24 16:10:38 +0100148 if (err)
149 return err;
150
151 return 0;
152
153out_delete:
154 perf_session__delete(session);
155 return err;
156}
157
158static int report_events(int argc, const char **argv, struct perf_mem *mem)
159{
160 const char **rep_argv;
161 int ret, i = 0, j, rep_argc;
162
163 if (mem->dump_raw)
164 return report_raw_events(mem);
165
166 rep_argc = argc + 3;
167 rep_argv = calloc(rep_argc + 1, sizeof(char *));
168 if (!rep_argv)
169 return -1;
170
Stephane Eranian67121f82014-12-17 16:23:55 +0100171 rep_argv[i++] = "report";
172 rep_argv[i++] = "--mem-mode";
173 rep_argv[i++] = "-n"; /* display number of samples */
Stephane Eranian028f12e2013-01-24 16:10:38 +0100174
175 /*
176 * there is no weight (cost) associated with stores, so don't print
177 * the column
178 */
Arnaldo Carvalho de Melo66024122014-12-17 13:53:27 -0300179 if (!(mem->operation & MEM_OPERATION_LOAD))
Stephane Eranian67121f82014-12-17 16:23:55 +0100180 rep_argv[i++] = "--sort=mem,sym,dso,symbol_daddr,"
181 "dso_daddr,tlb,locked";
Stephane Eranian028f12e2013-01-24 16:10:38 +0100182
183 for (j = 1; j < argc; j++, i++)
184 rep_argv[i] = argv[j];
185
186 ret = cmd_report(i, rep_argv, NULL);
187 free(rep_argv);
188 return ret;
189}
190
Stephane Eranian67121f82014-12-17 16:23:55 +0100191struct mem_mode {
192 const char *name;
193 int mode;
194};
195
196#define MEM_OPT(n, m) \
197 { .name = n, .mode = (m) }
198
199#define MEM_END { .name = NULL }
200
201static const struct mem_mode mem_modes[]={
202 MEM_OPT("load", MEM_OPERATION_LOAD),
203 MEM_OPT("store", MEM_OPERATION_STORE),
204 MEM_END
205};
206
207static int
208parse_mem_ops(const struct option *opt, const char *str, int unset)
209{
210 int *mode = (int *)opt->value;
211 const struct mem_mode *m;
212 char *s, *os = NULL, *p;
213 int ret = -1;
214
215 if (unset)
216 return 0;
217
218 /* str may be NULL in case no arg is passed to -t */
219 if (str) {
220 /* because str is read-only */
221 s = os = strdup(str);
222 if (!s)
223 return -1;
224
225 /* reset mode */
226 *mode = 0;
227
228 for (;;) {
229 p = strchr(s, ',');
230 if (p)
231 *p = '\0';
232
233 for (m = mem_modes; m->name; m++) {
234 if (!strcasecmp(s, m->name))
235 break;
236 }
237 if (!m->name) {
238 fprintf(stderr, "unknown sampling op %s,"
239 " check man page\n", s);
240 goto error;
241 }
242
243 *mode |= m->mode;
244
245 if (!p)
246 break;
247
248 s = p + 1;
249 }
250 }
251 ret = 0;
252
253 if (*mode == 0)
254 *mode = MEM_OPERATION_LOAD;
255error:
256 free(os);
257 return ret;
258}
259
Stephane Eranian028f12e2013-01-24 16:10:38 +0100260int cmd_mem(int argc, const char **argv, const char *prefix __maybe_unused)
261{
262 struct stat st;
263 struct perf_mem mem = {
264 .tool = {
265 .sample = process_sample_event,
266 .mmap = perf_event__process_mmap,
Stephane Eranian5c5e8542013-08-21 12:10:25 +0200267 .mmap2 = perf_event__process_mmap2,
Stephane Eranian028f12e2013-01-24 16:10:38 +0100268 .comm = perf_event__process_comm,
269 .lost = perf_event__process_lost,
270 .fork = perf_event__process_fork,
271 .build_id = perf_event__process_build_id,
Jiri Olsa0a8cb852014-07-06 14:18:21 +0200272 .ordered_events = true,
Stephane Eranian028f12e2013-01-24 16:10:38 +0100273 },
274 .input_name = "perf.data",
Arnaldo Carvalho de Melo66024122014-12-17 13:53:27 -0300275 /*
276 * default to both load an store sampling
277 */
278 .operation = MEM_OPERATION_LOAD | MEM_OPERATION_STORE,
Stephane Eranian028f12e2013-01-24 16:10:38 +0100279 };
280 const struct option mem_options[] = {
Arnaldo Carvalho de Melo66024122014-12-17 13:53:27 -0300281 OPT_CALLBACK('t', "type", &mem.operation,
Stephane Eranian67121f82014-12-17 16:23:55 +0100282 "type", "memory operations(load,store) Default load,store",
283 parse_mem_ops),
Stephane Eranian028f12e2013-01-24 16:10:38 +0100284 OPT_BOOLEAN('D', "dump-raw-samples", &mem.dump_raw,
285 "dump raw samples in ASCII"),
286 OPT_BOOLEAN('U', "hide-unresolved", &mem.hide_unresolved,
287 "Only display entries resolved to a symbol"),
288 OPT_STRING('i', "input", &input_name, "file",
289 "input file name"),
290 OPT_STRING('C', "cpu", &mem.cpu_list, "cpu",
291 "list of cpus to profile"),
Wang Nan8b8ca6e2015-03-20 02:57:52 +0000292 OPT_STRING_NOEMPTY('x', "field-separator", &symbol_conf.field_sep,
Stephane Eranian028f12e2013-01-24 16:10:38 +0100293 "separator",
294 "separator for columns, no spaces will be added"
295 " between columns '.' is reserved."),
Yunlong Song62a1a632015-04-02 21:47:15 +0800296 OPT_BOOLEAN('f', "force", &mem.force, "don't complain, do it"),
Stephane Eranian028f12e2013-01-24 16:10:38 +0100297 OPT_END()
298 };
Ramkumar Ramachandra8d2a2a12014-03-14 23:17:52 -0400299 const char *const mem_subcommands[] = { "record", "report", NULL };
300 const char *mem_usage[] = {
301 NULL,
302 NULL
303 };
Stephane Eranian028f12e2013-01-24 16:10:38 +0100304
Ramkumar Ramachandra8d2a2a12014-03-14 23:17:52 -0400305
306 argc = parse_options_subcommand(argc, argv, mem_options, mem_subcommands,
307 mem_usage, PARSE_OPT_STOP_AT_NON_OPTION);
Stephane Eranian028f12e2013-01-24 16:10:38 +0100308
Arnaldo Carvalho de Melo66024122014-12-17 13:53:27 -0300309 if (!argc || !(strncmp(argv[0], "rec", 3) || mem.operation))
Stephane Eranian028f12e2013-01-24 16:10:38 +0100310 usage_with_options(mem_usage, mem_options);
311
312 if (!mem.input_name || !strlen(mem.input_name)) {
313 if (!fstat(STDIN_FILENO, &st) && S_ISFIFO(st.st_mode))
314 mem.input_name = "-";
315 else
316 mem.input_name = "perf.data";
317 }
318
319 if (!strncmp(argv[0], "rec", 3))
Arnaldo Carvalho de Melo66024122014-12-17 13:53:27 -0300320 return __cmd_record(argc, argv, &mem);
Stephane Eranian028f12e2013-01-24 16:10:38 +0100321 else if (!strncmp(argv[0], "rep", 3))
322 return report_events(argc, argv, &mem);
323 else
324 usage_with_options(mem_usage, mem_options);
325
326 return 0;
327}