我们知道为了 CPU 之间减少“干扰”,每个 CPU 上都有一个任务队列。运行的过程种可能会出现有的 CPU 很忙,有的 CPU 很闲,如下图所示:为了避免这个问题的出现,Linux 内核实现了 CPU 可运行进程队列之间的负载均衡。因为负载均衡是在多个核上的均衡,所以在讲解负载均衡之前,我们先看下多核的架构。将 task 从负载较重的 CPU 上转移到负载相对较轻的 CPU 上执行,这个过程就是负载均衡的过程。
多核架构
这里以 Arm64 的 NUMA(Non Uniform Memory Access) 架构为例,看下多核架构的组成。从图中可以看出,这是非一致性内存访问。每个 CPU 访问 local memory,速度更快,延迟更小。因为 Interconnect 模块的存在,整体的内存会构成一个内存池,所以 CPU 也能访问 remote memory,但是相对 local memory 来说速度更慢,延迟更大。我们知道一个多核心的 SOC 片上系统,内部结构是很复杂的。内核采用 CPU 拓扑结构来描述一个 SOC 的架构,使用调度域和调度组来描述 CPU 之间的层次关系。
CPU 拓扑
每一个 CPU 都会维护这么一个结构体实例,用来描述 CPU 拓扑。structcpu_topology { int thread_id; int core_id; int cluster_id; cpumask_t thread_sibling; cpumask_t core_sibling; };
staticint __init parse_dt_topology(void) { structdevice_node *cn, *map; int ret = 0; int cpu;
cn = of_find_node_by_path("/cpus"); ------(1) if (!cn) { pr_err("No CPU information found in DT\n"); return0; }
/* * When topology is provided cpu-map is essentially a root * cluster with restricted subnodes. */ map = of_get_child_by_name(cn, "cpu-map"); ------(2) if (!map) goto out;
ret = parse_cluster(map, 0); ------(3) if (ret != 0) goto out_map;
topology_normalize_cpu_scale();
/* * Check that all cores are in the topology; the SMP code will * only mark cores described in the DT as possible. */ for_each_possible_cpu(cpu) if (cpu_topology[cpu].cluster_id == -1) ret = -EINVAL;