接上篇:【Linux内核十八】进程管理模块:CFS调度器enqueue(五),update_curr中的其他记账 & 内核追踪点
上篇已经将enqueue_entity函数的关键记账函数update_curr看完了。
接下里继续看enqueue_entity函数的其他部分。
enqueue_entity
curr与非curr
在enqueue_entity函数中:
/*
* If we're the current task, we must renormalise before calling
* update_curr().
*/
if (renorm && curr)
se->vruntime += cfs_rq->min_vruntime;
update_curr(cfs_rq);
/*
* Otherwise, renormalise after, such that we're placed at the current
* moment in time, instead of some random moment in the past. Being
* placed in the past could significantly boost this task to the
* fairness detriment of existing tasks.
*/
if (renorm && !curr)
se->vruntime += cfs_rq->min_vruntime;
这段代码表示,如果入队的这个进程就是当前运行的进程(之前的文章已经记录过了),那么就需要先归一化,然后再重新update,如果不是,则先更新完,然后再执行se->vruntime += cfs_rq->min_vruntime;
接下来,是一组函数:
/*
* When enqueuing a sched_entity, we must:
* – Update loads to have both entity and cfs_rq synced with now.
* – Add its load to cfs_rq->runnable_avg
* – For group_entity, update its weight to reflect the new share of
* its group cfs_rq
* – Add its new weight to cfs_rq->load.weight
*/
update_load_avg(cfs_rq, se, UPDATE_TG | DO_ATTACH);
se_update_runnable(se);
update_cfs_group(se);
account_entity_enqueue(cfs_rq, se);
用于更新权重等信息。
update_cfs_group
同样,我先不考虑group调度的部分,也就是CONFIG_FAIR_GROUP_SCHED宏不开启的情况。 这个时候,update_cfs_group是空函数:
static inline void update_cfs_group(struct sched_entity *se)
{
}
update_load_avg
这个函数是用于CPU根据负载来调整运行频率的。
这个函数和宏定义CONFIG_SMP有关,开启这个宏后,内核会从 “单 CPU(UP)模式” 切换为 “多 CPU 模式”,支持系统中多个 CPU 核心并行执行任务,是多核处理器(如服务器、PC、手机 SoC)的核心基础。先简单起见,考虑单处理器情况。
static inline void cpufreq_update_util(struct rq *rq, unsigned int flags) {}
static inline void cfs_rq_util_change(struct cfs_rq *cfs_rq, int flags)
{
struct rq *rq = rq_of(cfs_rq);
if (&rq->cfs == cfs_rq) {
/*
* There are a few boundary cases this might miss but it should
* get called often enough that that should (hopefully) not be
* a real problem.
*
* It will not get called when we go idle, because the idle
* thread is a different class (!fair), nor will the utilization
* number include things like RT tasks.
*
* As is, the util number is not freq-invariant (we'd have to
* implement arch_scale_freq_capacity() for that).
*
* See cpu_util().
*/
cpufreq_update_util(rq, flags);
}
}
static inline void update_load_avg(struct cfs_rq *cfs_rq, struct sched_entity *se, int not_used1)
{
cfs_rq_util_change(cfs_rq, 0);
}
-
代码中的rq_of,也是采用container_of的方式,通过offset来得到cfs_rq所在的rq队列结构体的内存地址。
static inline struct rq *rq_of(struct cfs_rq *cfs_rq)
{
return container_of(cfs_rq, struct rq, cfs);
} -
这里我好奇的一点是为什么增加if (&rq->cfs == cfs_rq)的判断,刚刚通过rq_of获得了rq队列,为什么又要通过rq->cfs_rq再来比对一下。后来才发现,在sched_entity的结构体中,有好几个cfs_rq的结构体:
struct sched_entity*parent;
struct cfs_rq*cfs_rq;而这里的本意是需要根据根cfs_rq,也就是rq->cfs这个队列来进行。所以当传入的是子cfs_rq时,就不会触发这个动作。简单来说,如果CONFIG_FAIR_GROUP_SCHED没开启的话,基本上也是永远为1。
-
紧接着的另一个疑问就是,很多和group调度的函数都会通过CONFIG_FAIR_GROUP_SCHED宏定义来控制,如果CONFIG_FAIR_GROUP_SCHED没开启的花,就不存在子cfs_rq的情况,为什么这里每增加CONFIG_FAIR_GROUP_SCHED的控制?查到的资料基本上是说,为了统一逻辑,因为这个if判断基本不耗时,增加条件编译的话,还要增加代码复杂度,所以没必要。
-
最终调度到的cpufreq_update_util函数是被CONFIG_CPU_FREQ宏来进行条件编译的。
CONFIG_CPU_FREQ宏是 Linux 内核中控制 CPU 频率缩放(CPU Frequency Scaling,简称 cpufreq)子系统是否启用的核心编译配置宏—— 简单说,这个宏决定了内核是否支持动态调整 CPU 的运行频率,是 Schedutil、ondemand 等频率调节器(governor)能工作的 “总开关”。
所以,在上面几个宏默认关闭的情况下,函数update_load_avg也是没干什么事。
se_update_runnable
在CONFIG_FAIR_GROUP_SCHED宏的默认关闭的情况下,也是空函数。
static inline void se_update_runnable(struct sched_entity *se) {}
account_entity_enqueue
去掉CONFIG_SMP条件编译的部分:
static inline void update_load_add(struct load_weight *lw, unsigned long inc)
{
lw->weight += inc;
lw->inv_weight = 0;
}
static void
account_entity_enqueue(struct cfs_rq *cfs_rq, struct sched_entity *se)
{
update_load_add(&cfs_rq->load, se->load.weight);
cfs_rq->nr_running++;
}
- update_load_add传入了cfs_rq的load成员变量,这个成员变量是记录的整个cfs队列的负载信息。还有就是这个入队进程的权重。
- 把cfs的权重加上当前要入队的进程的权重。
- cfs队列load成员的逆权重赋值为0。标记 “权重已变更,逆元失效”,强制后续重新计算 inv_weight,避免使用过期的逆元导致调度计算错误。
- 将对应cfs队列的nr_running加一。
wakeup进程的处理
前面看了那么多,实际上做了三件事情:
if (flags & ENQUEUE_WAKEUP)
place_entity(cfs_rq, se, 0);
/* Entity has migrated, no longer consider this task hot */
if (flags & ENQUEUE_MIGRATED)
se->exec_start = 0;
place_entity
这个函数的核心作用是:当进程(或调度实体)被加入 CFS 队列时,根据队列的min_vruntime(队列时间基准)调整进程的vruntime,保证调度公平性,避免新入队进程因vruntime过小而 “抢占” CPU,或因过大而 “饥饿”。
判断flags & ENQUEUE_WAKEUP就是对wakeup进程做处理。
static void
place_entity(struct cfs_rq *cfs_rq, struct sched_entity *se, int initial)
{
u64 vruntime = cfs_rq->min_vruntime;
if (initial && sched_feat(START_DEBIT))
vruntime += sched_vslice(cfs_rq, se);
/* sleeps up to a single latency don't count. */
if (!initial) {
unsigned long thresh = sysctl_sched_latency;
/*
* Halve their sleep time's effect, to allow
* for a gentler effect of sleepers:
*/
if (sched_feat(GENTLE_FAIR_SLEEPERS))
thresh >>= 1;
vruntime -= thresh;
}
if (entity_is_long_sleeper(se))
se->vruntime = vruntime;
else
se->vruntime = max_vruntime(se->vruntime, vruntime);
}
这个函数主要做了两件事情:
- 根据进程是否为初始化来设置一个vruntime变量,在这里这个initial=0。
- 首先,经过之前的update_curr,经过一些红黑树的调整之后,使用代码u64 vruntime = cfs_rq->min_vruntime;获得当前cfs的最小vruntime值。
- 根据entity_is_long_sleeper,来给需要入队的vruntime赋值。
接下来看看具体的处理流程:
-
sched_feat之前提过,就是判断是否存在START_DEBIT特性这个特性。
-
再来看下sched_vslice函数干了啥
static u64 __sched_period(unsigned long nr_running)
{
if (unlikely(nr_running > sched_nr_latency))
return nr_running * sysctl_sched_min_granularity;
else
return sysctl_sched_latency;
}static u64 sched_slice(struct cfs_rq *cfs_rq, struct sched_entity *se)
{
unsigned int nr_running = cfs_rq->nr_running;
u64 slice;if (sched_feat(ALT_PERIOD))
nr_running = rq_of(cfs_rq)->cfs.h_nr_running;slice = __sched_period(nr_running + !se->on_rq);
for_each_sched_entity(se) {
struct load_weight *load;
struct load_weight lw;cfs_rq = cfs_rq_of(se);
load = &cfs_rq->load;if (unlikely(!se->on_rq)) {
lw = cfs_rq->load;update_load_add(&lw, se->load.weight);
load = &lw;
}
slice = __calc_delta(slice, se->load.weight, load);
}if (sched_feat(BASE_SLICE))
slice = max(slice, (u64)sysctl_sched_min_granularity);return slice;
}/*
* We calculate the vruntime slice of a to-be-inserted task.
*
* vs = s/w
*/
static u64 sched_vslice(struct cfs_rq *cfs_rq, struct sched_entity *se)
{
return calc_delta_fair(sched_slice(cfs_rq, se), se);
}place_entity(cfs_rq, se, 0);
-
先看sched_slice函数。
- nr_running,h_nr_running 两个成员变量和宏ALT_PERIOD。nr_running 是 CFS 队列中 “实际可运行进程数”,h_nr_running 是 “历史平滑后的可运行进程数”,而 ALT_PERIOD 宏是基于 h_nr_running 动态调整调度周期的优化开关,三者共同作用于 CFS 的时间片分配和调度延迟控制。内核引入这个变量的核心目的是过滤瞬时波动,反映真实的长期负载:指数移动平均(EMA)的作用:让 h_nr_running 缓慢跟随 nr_running 变化,避免 “瞬时峰值” 影响调度决策; ALT_PERIOD(全称 ALTERNATIVE_PERIOD)是 Linux 内核中开启 “基于历史负载动态调整调度周期” 的编译宏(5.15 内核默认开启,CONFIG_SCHED_ALT_PERIOD=y),其核心逻辑是基于 h_nr_running(而非 nr_running)动态计算 CFS 的调度周期和时间片长度。
- 所以如果ALT_PERIOD宏开启的话,nr_running就要以历史数据h_nr_running为准。if (sched_feat(ALT_PERIOD))
nr_running = rq_of(cfs_rq)->cfs.h_nr_running;
-
接下来,sched_slice函数又调用了__sched_period方法。
- __sched_period方法计算的是period,而period(调度周期)是 CFS 调度器的核心概念 —— 简单说,它指的是CFS 队列中所有可运行进程 “轮询一次” 所需的总时间,也就是 CFS 保证 “队列里每个进程至少能运行一次” 的时间窗口。这个周期直接决定了进程时间片的大小,是平衡调度公平性和切换开销的关键。在这个函数的注释部分给了一个公式:p = (nr <= nl) ? l : l*nr/nl
- CFS 的核心设计理念是 “让所有可运行进程公平共享 CPU”,而 period 就是实现这个目标的 “时间窗口”:比如 period=100ms,意味着 CFS 会在 100ms 内,让队列里的每个可运行进程都至少获得一次运行机会;单个进程能分到的 “时间片(slice)” = period / 可运行进程数(nr_running); 几个和period相关的关键变量:
- sched_nr_latency,临界进程数,超过这个数,就需要拉长周期:/*
* This value is kept at sysctl_sched_latency/sysctl_sched_min_granularity
*/
static unsigned int sched_nr_latency = 8; - sysctl_sched_min_granularity,最小时间片粒度:unsigned int sysctl_sched_min_granularity = 750000ULL;
- sysctl_sched_latency,基础调度周期: unsigned int sysctl_sched_latency = 6000000ULL;
- sched_nr_latency = sysctl_sched_latency / sysctl_sched_min_granularity
- sched_nr_latency,临界进程数,超过这个数,就需要拉长周期:/*
- __sched_period方法中,如果没有超过临界负载(sched_nr_latency),那么就是直接返回基本的时间片:sysctl_sched_latency。
- __sched_period方法中,通过代码if (unlikely(nr_running > sched_nr_latency))判断,如果负载过大,返回nr_running * sysctl_sched_min_granularity,也就是会把基础调度时间片增加一点,保证一些进程可以得到稍微长一点的时间,尽快结束,减少调度切换,降低CPU负载。
-
通过标准的__sched_period计算得到了基础的时间片之后,sched_slice函数还会根据权重来调整时间片。
- for_each_sched_entity之前提到过,如果没有开启GROUP调度的话,可以忽略。
- 获得cfs_rq的整体负载:cfs_rq->loadstruct load_weight *load;
struct load_weight lw;cfs_rq = cfs_rq_of(se);
load = &cfs_rq->load; - unlikely的判断部分先不管,后面的__calc_delta之前已经说过,完成的核心操作就是将除法换成乘法类的计算:【Linux内核十七】进程管理模块:CFS调度器enqueue(四),update_curr中更新当前运行进程的vruntime。这里就是将时间片通过权重来重新计算一把。
-
计算完这个之后,防止通过权重计算得到的时间片过低,内核还加了一句代码:
if (sched_feat(BASE_SLICE))
slice = max(slice, (u64)sysctl_sched_min_granularity);BASE_SLICE是 CFS 调度器中保障进程时间片 “不低于最小运行粒度” 的最后一道防线—— 核心作用是:当开启 BASE_SLICE 调度特性时,强制单个进程的时间片(slice)不能小于内核设定的最小时间片粒度(sysctl_sched_min_granularity,默认 0.75ms),避免进程因时间片过小而频繁切换,导致内核开销暴增。
- 调用上几篇提到的关键函数:calc_delta_fair,将__sched_period函数返回的时间片,换算成vruntime这个调度单位。
-
如果是非初始化,也就是说从sleep状态中恢复的进程,存在一个“睡眠补偿的逻辑”:
/* sleeps up to a single latency don't count. */
if (!initial) {
unsigned long thresh = sysctl_sched_latency;/*
* Halve their sleep time's effect, to allow
* for a gentler effect of sleepers:
*/
if (sched_feat(GENTLE_FAIR_SLEEPERS))
thresh >>= 1;vruntime -= thresh;
}也就是说,如果是sleep的进程重新入队,会将vruntime 减少一个时间片。同时,如果开启了GENTLE_FAIR_SLEEPERS功能,减少的幅度会少一点。把睡眠时间的影响减半,让睡眠进程(唤醒后)的抢占效果更温和。
迁移进程的处理
se->exec_start = 0;



