varnish服务器在内存大量富余时使用交换空间的原因及解决方法
在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 ? ? ? ? ? ? ?對應內存區域的NUMA內存分布策略,如果沒有指定,缺省采用default策略。
anon=3 ? ? ? ? ? ? ? 分配了多少匿名頁
mapped=2621440 ? ? ? 分配了多個頁
dirty=54 ? ? ? ? ? ? 有多少臟頁,即內容被程序修改過
N0=830743 ? ? ? ? ? ?在Node0上分配了多少頁
N1=1790697 ? ? ? ? ? 在Node1上分配了多少頁 ? ? ? ??
從上面對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 ? ? ? ? ?在進程所運行的CPU對應的node上分配內存,這是缺省的方式。
--interleave=nodes ? ?以round-robin在方式在多個node上進行node分配。一個進程即使只在一個CPU上運行,也需要跨CPU進行內存訪問
--physcpubind=cpus ? ?將進程綁定在對應的CPU上
--cpunodebind=nodes ? 將進程綁定在對應的nodes上
這樣,需要修改varnish的起動腳本,/etc/init.d/varnish
#exec="/usr/sbin/varnishd"
exec="/usr/bin/numactl --interleave all /usr/sbin/varnishd"
這個varnish運行后,所使用的內存將均勻地從兩個node上進行分配。
轉載從:李小紅_新浪博客
總結
以上是生活随笔為你收集整理的varnish服务器在内存大量富余时使用交换空间的原因及解决方法的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 锋利的jQuery第2版学习笔记8~11
- 下一篇: Hybrid框架UI重构之路:一、师其长