【内核调度、负载均衡】【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

 

 

<think>好的,我现在需要帮助用户寻找与WALT相关的线程组信息,特别是在IT上下文中。首先,我需要回忆一下WALT(Window-Assisted Load Tracking)的相关知识。WALT主要用于Linux调度器中,特别是在任务负载跟踪和CPU资源分配方面。用户提到了引用[1]和引用[2],这些可能涉及到WALT在窗口翻滚时如何计算任务需求(demand)和预测需求(pred_demand),以及任务效用平均值(util_avg)的处理。 接下来,我需要确定线程组(thread group)在Linux中的定义。线程组通常指的是属于同一进程的多个线程,它们共享相同的进程ID(PID),但有不同的线程ID(TID)。在调度器中,线程组的调度策略和资源分配可能会有特殊处理,特别是当涉及到负载均衡和任务迁移时。 用户的问题是关于WALT相关的线程组信息,所以我需要查找WALT如何与线程组交互。例如,WALT可能在计算任务负载时,是否考虑线程组整体的负载,或者是否有特定的数据结构来管理线程组的调度信息。引用[3]提到了每个CPU有一个就绪队列(struct rq),这可能与线程组的调度有关,因为不同的线程可能分布在不同的CPU上,而调度器需要平衡它们的负载。 根据引用[1],WALT在窗口翻滚时计算demand和pred_demand,这可能涉及到将同一线程组中的线程的负载进行汇总或预测。例如,如果一个线程组包含多个线程,WALT可能需要聚合这些线程的负载需求,以便更准确地分配CPU资源。此外,引用[2]提到task的util_avg在最后一次出队时的值,这可能影响线程组的整体效用计算。 现在,我需要确认在Linux内核代码中,是否有与WALT相关的线程组处理部分。例如,在调度器代码中,当处理线程组时,是否调用了WALT的相关函数来更新负载或预测需求。另外,可能需要查看与cgroup(控制组)相关的部分,因为线程组可能与cgroup相关联,影响资源分配。 可能的代码路径包括kernel/sched目录下的文件,如fair.c、walt.c等。例如,在更新任务负载时,WALT可能会遍历线程组中的所有线程,累加它们的负载需求。或者在任务迁移时,调整线程组的负载统计。 如果用户需要具体的信息,可能需要指导他们查看内核代码中的相关结构体,如struct task_struct中的thread_group字段,或者与调度组(sched_group)相关的部分。此外,WALT可能为线程组维护特定的指标,如组级别的demand或util_avg,这些信息可能存储在特定的结构体中。 总结来说,用户可能需要了解WALT在处理线程组时的负载跟踪机制,包括如何聚合线程的负载、预测需求,以及这些信息如何影响调度决策。建议用户查阅内核源码中与WALT相关的线程组处理部分,特别是涉及任务组负载计算和调度的代码。</think>在 Linux 调度器框架中,WALT (Window-Assisted Load Tracking) 主要通过 `struct walt_task_group` 数据结构管理线程组级别的负载跟踪信息。该结构定义在 `kernel/sched/walt.h` 中,关键字段包括: $$tg_load = \sum_{i=1}^{n} task\_demand_i$$ 其中 $n$ 表示线程组内的任务数量,$task\_demand_i$ 是组内第 $i$ 个任务在窗口期的需求值[^1]。 ### WALT 线程组关联要素 1. **cgroup 层级集成** - 通过 `/sys/fs/cgroup/cpu` 目录下的控制组参数实现层级化负载跟踪 - `cpu.walt_util` 文件节点暴露线程组聚合利用率 2. **调度决策影响** ```c // kernel/sched/fair.c static void update_cpu_busy_time(struct task_struct *p, struct rq *rq) { struct walt_task_group *wtg = p->wtg; if (wtg) { wtg->demand += p->walt_stats.demand; update_tg_load_avg(wtg); } } ``` 该代码段展示了任务负载如何向上聚合到线程组[^2] 3. **跨 CPU 负载平衡** - 就绪队列 `struct rq` 的 `avg_walt_tg_load` 字段记录线程组负载 - 在 `find_busiest_queue()` 中用于比较不同 CPU 的负载压力[^3]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值