tools/perf/bench/inject-buildid.c

Source file repositories/reference/linux-study-clean/tools/perf/bench/inject-buildid.c

File Facts

System
Linux kernel
Corpus path
tools/perf/bench/inject-buildid.c
Extension
.c
Size
11467 bytes
Lines
487
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 bench_data {
	int			pid;
	int			input_pipe[2];
	int			output_pipe[2];
	pthread_t		th;
};

struct bench_dso {
	struct list_head	list;
	char			*name;
	int			ino;
};

static int nr_dsos;
static struct bench_dso *dsos;

extern int main(int argc, const char **argv);

static const struct option options[] = {
	OPT_UINTEGER('i', "iterations", &iterations,
		     "Number of iterations used to compute average (default: 100)"),
	OPT_UINTEGER('m', "nr-mmaps", &nr_mmaps,
		     "Number of mmap events for each iteration (default: 100)"),
	OPT_UINTEGER('n', "nr-samples", &nr_samples,
		     "Number of sample events per mmap event (default: 100)"),
	OPT_INCR('v', "verbose", &verbose,
		 "be more verbose (show iteration count, DSO name, etc)"),
	OPT_END()
};

static const char *const bench_usage[] = {
	"perf bench internals inject-build-id <options>",
	NULL
};

/*
 * Helper for collect_dso that adds the given file as a dso to dso_list
 * if it contains a build-id.  Stops after collecting 4 times more than
 * we need (for MMAP2 events).
 */
static int add_dso(const char *fpath, const struct stat *sb __maybe_unused,
		   int typeflag, struct FTW *ftwbuf __maybe_unused)
{
	struct bench_dso *dso = &dsos[nr_dsos];
	struct build_id bid = { .size = 0, };

	if (typeflag == FTW_D || typeflag == FTW_SL)
		return 0;

	if (filename__read_build_id(fpath, &bid) < 0)
		return 0;

	dso->name = realpath(fpath, NULL);
	if (dso->name == NULL)
		return -1;

	dso->ino = nr_dsos++;
	pr_debug2("  Adding DSO: %s\n", fpath);

	/* stop if we collected enough DSOs */
	if ((unsigned int)nr_dsos == DSO_MMAP_RATIO * nr_mmaps)
		return 1;

	return 0;
}

static void collect_dso(void)
{
	dsos = calloc(nr_mmaps * DSO_MMAP_RATIO, sizeof(*dsos));
	if (dsos == NULL) {
		printf("  Memory allocation failed\n");
		exit(1);
	}

	if (nftw("/usr/lib/", add_dso, 10, FTW_PHYS) < 0)
		return;

	pr_debug("  Collected %d DSOs\n", nr_dsos);
}

static void release_dso(void)
{
	int i;

	for (i = 0; i < nr_dsos; i++) {
		struct bench_dso *dso = &dsos[i];

		zfree(&dso->name);
	}
	free(dsos);

Annotation

Implementation Notes