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

在最繁忙的group中去寻找最繁忙的cpu

find_busiest_queue

/*
 * find_busiest_queue - find the busiest runqueue among the cpus in group.
 */
 //冒泡排序找到group内最忙的rq
static struct rq *find_busiest_queue(struct lb_env *env,
				     struct sched_group *group)
{
	struct rq *busiest = NULL, *rq;
	unsigned long busiest_load = 0, busiest_capacity = 1;
	int i;
	
	//冒泡排序遍历group内最忙的cpu
	for_each_cpu_and(i, sched_group_span(group), env->cpus) {
		unsigned long capacity, wl;
		enum fbq_type rt;

		rq = cpu_rq(i);
		rt = fbq_classify_rq(rq);

		/*
		 * We classify groups/runqueues into three groups:
		 *  - regular: there are !numa tasks
		 *  - remote:  there are numa tasks that run on the 'wrong' node
		 *  - all:     there is no distinction
		 *
		 * In order to avoid migrating ideally placed numa tasks,
		 * ignore those when there's better options.
		 *
		 * If we ignore the actual busiest queue to migrate another
		 * task, the next balance pass can still reduce the busiest
		 * queue by moving tasks around inside the node.
		 *
		 * If we cannot move enough load due to this classification
		 * the next pass will adjust the group classification and
		 * allow migration of more tasks.
		 *
		 * Both cases only affect the total convergence complexity.
		 */
		if (rt > env->fbq_type)
			continue;

		/*
		 * For ASYM_CPUCAPACITY domains with misfit(错配) tasks we simply
		 * seek the "biggest" misfit task.
		 */
		if (env->src_grp_type == group_misfit_task) {
			if (rq->misfit_task_load > busiest_load) {
				busiest_load = rq->misfit_task_load;
				busiest = rq;
			}
			continue;
		}

		capacity = capacity_of(i);

		/*
		 * For ASYM_CPUCAPACITY domains, don't pick a cpu that could
		 * eventually lead to active_balancing high->low capacity.
		 * Higher per-cpu capacity is considered better than balancing
		 * average load.
		 */
		if (env->sd->flags & SD_ASYM_CPUCAPACITY &&
		    capacity_of(env->dst_cpu) < capacity &&
		    rq->nr_running == 1)
			continue;

		wl = weighted_cpuload(rq);

		/*
		 * When comparing with imbalance, use weighted_cpuload()
		 * which is not scaled with the cpu capacity.
		 */

		if (rq->nr_running == 1 && wl > env->imbalance &&
		    !check_cpu_capacity(rq, env->sd))
			continue;

		/*
		 * For the load comparisons with the other cpu's, consider
		 * the weighted_cpuload() scaled with the cpu capacity, so
		 * that the load can be moved away from the cpu that is
		 * potentially running at a lower capacity.
		 *
		 * Thus we're looking for max(wl_i / capacity_i), crosswise
		 * multiplication to rid ourselves of the division works out
		 * to: wl_i * capacity_j > wl_j * capacity_i;  where j is
		 * our previous maximum.
		 */
		if (wl * busiest_capacity > busiest_load * capacity) {
			busiest_load = wl;
			busiest_capacity = capacity;
			busiest = rq;
		}
	}

	return busiest;
}

当时我为什么会去画这个图呢?感觉是没有必要的,去绘制C语言的函数的流程图。

<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、付费专栏及课程。

余额充值