首页 > 解决方案 > 如何避免 numa 架构中的远程内存分配?

问题描述

我有一台 numa 机器,有两个节点,每个节点有 8 个内核和 64G 内存,并且有两个服务:service-1 和 service-2,service-1 部署在 node-0 中,service-2 部署在 node-1 中。

我只是想让这两个服务分开运行,所以我启动服务如下:

numactl --membind=0 ./service-1

numactl --membind=1 ./service-2

在服务代码中,我使用 pthread_setaffinity_np 将线程绑定到相应节点的 cpus(绑定 service-1 到 cpu[0,2,4,6,8,10,12,14,16,18,20,22,24,26, 28,30]);

localhost:~$ lscpu
Architecture:          x86_64
CPU op-mode(s):        32-bit, 64-bit
Byte Order:            Little Endian
CPU(s):                32
On-line CPU(s) list:   0-31
Thread(s) per core:    2
Core(s) per socket:    8
Socket(s):             2
NUMA node(s):          2
NUMA node0 CPU(s):     0,2,4,6,8,10,12,14,16,18,20,22,24,26,28,30
NUMA node1 CPU(s):     1,3,5,7,9,11,13,15,17,19,21,23,25,27,29,31


localhost:~$ numactl -H
available: 2 nodes (0-1)
node 0 cpus: 0 2 4 6 8 10 12 14 16 18 20 22
node 0 size: 64327 MB
node 0 free: 17231 MB
node 1 cpus: 1 3 5 7 9 11 13 15 17 19 21 23
node 1 size: 64498 MB
node 1 free: 37633 MB
node distances:
node   0   1
  0:  10  21
  1:  21  10

我期望的是linux系统在node-1上为service-2分配内存,除了service-2主要访问node-1内存和node-0上的一些动态库页面。但遗憾的是我发现 service-2 在远程节点(node-0)上有很多内存,访问它花费太多时间。

service-2's numa_maps as followed:

7f07e8c00000 bind:1 anon=455168 dirty=455168 N0=442368 N1=12800
7f08e4200000 bind:1 anon=128000 dirty=128000 N0=121344 N1=6656
7f092be00000 bind:1 anon=21504 dirty=21504 N0=20992 N1=512
7f093ac00000 bind:1 anon=32768 dirty=32768 N0=27136 N1=5632
7f0944e00000 bind:1 anon=82432 dirty=82432 N0=81920 N1=512
7f0959400000 bind:1 anon=1024 dirty=1024 N0=1024
7f0959a00000 bind:1 anon=4096 dirty=4096 N0=4096
7f095aa00000 bind:1 anon=2560 dirty=2560 N0=2560
7f095b600000 bind:1 anon=4608 dirty=4608 N0=4608
7f095c800000 bind:1 anon=512 dirty=512 N0=512
7f095cc00000 bind:1 anon=512 dirty=512 N0=512
...

所以这是我的问题:

1、linux系统真的会为service-2分配远程内存(node-0)吗,不管service-2已经被membind命令绑定到node-1了吗?

--membind=nodes, -m nodes
Only allocate memory from nodes. Allocation will fail when there is not enough memory available on these nodes. nodes may be specified as noted above.

2、这是否与kernel.numa_balancing相关联,我的机器中的值为 1?在我看来,在 kernel.numa_balancing = 1 的情况下,linux 只会将任务迁移到最近的内存或将内存移动到执行任务的最近节点。既然 service-2 已经绑定到 node-1,就不会发生平衡了吗?

3、谁能解释一下这个远程分配是怎么发生的?有什么方法可以避免这种情况吗?

非常感谢 !

标签: c++linuxlinux-kernelnumanumactl

解决方案


推荐阅读