4 #include <subcmd/parse-options.h>
5 #include "util/trace-event.h"
7 #include "util/session.h"
9 #include "util/mem-events.h"
10 #include "util/debug.h"
12 #define MEM_OPERATION_LOAD 0x1
13 #define MEM_OPERATION_STORE 0x2
16 struct perf_tool tool
;
17 char const *input_name
;
23 DECLARE_BITMAP(cpu_bitmap
, MAX_NR_CPUS
);
26 static int parse_record_events(const struct option
*opt
,
27 const char *str
, int unset __maybe_unused
)
29 struct perf_mem
*mem
= *(struct perf_mem
**)opt
->value
;
32 if (strcmp(str
, "list")) {
33 if (!perf_mem_events__parse(str
)) {
40 for (j
= 0; j
< PERF_MEM_EVENTS__MAX
; j
++) {
41 struct perf_mem_event
*e
= &perf_mem_events
[j
];
43 fprintf(stderr
, "%-13s%-*s%s\n",
46 verbose
? perf_mem_events__name(j
) : "",
47 e
->supported
? ": available" : "");
52 static const char * const __usage
[] = {
53 "perf mem record [<options>] [<command>]",
54 "perf mem record [<options>] -- <command> [<options>]",
58 static const char * const *record_mem_usage
= __usage
;
60 static int __cmd_record(int argc
, const char **argv
, struct perf_mem
*mem
)
62 int rec_argc
, i
= 0, j
;
63 const char **rec_argv
;
65 bool all_user
= false, all_kernel
= false;
66 struct option options
[] = {
67 OPT_CALLBACK('e', "event", &mem
, "event",
68 "event selector. use 'perf mem record -e list' to list available events",
70 OPT_UINTEGER(0, "ldlat", &perf_mem_events__loads_ldlat
, "mem-loads latency"),
71 OPT_INCR('v', "verbose", &verbose
,
72 "be more verbose (show counter open errors, etc)"),
73 OPT_BOOLEAN('U', "--all-user", &all_user
, "collect only user level data"),
74 OPT_BOOLEAN('K', "--all-kernel", &all_kernel
, "collect only kernel level data"),
78 argc
= parse_options(argc
, argv
, options
, record_mem_usage
,
79 PARSE_OPT_STOP_AT_NON_OPTION
);
81 rec_argc
= argc
+ 9; /* max number of arguments */
82 rec_argv
= calloc(rec_argc
+ 1, sizeof(char *));
86 rec_argv
[i
++] = "record";
88 if (mem
->operation
& MEM_OPERATION_LOAD
)
89 perf_mem_events
[PERF_MEM_EVENTS__LOAD
].record
= true;
91 if (mem
->operation
& MEM_OPERATION_STORE
)
92 perf_mem_events
[PERF_MEM_EVENTS__STORE
].record
= true;
94 if (perf_mem_events
[PERF_MEM_EVENTS__LOAD
].record
)
99 for (j
= 0; j
< PERF_MEM_EVENTS__MAX
; j
++) {
100 if (!perf_mem_events
[j
].record
)
103 if (!perf_mem_events
[j
].supported
) {
104 pr_err("failed: event '%s' not supported\n",
105 perf_mem_events__name(j
));
109 rec_argv
[i
++] = "-e";
110 rec_argv
[i
++] = perf_mem_events__name(j
);
114 rec_argv
[i
++] = "--all-user";
117 rec_argv
[i
++] = "--all-kernel";
119 for (j
= 0; j
< argc
; j
++, i
++)
120 rec_argv
[i
] = argv
[j
];
123 pr_debug("calling: record ");
125 while (rec_argv
[j
]) {
126 pr_debug("%s ", rec_argv
[j
]);
132 ret
= cmd_record(i
, rec_argv
, NULL
);
138 dump_raw_samples(struct perf_tool
*tool
,
139 union perf_event
*event
,
140 struct perf_sample
*sample
,
141 struct machine
*machine
)
143 struct perf_mem
*mem
= container_of(tool
, struct perf_mem
, tool
);
144 struct addr_location al
;
147 if (machine__resolve(machine
, &al
, sample
) < 0) {
148 fprintf(stderr
, "problem processing %d event, skipping it.\n",
153 if (al
.filtered
|| (mem
->hide_unresolved
&& al
.sym
== NULL
))
157 al
.map
->dso
->hit
= 1;
159 if (symbol_conf
.field_sep
) {
160 fmt
= "%d%s%d%s0x%"PRIx64
"%s0x%"PRIx64
"%s%"PRIu64
161 "%s0x%"PRIx64
"%s%s:%s\n";
163 fmt
= "%5d%s%5d%s0x%016"PRIx64
"%s0x016%"PRIx64
164 "%s%5"PRIu64
"%s0x%06"PRIx64
"%s%s:%s\n";
165 symbol_conf
.field_sep
= " ";
170 symbol_conf
.field_sep
,
172 symbol_conf
.field_sep
,
174 symbol_conf
.field_sep
,
176 symbol_conf
.field_sep
,
178 symbol_conf
.field_sep
,
180 symbol_conf
.field_sep
,
181 al
.map
? (al
.map
->dso
? al
.map
->dso
->long_name
: "???") : "???",
182 al
.sym
? al
.sym
->name
: "???");
184 addr_location__put(&al
);
188 static int process_sample_event(struct perf_tool
*tool
,
189 union perf_event
*event
,
190 struct perf_sample
*sample
,
191 struct perf_evsel
*evsel __maybe_unused
,
192 struct machine
*machine
)
194 return dump_raw_samples(tool
, event
, sample
, machine
);
197 static int report_raw_events(struct perf_mem
*mem
)
199 struct perf_data_file file
= {
201 .mode
= PERF_DATA_MODE_READ
,
205 struct perf_session
*session
= perf_session__new(&file
, false,
212 ret
= perf_session__cpu_bitmap(session
, mem
->cpu_list
,
218 ret
= symbol__init(&session
->header
.env
);
222 printf("# PID, TID, IP, ADDR, LOCAL WEIGHT, DSRC, SYMBOL\n");
224 ret
= perf_session__process_events(session
);
227 perf_session__delete(session
);
231 static int report_events(int argc
, const char **argv
, struct perf_mem
*mem
)
233 const char **rep_argv
;
234 int ret
, i
= 0, j
, rep_argc
;
237 return report_raw_events(mem
);
240 rep_argv
= calloc(rep_argc
+ 1, sizeof(char *));
244 rep_argv
[i
++] = "report";
245 rep_argv
[i
++] = "--mem-mode";
246 rep_argv
[i
++] = "-n"; /* display number of samples */
249 * there is no weight (cost) associated with stores, so don't print
252 if (!(mem
->operation
& MEM_OPERATION_LOAD
))
253 rep_argv
[i
++] = "--sort=mem,sym,dso,symbol_daddr,"
254 "dso_daddr,tlb,locked";
256 for (j
= 1; j
< argc
; j
++, i
++)
257 rep_argv
[i
] = argv
[j
];
259 ret
= cmd_report(i
, rep_argv
, NULL
);
269 #define MEM_OPT(n, m) \
270 { .name = n, .mode = (m) }
272 #define MEM_END { .name = NULL }
274 static const struct mem_mode mem_modes
[]={
275 MEM_OPT("load", MEM_OPERATION_LOAD
),
276 MEM_OPT("store", MEM_OPERATION_STORE
),
281 parse_mem_ops(const struct option
*opt
, const char *str
, int unset
)
283 int *mode
= (int *)opt
->value
;
284 const struct mem_mode
*m
;
285 char *s
, *os
= NULL
, *p
;
291 /* str may be NULL in case no arg is passed to -t */
293 /* because str is read-only */
294 s
= os
= strdup(str
);
306 for (m
= mem_modes
; m
->name
; m
++) {
307 if (!strcasecmp(s
, m
->name
))
311 fprintf(stderr
, "unknown sampling op %s,"
312 " check man page\n", s
);
327 *mode
= MEM_OPERATION_LOAD
;
333 int cmd_mem(int argc
, const char **argv
, const char *prefix __maybe_unused
)
336 struct perf_mem mem
= {
338 .sample
= process_sample_event
,
339 .mmap
= perf_event__process_mmap
,
340 .mmap2
= perf_event__process_mmap2
,
341 .comm
= perf_event__process_comm
,
342 .lost
= perf_event__process_lost
,
343 .fork
= perf_event__process_fork
,
344 .build_id
= perf_event__process_build_id
,
345 .ordered_events
= true,
347 .input_name
= "perf.data",
349 * default to both load an store sampling
351 .operation
= MEM_OPERATION_LOAD
| MEM_OPERATION_STORE
,
353 const struct option mem_options
[] = {
354 OPT_CALLBACK('t', "type", &mem
.operation
,
355 "type", "memory operations(load,store) Default load,store",
357 OPT_BOOLEAN('D', "dump-raw-samples", &mem
.dump_raw
,
358 "dump raw samples in ASCII"),
359 OPT_BOOLEAN('U', "hide-unresolved", &mem
.hide_unresolved
,
360 "Only display entries resolved to a symbol"),
361 OPT_STRING('i', "input", &input_name
, "file",
363 OPT_STRING('C', "cpu", &mem
.cpu_list
, "cpu",
364 "list of cpus to profile"),
365 OPT_STRING_NOEMPTY('x', "field-separator", &symbol_conf
.field_sep
,
367 "separator for columns, no spaces will be added"
368 " between columns '.' is reserved."),
369 OPT_BOOLEAN('f', "force", &mem
.force
, "don't complain, do it"),
372 const char *const mem_subcommands
[] = { "record", "report", NULL
};
373 const char *mem_usage
[] = {
378 if (perf_mem_events__init()) {
379 pr_err("failed: memory events not supported\n");
383 argc
= parse_options_subcommand(argc
, argv
, mem_options
, mem_subcommands
,
384 mem_usage
, PARSE_OPT_STOP_AT_NON_OPTION
);
386 if (!argc
|| !(strncmp(argv
[0], "rec", 3) || mem
.operation
))
387 usage_with_options(mem_usage
, mem_options
);
389 if (!mem
.input_name
|| !strlen(mem
.input_name
)) {
390 if (!fstat(STDIN_FILENO
, &st
) && S_ISFIFO(st
.st_mode
))
391 mem
.input_name
= "-";
393 mem
.input_name
= "perf.data";
396 if (!strncmp(argv
[0], "rec", 3))
397 return __cmd_record(argc
, argv
, &mem
);
398 else if (!strncmp(argv
[0], "rep", 3))
399 return report_events(argc
, argv
, &mem
);
401 usage_with_options(mem_usage
, mem_options
);