tools/perf/bench/syscall.c

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

File Facts

System
Linux kernel
Corpus path
tools/perf/bench/syscall.c
Extension
.c
Size
3649 bytes
Lines
189
Domain
Support Tooling And Documentation
Bucket
tools
Inferred role
Support Tooling And Documentation: syscall or user/kernel boundary
Status
core 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 (waitpid(pid, NULL, 0) < 0) {
			fprintf(stderr, "waitpid failed\n");
			exit(1);
		}
	}
}

static void test_execve(void)
{
	const char *pathname = "/bin/true";
	char *const argv[] = { (char *)pathname, NULL };
	pid_t pid = fork();

	if (pid < 0) {
		fprintf(stderr, "fork failed\n");
		exit(1);
	} else if (pid == 0) {
		execve(pathname, argv, NULL);
		fprintf(stderr, "execve /bin/true failed\n");
		exit(1);
	} else {
		if (waitpid(pid, NULL, 0) < 0) {
			fprintf(stderr, "waitpid failed\n");
			exit(1);
		}
	}
}

static int bench_syscall_common(int argc, const char **argv, int syscall)
{
	struct timeval start, stop, diff;
	unsigned long long result_usec = 0;
	const char *name = NULL;
	int i;

	switch (syscall) {
	case __NR_fork:
	case __NR_execve:
		/* Limit default loop to 10000 times to save time */
		loops = 10000;
		break;
	default:
		loops = 10000000;
		break;
	}

	/* Options -l and --loops override default above */
	argc = parse_options(argc, argv, options, bench_syscall_usage, 0);

	gettimeofday(&start, NULL);

	for (i = 0; i < loops; i++) {
		switch (syscall) {
		case __NR_getppid:
			getppid();
			break;
		case __NR_getpgid:
			getpgid(0);
			break;
		case __NR_fork:
			test_fork();
			break;
		case __NR_execve:
			test_execve();
		default:
			break;
		}
	}

	gettimeofday(&stop, NULL);
	timersub(&stop, &start, &diff);

	switch (syscall) {
	case __NR_getppid:
		name = "getppid()";
		break;
	case __NR_getpgid:
		name = "getpgid()";
		break;
	case __NR_fork:
		name = "fork()";
		break;
	case __NR_execve:
		name = "execve()";
		break;
	default:
		break;
	}

	switch (bench_format) {

Annotation

Implementation Notes