8 #include <subcmd/parse-options.h>
9 #include "util/trace-event.h"
10 #include "util/tool.h"
11 #include "util/session.h"
12 #include "util/data.h"
13 #include "util/mem-events.h"
14 #include "util/debug.h"
15 #include "util/symbol.h"
17 #define MEM_OPERATION_LOAD 0x1
18 #define MEM_OPERATION_STORE 0x2
21 struct perf_tool tool
;
22 char const *input_name
;
29 DECLARE_BITMAP(cpu_bitmap
, MAX_NR_CPUS
);
32 static int parse_record_events(const struct option
*opt
,
33 const char *str
, int unset __maybe_unused
)
35 struct perf_mem
*mem
= *(struct perf_mem
**)opt
->value
;
38 if (strcmp(str
, "list")) {
39 if (!perf_mem_events__parse(str
)) {
46 for (j
= 0; j
< PERF_MEM_EVENTS__MAX
; j
++) {
47 struct perf_mem_event
*e
= &perf_mem_events
[j
];
49 fprintf(stderr
, "%-13s%-*s%s\n",
52 verbose
> 0 ? perf_mem_events__name(j
) : "",
53 e
->supported
? ": available" : "");
58 static const char * const __usage
[] = {
59 "perf mem record [<options>] [<command>]",
60 "perf mem record [<options>] -- <command> [<options>]",
64 static const char * const *record_mem_usage
= __usage
;
66 static int __cmd_record(int argc
, const char **argv
, struct perf_mem
*mem
)
68 int rec_argc
, i
= 0, j
;
69 const char **rec_argv
;
71 bool all_user
= false, all_kernel
= false;
72 struct option options
[] = {
73 OPT_CALLBACK('e', "event", &mem
, "event",
74 "event selector. use 'perf mem record -e list' to list available events",
76 OPT_UINTEGER(0, "ldlat", &perf_mem_events__loads_ldlat
, "mem-loads latency"),
77 OPT_INCR('v', "verbose", &verbose
,
78 "be more verbose (show counter open errors, etc)"),
79 OPT_BOOLEAN('U', "all-user", &all_user
, "collect only user level data"),
80 OPT_BOOLEAN('K', "all-kernel", &all_kernel
, "collect only kernel level data"),
84 argc
= parse_options(argc
, argv
, options
, record_mem_usage
,
85 PARSE_OPT_STOP_AT_NON_OPTION
);
87 rec_argc
= argc
+ 9; /* max number of arguments */
88 rec_argv
= calloc(rec_argc
+ 1, sizeof(char *));
92 rec_argv
[i
++] = "record";
94 if (mem
->operation
& MEM_OPERATION_LOAD
)
95 perf_mem_events
[PERF_MEM_EVENTS__LOAD
].record
= true;
97 if (mem
->operation
& MEM_OPERATION_STORE
)
98 perf_mem_events
[PERF_MEM_EVENTS__STORE
].record
= true;
100 if (perf_mem_events
[PERF_MEM_EVENTS__LOAD
].record
)
101 rec_argv
[i
++] = "-W";
103 rec_argv
[i
++] = "-d";
106 rec_argv
[i
++] = "--phys-data";
108 for (j
= 0; j
< PERF_MEM_EVENTS__MAX
; j
++) {
109 if (!perf_mem_events
[j
].record
)
112 if (!perf_mem_events
[j
].supported
) {
113 pr_err("failed: event '%s' not supported\n",
114 perf_mem_events__name(j
));
118 rec_argv
[i
++] = "-e";
119 rec_argv
[i
++] = perf_mem_events__name(j
);
123 rec_argv
[i
++] = "--all-user";
126 rec_argv
[i
++] = "--all-kernel";
128 for (j
= 0; j
< argc
; j
++, i
++)
129 rec_argv
[i
] = argv
[j
];
132 pr_debug("calling: record ");
134 while (rec_argv
[j
]) {
135 pr_debug("%s ", rec_argv
[j
]);
141 ret
= cmd_record(i
, rec_argv
);
147 dump_raw_samples(struct perf_tool
*tool
,
148 union perf_event
*event
,
149 struct perf_sample
*sample
,
150 struct machine
*machine
)
152 struct perf_mem
*mem
= container_of(tool
, struct perf_mem
, tool
);
153 struct addr_location al
;
156 if (machine__resolve(machine
, &al
, sample
) < 0) {
157 fprintf(stderr
, "problem processing %d event, skipping it.\n",
162 if (al
.filtered
|| (mem
->hide_unresolved
&& al
.sym
== NULL
))
166 al
.map
->dso
->hit
= 1;
168 if (mem
->phys_addr
) {
169 if (symbol_conf
.field_sep
) {
170 fmt
= "%d%s%d%s0x%"PRIx64
"%s0x%"PRIx64
"%s0x%016"PRIx64
171 "%s%"PRIu64
"%s0x%"PRIx64
"%s%s:%s\n";
173 fmt
= "%5d%s%5d%s0x%016"PRIx64
"%s0x016%"PRIx64
174 "%s0x%016"PRIx64
"%s%5"PRIu64
"%s0x%06"PRIx64
176 symbol_conf
.field_sep
= " ";
181 symbol_conf
.field_sep
,
183 symbol_conf
.field_sep
,
185 symbol_conf
.field_sep
,
187 symbol_conf
.field_sep
,
189 symbol_conf
.field_sep
,
191 symbol_conf
.field_sep
,
193 symbol_conf
.field_sep
,
194 al
.map
? (al
.map
->dso
? al
.map
->dso
->long_name
: "???") : "???",
195 al
.sym
? al
.sym
->name
: "???");
197 if (symbol_conf
.field_sep
) {
198 fmt
= "%d%s%d%s0x%"PRIx64
"%s0x%"PRIx64
"%s%"PRIu64
199 "%s0x%"PRIx64
"%s%s:%s\n";
201 fmt
= "%5d%s%5d%s0x%016"PRIx64
"%s0x016%"PRIx64
202 "%s%5"PRIu64
"%s0x%06"PRIx64
"%s%s:%s\n";
203 symbol_conf
.field_sep
= " ";
208 symbol_conf
.field_sep
,
210 symbol_conf
.field_sep
,
212 symbol_conf
.field_sep
,
214 symbol_conf
.field_sep
,
216 symbol_conf
.field_sep
,
218 symbol_conf
.field_sep
,
219 al
.map
? (al
.map
->dso
? al
.map
->dso
->long_name
: "???") : "???",
220 al
.sym
? al
.sym
->name
: "???");
223 addr_location__put(&al
);
227 static int process_sample_event(struct perf_tool
*tool
,
228 union perf_event
*event
,
229 struct perf_sample
*sample
,
230 struct perf_evsel
*evsel __maybe_unused
,
231 struct machine
*machine
)
233 return dump_raw_samples(tool
, event
, sample
, machine
);
236 static int report_raw_events(struct perf_mem
*mem
)
238 struct perf_data_file file
= {
240 .mode
= PERF_DATA_MODE_READ
,
244 struct perf_session
*session
= perf_session__new(&file
, false,
251 ret
= perf_session__cpu_bitmap(session
, mem
->cpu_list
,
257 ret
= symbol__init(&session
->header
.env
);
262 printf("# PID, TID, IP, ADDR, PHYS ADDR, LOCAL WEIGHT, DSRC, SYMBOL\n");
264 printf("# PID, TID, IP, ADDR, LOCAL WEIGHT, DSRC, SYMBOL\n");
266 ret
= perf_session__process_events(session
);
269 perf_session__delete(session
);
273 static int report_events(int argc
, const char **argv
, struct perf_mem
*mem
)
275 const char **rep_argv
;
276 int ret
, i
= 0, j
, rep_argc
;
279 return report_raw_events(mem
);
282 rep_argv
= calloc(rep_argc
+ 1, sizeof(char *));
286 rep_argv
[i
++] = "report";
287 rep_argv
[i
++] = "--mem-mode";
288 rep_argv
[i
++] = "-n"; /* display number of samples */
291 * there is no weight (cost) associated with stores, so don't print
294 if (!(mem
->operation
& MEM_OPERATION_LOAD
)) {
296 rep_argv
[i
++] = "--sort=mem,sym,dso,symbol_daddr,"
297 "dso_daddr,tlb,locked,phys_daddr";
299 rep_argv
[i
++] = "--sort=mem,sym,dso,symbol_daddr,"
300 "dso_daddr,tlb,locked";
301 } else if (mem
->phys_addr
)
302 rep_argv
[i
++] = "--sort=local_weight,mem,sym,dso,symbol_daddr,"
303 "dso_daddr,snoop,tlb,locked,phys_daddr";
305 for (j
= 1; j
< argc
; j
++, i
++)
306 rep_argv
[i
] = argv
[j
];
308 ret
= cmd_report(i
, rep_argv
);
318 #define MEM_OPT(n, m) \
319 { .name = n, .mode = (m) }
321 #define MEM_END { .name = NULL }
323 static const struct mem_mode mem_modes
[]={
324 MEM_OPT("load", MEM_OPERATION_LOAD
),
325 MEM_OPT("store", MEM_OPERATION_STORE
),
330 parse_mem_ops(const struct option
*opt
, const char *str
, int unset
)
332 int *mode
= (int *)opt
->value
;
333 const struct mem_mode
*m
;
334 char *s
, *os
= NULL
, *p
;
340 /* str may be NULL in case no arg is passed to -t */
342 /* because str is read-only */
343 s
= os
= strdup(str
);
355 for (m
= mem_modes
; m
->name
; m
++) {
356 if (!strcasecmp(s
, m
->name
))
360 fprintf(stderr
, "unknown sampling op %s,"
361 " check man page\n", s
);
376 *mode
= MEM_OPERATION_LOAD
;
382 int cmd_mem(int argc
, const char **argv
)
385 struct perf_mem mem
= {
387 .sample
= process_sample_event
,
388 .mmap
= perf_event__process_mmap
,
389 .mmap2
= perf_event__process_mmap2
,
390 .comm
= perf_event__process_comm
,
391 .lost
= perf_event__process_lost
,
392 .fork
= perf_event__process_fork
,
393 .build_id
= perf_event__process_build_id
,
394 .namespaces
= perf_event__process_namespaces
,
395 .ordered_events
= true,
397 .input_name
= "perf.data",
399 * default to both load an store sampling
401 .operation
= MEM_OPERATION_LOAD
| MEM_OPERATION_STORE
,
403 const struct option mem_options
[] = {
404 OPT_CALLBACK('t', "type", &mem
.operation
,
405 "type", "memory operations(load,store) Default load,store",
407 OPT_BOOLEAN('D', "dump-raw-samples", &mem
.dump_raw
,
408 "dump raw samples in ASCII"),
409 OPT_BOOLEAN('U', "hide-unresolved", &mem
.hide_unresolved
,
410 "Only display entries resolved to a symbol"),
411 OPT_STRING('i', "input", &input_name
, "file",
413 OPT_STRING('C', "cpu", &mem
.cpu_list
, "cpu",
414 "list of cpus to profile"),
415 OPT_STRING_NOEMPTY('x', "field-separator", &symbol_conf
.field_sep
,
417 "separator for columns, no spaces will be added"
418 " between columns '.' is reserved."),
419 OPT_BOOLEAN('f', "force", &mem
.force
, "don't complain, do it"),
420 OPT_BOOLEAN('p', "phys-data", &mem
.phys_addr
, "Record/Report sample physical addresses"),
423 const char *const mem_subcommands
[] = { "record", "report", NULL
};
424 const char *mem_usage
[] = {
429 if (perf_mem_events__init()) {
430 pr_err("failed: memory events not supported\n");
434 argc
= parse_options_subcommand(argc
, argv
, mem_options
, mem_subcommands
,
435 mem_usage
, PARSE_OPT_STOP_AT_NON_OPTION
);
437 if (!argc
|| !(strncmp(argv
[0], "rec", 3) || mem
.operation
))
438 usage_with_options(mem_usage
, mem_options
);
440 if (!mem
.input_name
|| !strlen(mem
.input_name
)) {
441 if (!fstat(STDIN_FILENO
, &st
) && S_ISFIFO(st
.st_mode
))
442 mem
.input_name
= "-";
444 mem
.input_name
= "perf.data";
447 if (!strncmp(argv
[0], "rec", 3))
448 return __cmd_record(argc
, argv
, &mem
);
449 else if (!strncmp(argv
[0], "rep", 3))
450 return report_events(argc
, argv
, &mem
);
452 usage_with_options(mem_usage
, mem_options
);