There are four sched_class, stop_sched_class --> rt_sched_class --> fair_sched_class --> idle_sched_class
They are linked one by one staticly by struct sched_class->next by their defination.
Each sched_class has method pick_next_task, which is used to select a perfect process to run from each sched_class‘s runqueue.
When we need schedule, the four pick_next_task will be called one by one.
As a optimization, most time there is no rt task in running state, in this case we can directly call fair_sched_class.
1 2 3 4 5 6
2322/* 2323 * Optimization: we know that if all tasks are in 2324 * the fair class we can call that function directly: 2325 */ 2326if (likely(rq->nr_running == rq->cfs.h_nr_running)) { 2327 p = fair_sched_class.pick_next_task(rq);
question:
how stop_class and idle_class, they has no task? idle_task?
0.2. data structure
0.2.1. sched_class
1 2 3 4 5 6 7 8
1009#define sched_class_highest (&stop_sched_class) 1010#define for_each_class(class) \ 1011 for (class = sched_class_highest; class; class = class->next) 1012 1013externconststructsched_classstop_sched_class; 1014externconststructsched_classrt_sched_class; 1015externconststructsched_classfair_sched_class; 1016externconststructsched_classidle_sched_class;
2313/* 2314 * Pick up the highest-prio task: 2315 */ 2316staticinlinestructtask_struct * 2317 pick_next_task(structrq *rq) 2318 { 2319conststructsched_class *class; 2320structtask_struct *p; 2321 2322/* 2323 * Optimization: we know that if all tasks are in 2324 * the fair class we can call that function directly: 2325 */ 2326if (likely(rq->nr_running == rq->cfs.h_nr_running)) { 2327 p = fair_sched_class.pick_next_task(rq); 2328if (likely(p)) 2329return p; 2330 } 2331 2332 for_each_class(class) { 2333 p = class->pick_next_task(rq); 2334if (p) 2335return p; 2336 } 2337 2338 BUG(); /* the idle class will always have a runnable task */ 2339 }