1. task_struct
說到調度,第一個首先相當?shù)膽撌莟ask_struct,它用于表示一個進程/線程,它可以看做調度的一個實體單位,該結構體中,幾個與調度較為密切的成員如下:
struct task_struct {
int prio, static_prio, normal_prio;
unsigned int rt_priority;
const struct sched_class *sched_class;
struct sched_entity se;
struct sched_rt_entity rt;
#ifdef CONFIG_CGROUP_SCHED
struct task_group *sched_task_group;
#endif
struct sched_dl_entity dl;
...
unsigned int policy;
int nr_cpus_allowed;
cpumask_t cpus_allowed;
}
- rt_priority是實時進程的優(yōu)先級,其值范圍為0到99,值越大優(yōu)先級越高;
- prio和normal_prio表示動態(tài)優(yōu)先級,static_prio是靜態(tài)優(yōu)先級,靜態(tài)優(yōu)先級由啟動時靜態(tài)分配,例如使用nice值或sched_setscheduler()系統(tǒng)調用。normal_prio基于靜態(tài)優(yōu)先級及調度策略計算,prio用于某些情況下內核需要暫時提高進程的優(yōu)先級。
- sched_class是調度器類;
2. sched_class
3. sched_entity
struct sched_entity {
struct load_weight load;
struct rb_node run_node;
unsigned int on_rq;
u64 exec_start;
u64 sum_exec_runtime;
u64 vruntime;
u64 prev_sum_exec_runtime;
}
- load用于負載均衡,指定了權重,計算該值是調度器的一項重任;
- run_node是一顆紅黑樹節(jié)點,使得實體可在紅黑樹上排序;
- on_rq指示該實體是否在就緒隊列中:struct rq;在就緒隊列中表示等待調度;
- exec_start以及接下來的幾個變量用于時間統(tǒng)計,主要是記錄CPU消耗時間用于CFS調度器。sum_exec_runtime跟蹤進程運行時在CPU上執(zhí)行的總時間,exec_start指示當前執(zhí)行的開始,例如新進程加入就緒隊列或周期性調度器中,每次調用會計算當前時間與exec_start差值疊加到sum_exec_runtime中,而當前時間則設為exec_start,prev_sum_exec_runtime用于計算上次進程執(zhí)行的CPU時間,vruntime記錄進程在執(zhí)行期間虛擬時鐘上流逝的時間數(shù)量。