cfs调度算法理解
时间片计算方法
整体理解:根据优先级权重分享调度周期的时间
公式:
sched_slice = (task_load / rq_load_total) * sched_latency
sched_latency 在linux上面是有配置i的,当任务小于8个的时候,使用sched_latency , 如果大于8个的话, rq上面的任务个数*sched_granurity.
ubuntu 上面的配置如下:
调度周期 sched_latency,即调度队列中所有任务时间片执行一次的时间
对应的函数代码:
680 /*
681 * We calculate the wall-time slice from the period by taking a part
682 * proportional to the weight.
683 *
684 * s = p*P[w/rw]
685 */
686 static u64 sched_slice(struct cfs_rq *cfs_rq, struct sched_entity *se)
687 {
688 u64 slice = __sched_period(cfs_rq->nr_running + !se->on_rq);
689
690 for_each_sched_entity(se) {
691 struct load_weight *load;
692 struct load_weight lw;
693
694 cfs_rq = cfs_rq_of(se);
695 load = &cfs_rq->load;
696
697 if (unlikely(!se->on_rq)) {
698 lw = cfs_rq->load;
699
700 update_load_add(&lw, se->load.weight);
701 load = &lw;
702 }
703 slice = __calc_delta(slice, se->load.weight, load);
704 }
705 return slice;
706 }
第688行计算调度周期
206 /*
207 * delta_exec * weight / lw.weight
208 * OR
209 * (delta_exec * (weight * lw->inv_weight)) >> WMULT_SHIFT
210 *
211 * Either weight := NICE_0_LOAD and lw \e sched_prio_to_wmult[], in which case
212 * we're guaranteed shift stays positive because inv_weight is guaranteed to
213 * fit 32 bits, and NICE_0_LOAD gives another 10 bits; therefore shift >= 22.
214 *
215 * Or, weight =< lw.weight (because lw.weight is the runqueue weight), thus
216 * weight/lw.weight <= 1, and therefore our shift will also be positive.
217 */
218 static u64 __calc_delta(u64 delta_exec, unsigned long weight, struct load_weight *lw)
219 {
220 u64 fact = scale_load_down(weight);
221 int shift = WMULT_SHIFT;
222