讲解CPU之NUMA硬件体系以及机制(lscpu查看相关信息)

讲解CPU之NUMA硬件体系以及机制(lscpu查看相关信息)

1、IMC 的全称是Integrated Memory Controller,集成内存控制器。IMC是内存通道控制模组。2、QPI ,又名CSI,Common System Interface公共系统接口,是一种可以实现芯片间直接互联的架构。3、Intel的QuickPath Interconnect技术缩写为QPI,译为快速通道互联。官方名字叫做CSI。4、NUMA 全称 Non-Uniform Memory Access,翻译为“非一致性内存访问”。

numa 构架下,每个node都有自己独立的内存控制器和CPU core, 我们看一个开启了numa架构的服务器.

[root@ht4 ~]# numactl --hardware

available: 2 nodes (0-1) #如果是2或多个nodes就说明numa没关掉

node 0 cpus: 0 1 2 3 4 5 6 7 16 17 18 19 20 21 22 23

node 0 size: 16355 MB

node 0 free: 12392 MB

node 1 cpus: 8 9 10 11 12 13 14 15 24 25 26 27 28 29 30 31

node 1 size: 16384 MB

node 1 free: 7290 MB

node distances:

node 0 1

0: 10 20

1: 20 10 也就是说每个 Node 都有自己的集成内存控制器(imc)

系统板的北桥最重要的功能是内存控制器。cpu里集成内存控制器的,因为这样可以减少我们【cpu<--->内存】之间的延迟,以往的内存控制器是集成在北桥里的。而现在的Intel CPU已经将内存控制器集成到了内部。在 Node内部,架构类似SMP架构,使用 IMC Bus 进行不同核心内部间的通信;不同的 Node 间通过QPI进行通信

intel numa cpu模型就比较清楚的解释了运行机制

+-----------------------Node0/smp架构---------------+ +-----------------------Node1/smp架构----------------+

| +++++++++++++++ +++++++++++++++ +++++++++++++++ | | +++++++++++++++ +++++++++++++++ +++++++++++++++ |

| | CPU0 | | CPU2 | | CPU3 | | | CPU6 | | CPU7 | | CPU8 | |

| +++++++++++++++ +++++++++++++++ +++++++++++++++ | | +++++++++++++++ +++++++++++++++ +++++++++++++++ |

| | | | | | | | | |

| | | | | | | | | |

| -------------------IMC BUS-------------------- | | -------------------IMC BUS-------------------- |

| | | | | | | |

| | | | | | | |

| +++++++++++++++ +++++++++++++++ | | +++++++++++++++ +++++++++++++++ |

| | memory | | memory | | | | memory | | memory | |

| +++++++++++++++ +++++++++++++++ | + +++++++++++++++++ +++++++++++++++ |

+___________________________________________________+ +___________________________________________________+

| |

| |

| |

----------------------------------------------------QPI---------------------------------------------------

一般来说,一个内存插槽对应一个 Node。需要注意的一个特点是,QPI的延迟要高于内存IMC Bus,也就是说CPU访问内存有了远近(remote/local)之别后,这个差别非常明显。

在Linux中,对于NUMA有以下几个需要注意的地方:

默认情况下,内核不会将内存页面从一个 NUMA Node 迁移到另外一个 NUMA Node;

但是有现成的工具可以实现将冷页面迁移到远程(Remote)的节点:NUMA Balancing;

关于不同 NUMA Node 上内存页面迁移的规则,社区中有依然有不少争论。

下面我们看下内核分析 numa node

E:\linux内核\linux-2.6.0\linux-2.6.0\arch\x86_64\kernel\head64.c //内核启动代码时候#ifdef CONFIG_DISCONTIGMEM s = strstr(saved_command_line, "numa="); //启动参数存入后command_line后,会在setup_command_line中,再次将启动参数拷贝到static_command_line中 if (s != NULL) numa_setup(s+5);#endif

E:\linux内核\linux-2.6.0\linux-2.6.0\arch\x86_64\mm\numa.c //初始化的时候

{.....

void __init paging_init(void){ int i; for_all_nodes(i) { setup_node_zones(i); }}

/* [numa=off] */__init int numa_setup(char *opt) { if (!strncmp(opt,"off",3)) numa_off = 1; //默认关闭 return 1;}......

}

在centos下查看和关闭numa,看查看文档 https://access.redhat.com/solutions/23216E:\linux内核\linux-2.6.0\linux-2.6.0\Documentation\x86_64\boot-options.txt //内核文档有阐述,系统关闭默认将仅仅安装为单节点。(124行)

//查看linux内核启动行,这个是grub会读取的

[root@ht2 cpu0]# cat /proc/cmdline

BOOT_IMAGE=/vmlinuz-3.10.0-693.el7.x86_64 root=/dev/mapper/centos-root ro rd.lvm.lv=centos/root rd.lvm.lv=centos/swap rhgb quiet LANG=en_US.UTF-8

[root@ht2 cpu0]# cat /etc/default/grub

GRUB_TIMEOUT=5

GRUB_DISTRIBUTOR="$(sed 's, release .*$,,g' /etc/system-release)"

GRUB_DEFAULT=saved

GRUB_DISABLE_SUBMENU=true

GRUB_TERMINAL_OUTPUT="console"

GRUB_CMDLINE_LINUX="rd.lvm.lv=centos/root rd.lvm.lv=centos/swap rhgb quiet"

GRUB_DISABLE_RECOVERY="true"

//安装后的系统里面修改启用numa。

[root@ht2 cpu0]# vi /etc/default/grub

## replace numa=off to numa=on

[root@ht2 cpu0]#grub2-mkconfig -o /boot/efi/EFI/redhat/grub.cfg

[root@ht2 cpu0]#shutdown -r now

安装numa的工具,查看numa和管理numa,工具使用我用了另外一台机器,因为ht2机房太慢了这个工具有点类似 taskset 可以对cpu的行为造成影响。

[root@fp-web-112 ~]# yum -y install numactl //centos7下很容易安装,网速足够快的情况下,安装很简单。

[root@fp-web-112 ~]# numactl -h

语法:

numactl [--interleave nodes] [--preferred node] [--membind nodes]

[--cpunodebind nodes] [--physcpubind cpus] [--localalloc] [--] {arguments ...}

numactl --show

numactl --hardware

numactl [--huge] [--offset offset] [--shmmode shmmode] [--length length] [--strict]

[--shmid id] --shm shmkeyfile | --file tmpfsfile

[--touch] [--dump] [--dump-nodes] memory policy

主要参数:

--interleave=nodes, -i nodes

这个选项用于设定内存的交织分配模式。 也就是说系统在为多个节点分配内存空间的时候,将会以轮询分发的方式被分配给这多个节点.

如果在当前众多的交织分配内存节点中的目标节点无法正确的分配内存空间的话,内存空间将会由其他的节点来分配。

--membind=nodes, -m nodes

选项 '--membind' 仅用来从节点中分配内存空间所用。 如果在这些节点中无法分配出所请求的空间大小的话该分配操作将会失败.

上述命令中指定需要分配空间的 nodes 的方式可以遵照上述 N,N,N , N-N ,N 这种方式来指定.

--cpunodebind=nodes, -N nodes

上述命令仅用于施加在运行与 cpu 上的进程。这个命令用于显示 cpu 的个数, cpu 数目信息同样记录在系统中的存放处理器领域信息的 /proc/cpuinfo 文件夹下,

或者是按照关联的中央处理器信息 在当前的中央处理器集中所存放.

--localalloc , -l

这个命令选项通常是为当前的节点分配内存的

--preferred=node

该命令由于指定优先分配内存空间的节点,如果无法将空间分配给该节点的话,应该分配给该节点上的空间将会被分发到其他的节点上

该命令选项后面仅接收一个单独的节点标号. 相关的表示方式也可以使用.

--show,-s 该命令用于显示 NUMA 机制作用在当前运行的那些进程上

--hardware , -H 该命令用于显示当前系统中有多少个可用的节点.

--huge 当创建一个基于大内存页面的系统级的共享内存段的时候,使用 --huge 这个选项。

--huge 选项仅在 --shmid 或是 --shm 命令的后面使用才有效.

--offset 该参数选项用于指定共享内存段中的位移量的偏移。 默认的情况下偏移量是 0 。 有效的偏移量单位是 m (用于表示 MB)

g (用于表示 GB) , k (用于表示 KB ), 其他没有指定的被认为是以字节为单位.

--strict

这个参数选项 当施加了 NUMA 调度机制的共享内存段区域的页面被施加了另一个机制而导致错误的时候,

使用 --strict 选项将会把错误信息显示出来. 默认情况是不使用该选项的。

--shmmode shmmode

该选项仅在 --shmid 或是 --shm 之前使用才会生效。 当创建一个共享内存段的时候,通过整型数值来指定

共享内存的共享的模式类型.

--length length

Apply policy to length range in the shared memory segment or make the segment length long Default is to use the remaining

length Required when a shared memory segment is created and specifies the length of the new segment then .

Valid units are m ( for MB ) , g( for GB) , k ( for KB) , otherwise it specifies bytes.

--shmid id

通过ID 号码来创建或使用一个共享内存段。

(如果共享内存段已经存在,那么通过 shmid 来指定下面要使用某个 ID 的共享内存段 ; 如果该 ID 对应的共享内存段并不存在的话,那么就创建一个)

--shm shmkeyfile

通过存放在 shmkeyfile(共享内存-键文件)中的 ID 号码来创建或者是使用一个共享内存段。

访问 shmkeyfile 文件的进程是通过 fork(3 arguments) 方法来实现的.

--file tmpfsfile

将 numa 机制施加于文件上面, 这个文件属于 tmpfs或者是 hugetlbfs 这种特殊的文件系统

--touch

通过将 numa 机制施加于刚刚页面上来实现内存的早期 numa 化。

默认情况下是不使用该选项,如果存在映射或是访问页面的应用的话,将会使用该早期实行 NUMA 机制的这种方法.

--dump

该选项用于废除将已经 numa 化的特定区域上的 NUMA性质.

(--dump ) 选项后,有效指定 node 的书写方式

all 用于将所有的节点上的 NUMA 特性移除

number 通过指定 node 后接的数值来废除该数字对应的 node

number1(number2) node number1(node number2)上的 NUMA 特性将会被移除

number1-number2 node number1 -- node number2 区间上的所有存在的 node 的 NUMA 特性将会被移除

!nodes 除了 nodes 所指定的节点以外的所有节点上的 NUMA 特性全都会被移除

//我们查看,linux内核文档写的很清楚,默认是关闭的,关闭的情况下仅仅是安装为单个节点。

//原文: numa=off Only set up a single NUMA node spanning all memory.

[root@fp-web-112 ~]# numactl --hardware

available: 1 nodes (0) //1个节点有效

node 0 cpus: 0 1 2 3 4 5 6 7 //8核都安装在这个节点下

node 0 size: 16383 MB //将近16G内存都由该节点控制

node 0 free: 3710 MB //3.6g是空闲

node distances:

node 0

0: 10查看当前的numa策略

[root@ht4 drivers]# numactl --show policy: default preferred node: current physcpubind: 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 cpubind: 0 1 nodebind: 0 1 membind: 0 1

可显示各node中内存使用情况

[root@ht4 drivers]# numactl -H available: 2 nodes (0-1) node 0 cpus: 0 1 2 3 4 5 6 7 16 17 18 19 20 21 22 23 node 0 size: 16355 MB node 0 free: 12093 MB node 1 cpus: 8 9 10 11 12 13 14 15 24 25 26 27 28 29 30 31 node 1 size: 16384 MB node 1 free: 7247 MB node distances: node 0 1 0: 10 20 1: 20 10

//查看numa状态

[root@fp-web-112 ~]# numastat

node0

numa_hit 5740940530

numa_miss 0

numa_foreign 0

interleave_hit 21170

local_node 5740940530

other_node 0

-m:显示每个节点中,系统范围内使用内存的情况,可以与其他参数组合使用

[root@ht4 drivers]# numastat -m

Per-node system memory usage (in MBs):

Node 0 Node 1 Total

--------------- --------------- ---------------

MemTotal 16355.61 16384.00 32739.61

MemFree 12096.06 7244.43 19340.49

MemUsed 4259.55 9139.57 13399.12

Active 799.33 3653.24 4452.57

Inactive 1053.88 2466.18 3520.05

Active(anon) 171.07 145.52 316.58

Inactive(anon) 97.50 107.89 205.38

Active(file) 628.27 3507.73 4135.99

Inactive(file) 956.38 2358.29 3314.67

Unevictable 0.00 0.00 0.00

Mlocked 0.00 0.00 0.00

Dirty 0.02 0.23 0.25

Writeback 0.00 0.00 0.00

FilePages 1587.35 5874.20 7461.55

Mapped 38.65 207.97 246.62

AnonPages 266.27 245.56 511.82

Shmem 0.40 0.64 1.05

KernelStack 11.89 11.91 23.80

PageTables 5.46 5.90 11.36

NFS_Unstable 0.00 0.00 0.00

Bounce 0.00 0.00 0.00

WritebackTmp 0.00 0.00 0.00

Slab 1639.97 2257.53 3897.50

SReclaimable 810.03 1119.70 1929.73

SUnreclaim 829.94 1137.82 1967.76

AnonHugePages 124.00 128.00 252.00

HugePages_Total 0.00 0.00 0.00

HugePages_Free 0.00 0.00 0.00

HugePages_Surp 0.00 0.00 0.00

-c:紧凑的显示信息,将内存四舍五入到MB单位,适合节点较多时使用

[root@ht4 drivers]# numastat -c

Per-node numastat info (in MBs):

Node 0 Node 1 Total

------- -------- --------

Numa_Hit 8830145 17715290 26545435

Numa_Miss 59522 6479342 6538864

Numa_Foreign 6479342 59522 6538864

Interleave_Hit 61 62 123

Local_Node 8829485 17723254 26552738

Other_Node 60182 6471378 6531560

https://www.percona.com/doc/percona-server/5.5/performance/innodb_numa_support.htmlhttps://dom.as/2014/01/17/on-swapping-and-kernels/https://engineering.linkedin.com/performance/optimizing-linux-memory-management-low-latency-high-throughput-databases

相关文章

新人如何做报表快速学习常用函数和宏操作?
365bet365.com

新人如何做报表快速学习常用函数和宏操作?

⌛ 07-04 👁️‍🗨️ 9581
大话西游2男仙法门选择什么好?(大话西游2男仙用什么套装)
换卡攻略:手机卡与银行卡更换的流程与注意事项详解
365bet365.com

换卡攻略:手机卡与银行卡更换的流程与注意事项详解

⌛ 11-08 👁️‍🗨️ 7716
备孕检查别瞎做!男女必查项目清单(附最佳时间)
365bet365.com

备孕检查别瞎做!男女必查项目清单(附最佳时间)

⌛ 09-30 👁️‍🗨️ 555
h81主板笔记
365平台被黑

h81主板笔记

⌛ 08-02 👁️‍🗨️ 2593
Adobe After Effects
365平台被黑

Adobe After Effects

⌛ 09-04 👁️‍🗨️ 7969