【内核调度、负载均衡】【find_busiest_group】

本文关注Linux内核调度中find_busiest_group函数的使用,该函数用于确定系统中最忙碌的任务组。文章介绍了group_type枚举类型,包括group_other、group_misfit_task、group_imbalanced和group_overloaded四个状态,并提到了初始化和更新调度器负载平衡统计的相关链接。

find_busiest_group

enum group_type {
    group_other = 0,
    group_misfit_task,
    group_imbalanced,
    group_overloaded,
};

/**
 * find_busiest_group - Returns the busiest group within the sched_domain
 * if there is an imbalance(不平衡).
 *
 * Also calculates the amount of weighted load which should be moved
 * to restore(恢复) balance.
 *
 * @env: The load balancing environment.
 *
 * Return:	- The busiest group if imbalance exists.
 */
static struct sched_group *find_busiest_group(struct lb_env *env)
{
	struct sg_lb_stats *local, *busiest;
	struct sd_lb_stats sds;

	init_sd_lb_stats(&sds);

	/*
	 * Compute the various statistics relavent for load balancing at
	 * this level.
	 */
	 /* (7.3.1) 更新本层级sched_group链表中,每个sched_group的负载,
        并选出busiest的一个sched_group*/
	update_sd_lb_stats(env, &sds);

	
/* (7.3.2) 如果EAS使能,跨cluster的任务迁移使用EAS来做 */
	if (energy_aware() && !sd_overutilized(env->sd) &&
			!env->sd->parent)
		goto out_balanced;

	local = &sds.local_stat;
	busiest = &sds.busiest_stat;

	/* ASYM feature bypasses nice load balance check */
//ASYM功能绕过了不错的负载平衡检查
	if (check_asym_packing(env, &sds))
		return sds.busiest;

	/* There is no busy sibling group to pull tasks from */
	/* (7.3.4) busiest sg上没有负载,返回空 */
	if (!sds.busiest || busiest->sum_nr_running == 0)
		goto out_balanced;

	/* XXX broken for overlapping NUMA groups */
	/* (7.3.5) sg链表里的平均负载 */
	sds.avg_load = (SCHED_CAPACITY_SCALE * sds.total_load)
						/ sds.total_capacity;

	/*
	 * If the busiest group is imbalanced the below checks don't
	 * work because they assume all things are equal, which typically
	 * isn't true due to cpus_allowed constraints and the like.
	 */
	 /* (7.3.6) 如果busiest sg低一级别的因为cpu affinity没有balance成功,设置了group_imbalanced标志 
        强制在当前级别上进行balance
     */
	if (busiest->group_type == group_imbalanced)
		goto force_balance;

	/*
	 * When dst_cpu is idle, prevent SMP nice and/or asymmetric group
	 * capacities from resulting in underutilization due to avg_load.
	 */
	  /* (7.3.7) 如果dest cpu/group很闲,busiest负载很重,  
        强制开展balance
     */
	if (env->idle != CPU_NOT_IDLE && group_has_capacity(env, local) &&
	    busiest->group_no_capacity)
		goto force_balance;

	/* Misfit tasks should be dealt with regardless of the avg load */
	if (busiest->group_type == group_misfit_task)
		goto force_balance;

	/*
	 * If the local group is busier than the selected busiest group
	 * don't try and pull any tasks.
	 */
	  /* (7.3.8)  如果dest_cpu所在sg的负载都大于busiest sg的负载,
        放弃balance
     */
	if (local->avg_load >= busiest->avg_load)
		goto out_balanced;

	/*
	 * Don't pull any tasks if this group is already above the domain
	 * average load.
	 */
	if (local->avg_load >= sds.avg_load)
		goto out_balanced;

	if (env->idle == CPU_IDLE) {
		/*
		 * This cpu is idle. If the busiest group is not overloaded
		 * and there is no imbalance between this and busiest group
		 * wrt idle cpus, it is balanced. The imbalance becomes
		 * significant if the diff is greater than 1 otherwise we
		 * might end up to just move the imbalance on another group
		 */
		if ((busiest->group_type != group_overloaded) &&
				(local->idle_cpus <= (busiest->idle_cpus + 1)))
			goto out_balanced;
	} else {
		/*
		 * In the CPU_NEWLY_IDLE, CPU_NOT_IDLE cases, use
		 * imbalance_pct to be conservative.
		 */
		if (100 * busiest->avg_load <=
				env->sd->imbalance_pct * local->avg_load)
			goto out_balanced;
	}

force_balance:
	/* Looks like there is an imbalance. Compute it */
	env->src_grp_type = busiest->group_type;
	calculate_imbalance(env, &sds);//也去计算需要迁移的负载量
	return env->imbalance ? sds.busiest : NULL;

out_balanced:
	env->imbalance = 0;
	return NULL;
}

init_sd_lb_stats(&sds)详见

https://blog.youkuaiyun.com/feifei_csdn/article/details/107160808

update_sd_lb_stats(env, &sds);详见

https://blog.youkuaiyun.com/feifei_csdn/article/details/107317450

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值