should_we_balance
判断是否应该做balance
在should_we_balance()做各种判断,做dst_cpu的条件有:要么是本sg的第一个idle cpu,要么是本sg的第一个cpu
判断是否需要在当前cpu上做load balance
(1) 如果是NEWLY IDLE,需要做load balance
(2) 否则的需要在idle cpu上做balance
(3) 如果没有idle cpu的话,就在group的第一个cpu上做load balance
static int should_we_balance(struct lb_env *env)
{struct sched_group *sg = env->sd->groups;int cpu, balance_cpu = -1;/** Ensure the balancing environment is consistent; can happen* when the softirq triggers 'during' hotplug.*/if (!cpumask_test_cpu(env->dst_cpu, env->cpus))return 0;/** In the newly idle case, we will allow all the cpu's* to do the newly idle load balance.*//* (7.2.1) 如果本cpu为CPU_NEWLY_IDLE,直接符合迁移条件 */if (env->idle == CPU_NEWLY_IDLE)return 1;/* Try to find first idle cpu *//* (7.2.2) 本sched_group的第一个idle cpu适合做load_balance *///如果这里的第一个idle cpu是dst cpu,则开始做load balancefor_each_cpu_and(cpu, group_balance_mask(sg), env->cpus) {if (!idle_cpu(cpu))continue;balance_cpu = cpu;break;}/* (7.2.3) 没有idle cpu,则选取本sched_group的第一个cpu做load_balance */// sg的第一个cpu是dst cpu,则开始做load balanceif (balance_cpu == -1)balance_cpu = group_balance_cpu(sg);//the first cpu(busiest) in this sched group/** First idle cpu or the first cpu(busiest) in this sched group* is eligible(合格) for doing load balancing at this and above domains.*/return balance_cpu == env->dst_cpu;
}