This is a great technical question about the Linux Completely Fair Scheduler (CFS). Let me break this down comprehensively.
Linux CFS Virtual Runtime Explanation
1. The Virtual Runtime Concept
The CFS scheduler was introduced in Linux 2.6.23 and is based on a concept of fairness through weighted time distribution. The key concept is virtual runtime (vruntime).
What is vruntime?
- It represents the amount of CPU time a task has used, normalized by its weight/priority
- It's NOT the same as absolute wall-clock time
- It's a virtual measure that makes the scheduler decisions fair
The Formula:
The virtual runtime advances according to:
vruntime = wall_clock_time / weight
More specifically:
actual_time_slice = ideal_time_slice * (task_weight / total_group_weight)
vruntime_increment = actual_time_slice / task_weight
= (ideal_time_slice * task_weight / total_weight) / task_weight
= ideal_time_slice / total_weight (but adjusted by priority)
Actually, let me be more precise. The vruntime increment is calculated as:
vruntime_delta = time_delta * NICE_0_LOAD / current->load.weight
Where:
time_delta is the actual CPU time consumed
NICE_0_LOAD is the load weight of a task with nice value 0 (1024)
current->load.weight is the load weight of the current task
2. How vruntime Advances
For high-priority tasks (nice -20):
-
Higher weight value
-
vruntime advances SLOWER
-
They accumulate less virtual time for the same wall-clock time
-
Example: A task with nice -20 might have weight ~88761, vruntime increments as: 1024/88761 per unit time
-
So they get more actual CPU time before vruntime catches up
For low-priority tasks (nice +19):
- Lower weight value
- vruntime advances FASTER
- They accumulate more virtual time for the same wall-clock time
- Example: A task with nice +19 might have weight ~15, vruntime increments as: 1024/15 per unit time
- Their vruntime pulls ahead proportionally, ensuring the scheduler deprioritizes them in favor of tasks with lower vruntime values
The mechanism maintains fairness by making low-priority tasks accumulate virtual time quickly—this pushes them further right in the scheduling tree, so they're selected less frequently. Meanwhile, high-priority tasks advance their vruntime more slowly, keeping them left in the tree and getting more CPU access. The red-black tree structure enables the scheduler to always grab the leftmost task (smallest vruntime) in O(log n) time, making this selection process both efficient and fair. this Fair?
All tasks progress through the virtual timeline at roughly the same rate. Consider a scenario with three tasks of different priorities:
- Task A (nice -10): weight 2048, accumulates vruntime slowly per unit of wall-clock time
- Task B (nice 0): weight 1024, the reference point
- Task C (nice +10): weight 512, accumulates vruntime quickly per unit of wall-clock time
When all three reach vruntime ≈ 1000, Task A has consumed more actual CPU time to reach that point, Task B has consumed a medium amount, and Task C has consumed less. This distribution respects their priority levels.
The scheduler achieves fairness by ensuring equal virtual time progress rather than equal wall-clock time. A higher-priority task gets more actual CPU to advance its vruntime at the same pace as lower-priority tasks, which is exactly what we want.
On a single CPU with Task A (nice 0, weight 1024) and Task B (nice 10, weight 512), the scheduler allocates time proportionally: Task A gets roughly 66.7ms per 100ms interval while Task B gets 33.3ms. The vruntime increments are calculated as wall_clock_time × (NICE_0_LOAD / weight), so both tasks advance their vruntime by approximately the same amount despite receiving different amounts of actual CPU time. This is where the fairness emerges—in the virtual timeline, both tasks make equal progress.
The scheduler always selects whichever task has the lowest vruntime to run next. Since high-priority tasks accumulate vruntime slowly (small weight means large divisor) while low-priority tasks accumulate it quickly (large divisor), the system naturally gives more CPU time to higher-priority tasks while keeping all tasks relatively balanced in their virtual progress.
Linux CFS Virtual Runtime Explained
The Virtual Runtime Concept
Virtual runtime (vruntime) is the scheduler's measure of how much CPU time each task has received, normalized by priority. It's the fundamental mechanism that enables fair CPU distribution.
// Simplified: vruntime advances as
vruntime_delta = wall_clock_time × (NICE_0_LOAD / task_weight)
// Where NICE_0_LOAD = 1024 (reference weight for nice=0)
How vruntime Advances (The Fairness Engine)
Example with Two Tasks
Imagine Task A (nice 0) and Task B (nice +10) over 100ms:
Weights:
Task A (nice 0): weight = 1024
Task B (nice +10): weight = 512
Total weight = 1536
CPU allocation proportional to weight:
Task A: 100ms × (1024/1536) ≈ 67ms wall-clock time
Task B: 100ms × (512/1536) ≈ 33ms wall-clock time
vruntime increments (the key insight):
Task A: 67ms × (1024/1024) = 67 virtual units
Task B: 33ms × (1024/512) = 66 virtual units ← SAME!
This is fairness: both tasks advance their virtual timeline at the same rate, ensuring equal progress in virtual time despite unequal wall-clock allocation.
Why Low-Priority Tasks Make This Work
Low priority = lower weight
↓
For same wall-clock time: vruntime_delta = time × (1024/low_weight) = LARGE
↓
vruntime advances quickly
↓
Selected less frequently by the scheduler (next task selection!)
High priority tasks do the opposite—their vruntime grows slowly, so they're selected more often.
Red-Black Tree Task Selection
The Structure
struct cfs_rq {
struct rb_root_cached tasks_timeline; // RB-tree of sched_entity
struct rb_node *rb_leftmost; // Cached leftmost node (O(1) lookup)
};
// Each node's key is: task->se.vruntime
How the Next Task is Selected
// O(1) operation - no tree traversal needed!
struct sched_entity *pick_next_entity(struct cfs_rq *cfs_rq) {
struct sched_entity *left = __pick_first_entity(cfs_rq);
if (!left)
return NULL;
// The leftmost node = minimum vruntime = next task to run
return left;
}
// Equivalent to: return minimum(all_tasks.vruntime)
Visual Representation
[vruntime=500]
/ \
[vruntime=300] [vruntime=700]
/ \
[vruntime=100] [vruntime=400]
↑ (cached: rb_leftmost)
This task is picked next!
Why Red-Black Tree?
- Self-balancing: maintains O(log n) operations
rb_leftmost cached pointer gives O(1) access to minimum vruntime
- Efficient insertion/deletion as tasks sleep/wake
Complete Fairness Picture
The Virtual Timeline Guarantee
After any time interval, if multiple tasks are runnable:
All tasks have approximately equal vruntime
↓
Next task picked = minimum vruntime
↓
Over time, each task accumulates proportional wall-clock time
based on its weight (priority)
Preemption Rule
// A task yields when:
(current->vruntime - min_vruntime) > ideal_runtime
// Where ideal_runtime depends on task count
ideal_runtime = target_latency / nr_running
(e.g., 20ms target_latency)
The task with minimum vruntime is preempted last.
Complete Example: Three Tasks
Tasks: A(nice -5), B(nice 0), C(nice +5)
Weights: A=1280, B=1024, C=820
After 1 second of execution:
┌─────────────────────────────────────┐
│ Wall-Clock Time Allocation │
├─────────────────────────────────────┤
│ A: 1280/(1280+1024+820) = 40.8% = 408ms
│ B: 1024/(...) = 32.6% = 326ms
│ C: 820/(...) = 26.1% = 261ms
└─────────────────────────────────────┘
Virtual Runtime After 1 Second:
┌─────────────────────────────────────┐
│ vruntime = wall_clock × (1024/weight)│
├─────────────────────────────────────┤
│ A: 408 × (1024/1280) = 326 vruntime
│ B: 326 × (1024/1024) = 326 vruntime ← All equal!
│ C: 261 × (1024/820) = 326 vruntime
└─────────────────────────────────────┘
Selection order (repeatedly picks min vruntime):
All have ~326 → round-robin at virtual timeline level
Key Takeaways
| Concept | Why It Works |
|---|
| Low weight = fast vruntime | Keeps priority tasks from hogging the leftmost slot |
| vruntime normalization | Creates a "virtual" timeline where fairness is absolute |
| RB-tree minimum selection | O(1) task selection via cached leftmost node |
| No time slices | CFS adapts based on task count dynamically |
The genius: the scheduler doesn't need to explicitly allocate time slices—the virtual timeline naturally enforces fairness through the mathematical properties of weighted vruntime.