tools/perf/util/synthetic-events.c

Source file repositories/reference/linux-study-clean/tools/perf/util/synthetic-events.c

File Facts

System
Linux kernel
Corpus path
tools/perf/util/synthetic-events.c
Extension
.c
Size
67949 bytes
Lines
2733
Domain
Support Tooling And Documentation
Bucket
tools
Inferred role
Support Tooling And Documentation: implementation source
Status
source implementation candidate

Why This File Exists

Repository support layer: documentation, build tooling, samples, user-space helper tools, generated initramfs support, licenses, and validation utilities.

Dependency Surface

Detected Declarations

Annotated Snippet

struct perf_event__synthesize_modules_maps_cb_args {
	const struct perf_tool *tool;
	perf_event__handler_t process;
	struct machine *machine;
	union perf_event *event;
};

static int perf_event__synthesize_modules_maps_cb(struct map *map, void *data)
{
	struct perf_event__synthesize_modules_maps_cb_args *args = data;
	union perf_event *event = args->event;
	struct dso *dso;
	size_t size;

	if (!__map__is_kmodule(map))
		return 0;

	dso = map__dso(map);
	if (!symbol_conf.no_buildid_mmap2) {
		size = PERF_ALIGN(dso__long_name_len(dso) + 1, sizeof(u64));
		event->mmap2.header.type = PERF_RECORD_MMAP2;
		event->mmap2.header.size = (sizeof(event->mmap2) -
					(sizeof(event->mmap2.filename) - size));
		memset(event->mmap2.filename + size, 0, args->machine->id_hdr_size);
		event->mmap2.header.size += args->machine->id_hdr_size;
		event->mmap2.start = map__start(map);
		event->mmap2.len   = map__size(map);
		event->mmap2.pid   = args->machine->pid;

		memcpy(event->mmap2.filename, dso__long_name(dso), dso__long_name_len(dso) + 1);

		/* Clear stale build ID from previous module iteration */
		event->mmap2.header.misc &= ~PERF_RECORD_MISC_MMAP_BUILD_ID;
		memset(event->mmap2.build_id, 0, sizeof(event->mmap2.build_id));
		event->mmap2.build_id_size = 0;

		perf_record_mmap2__read_build_id(&event->mmap2, args->machine, false);
	} else {
		size = PERF_ALIGN(dso__long_name_len(dso) + 1, sizeof(u64));
		event->mmap.header.type = PERF_RECORD_MMAP;
		event->mmap.header.size = (sizeof(event->mmap) -
					(sizeof(event->mmap.filename) - size));
		memset(event->mmap.filename + size, 0, args->machine->id_hdr_size);
		event->mmap.header.size += args->machine->id_hdr_size;
		event->mmap.start = map__start(map);
		event->mmap.len   = map__size(map);
		event->mmap.pid   = args->machine->pid;

		memcpy(event->mmap.filename, dso__long_name(dso), dso__long_name_len(dso) + 1);
	}

	if (perf_tool__process_synth_event(args->tool, event, args->machine, args->process) != 0)
		return -1;

	return 0;
}

int perf_event__synthesize_modules(const struct perf_tool *tool, perf_event__handler_t process,
				   struct machine *machine)
{
	int rc;
	struct maps *maps = machine__kernel_maps(machine);
	struct perf_event__synthesize_modules_maps_cb_args args = {
		.tool = tool,
		.process = process,
		.machine = machine,
	};
	size_t size = symbol_conf.no_buildid_mmap2
		? sizeof(args.event->mmap)
		: sizeof(args.event->mmap2);

	args.event = zalloc(size + machine->id_hdr_size);
	if (args.event == NULL) {
		pr_debug("Not enough memory synthesizing mmap event "
			 "for kernel modules\n");
		return -1;
	}

	/*
	 * kernel uses 0 for user space maps, see kernel/perf_event.c
	 * __perf_event_mmap
	 */
	if (machine__is_host(machine))
		args.event->header.misc = PERF_RECORD_MISC_KERNEL;
	else
		args.event->header.misc = PERF_RECORD_MISC_GUEST_KERNEL;

	rc = maps__for_each_map(maps, perf_event__synthesize_modules_maps_cb, &args);

	free(args.event);

Annotation

Implementation Notes