The user asked for a custom Linux scheduler focusing on I/O-bound tasks, which is quite broad. I think they may want guidance on designing or modifying a kernel policy, maybe even the Completely Fair Scheduler (CFS). Since they didn't specify their level of expertise, I want to ensure I provide structured and high-level information without being trivial. This project seems significant, so outlining steps, necessary data structures, and sample pseudocode referencing Linux kernel version 6.x would be helpful. I need to emphasize the complexity involved and avoid claiming tested code. I could suggest creating a new scheduling class, io_sched_class, or enhancing CFS to prioritize I/O-bound tasks.Understanding I/O-bound scheduling
Since the goal is to prioritize I/O-bound tasks, I need to focus on detecting tasks that frequently sleep on I/O. This means giving them shorter timeslices or higher priority when they wake. Linux already has features like wake-up preemption, especially before version 2.6.23. For a custom scheduler, I should explain the differences between I/O-bound and CPU-bound tasks, and discuss metrics like sleep/run time and I/O wait.
I could also share approaches for classifying tasks, like tracking their runtime versus sleep time and monitoring I/O system calls. Implementing a new scheduling policy, like SCHED_IOPRIO, or adjusting dynamic priority with io_priority might work, but I need to clarify that this involves the CPU scheduler, not the I/O scheduler.Clarifying custom scheduling
They likely want a CPU scheduler that prioritizes I/O-bound processes to reduce latency. While the term "Linux scheduler" could refer to either CPU or I/O schedulers, I believe they're focused on the CPU scheduler. My response should be step-by-step, focusing on designing, patching the kernel, compiling, and testing.
I'd provide code modifications referencing files like kernel/sched/core.c, kernel/sched/fair.c, and include/linux/sched.h. I need to clarify that we can customize the Completely Fair Scheduler (CFS) to boost tasks with high I/O-wait ratios.
To approach this, maintaining a per-task metric, io_bound_score, updated during sleep and wake cycles based on whether the sleep was due to I/O, would be crucial. However, the scheduler can't easily determine the reason for sleep, though checking TASK_UNINTERRUPTIBLE may help identify I/O wait tasks.Simplifying scheduling modifications
To prioritize I/O-bound tasks, I need to measure runnable versus sleeping time. If a process frequently sleeps and wakes up, it's likely interactive or I/O-bound, so reducing its vruntime or increasing its priority would help. The Completely Fair Scheduler (CFS) uses vruntime to prioritize tasks—smaller vruntime means higher priority.
I could modify update_curr() to scale vruntime incrementally for CPU-bound tasks but less so for I/O-bound ones. Alternatively, I could create a new scheduling policy with multiple queues, one for I/O-bound and the other for CPU-bound tasks, although implementing a new sched_class adds complexity. Since the user may not be a pro in kernel development, I might propose a simpler "toy scheduler" for educational purposes or a user-space scheduler, but I still need to keep it relevant to Linux.Developing I/O-aware scheduler
I’m putting together a plan to create an I/O-aware scheduler. First, I’ll track runnable and sleeping times. If a process often sleeps and wakes up, it’s likely interactive or I/O-bound, so I'll decrease its vruntime or boost its priority. I’ll modify update_io_profile_on_dequeue and update_io_profile_on_enqueue for I/O tracking.
In place_entity(), I'll apply an I/O bonus based on io_score to adjust vruntime. This bonus should be capped to maintain fairness. I need to ensure my explanation is digestible.
I’m suggesting custom implementations, mentioning both kernel patches and eBPF-based scheduling approaches to satisfy various preferences. Lastly, I’ll clarify that these kernel operations require admin privileges and will provide a structured answer covering design, implementation, and potential improvements without claiming it's production-ready. Below is a concrete design and example implementation strategy for a CPU scheduler that favors I/O‑bound tasks on Linux by modifying the Completely Fair Scheduler (CFS). This is not a drop‑in patch, but a blueprint you can adapt to a specific kernel version.
1. Design goals
Detect tasks that are I/O‑bound (frequently sleep in I/O wait, use little CPU).
Give such tasks:
Low latency (they run quickly after wakeup).
Limited CPU share (they don’t starve CPU‑bound tasks).
Integrate with CFS rather than rewriting the whole scheduler.
Key idea: use task->in_iowait and sleep/wakeup durations to compute an “I/O score”, then bias CFS’s vruntime placement so high I/O‑score tasks get picked earlier.
2. High‑level approach
Add per‑task accounting for I/O wait:
Track when a task goes to sleep in I/O wait and when it wakes.
Maintain an exponentially decayed “I/O score” representing how I/O‑bound the task has been recently.
Bias CFS scheduling:
When a task is enqueued after wakeup, use its I/O score to give it a vruntime bonus (smaller vruntime → chosen sooner).
Cap the bonus to avoid starvation of CPU‑bound tasks.
Build and run a custom kernel:
Patch the scheduler.
Build, install, and boot.
Compare behavior with synthetic I/O‑bound and CPU‑bound workloads.
3. Implementation details
Assume a recent 6.x kernel. Exact line numbers and helper function names can vary; adjust to your tree.
3.1. Add I/O profiling fields
In include/linux/sched.h, extend struct task_struct or struct sched_entity. Using sched_entity keeps scheduling data grouped:
c
/* include/linux/sched.h */
struct sched_entity {
...
/* Our additions for I/O-bound detection */
u64 io_last_iowait_start; /* rq clock when I/O wait began */
u32 io_score; /* 0..(arbitrary max), higher = more I/O-bound */
...
};
Alternatively, place this in task_struct and access via task_of(se) where needed.
3.2. Update I/O score on sleep/wakeup
We want to detect when a task sleeps for I/O (i.e., task->in_iowait is true), measure duration, and feed it into io_score.
Logical places:
On dequeue (task leaving runqueue to sleep): record start of I/O wait.
On enqueue (task waking): update I/O score using sleep duration.
In kernel/sched/fair.c (or sometimes core.c, depending on kernel), hook around enqueue_task_fair() and dequeue_task_fair().
c
/* kernel/sched/fair.c */
static void update_io_profile_on_dequeue(struct rq *rq, struct task_struct *p, int flags)
{
struct sched_entity *se = &p->se;
if (p->in_iowait) {
/* Record start time of I/O wait in rq's timebase */
se->io_last_iowait_start = rq_clock(rq);
}
}
static void update_io_profile_on_enqueue(struct rq *rq, struct task_struct *p, int flags)
{
struct sched_entity *se = &p->se;
u64 now = rq_clock(rq);
/* If we recorded an I/O wait start, update score */
if (se->io_last_iowait_start) {
u64 delta = now - se->io_last_iowait_start;
/* Simple exponential moving average. Scale down to avoid overflow. */
u32 sample = (u32)min(delta >> 10, (u64)100000); /* arbitrary scaling */
/* io_score_new = 7/8 old + 1/8 sample */
se->io_score = (se->io_score * 7 + sample) / 8;
se->io_last_iowait_start = 0;
}
/* Passive decay each enqueue, so “old” I/O behavior fades out */
se->io_score = (se->io_score * 95) / 100;
}
The precise function names may differ slightly; match them to your kernel.
3.3. Bias vruntime for I/O‑bound tasks
CFS chooses the task with the smallest se->vruntime. When a task is enqueued (especially on wakeup), place_entity() decides where its vruntime starts.
We’ll adjust place_entity() to subtract an “I/O bonus” from vruntime based on io_score.
In kernel/sched/fair.c:
c
/* Tuneable parameters */
static unsigned int sysctl_sched_io_bonus_pct = 25; /* up to 25% of slice */
static unsigned int sysctl_sched_io_score_div = 1024; /* scaling for io_score */
static inline u64 io_vruntime_bonus(struct cfs_rq *cfs_rq, struct sched_entity *se)
{
/*
* Compute a bonus proportional to io_score.
* Maximum bonus limited to some fraction of sched_latency.
*/
u64 latency = sysctl_sched_latency; /* existing CFS tunable */
u64 max_bonus = (latency * sysctl_sched_io_bonus_pct) / 100;
u64 bonus;
/* Higher io_score -> more bonus, scaled down by io_score_div */
bonus = (u64)se->io_score * (latency / sysctl_sched_io_score_div);
if (bonus > max_bonus)
bonus = max_bonus;
return bonus;
}
static void place_entity(struct cfs_rq *cfs_rq, struct sched_entity *se, int initial)
{
u64 vruntime = cfs_rq->min_vruntime;
if (!initial) {
vruntime = max_vruntime(se->vruntime, vruntime);
if (sched_feat(START_DEBIT))
vruntime += sched_vslice(cfs_rq, se);
}
/* Apply I/O bonus: move entity to the left (earlier scheduling) */
if (se->io_score) {
u64 bonus = io_vruntime_bonus(cfs_rq, se);
/*
* Protect min_vruntime invariants: don't go too far left.
* Allow at most (latency) distance.
*/
if (bonus > vruntime - cfs_rq->min_vruntime)
bonus = vruntime - cfs_rq->min_vruntime;
vruntime -= bonus;
}
se->vruntime = vruntime;
}
Conceptually:
Frequent I/O sleep → higher io_score.
Higher io_score → more vruntime bonus → task tends to run sooner after it wakes.
The bonus is capped per task to maintain some fairness.
You can expose sysctl_sched_io_bonus_pct and sysctl_sched_io_score_div via /proc/sys/kernel/ by wiring them into the existing sched sysctl table if you want runtime tuning.
4. Building and testing
4.1. Build a custom kernel
Get kernel source:
bash
git clone https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
cd linux
# or checkout the same major version your distro uses
Apply your modifications.
Configure:
bash
cp /boot/config-$(uname -r) .config
make oldconfig
Build and install (this may take a while):
bash
make -j$(nproc)
sudo make modules_install
sudo make install
Update your bootloader if needed and reboot into the new kernel.
Use top, htop, or sched/tracing tools (perf sched record/report, trace-cmd) to observe:
The I/O‑bound task should get scheduled promptly when it wakes (low delay from I/O completion to running).
CPU hogs should not fully starve but may see slightly less CPU share when I/O‑bound jobs are active.
5. Considerations and extensions
Starvation/fairness:
The bonus must be capped; otherwise a constantly I/O‑bound task can starve others. Watch rq->min_vruntime and load to ensure fairness remains roughly intact.
SMP behavior:
The above acts per runqueue; tasks migrating between CPUs retain their io_score. That’s generally fine, but you may want to test under multi‑core stress.
More precise I/O classification:
Differentiate between disk I/O vs. network I/O.
Use task->in_iowait plus additional kernel instrumentation.
Track CPU time vs. sleep time ratios to distinguish interactive vs. I/O‑bound vs. CPU‑bound more accurately.
Alternative: BPF extensible scheduler (sched_ext):
Newer kernels support a BPF‑based extensible scheduler. You can implement an I/O‑aware scheduling policy in eBPF and load it without patching the kernel core. If you’re experimenting and not shipping a distro kernel, this is often cleaner.
If you specify your target kernel version and whether you’d rather patch CFS or use sched_ext/eBPF, I can refine this into a version‑specific patch or a minimal working example.