上帝视角:多核系统的负载均衡
扫描二维码
随时随地手机看文章
多核架构
这里以 Arm64 的 NUMA(Non Uniform Memory Access) 架构为例,看下多核架构的组成。CPU 拓扑
每一个 CPU 都会维护这么一个结构体实例,用来描述 CPU 拓扑。struct cpu_topology {
int thread_id;
int core_id;
int cluster_id;
cpumask_t thread_sibling;
cpumask_t core_sibling;
};
- thread_id: 从 mpidr_el1 寄存器中获取
- core_id:从 mpidr_el1 寄存器中获取
- cluster_id:从mpidr_el1寄存器中获取
- thread_sibling:当前 CPU 的兄弟 thread。
- core_sibling:当前 CPU 的兄弟Core,即在同一个 Cluster 中的 CPU。
kernel_init() -> kernel_init_freeable() -> smp_prepare_cpus() -> init_cpu_topology() -> parse_dt_topology()
static int __init parse_dt_topology(void)
{
struct device_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");
return 0;
}
/*
* 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;
out_map:
of_node_put(map);
out:
of_node_put(cn);
return ret;
}
- 找到 dts 中 cpu topology 的根节点 "/cpus"
- 找到 "cpu-map" 节点
- 解析 "cpu-map" 中的 cluster
# imx8qm.dtsi
cpus: cpus {
#address-cells = <2>;
#size-cells = <0>;
A53_0: cpu@0 {
device_type = "cpu";
compatible = "arm,cortex-a53", "arm,armv8";
reg = <0x0 0x0>;
clocks = <