接前一篇文章:Linux CFS(完全公平调度器)原理与实现细节全解析(1)
二、核心概念与关键抽象
2.2 调度实体(sched_entity)
CFS的调度基本单位不是传统的task_struct,而是更通用的抽象 —— 调度实体struct sched_entity。
设计原因很简单:
- 有时调度对象是单个进程/线程;
- 有时调度对象是任务组(task group)
或 cgroup;
- 希望在“调度层”统一地处理这些对象,而不关心其内部构成。
因此:
- 普通进程:task_struct中内嵌一个struct sched_entity;
- 任务组:task_group中也内嵌一个struct sched_entity;
-
CFS的核心调度逻辑只围绕sched_entity展开。
其关键字段如下(节选):
// include/linux/sched.h
struct sched_entity {
/* For load-balancing: */
struct load_weight load;
struct rb_node run_node;
struct list_head group_node;
unsigned int on_rq;
u64 exec_start;
u64 sum_exec_runtime;
u64 vruntime;
u64 prev_sum_exec_runtime;
u64 nr_migrations;
#ifdef CONFIG_FAIR_GROUP_SCHED
struct sched_entity *parent;
/* rq on which this entity is (to be) queued: */
struct cfs_rq *cfs_rq;
/* rq "owned" by this entity/group: */
struct cfs_rq *my_q;
#endif
};
其中:
- load
任务权重及其倒数等信息,由nice值通过sched_prio_to_weight映射而来。
- vruntime
虚拟运行时间,是CFS调度排序的核心字段。
- run_node
作为红黑树节点,使得该实体可插入CFS运行队列的rb-tree中。
- cfs_rq
/my_q/parent:在启用组调度时构建层次化调度树:
- cfs_rq
该实体所属运行队列。
- my_q
如果该实体本身代表一个任务组,则它拥有自己的子运行队列my_q。
- parent
父级调度实体,实现自上而下的嵌套调度。
这种设计将“调度逻辑”与“被调度对象”解耦,使CFS可以自然支持从单进程到多级cgroup的复杂层次结构。
更多内容请看下回。
293

被折叠的 条评论
为什么被折叠?



