Article directory
1. Introduction to the scheduler class sched_class
in previous blog
- [Linux kernel] Scheduler ② ( sched_class scheduling class structure source code | source code path linux-5.6.18\kernel\sched\sched.h )
- [Linux kernel] Scheduler ③ ( sched_class scheduling class structure analysis | next field | enqueue_task function | dequeue_task function )
- [Linux kernel] Scheduler ④ ( sched_class scheduling class structure analysis | yield_task function | heck_preempt_curr function | task_struct function )
- [Linux kernel] Scheduler ⑤ (put_prev_task, set_next_task function | select_task_rq function | migrate_task_rq function)
- [Linux Kernel] Scheduler ⑦ (Scheduler Type | Stop Scheduling Class stop_sched_class | Deadline Scheduling Class dl_sched_class | Real Time Scheduling Class | Fair Scheduling Class | Idle Scheduling Class )
, introduces the source code of the scheduling class sched_class structure , important fields and function pointers;
The CFS scheduler class fair_sched_class
is of sched_class
structure type;
2. CFS scheduler class source code
The source code of the CFS scheduler class fair_sched_class
is in the Linux kernel source linux-5.6.18\kernel\sched\fair.c
code ,
/*
* All the scheduling class methods:
*/
const struct sched_class fair_sched_class = {
.next = &idle_sched_class,
.enqueue_task = enqueue_task_fair,
.dequeue_task = dequeue_task_fair,
.yield_task = yield_task_fair,
.yield_to_task = yield_to_task_fair,
.check_preempt_curr = check_preempt_wakeup,
.pick_next_task = __pick_next_task_fair,
.put_prev_task = put_prev_task_fair,
.set_next_task = set_next_task_fair,
}
Three, next assignment
Fieldfair_sched_class
assignment of the CFS scheduler class ,next
.next = &idle_sched_class
idle_sched_class
is the idle scheduling class;
There are multiple "scheduling classes" in the whole Linux system, which are sorted by priority . These "scheduling classes" are placed in a "linked list" , and the "scheduling classes" with high priority are executed first, and those with lower priority are executed later;
sched_class
The next
field points to the next "scheduling class" in the " scheduling class" linked list ; (the priority is lower than this scheduling class)
const struct sched_class *next;
Source path: linux-5.6.18\kernel\sched\sched.h#1709 ;
Four, enqueue_task assignment
Fieldfair_sched_class
assignment of the CFS scheduler class , when the process task enters the "runnable state" , this function to store the scheduling entity, that is, the process , in the execution queue (red-black tree);enqueue_task
enqueue_task_fair
.enqueue_task = enqueue_task_fair,
sched_class
The enqueue_task
function points to a function, and when the function is called, the "process" can be added to the "execution queue" , and it is nr_running
automatically incremented by 1 11 ;
- A process is a scheduling entity;
- The execution queue is a red- black tree;
void (*enqueue_task) (struct rq *rq, struct task_struct *p, int flags);
Source code path: linux-5.6.18\kernel\sched\sched.h#1715 ;
Five, dequeue_task assignment
fair_sched_class
The dequeue_task
field assignment of the CFS scheduler class , when the process task exits the "runnable state" , the dequeue_task_fair
function to delete the scheduling entity, that is, the process, from the execution queue (red-black tree) and perform the dequeue operation;
.dequeue_task = dequeue_task_fair,
dequeue_task
The dequeue_task
function points to a function, calling this function can delete the "process" from the "execution queue" and decrement it by 1 1nr_running
1 ;
- A process is a scheduling entity;
- The execution queue is a red- black tree;
void (*dequeue_task) (struct rq *rq, struct task_struct *p, int flags);
Source code path: linux-5.6.18\kernel\sched\sched.h#1716 ;