File Index
Files and directories
Sign in
FILE

builtin-mem.c

File preview with syntax highlighting.

linux-6.19.14/tools/perf/builtin-mem.c C
  1// SPDX-License-Identifier: GPL-2.0
  2#include <errno.h>
  3#include <inttypes.h>
  4#include <sys/types.h>
  5#include <sys/stat.h>
  6#include <unistd.h>
  7#include "builtin.h"
  8#include "perf.h"
  9
 10#include <subcmd/parse-options.h>
 11#include "util/auxtrace.h"
 12#include "util/trace-event.h"
 13#include "util/tool.h"
 14#include "util/session.h"
 15#include "util/data.h"
 16#include "util/map_symbol.h"
 17#include "util/mem-events.h"
 18#include "util/debug.h"
 19#include "util/dso.h"
 20#include "util/map.h"
 21#include "util/symbol.h"
 22#include "util/pmus.h"
 23#include "util/sample.h"
 24#include "util/sort.h"
 25#include "util/string2.h"
 26#include "util/util.h"
 27#include <linux/err.h>
 28
 29#define MEM_OPERATION_LOAD	0x1
 30#define MEM_OPERATION_STORE	0x2
 31
 32struct perf_mem {
 33	struct perf_tool	tool;
 34	const char		*input_name;
 35	const char		*sort_key;
 36	bool			hide_unresolved;
 37	bool			dump_raw;
 38	bool			force;
 39	bool			phys_addr;
 40	bool			data_page_size;
 41	bool			all_kernel;
 42	bool			all_user;
 43	bool			data_type;
 44	int			operation;
 45	const char		*cpu_list;
 46	DECLARE_BITMAP(cpu_bitmap, MAX_NR_CPUS);
 47};
 48
 49static int parse_record_events(const struct option *opt,
 50			       const char *str, int unset __maybe_unused)
 51{
 52	struct perf_mem *mem = (struct perf_mem *)opt->value;
 53	struct perf_pmu *pmu;
 54
 55	pmu = perf_mem_events_find_pmu();
 56	if (!pmu) {
 57		pr_err("failed: there is no PMU that supports perf mem\n");
 58		exit(-1);
 59	}
 60
 61	if (!strcmp(str, "list")) {
 62		perf_pmu__mem_events_list(pmu);
 63		exit(0);
 64	}
 65	if (perf_pmu__mem_events_parse(pmu, str))
 66		exit(-1);
 67
 68	mem->operation = 0;
 69	return 0;
 70}
 71
 72static int __cmd_record(int argc, const char **argv, struct perf_mem *mem,
 73			const struct option *options)
 74{
 75	int rec_argc, i = 0, j;
 76	int start, end;
 77	const char **rec_argv;
 78	char *event_name_storage = NULL;
 79	int ret;
 80	struct perf_mem_event *e;
 81	struct perf_pmu *pmu;
 82	const char * const record_usage[] = {
 83		"perf mem record [<options>] [<command>]",
 84		"perf mem record [<options>] -- <command> [<options>]",
 85		NULL
 86	};
 87
 88	pmu = perf_mem_events_find_pmu();
 89	if (!pmu) {
 90		pr_err("failed: no PMU supports the memory events\n");
 91		return -1;
 92	}
 93
 94	if (perf_pmu__mem_events_init()) {
 95		pr_err("failed: memory events not supported\n");
 96		return -1;
 97	}
 98
 99	argc = parse_options(argc, argv, options, record_usage,
100			     PARSE_OPT_KEEP_UNKNOWN);
101
102	/* Max number of arguments multiplied by number of PMUs that can support them. */
103	rec_argc = argc + 9 * (perf_pmu__mem_events_num_mem_pmus(pmu) + 1);
104
105	if (mem->cpu_list)
106		rec_argc += 2;
107
108	rec_argv = calloc(rec_argc + 1, sizeof(char *));
109	if (!rec_argv)
110		return -1;
111
112	rec_argv[i++] = "record";
113
114	e = perf_pmu__mem_events_ptr(pmu, PERF_MEM_EVENTS__LOAD_STORE);
115
116	/*
117	 * The load and store operations are required, use the event
118	 * PERF_MEM_EVENTS__LOAD_STORE if it is supported.
119	 */
120	if (e->tag &&
121	    (mem->operation & MEM_OPERATION_LOAD) &&
122	    (mem->operation & MEM_OPERATION_STORE)) {
123		perf_mem_record[PERF_MEM_EVENTS__LOAD_STORE] = true;
124		rec_argv[i++] = "-W";
125	} else {
126		if (mem->operation & MEM_OPERATION_LOAD)
127			perf_mem_record[PERF_MEM_EVENTS__LOAD] = true;
128
129		if (mem->operation & MEM_OPERATION_STORE)
130			perf_mem_record[PERF_MEM_EVENTS__STORE] = true;
131	}
132
133	if (perf_mem_record[PERF_MEM_EVENTS__LOAD])
134		rec_argv[i++] = "-W";
135
136	rec_argv[i++] = "-d";
137
138	if (mem->phys_addr)
139		rec_argv[i++] = "--phys-data";
140
141	if (mem->data_page_size)
142		rec_argv[i++] = "--data-page-size";
143
144	start = i;
145	ret = perf_mem_events__record_args(rec_argv, &i, &event_name_storage);
146	if (ret)
147		goto out;
148	end = i;
149
150	if (mem->all_user)
151		rec_argv[i++] = "--all-user";
152
153	if (mem->all_kernel)
154		rec_argv[i++] = "--all-kernel";
155
156	if (mem->cpu_list) {
157		rec_argv[i++] = "-C";
158		rec_argv[i++] = mem->cpu_list;
159	}
160
161	for (j = 0; j < argc; j++, i++)
162		rec_argv[i] = argv[j];
163
164	if (verbose > 0) {
165		pr_debug("calling: record ");
166
167		for (j = start; j < end; j++)
168			pr_debug("%s ", rec_argv[j]);
169
170		pr_debug("\n");
171	}
172
173	ret = cmd_record(i, rec_argv);
174out:
175	free(event_name_storage);
176	free(rec_argv);
177	return ret;
178}
179
180static int
181dump_raw_samples(const struct perf_tool *tool,
182		 union perf_event *event,
183		 struct perf_sample *sample,
184		 struct machine *machine)
185{
186	struct perf_mem *mem = container_of(tool, struct perf_mem, tool);
187	struct addr_location al;
188	const char *fmt, *field_sep;
189	char str[PAGE_SIZE_NAME_LEN];
190	struct dso *dso = NULL;
191
192	addr_location__init(&al);
193	if (machine__resolve(machine, &al, sample) < 0) {
194		fprintf(stderr, "problem processing %d event, skipping it.\n",
195				event->header.type);
196		addr_location__exit(&al);
197		return -1;
198	}
199
200	if (al.filtered || (mem->hide_unresolved && al.sym == NULL))
201		goto out_put;
202
203	if (al.map != NULL) {
204		dso = map__dso(al.map);
205		if (dso)
206			dso__set_hit(dso);
207	}
208
209	field_sep = symbol_conf.field_sep;
210	if (field_sep) {
211		fmt = "%d%s%d%s0x%"PRIx64"%s0x%"PRIx64"%s";
212	} else {
213		fmt = "%5d%s%5d%s0x%016"PRIx64"%s0x016%"PRIx64"%s";
214		symbol_conf.field_sep = " ";
215	}
216	printf(fmt,
217		sample->pid,
218		symbol_conf.field_sep,
219		sample->tid,
220		symbol_conf.field_sep,
221		sample->ip,
222		symbol_conf.field_sep,
223		sample->addr,
224		symbol_conf.field_sep);
225
226	if (mem->phys_addr) {
227		printf("0x%016"PRIx64"%s",
228			sample->phys_addr,
229			symbol_conf.field_sep);
230	}
231
232	if (mem->data_page_size) {
233		printf("%s%s",
234			get_page_size_name(sample->data_page_size, str),
235			symbol_conf.field_sep);
236	}
237
238	if (field_sep)
239		fmt = "%"PRIu64"%s0x%"PRIx64"%s%s:%s\n";
240	else
241		fmt = "%5"PRIu64"%s0x%06"PRIx64"%s%s:%s\n";
242
243	printf(fmt,
244		sample->weight,
245		symbol_conf.field_sep,
246		sample->data_src,
247		symbol_conf.field_sep,
248		dso ? dso__long_name(dso) : "???",
249		al.sym ? al.sym->name : "???");
250out_put:
251	addr_location__exit(&al);
252	return 0;
253}
254
255static int process_sample_event(const struct perf_tool *tool,
256				union perf_event *event,
257				struct perf_sample *sample,
258				struct evsel *evsel __maybe_unused,
259				struct machine *machine)
260{
261	return dump_raw_samples(tool, event, sample, machine);
262}
263
264static int report_raw_events(struct perf_mem *mem)
265{
266	struct itrace_synth_opts itrace_synth_opts = {
267		.set = true,
268		.mem = true,	/* Only enable memory event */
269		.default_no_sample = true,
270	};
271
272	struct perf_data data = {
273		.path  = input_name,
274		.mode  = PERF_DATA_MODE_READ,
275		.force = mem->force,
276	};
277	int ret;
278	struct perf_session *session;
279
280	perf_tool__init(&mem->tool, /*ordered_events=*/true);
281	mem->tool.sample		= process_sample_event;
282	mem->tool.mmap		= perf_event__process_mmap;
283	mem->tool.mmap2		= perf_event__process_mmap2;
284	mem->tool.comm		= perf_event__process_comm;
285	mem->tool.lost		= perf_event__process_lost;
286	mem->tool.fork		= perf_event__process_fork;
287	mem->tool.attr		= perf_event__process_attr;
288	mem->tool.build_id	= perf_event__process_build_id;
289	mem->tool.namespaces	= perf_event__process_namespaces;
290	mem->tool.auxtrace_info  = perf_event__process_auxtrace_info;
291	mem->tool.auxtrace       = perf_event__process_auxtrace;
292	mem->tool.auxtrace_error = perf_event__process_auxtrace_error;
293
294	session = perf_session__new(&data, &mem->tool);
295
296	if (IS_ERR(session))
297		return PTR_ERR(session);
298
299	session->itrace_synth_opts = &itrace_synth_opts;
300
301	if (mem->cpu_list) {
302		ret = perf_session__cpu_bitmap(session, mem->cpu_list,
303					       mem->cpu_bitmap);
304		if (ret < 0)
305			goto out_delete;
306	}
307
308	ret = symbol__init(perf_session__env(session));
309	if (ret < 0)
310		goto out_delete;
311
312	printf("# PID, TID, IP, ADDR, ");
313
314	if (mem->phys_addr)
315		printf("PHYS ADDR, ");
316
317	if (mem->data_page_size)
318		printf("DATA PAGE SIZE, ");
319
320	printf("LOCAL WEIGHT, DSRC, SYMBOL\n");
321
322	ret = perf_session__process_events(session);
323
324out_delete:
325	perf_session__delete(session);
326	return ret;
327}
328
329static char *get_sort_order(struct perf_mem *mem)
330{
331	bool has_extra_options = (mem->phys_addr | mem->data_page_size) ? true : false;
332	char sort[128];
333
334	if (mem->sort_key)
335		scnprintf(sort, sizeof(sort), "--sort=%s", mem->sort_key);
336	else if (mem->data_type)
337		strcpy(sort, "--sort=mem,snoop,tlb,type");
338	/*
339	 * there is no weight (cost) associated with stores, so don't print
340	 * the column
341	 */
342	else if (!(mem->operation & MEM_OPERATION_LOAD)) {
343		strcpy(sort, "--sort=mem,sym,dso,symbol_daddr,"
344			     "dso_daddr,tlb,locked");
345	} else if (has_extra_options) {
346		strcpy(sort, "--sort=local_weight,mem,sym,dso,symbol_daddr,"
347			     "dso_daddr,snoop,tlb,locked,blocked");
348	} else
349		return NULL;
350
351	if (mem->phys_addr)
352		strcat(sort, ",phys_daddr");
353
354	if (mem->data_page_size)
355		strcat(sort, ",data_page_size");
356
357	/* make sure it has 'type' sort key even -s option is used */
358	if (mem->data_type && !strstr(sort, "type"))
359		strcat(sort, ",type");
360
361	return strdup(sort);
362}
363
364static int __cmd_report(int argc, const char **argv, struct perf_mem *mem,
365			const struct option *options)
366{
367	const char **rep_argv;
368	int ret, i = 0, j, rep_argc;
369	char *new_sort_order;
370	const char * const report_usage[] = {
371		"perf mem report [<options>]",
372		NULL
373	};
374
375	argc = parse_options(argc, argv, options, report_usage,
376			     PARSE_OPT_KEEP_UNKNOWN);
377
378	if (mem->dump_raw)
379		return report_raw_events(mem);
380
381	rep_argc = argc + 3;
382	rep_argv = calloc(rep_argc + 1, sizeof(char *));
383	if (!rep_argv)
384		return -1;
385
386	rep_argv[i++] = "report";
387	rep_argv[i++] = "--mem-mode";
388	rep_argv[i++] = "-n"; /* display number of samples */
389
390	new_sort_order = get_sort_order(mem);
391	if (new_sort_order)
392		rep_argv[i++] = new_sort_order;
393
394	for (j = 0; j < argc; j++, i++)
395		rep_argv[i] = argv[j];
396
397	ret = cmd_report(i, rep_argv);
398	free(new_sort_order);
399	free(rep_argv);
400	return ret;
401}
402
403struct mem_mode {
404	const char *name;
405	int mode;
406};
407
408#define MEM_OPT(n, m) \
409	{ .name = n, .mode = (m) }
410
411#define MEM_END { .name = NULL }
412
413static const struct mem_mode mem_modes[]={
414	MEM_OPT("load", MEM_OPERATION_LOAD),
415	MEM_OPT("store", MEM_OPERATION_STORE),
416	MEM_END
417};
418
419static int
420parse_mem_ops(const struct option *opt, const char *str, int unset)
421{
422	int *mode = (int *)opt->value;
423	const struct mem_mode *m;
424	char *s, *os = NULL, *p;
425	int ret = -1;
426
427	if (unset)
428		return 0;
429
430	/* str may be NULL in case no arg is passed to -t */
431	if (str) {
432		/* because str is read-only */
433		s = os = strdup(str);
434		if (!s)
435			return -1;
436
437		/* reset mode */
438		*mode = 0;
439
440		for (;;) {
441			p = strchr(s, ',');
442			if (p)
443				*p = '\0';
444
445			for (m = mem_modes; m->name; m++) {
446				if (!strcasecmp(s, m->name))
447					break;
448			}
449			if (!m->name) {
450				fprintf(stderr, "unknown sampling op %s,"
451					    " check man page\n", s);
452				goto error;
453			}
454
455			*mode |= m->mode;
456
457			if (!p)
458				break;
459
460			s = p + 1;
461		}
462	}
463	ret = 0;
464
465	if (*mode == 0)
466		*mode = MEM_OPERATION_LOAD;
467error:
468	free(os);
469	return ret;
470}
471
472int cmd_mem(int argc, const char **argv)
473{
474	struct stat st;
475	struct perf_mem mem = {
476		.input_name		 = "perf.data",
477		/*
478		 * default to both load an store sampling
479		 */
480		.operation		 = MEM_OPERATION_LOAD | MEM_OPERATION_STORE,
481	};
482	char *sort_order_help = sort_help("sort by key(s):", SORT_MODE__MEMORY);
483	const struct option mem_options[] = {
484	OPT_CALLBACK('t', "type", &mem.operation,
485		   "type", "memory operations(load,store) Default load,store",
486		    parse_mem_ops),
487	OPT_STRING('C', "cpu", &mem.cpu_list, "cpu",
488		   "list of cpus to profile"),
489	OPT_BOOLEAN('f', "force", &mem.force, "don't complain, do it"),
490	OPT_INCR('v', "verbose", &verbose,
491		 "be more verbose (show counter open errors, etc)"),
492	OPT_BOOLEAN('p', "phys-data", &mem.phys_addr, "Record/Report sample physical addresses"),
493	OPT_BOOLEAN(0, "data-page-size", &mem.data_page_size, "Record/Report sample data address page size"),
494	OPT_END()
495	};
496	const struct option record_options[] = {
497	OPT_CALLBACK('e', "event", &mem, "event",
498		     "event selector. use 'perf mem record -e list' to list available events",
499		     parse_record_events),
500	OPT_UINTEGER(0, "ldlat", &perf_mem_events__loads_ldlat, "mem-loads latency"),
501	OPT_BOOLEAN('U', "all-user", &mem.all_user, "collect only user level data"),
502	OPT_BOOLEAN('K', "all-kernel", &mem.all_kernel, "collect only kernel level data"),
503	OPT_PARENT(mem_options)
504	};
505	const struct option report_options[] = {
506	OPT_BOOLEAN('D', "dump-raw-samples", &mem.dump_raw,
507		    "dump raw samples in ASCII"),
508	OPT_BOOLEAN('U', "hide-unresolved", &mem.hide_unresolved,
509		    "Only display entries resolved to a symbol"),
510	OPT_STRING('i', "input", &input_name, "file",
511		   "input file name"),
512	OPT_STRING_NOEMPTY('x', "field-separator", &symbol_conf.field_sep,
513		   "separator",
514		   "separator for columns, no spaces will be added"
515		   " between columns '.' is reserved."),
516	OPT_STRING('s', "sort", &mem.sort_key, "key[,key2...]",
517		   sort_order_help),
518	OPT_BOOLEAN('T', "type-profile", &mem.data_type,
519		    "Show data-type profile result"),
520	OPT_PARENT(mem_options)
521	};
522	const char *const mem_subcommands[] = { "record", "report", NULL };
523	const char *mem_usage[] = {
524		NULL,
525		NULL
526	};
527	int ret;
528
529	argc = parse_options_subcommand(argc, argv, mem_options, mem_subcommands,
530					mem_usage, PARSE_OPT_STOP_AT_NON_OPTION);
531
532	if (!argc || !(strncmp(argv[0], "rec", 3) || mem.operation))
533		usage_with_options(mem_usage, mem_options);
534
535	if (!mem.input_name || !strlen(mem.input_name)) {
536		if (!fstat(STDIN_FILENO, &st) && S_ISFIFO(st.st_mode))
537			mem.input_name = "-";
538		else
539			mem.input_name = "perf.data";
540	}
541
542	if (strlen(argv[0]) > 2 && strstarts("record", argv[0]))
543		ret = __cmd_record(argc, argv, &mem, record_options);
544	else if (strlen(argv[0]) > 2 && strstarts("report", argv[0]))
545		ret = __cmd_report(argc, argv, &mem, report_options);
546	else
547		usage_with_options(mem_usage, mem_options);
548
549	/* free usage string allocated by parse_options_subcommand */
550	free((void *)mem_usage[0]);
551	free(sort_order_help);
552
553	return ret;
554}
~/ Read-only file preview.