tools/sched_ext/scx_userland.c
Source file repositories/reference/linux-study-clean/tools/sched_ext/scx_userland.c
File Facts
- System
- Linux kernel
- Corpus path
tools/sched_ext/scx_userland.c- Extension
.c- Size
- 11984 bytes
- Lines
- 447
- 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.
- Repository support layer: documentation, build tooling, samples, user-space helper tools, generated initramfs support, licenses, and validation utilities.
- Uses kernel synchronization; read lock ordering, sleepability, and interrupt context assumptions before translating.
- Defines or uses C structs; map object ownership, embedded links, reference counts, and lock ownership.
Dependency Surface
stdio.hunistd.hsched.hsignal.hassert.hlibgen.hpthread.hbpf/bpf.hsys/mman.hsys/queue.hsys/syscall.hscx/common.hscx_userland.hscx_userland.bpf.skel.h
Detected Declarations
struct enqueued_taskfunction libbpf_print_fnfunction sigint_handlerfunction get_pid_maxfunction init_tasksfunction task_pidfunction dispatch_taskfunction calc_vruntime_deltafunction update_enqueuedfunction vruntime_enqueuefunction LIST_FOREACHfunction drain_enqueued_mapfunction dispatch_batchfunction spawn_stats_threadfunction pre_bootstrapfunction bootstrapfunction sched_main_loopfunction main
Annotated Snippet
struct enqueued_task {
LIST_ENTRY(enqueued_task) entries;
__u64 sum_exec_runtime;
double vruntime;
};
/*
* Use a vruntime-sorted list to store tasks. This could easily be extended to
* a more optimal data structure, such as an rbtree as is done in CFS. We
* currently elect to use a sorted list to simplify the example for
* illustrative purposes.
*/
LIST_HEAD(listhead, enqueued_task);
/*
* A vruntime-sorted list of tasks. The head of the list contains the task with
* the lowest vruntime. That is, the task that has the "highest" claim to be
* scheduled.
*/
static struct listhead vruntime_head = LIST_HEAD_INITIALIZER(vruntime_head);
/*
* The main array of tasks. The array is allocated all at once during
* initialization, based on /proc/sys/kernel/pid_max, to avoid having to
* dynamically allocate memory on the enqueue path, which could cause a
* deadlock. A more substantive user space scheduler could e.g. provide a hook
* for newly enabled tasks that are passed to the scheduler from the
* .prep_enable() callback to allows the scheduler to allocate on safe paths.
*/
struct enqueued_task *tasks;
static int pid_max;
static double min_vruntime;
static int libbpf_print_fn(enum libbpf_print_level level, const char *format, va_list args)
{
if (level == LIBBPF_DEBUG && !verbose)
return 0;
return vfprintf(stderr, format, args);
}
static void sigint_handler(int userland)
{
exit_req = 1;
}
static int get_pid_max(void)
{
FILE *fp;
int pid_max;
fp = fopen("/proc/sys/kernel/pid_max", "r");
if (fp == NULL) {
fprintf(stderr, "Error opening /proc/sys/kernel/pid_max\n");
return -1;
}
if (fscanf(fp, "%d", &pid_max) != 1) {
fprintf(stderr, "Error reading from /proc/sys/kernel/pid_max\n");
fclose(fp);
return -1;
}
fclose(fp);
return pid_max;
}
static int init_tasks(void)
{
pid_max = get_pid_max();
if (pid_max < 0)
return pid_max;
tasks = calloc(pid_max, sizeof(*tasks));
if (!tasks) {
fprintf(stderr, "Error allocating tasks array\n");
return -ENOMEM;
}
return 0;
}
static __u32 task_pid(const struct enqueued_task *task)
{
return ((uintptr_t)task - (uintptr_t)tasks) / sizeof(*task);
}
static int dispatch_task(__s32 pid)
{
int err;
Annotation
- Immediate include surface: `stdio.h`, `unistd.h`, `sched.h`, `signal.h`, `assert.h`, `libgen.h`, `pthread.h`, `bpf/bpf.h`.
- Detected declarations: `struct enqueued_task`, `function libbpf_print_fn`, `function sigint_handler`, `function get_pid_max`, `function init_tasks`, `function task_pid`, `function dispatch_task`, `function calc_vruntime_delta`, `function update_enqueued`, `function vruntime_enqueue`.
- Atlas domain: Support Tooling And Documentation / tools.
- Implementation status: source implementation candidate.
- Synchronization appears in or near this file; preserve lock ordering, sleepability, and interrupt-context constraints.
Implementation Notes
- This generated page is the file-by-file coverage layer; curated subsystem chapters should link here when they synthesize a multi-file control flow.
- Core OS pages should be promoted from atlas-only to deep-reviewed when they explain data structures, invariants, locking, lifecycle, and C implementation snippets.
- Driver-family pages are intentionally pattern-oriented unless they are part of the selected PCIe/NVMe representative device path.