当前位置:首页 > 单片机 > CPP开发者
[导读]我们知道为了CPU之间减少“干扰”,每个CPU上都有一个任务队列。运行的过程种可能会出现有的CPU很忙,有的CPU很闲,如下图所示:为了避免这个问题的出现,Linux内核实现了CPU可运行进程队列之间的负载均衡。因为负载均衡是在多个核上的均衡,所以在讲解负载均衡之前,我们先看下多...

我们知道为了 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 拓扑。

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。
可以通过 /sys/devices/system/cpu/cpuX/topology 查看 cpu topology 的信息。

cpu_topology 结构体是通过函数 parse_dt_topology() 解析 DTS 中的信息建立的:

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(map0);                 ------(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;
}
  1. 找到 dts 中 cpu topology 的根节点 "/cpus"
  2. 找到 "cpu-map" 节点
  3. 解析 "cpu-map" 中的 cluster
以 i.mx8qm 为例,topology 为:”4A53 2A72”,dts中定义如下:

# 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 = <
本站声明: 本文章由作者或相关机构授权发布,目的在于传递更多信息,并不代表本站赞同其观点,本站亦不保证或承诺内容真实性等。需要转载请联系该专栏作者,如若文章内容侵犯您的权益,请及时联系本站删除( 邮箱:macysun@21ic.com )。
换一批
延伸阅读
关闭