在varnish机器上发现一个比较奇怪的现象。在可用内存还有3G左右,系统已经开始使用交换空间,也就是说有数据在内存与硬盘之间换进换出了。在当前的流量下,这个对整体性能的影响虽不大。但这个问题有必要彻底查一下。以避免在高负载下可能的问题。
硬件
机器使用Dell r710,两颗CPU,每个CPU上配了8G内存,一共是16G内存。给varnish进程分配了10G。
虚虚内存相关的参数
vm.swappiness = 0
经过一段时间的苦思,我觉得这可能跟NUMA内存分配与访问模式有关。下面是对应的探索过程。
NUMA内存布局及使用情况:
[root@lion ~]# numactl --hardware available: 2 nodes (0-1) node 0 size: 8080 MB (Node0总内存) node 0 free: 3326 MB (Node0空闲内存) node 1 size: 8054 MB (Node1总内存) node 1 free: 171 MB (Node1空闲内存) node distances: node 0 1 0: 10 20 1: 20 10
从上面可以看出,两个node的内存分配不均衡。虽然node0上还有将近3G的空闲内存,但node1只有171M空间内存,这样在node1内,仍然会将部分数据交换到硬盘上。这也是系统使用交换空间的原因。
这种分配不均衡,我直觉跟varnish有关,下面先找到varnish的进程号:
[root@lion ~]# ps auwx | grep varnish root 1758 0.0 0.0 118016 1212 ? Ss Apr28 0:00 /usr/sbin/varnishd -P /var/run/varnish.pid -a :80 -f /etc/varnish/default.vcl -T 127.0.0.1:6082 -t 120 -w 1,1000,120 -u varnish -g varnish -S /etc/varnish/secret -s file,/var/lib/varnish/varnish_storage.bin,10G -p first_byte_timeout 600 -p between_bytes_timeout 600 varnish 1760 2.5 67.6 11740556 11112408 ? Sl Apr28 550:00 /usr/sbin/varnishd -P /var/run/varnish.pid -a :80 -f /etc/varnish/default.vcl -T 127.0.0.1:6082 -t 120 -w 1,1000,120 -u varnish -g varnish -S /etc/varnish/secret -s file,/var/lib/varnish/varnish_storage.bin,10G -p first_byte_timeout 600 -p between_bytes_timeout 600
再看varnish的内存map情况:
[root@lion ~]# cat /proc/1760/numa_maps 00400000 default file=/usr/sbin/varnishd mapped=65 mapmax=2 N0=48 N1=17 00658000 default file=/usr/sbin/varnishd anon=3 dirty=3 N0=3 0065b000 default anon=2 dirty=2 mapmax=2 N0=2 0085a000 default file=/usr/sbin/varnishd mapped=2 mapmax=2 N0=2 2aaaab800000 default file=/var/lib/varnish/varnish_storage.bin dirty=54 mapped=2621440 N0=830743 N1=1790697
2aaaab800000
default
anon=3
mapped=2621440
dirty=54
N0=830743
N1=1790697
从上面对varnish_storage.bin的映射情况看出,Node0上有83万左右的页,而Node1上有179万左右的页,两边很不均衡。
那应该怎么样才能将两个node的内存使用变得均衡呢。
首先,linux在NUMA方面的策略在控制到进程粒度,可以设置某个进程的NUMA分配策略,子进程缺省是继承父进程的分配策略。通过运行numactl命令起动进程,新起动的进程将按numactl中指定的参数修改NUMA方面的策略。
看一下numactl命令的参数:
[root@lion ~]# numactl --help numactl: unrecognized option `--help' usage: numactl [--interleave=nodes] [--preferred=node] [--physcpubind=cpus] [--cpunodebind=nodes] [--membind=nodes] [--localalloc] command args ... numactl [--show] numactl [--hardware] numactl [--length length] [--offset offset] [--mode shmmode] [--strict] --shm shmkeyfile | --file tmpfsfile | --shmid id [--huge] [--touch] memory policy memory policy is --interleave, --preferred, --membind, --localalloc nodes is a comma delimited list of node numbers or A-B ranges or none/all. cpus is a comma delimited list of cpu numbers or A-B ranges or all all ranges can be inverted with ! the old --cpubind argument is deprecated. use --cpunodebind or --physcpubind instead length can have g (GB), m (MB) or k (KB) suffixes
比较重要参数有:
–localalloc
–interleave=nodes
–physcpubind=cpus
–cpunodebind=nodes
这样,需要修改varnish的起动脚本,/etc/init.d/varnish
#exec="/usr/sbin/varnishd" exec="/usr/bin/numactl --interleave all /usr/sbin/varnishd"
这个varnish运行后,所使用的内存将均匀地从两个node上进行分配。
注:以上内容摘自于互联网。