tools/testing/selftests/powerpc/benchmarks/context_switch.c

Source file repositories/reference/linux-study-clean/tools/testing/selftests/powerpc/benchmarks/context_switch.c

File Facts

System
Linux kernel
Corpus path
tools/testing/selftests/powerpc/benchmarks/context_switch.c
Extension
.c
Size
9044 bytes
Lines
509
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 actions {
	void (*setup)(int, int);
	void *(*thread1)(void *);
	void *(*thread2)(void *);
};

#define READ 0
#define WRITE 1

static int pipe_fd1[2];
static int pipe_fd2[2];

static void pipe_setup(int cpu1, int cpu2)
{
	if (pipe(pipe_fd1) || pipe(pipe_fd2))
		exit(1);
}

static void *pipe_thread1(void *arg)
{
	signal(SIGALRM, sigalrm_handler);
	alarm(1);

	while (1) {
		assert(read(pipe_fd1[READ], &c, 1) == 1);
		touch();

		assert(write(pipe_fd2[WRITE], &c, 1) == 1);
		touch();

		iterations += 2;
	}

	return NULL;
}

static void *pipe_thread2(void *arg)
{
	while (1) {
		assert(write(pipe_fd1[WRITE], &c, 1) == 1);
		touch();

		assert(read(pipe_fd2[READ], &c, 1) == 1);
		touch();
	}

	return NULL;
}

static struct actions pipe_actions = {
	.setup = pipe_setup,
	.thread1 = pipe_thread1,
	.thread2 = pipe_thread2,
};

static void yield_setup(int cpu1, int cpu2)
{
	if (cpu1 != cpu2) {
		fprintf(stderr, "Both threads must be on the same CPU for yield test\n");
		exit(1);
	}
}

static void *yield_thread1(void *arg)
{
	signal(SIGALRM, sigalrm_handler);
	alarm(1);

	while (1) {
		sched_yield();
		touch();

		iterations += 2;
	}

	return NULL;
}

static void *yield_thread2(void *arg)
{
	while (1) {
		sched_yield();
		touch();
	}

	return NULL;
}

static struct actions yield_actions = {
	.setup = yield_setup,

Annotation

Implementation Notes