Kernel Scheduler学习之七:CFS调度器之选核流程

  1. Overview
    本博客研究cfs调度器选核的目标,搞清楚如下问题:
    a. 选核的流程是什么
    b.选核的规则是什么

  2. 内容记录
    2.1 整体flow

    /*
     * select_task_rq_fair: Select target runqueue for the waking task in domains
     * that have the 'sd_flag' flag set. In practice, this is SD_BALANCE_WAKE,
     * SD_BALANCE_FORK, or SD_BALANCE_EXEC.
     *
     * Balances load by selecting the idlest CPU in the idlest group, or under
     * certain conditions an idle sibling CPU if the domain has SD_WAKE_AFFINE set.
     *
     * Returns the target CPU number.
     *
     * preempt must be disabled.
     */
    static int
    select_task_rq_fair(struct task_struct *p, int prev_cpu, int sd_flag, int wake_flags)
    {
    	struct sched_domain *tmp, *sd = NULL;
    	int cpu = smp_processor_id();
    	int new_cpu = prev_cpu; //默认选task之前所在的cpu
    	int want_affine = 0;
    	int sync = (wake_flags & WF_SYNC) && !(current->flags & PF_EXITING);
        //根据后面的内容,sync表示A来唤醒B,A可能很快进入sleep状态,为了提高性能,尽量把B选择到当前所在的cpu,也就是此时A运行的cpu
    	if (sd_flag & SD_BALANCE_WAKE) { //sched/core.c当中,调用select_task的时候,sd_flag的值为:SD_BALANCE_WAKE
    		record_wakee(p);//记录唤醒关系,为后面want affine提供依据.
    
    		if (sched_energy_enabled()) { //通过eas的方式选核
    			new_cpu = find_energy_efficient_cpu(p, prev_cpu);
    			if (new_cpu >= 0)
    				return new_cpu;
    			new_cpu = prev_cpu;
    		}
            //如果没有选到,则进入后面的流程.
            //1.1判断是否需要want affine
    		want_affine = !wake_wide(p) && !wake_cap(p, cpu, prev_cpu) &&
    			      cpumask_test_cpu(cpu, p->cpus_ptr);
    	}
    
    	rcu_read_lock();
    /*
    如下的code实现如下任务:
    1. 如果want_affine,则尝试affine到当前cpu或者prev cpu,主要是通过wake_affine实现
    2. 找到最高层次满足wake flag的schedule domain
    */
    	for_each_domain(cpu, tmp) {
    		if (!(tmp->flags & SD_LOAD_BALANCE))
    			break;
    
    		/*
    		 * If both 'cpu' and 'prev_cpu' are part of this domain,
    		 * cpu is a valid SD_WAKE_AFFINE target.
    		 */
            //1.2优先从共cache中选择CPU,
    		if (want_affine && (tmp->flags & SD_WAKE_AFFINE) &&
    		    cpumask_test_cpu(prev_cpu, sched_domain_span(tmp))) {
    			if (cpu != prev_cpu)
    				new_cpu = wake_affine(tmp, p, cpu, prev_cpu, sync);
    
    			sd = NULL; /* Prefer wake_affine over balance flags */
    			break;
    		}
            //2.1获取最高层级的sched_domain,即整个cpu(包括大小核)
    		if (tmp->flags & sd_flag)
    			sd = tmp;
    		else if (!want_affine)
    			break;
    	}
    
    	if (unlikely(sd)) { //因为如果want affine为true的话,sd为空,所以走不进来.
    //所以走到这个地方的条件为:eas没有选到cpu且want affine为false.
    		/* Slow path */
    		new_cpu = find_idlest_cpu(sd, p, cpu, prev_cpu, sd_flag);
    	} else if (sd_flag & SD_BALANCE_WAKE) { /* XXX always ? */
    		/* Fast path */
    //从兄弟cpu中选择.
    		new_cpu = select_idle_sibling(p, prev_cpu, new_cpu);
    
    		if (want_affine)
    			current->recent_used_cpu = cpu;
    	}
    	rcu_read_unlock();
    
    	return new_cpu;
    }
    

    所以,select_task_rq_fair的主要思路如下:
    1.首先尝试通过eas策略进行选核
    2.如果eas没有选到(例如,cpu发生overutilize),则判断是否需要want affine,如果需要affine,则尝试在当前cpu或者prev cpu中进行affine
    3.如果没有affine的话,则采用find idlest cpu
    4.如果有affine的话,则尝试通过在兄弟cpu中做balance选核
    其流程图如下所示:

    2.2 EAS选核

    /*
     * find_energy_efficient_cpu(): Find most energy-efficient target CPU for the
     * waking task. find_energy_efficient_cpu() looks for the CPU with maximum
     * spare capacity in each performance domain and uses it as a potential
     * candidate to execute the task. Then, it uses the Energy Model to figure
     * out which of the CPU candidates is the most energy-efficient.
     *
     * The rationale for this heuristic is as follows. In a performance domain,
     * all the most energy efficient CPU candidates (according to the Energy
     * Model) are those for which we'll request a low frequency. When there are
     * several CPUs for which the frequency request will be the same, we don't
     * have enough data to break the tie between them, because the Energy Model
     * only includes active power costs. With this model, if we assume that
     * frequency requests follow utilization (e.g. using schedutil), the CPU with
     * the maximum spare capacity in a performance domain is guaranteed to be among
     * the best candidates of the performance domain.
     *
     * In practice, it could be preferable from an energy standpoint to pack
     * small tasks on a CPU in order to let other CPUs go in deeper idle states,
     * but that could also hurt our chances to go cluster idle, and we have no
     * ways to tell with the current Energy Model if this is actually a good
     * idea or not. So, find_energy_efficient_cpu() basically favors
     * cluster-packing, and spreading inside a cluster. That should at least be
     * a good thing for latency, and this is consistent with the idea that most
     * of the energy savings of EAS come from the asymmetry of the system, and
     * not so much from breaking the tie between identical CPUs. That's also the
     * reason why EAS is enabled in the topology code only for systems where
     * SD_ASYM_CPUCAPACITY is set.
     *
     * NOTE: Forkees are not accepted in the energy-aware wake-up path because
     * they don't have any useful utilization data yet and it's not possible to
     * forecast their impact on energy consumption. Consequently, they will be
     * placed by find_idlest_cpu() on the least loaded CPU, which might turn out
     * to be energy-inefficient in some use-cases. The alternative would be to
     * bias new tasks towa
  • 2
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值