tools/testing/selftests/sched/cs_prctl_test.c

Source file repositories/reference/linux-study-clean/tools/testing/selftests/sched/cs_prctl_test.c

File Facts

System
Linux kernel
Corpus path
tools/testing/selftests/sched/cs_prctl_test.c
Extension
.c
Size
9507 bytes
Lines
360
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 child_args {
	int num_threads;
	int pfd[2];
	int cpid;
	int thr_tids[MAX_THREADS];
};

static struct child_args procs[MAX_PROCESSES];
static int num_processes = 2;
static int need_cleanup;

static int _prctl(int option, unsigned long arg2, unsigned long arg3, unsigned long arg4,
		  unsigned long arg5)
{
	int res;

	res = prctl(option, arg2, arg3, arg4, arg5);
	printf("%d = prctl(%d, %ld, %ld, %ld, %lx)\n", res, option, (long)arg2, (long)arg3,
	       (long)arg4, arg5);
	return res;
}

#define STACK_SIZE (1024 * 1024)

#define handle_error(msg) __handle_error(__FILE__, __LINE__, msg)
static void __handle_error(char *fn, int ln, char *msg)
{
	int pidx;
	printf("(%s:%d) - ", fn, ln);
	perror(msg);
	if (need_cleanup) {
		for (pidx = 0; pidx < num_processes; ++pidx)
			kill(procs[pidx].cpid, 15);
		need_cleanup = 0;
	}
	exit(EXIT_FAILURE);
}

static void handle_usage(int rc, char *msg)
{
	puts(USAGE);
	puts(msg);
	putchar('\n');
	exit(rc);
}

static unsigned long get_cs_cookie(int pid)
{
	unsigned long long cookie;
	int ret;

	ret = prctl(PR_SCHED_CORE, PR_SCHED_CORE_GET, pid, PIDTYPE_PID,
		    (unsigned long)&cookie);
	if (ret) {
		printf("Not a core sched system\n");
		return -1UL;
	}

	return cookie;
}

static int child_func_thread(void __attribute__((unused))*arg)
{
	while (1)
		usleep(20000);
	return 0;
}

static void create_threads(int num_threads, int thr_tids[])
{
	void *child_stack;
	pid_t tid;
	int i;

	for (i = 0; i < num_threads; ++i) {
		child_stack = malloc(STACK_SIZE);
		if (!child_stack)
			handle_error("child stack allocate");

		tid = clone(child_func_thread, child_stack + STACK_SIZE, THREAD_CLONE_FLAGS, NULL);
		if (tid == -1)
			handle_error("clone thread");
		thr_tids[i] = tid;
	}
}

static int child_func_process(void *arg)
{
	struct child_args *ca = (struct child_args *)arg;
	int ret;

Annotation

Implementation Notes