tools/perf/tests/perf-record.c

Source file repositories/reference/linux-study-clean/tools/perf/tests/perf-record.c

File Facts

System
Linux kernel
Corpus path
tools/perf/tests/perf-record.c
Extension
.c
Size
9593 bytes
Lines
376
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

if (errno == EINVAL && nrcpus < (cpu__max_cpu().cpu << 8)) {
			nrcpus = nrcpus << 2;
			goto realloc;
		}
		perror("sched_getaffinity");
			return -1;
	}

	for (i = 0; i < nrcpus; i++) {
		if (CPU_ISSET_S(i, size, maskp)) {
			if (cpu == -1)
				cpu = i;
			else
				CPU_CLR_S(i, size, maskp);
		}
	}

	return cpu;
}

static int test__PERF_RECORD(struct test_suite *test __maybe_unused, int subtest __maybe_unused)
{
	struct record_opts opts = {
		.target = {
			.uses_mmap = true,
		},
		.no_buffering = true,
		.mmap_pages   = 256,
	};
	int nrcpus = cpu__max_cpu().cpu;
	cpu_set_t *cpu_mask;
	size_t cpu_mask_size;
	struct evlist *evlist = evlist__new_dummy();
	struct evsel *evsel;
	struct perf_sample sample;
	const char *cmd = "sleep";
	const char *argv[] = { cmd, "1", NULL, };
	char *bname, *mmap_filename;
	u64 prev_time = 0;
	bool found_cmd_mmap = false,
	     found_coreutils_mmap = false,
	     found_libc_mmap = false,
	     found_vdso_mmap = false,
	     found_ld_mmap = false;
	int err = -1, errs = 0, i, wakeups = 0;
	u32 cpu;
	int total_events = 0, nr_events[PERF_RECORD_MAX] = { 0, };
	char sbuf[STRERR_BUFSIZE];

	cpu_mask = CPU_ALLOC(nrcpus);
	if (!cpu_mask) {
		pr_debug("failed to create cpumask\n");
		goto out;
	}

	cpu_mask_size = CPU_ALLOC_SIZE(nrcpus);
	CPU_ZERO_S(cpu_mask_size, cpu_mask);

	perf_sample__init(&sample, /*all=*/false);
	if (evlist == NULL) { /* Fallback for kernels lacking PERF_COUNT_SW_DUMMY */
		struct target target = {};

		evlist = evlist__new_default(&target, /*sample_callchains=*/false);
	}

	if (evlist == NULL) {
		pr_debug("Not enough memory to create evlist\n");
		CPU_FREE(cpu_mask);
		goto out;
	}

	/*
	 * Create maps of threads and cpus to monitor. In this case
	 * we start with all threads and cpus (-1, -1) but then in
	 * evlist__prepare_workload we'll fill in the only thread
	 * we're monitoring, the one forked there.
	 */
	err = evlist__create_maps(evlist, &opts.target);
	if (err < 0) {
		pr_debug("Not enough memory to create thread/cpu maps\n");
		goto out_delete_evlist;
	}

	/*
	 * Prepare the workload in argv[] to run, it'll fork it, and then wait
	 * for evlist__start_workload() to exec it. This is done this way
	 * so that we have time to open the evlist (calling sys_perf_event_open
	 * on all the fds) and then mmap them.
	 */
	err = evlist__prepare_workload(evlist, &opts.target, argv, false, NULL);

Annotation

Implementation Notes