blob: 36c56a4b778bbda33563f2c51a80fce26cc8438a [file] [log] [blame]
Stephane Eranian028f12e2013-01-24 16:10:38 +01001#include "builtin.h"
2#include "perf.h"
3
Josh Poimboeuf4b6ab942015-12-15 09:39:39 -06004#include <subcmd/parse-options.h>
Stephane Eranian028f12e2013-01-24 16:10:38 +01005#include "util/trace-event.h"
6#include "util/tool.h"
7#include "util/session.h"
Jiri Olsaf5fc14122013-10-15 16:27:32 +02008#include "util/data.h"
Jiri Olsaacbe6132016-02-15 09:34:34 +01009#include "util/mem-events.h"
Stephane Eranian028f12e2013-01-24 16:10:38 +010010
Stephane Eranian67121f82014-12-17 16:23:55 +010011#define MEM_OPERATION_LOAD 0x1
12#define MEM_OPERATION_STORE 0x2
Stephane Eranian028f12e2013-01-24 16:10:38 +010013
Stephane Eranian028f12e2013-01-24 16:10:38 +010014struct perf_mem {
15 struct perf_tool tool;
16 char const *input_name;
Stephane Eranian028f12e2013-01-24 16:10:38 +010017 bool hide_unresolved;
18 bool dump_raw;
Yunlong Song62a1a632015-04-02 21:47:15 +080019 bool force;
Arnaldo Carvalho de Melo66024122014-12-17 13:53:27 -030020 int operation;
Stephane Eranian028f12e2013-01-24 16:10:38 +010021 const char *cpu_list;
22 DECLARE_BITMAP(cpu_bitmap, MAX_NR_CPUS);
23};
24
Arnaldo Carvalho de Melo66024122014-12-17 13:53:27 -030025static int __cmd_record(int argc, const char **argv, struct perf_mem *mem)
Stephane Eranian028f12e2013-01-24 16:10:38 +010026{
27 int rec_argc, i = 0, j;
28 const char **rec_argv;
Stephane Eranian028f12e2013-01-24 16:10:38 +010029 int ret;
30
Stephane Eranian67121f82014-12-17 16:23:55 +010031 rec_argc = argc + 7; /* max number of arguments */
Stephane Eranian028f12e2013-01-24 16:10:38 +010032 rec_argv = calloc(rec_argc + 1, sizeof(char *));
33 if (!rec_argv)
34 return -1;
35
Stephane Eranian67121f82014-12-17 16:23:55 +010036 rec_argv[i++] = "record";
Stephane Eranian028f12e2013-01-24 16:10:38 +010037
Jiri Olsaacbe6132016-02-15 09:34:34 +010038 if (mem->operation & MEM_OPERATION_LOAD) {
39 perf_mem_events[PERF_MEM_EVENTS__LOAD].record = true;
Stephane Eranian67121f82014-12-17 16:23:55 +010040 rec_argv[i++] = "-W";
Jiri Olsaacbe6132016-02-15 09:34:34 +010041 }
Stephane Eranian028f12e2013-01-24 16:10:38 +010042
Stephane Eranian67121f82014-12-17 16:23:55 +010043 rec_argv[i++] = "-d";
44
Jiri Olsaacbe6132016-02-15 09:34:34 +010045 for (j = 0; j < PERF_MEM_EVENTS__MAX; j++) {
46 if (!perf_mem_events[j].record)
47 continue;
Stephane Eranian67121f82014-12-17 16:23:55 +010048
Stephane Eranian67121f82014-12-17 16:23:55 +010049 rec_argv[i++] = "-e";
Jiri Olsaacbe6132016-02-15 09:34:34 +010050 rec_argv[i++] = perf_mem_events[j].name;
51 };
Stephane Eranian67121f82014-12-17 16:23:55 +010052
Stephane Eranian028f12e2013-01-24 16:10:38 +010053 for (j = 1; j < argc; j++, i++)
54 rec_argv[i] = argv[j];
55
56 ret = cmd_record(i, rec_argv, NULL);
57 free(rec_argv);
58 return ret;
59}
60
61static int
62dump_raw_samples(struct perf_tool *tool,
63 union perf_event *event,
64 struct perf_sample *sample,
Stephane Eranian028f12e2013-01-24 16:10:38 +010065 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
Adrian Huntere44baa32013-08-08 14:32:25 +030071 if (perf_event__preprocess_sample(event, machine, &al, sample) < 0) {
Stephane Eranian028f12e2013-01-24 16:10:38 +010072 fprintf(stderr, "problem processing %d event, skipping it.\n",
73 event->header.type);
74 return -1;
75 }
76
77 if (al.filtered || (mem->hide_unresolved && al.sym == NULL))
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -030078 goto out_put;
Stephane Eranian028f12e2013-01-24 16:10:38 +010079
80 if (al.map != NULL)
81 al.map->dso->hit = 1;
82
83 if (symbol_conf.field_sep) {
84 fmt = "%d%s%d%s0x%"PRIx64"%s0x%"PRIx64"%s%"PRIu64
85 "%s0x%"PRIx64"%s%s:%s\n";
86 } else {
87 fmt = "%5d%s%5d%s0x%016"PRIx64"%s0x016%"PRIx64
88 "%s%5"PRIu64"%s0x%06"PRIx64"%s%s:%s\n";
89 symbol_conf.field_sep = " ";
90 }
91
92 printf(fmt,
93 sample->pid,
94 symbol_conf.field_sep,
95 sample->tid,
96 symbol_conf.field_sep,
Adrian Hunteref893252013-08-27 11:23:06 +030097 sample->ip,
Stephane Eranian028f12e2013-01-24 16:10:38 +010098 symbol_conf.field_sep,
99 sample->addr,
100 symbol_conf.field_sep,
101 sample->weight,
102 symbol_conf.field_sep,
103 sample->data_src,
104 symbol_conf.field_sep,
105 al.map ? (al.map->dso ? al.map->dso->long_name : "???") : "???",
106 al.sym ? al.sym->name : "???");
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -0300107out_put:
108 addr_location__put(&al);
Stephane Eranian028f12e2013-01-24 16:10:38 +0100109 return 0;
110}
111
112static int process_sample_event(struct perf_tool *tool,
113 union perf_event *event,
114 struct perf_sample *sample,
Arnaldo Carvalho de Melo8b640cc2013-12-19 17:00:45 -0300115 struct perf_evsel *evsel __maybe_unused,
Stephane Eranian028f12e2013-01-24 16:10:38 +0100116 struct machine *machine)
117{
Arnaldo Carvalho de Melo8b640cc2013-12-19 17:00:45 -0300118 return dump_raw_samples(tool, event, sample, machine);
Stephane Eranian028f12e2013-01-24 16:10:38 +0100119}
120
121static int report_raw_events(struct perf_mem *mem)
122{
Jiri Olsaf5fc14122013-10-15 16:27:32 +0200123 struct perf_data_file file = {
124 .path = input_name,
125 .mode = PERF_DATA_MODE_READ,
Yunlong Song62a1a632015-04-02 21:47:15 +0800126 .force = mem->force,
Jiri Olsaf5fc14122013-10-15 16:27:32 +0200127 };
Stephane Eranian028f12e2013-01-24 16:10:38 +0100128 int ret;
Jiri Olsaf5fc14122013-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);
Taeung Song1df9fade2015-07-01 21:08:19 +0900138 if (ret < 0)
Stephane Eranian028f12e2013-01-24 16:10:38 +0100139 goto out_delete;
140 }
141
Taeung Song1df9fade2015-07-01 21:08:19 +0900142 ret = symbol__init(&session->header.env);
143 if (ret < 0)
144 goto out_delete;
Stephane Eranian028f12e2013-01-24 16:10:38 +0100145
146 printf("# PID, TID, IP, ADDR, LOCAL WEIGHT, DSRC, SYMBOL\n");
147
Taeung Song1df9fade2015-07-01 21:08:19 +0900148 ret = perf_session__process_events(session);
Stephane Eranian028f12e2013-01-24 16:10:38 +0100149
150out_delete:
151 perf_session__delete(session);
Taeung Song1df9fade2015-07-01 21:08:19 +0900152 return ret;
Stephane Eranian028f12e2013-01-24 16:10:38 +0100153}
154
155static int report_events(int argc, const char **argv, struct perf_mem *mem)
156{
157 const char **rep_argv;
158 int ret, i = 0, j, rep_argc;
159
160 if (mem->dump_raw)
161 return report_raw_events(mem);
162
163 rep_argc = argc + 3;
164 rep_argv = calloc(rep_argc + 1, sizeof(char *));
165 if (!rep_argv)
166 return -1;
167
Stephane Eranian67121f82014-12-17 16:23:55 +0100168 rep_argv[i++] = "report";
169 rep_argv[i++] = "--mem-mode";
170 rep_argv[i++] = "-n"; /* display number of samples */
Stephane Eranian028f12e2013-01-24 16:10:38 +0100171
172 /*
173 * there is no weight (cost) associated with stores, so don't print
174 * the column
175 */
Arnaldo Carvalho de Melo66024122014-12-17 13:53:27 -0300176 if (!(mem->operation & MEM_OPERATION_LOAD))
Stephane Eranian67121f82014-12-17 16:23:55 +0100177 rep_argv[i++] = "--sort=mem,sym,dso,symbol_daddr,"
178 "dso_daddr,tlb,locked";
Stephane Eranian028f12e2013-01-24 16:10:38 +0100179
180 for (j = 1; j < argc; j++, i++)
181 rep_argv[i] = argv[j];
182
183 ret = cmd_report(i, rep_argv, NULL);
184 free(rep_argv);
185 return ret;
186}
187
Stephane Eranian67121f82014-12-17 16:23:55 +0100188struct mem_mode {
189 const char *name;
190 int mode;
191};
192
193#define MEM_OPT(n, m) \
194 { .name = n, .mode = (m) }
195
196#define MEM_END { .name = NULL }
197
198static const struct mem_mode mem_modes[]={
199 MEM_OPT("load", MEM_OPERATION_LOAD),
200 MEM_OPT("store", MEM_OPERATION_STORE),
201 MEM_END
202};
203
204static int
205parse_mem_ops(const struct option *opt, const char *str, int unset)
206{
207 int *mode = (int *)opt->value;
208 const struct mem_mode *m;
209 char *s, *os = NULL, *p;
210 int ret = -1;
211
212 if (unset)
213 return 0;
214
215 /* str may be NULL in case no arg is passed to -t */
216 if (str) {
217 /* because str is read-only */
218 s = os = strdup(str);
219 if (!s)
220 return -1;
221
222 /* reset mode */
223 *mode = 0;
224
225 for (;;) {
226 p = strchr(s, ',');
227 if (p)
228 *p = '\0';
229
230 for (m = mem_modes; m->name; m++) {
231 if (!strcasecmp(s, m->name))
232 break;
233 }
234 if (!m->name) {
235 fprintf(stderr, "unknown sampling op %s,"
236 " check man page\n", s);
237 goto error;
238 }
239
240 *mode |= m->mode;
241
242 if (!p)
243 break;
244
245 s = p + 1;
246 }
247 }
248 ret = 0;
249
250 if (*mode == 0)
251 *mode = MEM_OPERATION_LOAD;
252error:
253 free(os);
254 return ret;
255}
256
Stephane Eranian028f12e2013-01-24 16:10:38 +0100257int cmd_mem(int argc, const char **argv, const char *prefix __maybe_unused)
258{
259 struct stat st;
260 struct perf_mem mem = {
261 .tool = {
262 .sample = process_sample_event,
263 .mmap = perf_event__process_mmap,
Stephane Eranian5c5e8542013-08-21 12:10:25 +0200264 .mmap2 = perf_event__process_mmap2,
Stephane Eranian028f12e2013-01-24 16:10:38 +0100265 .comm = perf_event__process_comm,
266 .lost = perf_event__process_lost,
267 .fork = perf_event__process_fork,
268 .build_id = perf_event__process_build_id,
Jiri Olsa0a8cb852014-07-06 14:18:21 +0200269 .ordered_events = true,
Stephane Eranian028f12e2013-01-24 16:10:38 +0100270 },
271 .input_name = "perf.data",
Arnaldo Carvalho de Melo66024122014-12-17 13:53:27 -0300272 /*
273 * default to both load an store sampling
274 */
275 .operation = MEM_OPERATION_LOAD | MEM_OPERATION_STORE,
Stephane Eranian028f12e2013-01-24 16:10:38 +0100276 };
277 const struct option mem_options[] = {
Arnaldo Carvalho de Melo66024122014-12-17 13:53:27 -0300278 OPT_CALLBACK('t', "type", &mem.operation,
Stephane Eranian67121f82014-12-17 16:23:55 +0100279 "type", "memory operations(load,store) Default load,store",
280 parse_mem_ops),
Stephane Eranian028f12e2013-01-24 16:10:38 +0100281 OPT_BOOLEAN('D', "dump-raw-samples", &mem.dump_raw,
282 "dump raw samples in ASCII"),
283 OPT_BOOLEAN('U', "hide-unresolved", &mem.hide_unresolved,
284 "Only display entries resolved to a symbol"),
285 OPT_STRING('i', "input", &input_name, "file",
286 "input file name"),
287 OPT_STRING('C', "cpu", &mem.cpu_list, "cpu",
288 "list of cpus to profile"),
Wang Nan8b8ca6e2015-03-20 02:57:52 +0000289 OPT_STRING_NOEMPTY('x', "field-separator", &symbol_conf.field_sep,
Stephane Eranian028f12e2013-01-24 16:10:38 +0100290 "separator",
291 "separator for columns, no spaces will be added"
292 " between columns '.' is reserved."),
Yunlong Song62a1a632015-04-02 21:47:15 +0800293 OPT_BOOLEAN('f', "force", &mem.force, "don't complain, do it"),
Stephane Eranian028f12e2013-01-24 16:10:38 +0100294 OPT_END()
295 };
Ramkumar Ramachandra8d2a2a12014-03-14 23:17:52 -0400296 const char *const mem_subcommands[] = { "record", "report", NULL };
297 const char *mem_usage[] = {
298 NULL,
299 NULL
300 };
Stephane Eranian028f12e2013-01-24 16:10:38 +0100301
Ramkumar Ramachandra8d2a2a12014-03-14 23:17:52 -0400302
303 argc = parse_options_subcommand(argc, argv, mem_options, mem_subcommands,
304 mem_usage, PARSE_OPT_STOP_AT_NON_OPTION);
Stephane Eranian028f12e2013-01-24 16:10:38 +0100305
Arnaldo Carvalho de Melo66024122014-12-17 13:53:27 -0300306 if (!argc || !(strncmp(argv[0], "rec", 3) || mem.operation))
Stephane Eranian028f12e2013-01-24 16:10:38 +0100307 usage_with_options(mem_usage, mem_options);
308
309 if (!mem.input_name || !strlen(mem.input_name)) {
310 if (!fstat(STDIN_FILENO, &st) && S_ISFIFO(st.st_mode))
311 mem.input_name = "-";
312 else
313 mem.input_name = "perf.data";
314 }
315
316 if (!strncmp(argv[0], "rec", 3))
Arnaldo Carvalho de Melo66024122014-12-17 13:53:27 -0300317 return __cmd_record(argc, argv, &mem);
Stephane Eranian028f12e2013-01-24 16:10:38 +0100318 else if (!strncmp(argv[0], "rep", 3))
319 return report_events(argc, argv, &mem);
320 else
321 usage_with_options(mem_usage, mem_options);
322
323 return 0;
324}