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
;
28 DECLARE_BITMAP(cpu_bitmap
, MAX_NR_CPUS
);
31 static int parse_record_events(const struct option
*opt
,
32 const char *str
, int unset __maybe_unused
)
34 struct perf_mem
*mem
= *(struct perf_mem
**)opt
->value
;
37 if (strcmp(str
, "list")) {
38 if (!perf_mem_events__parse(str
)) {
45 for (j
= 0; j
< PERF_MEM_EVENTS__MAX
; j
++) {
46 struct perf_mem_event
*e
= &perf_mem_events
[j
];
48 fprintf(stderr
, "%-13s%-*s%s\n",
51 verbose
> 0 ? perf_mem_events__name(j
) : "",
52 e
->supported
? ": available" : "");
57 static const char * const __usage
[] = {
58 "perf mem record [<options>] [<command>]",
59 "perf mem record [<options>] -- <command> [<options>]",
63 static const char * const *record_mem_usage
= __usage
;
65 static int __cmd_record(int argc
, const char **argv
, struct perf_mem
*mem
)
67 int rec_argc
, i
= 0, j
;
68 const char **rec_argv
;
70 bool all_user
= false, all_kernel
= false;
71 struct option options
[] = {
72 OPT_CALLBACK('e', "event", &mem
, "event",
73 "event selector. use 'perf mem record -e list' to list available events",
75 OPT_UINTEGER(0, "ldlat", &perf_mem_events__loads_ldlat
, "mem-loads latency"),
76 OPT_INCR('v', "verbose", &verbose
,
77 "be more verbose (show counter open errors, etc)"),
78 OPT_BOOLEAN('U', "all-user", &all_user
, "collect only user level data"),
79 OPT_BOOLEAN('K', "all-kernel", &all_kernel
, "collect only kernel level data"),
83 argc
= parse_options(argc
, argv
, options
, record_mem_usage
,
84 PARSE_OPT_STOP_AT_NON_OPTION
);
86 rec_argc
= argc
+ 9; /* max number of arguments */
87 rec_argv
= calloc(rec_argc
+ 1, sizeof(char *));
91 rec_argv
[i
++] = "record";
93 if (mem
->operation
& MEM_OPERATION_LOAD
)
94 perf_mem_events
[PERF_MEM_EVENTS__LOAD
].record
= true;
96 if (mem
->operation
& MEM_OPERATION_STORE
)
97 perf_mem_events
[PERF_MEM_EVENTS__STORE
].record
= true;
99 if (perf_mem_events
[PERF_MEM_EVENTS__LOAD
].record
)
100 rec_argv
[i
++] = "-W";
102 rec_argv
[i
++] = "-d";
104 for (j
= 0; j
< PERF_MEM_EVENTS__MAX
; j
++) {
105 if (!perf_mem_events
[j
].record
)
108 if (!perf_mem_events
[j
].supported
) {
109 pr_err("failed: event '%s' not supported\n",
110 perf_mem_events__name(j
));
114 rec_argv
[i
++] = "-e";
115 rec_argv
[i
++] = perf_mem_events__name(j
);
119 rec_argv
[i
++] = "--all-user";
122 rec_argv
[i
++] = "--all-kernel";
124 for (j
= 0; j
< argc
; j
++, i
++)
125 rec_argv
[i
] = argv
[j
];
128 pr_debug("calling: record ");
130 while (rec_argv
[j
]) {
131 pr_debug("%s ", rec_argv
[j
]);
137 ret
= cmd_record(i
, rec_argv
);
143 dump_raw_samples(struct perf_tool
*tool
,
144 union perf_event
*event
,
145 struct perf_sample
*sample
,
146 struct machine
*machine
)
148 struct perf_mem
*mem
= container_of(tool
, struct perf_mem
, tool
);
149 struct addr_location al
;
152 if (machine__resolve(machine
, &al
, sample
) < 0) {
153 fprintf(stderr
, "problem processing %d event, skipping it.\n",
158 if (al
.filtered
|| (mem
->hide_unresolved
&& al
.sym
== NULL
))
162 al
.map
->dso
->hit
= 1;
164 if (symbol_conf
.field_sep
) {
165 fmt
= "%d%s%d%s0x%"PRIx64
"%s0x%"PRIx64
"%s%"PRIu64
166 "%s0x%"PRIx64
"%s%s:%s\n";
168 fmt
= "%5d%s%5d%s0x%016"PRIx64
"%s0x016%"PRIx64
169 "%s%5"PRIu64
"%s0x%06"PRIx64
"%s%s:%s\n";
170 symbol_conf
.field_sep
= " ";
175 symbol_conf
.field_sep
,
177 symbol_conf
.field_sep
,
179 symbol_conf
.field_sep
,
181 symbol_conf
.field_sep
,
183 symbol_conf
.field_sep
,
185 symbol_conf
.field_sep
,
186 al
.map
? (al
.map
->dso
? al
.map
->dso
->long_name
: "???") : "???",
187 al
.sym
? al
.sym
->name
: "???");
189 addr_location__put(&al
);
193 static int process_sample_event(struct perf_tool
*tool
,
194 union perf_event
*event
,
195 struct perf_sample
*sample
,
196 struct perf_evsel
*evsel __maybe_unused
,
197 struct machine
*machine
)
199 return dump_raw_samples(tool
, event
, sample
, machine
);
202 static int report_raw_events(struct perf_mem
*mem
)
204 struct perf_data_file file
= {
206 .mode
= PERF_DATA_MODE_READ
,
210 struct perf_session
*session
= perf_session__new(&file
, false,
217 ret
= perf_session__cpu_bitmap(session
, mem
->cpu_list
,
223 ret
= symbol__init(&session
->header
.env
);
227 printf("# PID, TID, IP, ADDR, LOCAL WEIGHT, DSRC, SYMBOL\n");
229 ret
= perf_session__process_events(session
);
232 perf_session__delete(session
);
236 static int report_events(int argc
, const char **argv
, struct perf_mem
*mem
)
238 const char **rep_argv
;
239 int ret
, i
= 0, j
, rep_argc
;
242 return report_raw_events(mem
);
245 rep_argv
= calloc(rep_argc
+ 1, sizeof(char *));
249 rep_argv
[i
++] = "report";
250 rep_argv
[i
++] = "--mem-mode";
251 rep_argv
[i
++] = "-n"; /* display number of samples */
254 * there is no weight (cost) associated with stores, so don't print
257 if (!(mem
->operation
& MEM_OPERATION_LOAD
))
258 rep_argv
[i
++] = "--sort=mem,sym,dso,symbol_daddr,"
259 "dso_daddr,tlb,locked";
261 for (j
= 1; j
< argc
; j
++, i
++)
262 rep_argv
[i
] = argv
[j
];
264 ret
= cmd_report(i
, rep_argv
);
274 #define MEM_OPT(n, m) \
275 { .name = n, .mode = (m) }
277 #define MEM_END { .name = NULL }
279 static const struct mem_mode mem_modes
[]={
280 MEM_OPT("load", MEM_OPERATION_LOAD
),
281 MEM_OPT("store", MEM_OPERATION_STORE
),
286 parse_mem_ops(const struct option
*opt
, const char *str
, int unset
)
288 int *mode
= (int *)opt
->value
;
289 const struct mem_mode
*m
;
290 char *s
, *os
= NULL
, *p
;
296 /* str may be NULL in case no arg is passed to -t */
298 /* because str is read-only */
299 s
= os
= strdup(str
);
311 for (m
= mem_modes
; m
->name
; m
++) {
312 if (!strcasecmp(s
, m
->name
))
316 fprintf(stderr
, "unknown sampling op %s,"
317 " check man page\n", s
);
332 *mode
= MEM_OPERATION_LOAD
;
338 int cmd_mem(int argc
, const char **argv
)
341 struct perf_mem mem
= {
343 .sample
= process_sample_event
,
344 .mmap
= perf_event__process_mmap
,
345 .mmap2
= perf_event__process_mmap2
,
346 .comm
= perf_event__process_comm
,
347 .lost
= perf_event__process_lost
,
348 .fork
= perf_event__process_fork
,
349 .build_id
= perf_event__process_build_id
,
350 .namespaces
= perf_event__process_namespaces
,
351 .ordered_events
= true,
353 .input_name
= "perf.data",
355 * default to both load an store sampling
357 .operation
= MEM_OPERATION_LOAD
| MEM_OPERATION_STORE
,
359 const struct option mem_options
[] = {
360 OPT_CALLBACK('t', "type", &mem
.operation
,
361 "type", "memory operations(load,store) Default load,store",
363 OPT_BOOLEAN('D', "dump-raw-samples", &mem
.dump_raw
,
364 "dump raw samples in ASCII"),
365 OPT_BOOLEAN('U', "hide-unresolved", &mem
.hide_unresolved
,
366 "Only display entries resolved to a symbol"),
367 OPT_STRING('i', "input", &input_name
, "file",
369 OPT_STRING('C', "cpu", &mem
.cpu_list
, "cpu",
370 "list of cpus to profile"),
371 OPT_STRING_NOEMPTY('x', "field-separator", &symbol_conf
.field_sep
,
373 "separator for columns, no spaces will be added"
374 " between columns '.' is reserved."),
375 OPT_BOOLEAN('f', "force", &mem
.force
, "don't complain, do it"),
378 const char *const mem_subcommands
[] = { "record", "report", NULL
};
379 const char *mem_usage
[] = {
384 if (perf_mem_events__init()) {
385 pr_err("failed: memory events not supported\n");
389 argc
= parse_options_subcommand(argc
, argv
, mem_options
, mem_subcommands
,
390 mem_usage
, PARSE_OPT_STOP_AT_NON_OPTION
);
392 if (!argc
|| !(strncmp(argv
[0], "rec", 3) || mem
.operation
))
393 usage_with_options(mem_usage
, mem_options
);
395 if (!mem
.input_name
|| !strlen(mem
.input_name
)) {
396 if (!fstat(STDIN_FILENO
, &st
) && S_ISFIFO(st
.st_mode
))
397 mem
.input_name
= "-";
399 mem
.input_name
= "perf.data";
402 if (!strncmp(argv
[0], "rec", 3))
403 return __cmd_record(argc
, argv
, &mem
);
404 else if (!strncmp(argv
[0], "rep", 3))
405 return report_events(argc
, argv
, &mem
);
407 usage_with_options(mem_usage
, mem_options
);