前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >【Linux 内核】CFS 调度器 ⑥ ( CFS 调度器就绪队列 cfs_rq | Linux 内核调度实体 sched_entity | “ 红黑树 “ 数据结构 rb_root_cached )

【Linux 内核】CFS 调度器 ⑥ ( CFS 调度器就绪队列 cfs_rq | Linux 内核调度实体 sched_entity | “ 红黑树 “ 数据结构 rb_root_cached )

作者头像
韩曙亮
发布2023-03-30 13:59:41
8280
发布2023-03-30 13:59:41
举报
文章被收录于专栏:韩曙亮的移动开发专栏

文章目录

一、CFS 调度器就绪队列 cfs_rq


调度器 的 主要职责 就是 对 " 进程 " 进行 " 调度管理 " , 调度时 进程 是放在 " 调度队列 " 中的 ,

CFS 调度器 的 调度队列 是 struct cfs_rq ;

通过 该 " CFS 调度器就绪队列 " cfs_rq , 可以 跟踪 " 就绪队列 " 信息 , 管理 " 就绪状态 " 调度实体 , 维护着一个 按照 虚拟时钟 排序的 " 红黑树 " 数据结构 ;

struct cfs_rq 结构体在 Linux 内核源码 的 linux-5.6.18\kernel\sched\sched.h 头文件中定义 ;

代码语言:javascript
复制
/* CFS-related fields in a runqueue */
struct cfs_rq {
	struct load_weight	load;
	unsigned long		runnable_weight;
	unsigned int		nr_running;
	unsigned int		h_nr_running;      /* SCHED_{NORMAL,BATCH,IDLE} */
	unsigned int		idle_h_nr_running; /* SCHED_IDLE */

	u64			exec_clock;
	u64			min_vruntime;

	struct rb_root_cached	tasks_timeline;

	/*
	 * 'curr' points to currently running entity on this cfs_rq.
	 * It is set to NULL otherwise (i.e when none are currently running).
	 */
	struct sched_entity	*curr;
	struct sched_entity	*next;
	struct sched_entity	*last;
	struct sched_entity	*skip;
}
在这里插入图片描述
在这里插入图片描述

二、Linux 内核调度实体 sched_entity


sched_entity 结构体 就是可以 被 Linux 内核 调度 的 实体 ;

可以将该 " 调度实体 " 插入到 红黑树 ( 执行队列 ) 节点 中 ;

代码语言:javascript
复制
	struct sched_entity	*curr;
	struct sched_entity	*next;
	struct sched_entity	*last;
	struct sched_entity	*skip;

三、" 红黑树 " 数据结构 rb_root_cached


在 " CFS 调度器就绪队列 " cfs_rq中定义的

代码语言:javascript
复制
struct rb_root_cached	tasks_timeline;

字段 , 就是 按照 " 虚拟时钟 " 排序的 " 红黑树 " 数据结构 ,

tasks_timeline 指针指向 rb_root_cached 类型的结构体 ,

rb_root 成员 是 这个 " 红黑树 " 数据结构 的 根节点 ;

rb_leftmost 成员 指向 这个 " 红黑树 " 数据结构 的 最左侧的 " 调度实体 " , 就是 " 虚拟时间 " 最小的调度实体 ;

该 " 红黑树 " 数据结构 如下 : 在 Linux 内核源码 linux-5.6.18\include\linux\rbtree.h 路径对应的源码中定义 ;

代码语言:javascript
复制
/*
 * Leftmost-cached rbtrees.
 *
 * We do not cache the rightmost node based on footprint
 * size vs number of potential users that could benefit
 * from O(1) rb_last(). Just not worth it, users that want
 * this feature can always implement the logic explicitly.
 * Furthermore, users that want to cache both pointers may
 * find it a bit asymmetric, but that's ok.
 */
struct rb_root_cached {
	struct rb_root rb_root;
	struct rb_node *rb_leftmost;
};
在这里插入图片描述
在这里插入图片描述
本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2022-03-30,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 文章目录
  • 一、CFS 调度器就绪队列 cfs_rq
  • 二、Linux 内核调度实体 sched_entity
  • 三、" 红黑树 " 数据结构 rb_root_cached
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档