4 #include "util/parse-options.h"
5 #include "util/trace-event.h"
7 #include "util/session.h"
10 #define MEM_OPERATION_LOAD 0x1
11 #define MEM_OPERATION_STORE 0x2
14 struct perf_tool tool
;
15 char const *input_name
;
20 DECLARE_BITMAP(cpu_bitmap
, MAX_NR_CPUS
);
23 static int __cmd_record(int argc
, const char **argv
, struct perf_mem
*mem
)
25 int rec_argc
, i
= 0, j
;
26 const char **rec_argv
;
29 rec_argc
= argc
+ 7; /* max number of arguments */
30 rec_argv
= calloc(rec_argc
+ 1, sizeof(char *));
34 rec_argv
[i
++] = "record";
36 if (mem
->operation
& MEM_OPERATION_LOAD
)
41 if (mem
->operation
& MEM_OPERATION_LOAD
) {
43 rec_argv
[i
++] = "cpu/mem-loads/pp";
46 if (mem
->operation
& MEM_OPERATION_STORE
) {
48 rec_argv
[i
++] = "cpu/mem-stores/pp";
51 for (j
= 1; j
< argc
; j
++, i
++)
52 rec_argv
[i
] = argv
[j
];
54 ret
= cmd_record(i
, rec_argv
, NULL
);
60 dump_raw_samples(struct perf_tool
*tool
,
61 union perf_event
*event
,
62 struct perf_sample
*sample
,
63 struct machine
*machine
)
65 struct perf_mem
*mem
= container_of(tool
, struct perf_mem
, tool
);
66 struct addr_location al
;
69 if (perf_event__preprocess_sample(event
, machine
, &al
, sample
) < 0) {
70 fprintf(stderr
, "problem processing %d event, skipping it.\n",
75 if (al
.filtered
|| (mem
->hide_unresolved
&& al
.sym
== NULL
))
81 if (symbol_conf
.field_sep
) {
82 fmt
= "%d%s%d%s0x%"PRIx64
"%s0x%"PRIx64
"%s%"PRIu64
83 "%s0x%"PRIx64
"%s%s:%s\n";
85 fmt
= "%5d%s%5d%s0x%016"PRIx64
"%s0x016%"PRIx64
86 "%s%5"PRIu64
"%s0x%06"PRIx64
"%s%s:%s\n";
87 symbol_conf
.field_sep
= " ";
92 symbol_conf
.field_sep
,
94 symbol_conf
.field_sep
,
96 symbol_conf
.field_sep
,
98 symbol_conf
.field_sep
,
100 symbol_conf
.field_sep
,
102 symbol_conf
.field_sep
,
103 al
.map
? (al
.map
->dso
? al
.map
->dso
->long_name
: "???") : "???",
104 al
.sym
? al
.sym
->name
: "???");
109 static int process_sample_event(struct perf_tool
*tool
,
110 union perf_event
*event
,
111 struct perf_sample
*sample
,
112 struct perf_evsel
*evsel __maybe_unused
,
113 struct machine
*machine
)
115 return dump_raw_samples(tool
, event
, sample
, machine
);
118 static int report_raw_events(struct perf_mem
*mem
)
120 struct perf_data_file file
= {
122 .mode
= PERF_DATA_MODE_READ
,
126 struct perf_session
*session
= perf_session__new(&file
, false,
133 ret
= perf_session__cpu_bitmap(session
, mem
->cpu_list
,
139 if (symbol__init(&session
->header
.env
) < 0)
142 printf("# PID, TID, IP, ADDR, LOCAL WEIGHT, DSRC, SYMBOL\n");
144 err
= perf_session__process_events(session
, &mem
->tool
);
151 perf_session__delete(session
);
155 static int report_events(int argc
, const char **argv
, struct perf_mem
*mem
)
157 const char **rep_argv
;
158 int ret
, i
= 0, j
, rep_argc
;
161 return report_raw_events(mem
);
164 rep_argv
= calloc(rep_argc
+ 1, sizeof(char *));
168 rep_argv
[i
++] = "report";
169 rep_argv
[i
++] = "--mem-mode";
170 rep_argv
[i
++] = "-n"; /* display number of samples */
173 * there is no weight (cost) associated with stores, so don't print
176 if (!(mem
->operation
& MEM_OPERATION_LOAD
))
177 rep_argv
[i
++] = "--sort=mem,sym,dso,symbol_daddr,"
178 "dso_daddr,tlb,locked";
180 for (j
= 1; j
< argc
; j
++, i
++)
181 rep_argv
[i
] = argv
[j
];
183 ret
= cmd_report(i
, rep_argv
, NULL
);
193 #define MEM_OPT(n, m) \
194 { .name = n, .mode = (m) }
196 #define MEM_END { .name = NULL }
198 static const struct mem_mode mem_modes
[]={
199 MEM_OPT("load", MEM_OPERATION_LOAD
),
200 MEM_OPT("store", MEM_OPERATION_STORE
),
205 parse_mem_ops(const struct option
*opt
, const char *str
, int unset
)
207 int *mode
= (int *)opt
->value
;
208 const struct mem_mode
*m
;
209 char *s
, *os
= NULL
, *p
;
215 /* str may be NULL in case no arg is passed to -t */
217 /* because str is read-only */
218 s
= os
= strdup(str
);
230 for (m
= mem_modes
; m
->name
; m
++) {
231 if (!strcasecmp(s
, m
->name
))
235 fprintf(stderr
, "unknown sampling op %s,"
236 " check man page\n", s
);
251 *mode
= MEM_OPERATION_LOAD
;
257 int cmd_mem(int argc
, const char **argv
, const char *prefix __maybe_unused
)
260 struct perf_mem mem
= {
262 .sample
= process_sample_event
,
263 .mmap
= perf_event__process_mmap
,
264 .mmap2
= perf_event__process_mmap2
,
265 .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
,
269 .ordered_events
= true,
271 .input_name
= "perf.data",
273 * default to both load an store sampling
275 .operation
= MEM_OPERATION_LOAD
| MEM_OPERATION_STORE
,
277 const struct option mem_options
[] = {
278 OPT_CALLBACK('t', "type", &mem
.operation
,
279 "type", "memory operations(load,store) Default load,store",
281 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",
287 OPT_STRING('C', "cpu", &mem
.cpu_list
, "cpu",
288 "list of cpus to profile"),
289 OPT_STRING('x', "field-separator", &symbol_conf
.field_sep
,
291 "separator for columns, no spaces will be added"
292 " between columns '.' is reserved."),
295 const char *const mem_subcommands
[] = { "record", "report", NULL
};
296 const char *mem_usage
[] = {
302 argc
= parse_options_subcommand(argc
, argv
, mem_options
, mem_subcommands
,
303 mem_usage
, PARSE_OPT_STOP_AT_NON_OPTION
);
305 if (!argc
|| !(strncmp(argv
[0], "rec", 3) || mem
.operation
))
306 usage_with_options(mem_usage
, mem_options
);
308 if (!mem
.input_name
|| !strlen(mem
.input_name
)) {
309 if (!fstat(STDIN_FILENO
, &st
) && S_ISFIFO(st
.st_mode
))
310 mem
.input_name
= "-";
312 mem
.input_name
= "perf.data";
315 if (!strncmp(argv
[0], "rec", 3))
316 return __cmd_record(argc
, argv
, &mem
);
317 else if (!strncmp(argv
[0], "rep", 3))
318 return report_events(argc
, argv
, &mem
);
320 usage_with_options(mem_usage
, mem_options
);