tools/perf/bench/mem-functions.c

Source file repositories/reference/linux-study-clean/tools/perf/bench/mem-functions.c

File Facts

System
Linux kernel
Corpus path
tools/perf/bench/mem-functions.c
Extension
.c
Size
15211 bytes
Lines
631
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

static int do_mmap(const struct function *r, struct bench_params *p,
		  void *src __maybe_unused, void *dst __maybe_unused,
		  union bench_clock *accum)
{
	struct mmap_data *data;
	int error = 0;

	data = calloc(nr_threads, sizeof(*data));
	if (!data) {
		printf("# Failed to allocate thread resources\n");
		return -1;
	}

	for (unsigned int i = 0; i < nr_threads; i++) {
		data[i].func = r;
		data[i].params = p;
		if (p->seed)
			data[i].seed = p->seed + i;

		if (pthread_create(&data[i].id, NULL, do_mmap_thread, &data[i]) < 0)
			data[i].error = -errno;
	}

	for (unsigned int i = 0; i < nr_threads; i++) {
		union bench_clock *t = &data[i].result;

		pthread_join(data[i].id, NULL);

		clock_accum(accum, t);
		if (use_cycles)
			update_stats(&stats, t->cycles);
		else
			update_stats(&stats, t->tv.tv_sec * 1e6 + t->tv.tv_usec);
		error |= data[i].error;
	}
	free(data);

	if (error) {
		printf("# Memory allocation failed - maybe size (%s) %s?\n", size_str,
		       p->page_shift != PAGE_SHIFT_4KB ? "has insufficient hugepages" : "is too large");
	}
	return error ? -1 : 0;
}

static const char * const bench_mem_mmap_usage[] = {
	"perf bench mem mmap <options>",
	NULL
};

static const struct function mmap_functions[] = {
	{ .name		= "demand",
	  .desc		= "Demand loaded mmap()",
	  .fn.mmap_op	= mmap_page_touch },

	{ .name		= "populate",
	  .desc		= "Eagerly populated mmap()",
	  .fn.mmap_op	= mmap_page_touch },

	{ .name = NULL, }
};

int bench_mem_mmap(int argc, const char **argv)
{
	static const struct option bench_mmap_options[] = {
		OPT_UINTEGER('r', "randomize", &seed,
			    "Seed to randomize page access offset."),
		OPT_UINTEGER('t', "threads", &nr_threads,
			    "Number of threads to run concurrently (default: 1)."),
		OPT_PARENT(bench_common_options),
		OPT_END()
	};

	struct bench_mem_info info = {
		.functions		= mmap_functions,
		.do_op			= do_mmap,
		.usage			= bench_mem_mmap_usage,
		.options		= bench_mmap_options,
	};

	return bench_mem_common(argc, argv, &info);
}

Annotation

Implementation Notes