tools/testing/selftests/sched_ext/cyclic_kick_wait.c

Source file repositories/reference/linux-study-clean/tools/testing/selftests/sched_ext/cyclic_kick_wait.c

File Facts

System
Linux kernel
Corpus path
tools/testing/selftests/sched_ext/cyclic_kick_wait.c
Extension
.c
Size
4481 bytes
Lines
195
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 worker_ctx {
	pthread_t tid;
	int cpu;
	volatile bool stop;
	volatile __u64 iters;
	bool started;
};

static void *worker_fn(void *arg)
{
	struct worker_ctx *worker = arg;
	cpu_set_t mask;

	CPU_ZERO(&mask);
	CPU_SET(worker->cpu, &mask);

	if (sched_setaffinity(0, sizeof(mask), &mask))
		return (void *)(uintptr_t)errno;

	while (!worker->stop) {
		sched_yield();
		worker->iters++;
	}

	return NULL;
}

static int join_worker(struct worker_ctx *worker)
{
	void *ret;
	struct timespec ts;
	int err;

	if (!worker->started)
		return 0;

	if (clock_gettime(CLOCK_REALTIME, &ts))
		return -errno;

	ts.tv_sec += 2;
	err = pthread_timedjoin_np(worker->tid, &ret, &ts);
	if (err == ETIMEDOUT)
		pthread_detach(worker->tid);
	if (err)
		return -err;

	if ((uintptr_t)ret)
		return -(int)(uintptr_t)ret;

	return 0;
}

static enum scx_test_status setup(void **ctx)
{
	struct cyclic_kick_wait *skel;

	skel = cyclic_kick_wait__open();
	SCX_FAIL_IF(!skel, "Failed to open skel");
	SCX_ENUM_INIT(skel);

	*ctx = skel;
	return SCX_TEST_PASS;
}

static enum scx_test_status run(void *ctx)
{
	struct cyclic_kick_wait *skel = ctx;
	struct worker_ctx workers[NR_WORKERS] = {};
	struct bpf_link *link = NULL;
	enum scx_test_status status = SCX_TEST_PASS;
	int test_cpus[NR_TEST_CPUS];
	int nr_cpus = 0;
	cpu_set_t mask;
	int ret, i;

	if (sched_getaffinity(0, sizeof(mask), &mask)) {
		SCX_ERR("Failed to get affinity (%d)", errno);
		return SCX_TEST_FAIL;
	}

	for (i = 0; i < CPU_SETSIZE; i++) {
		if (CPU_ISSET(i, &mask))
			test_cpus[nr_cpus++] = i;
		if (nr_cpus == NR_TEST_CPUS)
			break;
	}

	if (nr_cpus < NR_TEST_CPUS)
		return SCX_TEST_SKIP;

Annotation

Implementation Notes