Presenter: YangYeh-PD
video
My Question List
SCHED_IDLE
does belong to idle_sched_class
, what is the difference?sleep
process doing in Early Linux Scheduler? Why the process is to do with interactivity?In Linhoward0522
's research on Scheduler, there are related operational instructions.
Go to sched-analyzer and download. After generate trace file, we can put it to perfetto and visualize the behavior of the scheduler.
The CPU scheduler not only plays an important role in preemptive multi-tasking system, but also in non-preemptive multi-tasking system.
The CPU scheduler aims to balance interactiveness and throughput. For a system to achieve immediate responsiveness and interact effectively with users, preemptive multi-tasking is required. However, this preemptive multi-tasking often causes the CPU to be idle at certain moments, reducing overall CPU efficiency.
Fairness means that tasks with the same priority should receive an equal amount of CPU time. However, this alone is not sufficient. If a high-priority task continuously occupies the CPU, other tasks will be unable to execute. Therefore, the design of liveness becomes crucial. Liveness ensures that every task gets CPU resources within a certain timeframe. With liveness, even lower-priority tasks that need to be completed can be executed.
Real-time OS are divided into two types: soft real-time and hard real-time.
The Linux CPU scheduler employs different power-saving methods depending on the platform. On desktops and laptops, there are two main power-saving strategies:
In servers, because the system needs to operate continuously, it is essential to strike a balance between throughput and power consumption. The strategy is
So how does the scheduler know if a task needs to be executed first? Instead of considering other solutions, the Linux Kernel directly requires the source program to specify the priority before handing the task over to the scheduler for scheduling.
The scheduler subsystem of the Linux kernel uses the concept of scheduling classes to implement certain scheduling algorithms. There are 5 different scheduling classes in the current kernel version.
Scheduling classes | Scheduling Policies |
---|---|
stop_sched_class |
|
dl_sched_class |
SCHED_DEADLINE |
rt_sched_class |
SCHED_FIFO &SCHED_RR |
fair_sched_class |
SCHED_NORMAL &SCHED_BATCH &SCHED_IDLE |
idle_sched_class |
Stop is used to schedule the pre-CPU stop task, whic is only available for symmetric multiprocessing (SMP) systems. It includes a mechanism to stop a kernel threwad running on a specific CPU and migrate it to another CPU.
Deadline guarantees that the deadline is respected. A task with this policy defines a deadline, before which the task has to finish its execution.
In the 1rt_sched_class
, there are two scheduling policies, SCHED_FIFO
and SCHED_RR
, which are very similar.
SCHED_FIFO
, FIFO means First In First Out, so tasks are executed in the order they arrive. However, if a higher priority task needs to be executed, it will be given priority.SCHED_RR
(round-robin) is similar to FIFO, but the difference is that RR has a timeslice. It uses round-robin to cycle through all the tasks with the same priority. Each task can run only up to a maximum fixed timeslice.SCHED_NORMAL
and SCHED_OTHER
is the default policy that is used for regular tasks and uses CFS (the Completely Fair Scheduler). SCHED_BATCH
is similar to SCHED_NORMAL
but it will preempt less frequently.
SCHED_IDLE
is for tasks with very low priority, almost any task can preempt tasks with the policy.
Idle is used to schedule the pre-CPU idle task (also called swapper task), which is run if there's no other task runnable.
In v0.01 of Linux kernel, all tasks are expressed by the array. This array is not only the list of all tasks but also the runqueue. The length of this array is NR_TASKS
, which is 64. An empty entry is expressed as NULL
.
The algorithm is simple.
The process will continue. When the task is finished, it would leave the array and set the element to NULL
.
The linear complexity of this algorithm resulted from the requirement to iterate through every task during scheduling events. To reduce the imbalance between batch and interactive tasks, the scheduler incorporated epoch, which represetned the lifetime (timeslice) of every task within the system.
At this point, all tasks, regardless of their runnable status, are stored in a linked list. When Linux need to select a task for execution, it engages O(n) scheduler and iterate every task, give each task a goodness score. The task with the highest goodness score earns access to the core. When the task's timeslice is consumed, it is preemptted, and another task is run.
The experiments aims to demonstrate the difference between FIFO and RR policy, both the policy are real-time
scheduling class. To present the two policies in detail, 3 experiments will be covered.
6 tasks (3 of each) will be generated in this part. The tasks under FIFO policy will be scheduled on the third CPU, and those under RR policy will be scheduled on the second CPU.
There are 4 tasks (2 of each) will be generated in this part. All of them are all scheduled on the third CPU.
We expect that in a FIFO scheduler, each task will be completed before moving on to the next one. In a Round Robin (RR) scheduler, each task is executed in turn each timeslice.
For tasks with different priorities, it is natural that high-priority tasks are executed first. Among tasks with the same priority, a FIFO scheduler will execute the task that arrived first, completing it entirely before moving on to the next task. In contrast, a Round Robin (RR) scheduler switches between tasks at each timeslice.