linux中優化主要參數主要有文件系統,內存與swap,cpu三方面
文件系統(Filesystem)
1)使用ext4或者xfs文件系統,mount選項使用noatime選項。
系統默認記錄文件創建、修改和上一次訪問等信息,記錄上last access time需要一定的開銷。使用noatime選項,不記錄last access time,可以提升系統的性能。
參考:The atime and noatime attribute
2)IO調度算法選擇NOOP或則Deadline。
echo deadline > /sys/block/sda/queue/scheduler
add "elevator=deadline" to grub.conf
內存優化(Memory&Swap)
優先使用內存
儘可能使用內存,而少使用swap。只有當內存不夠用的時候,系統纔會使用swap。
echo 0 > /proc/sys/vm/swappiness
add "vm.swappiness = 0" to /etc/sysctl.conf
屏蔽NUMA特性
設置numactl的interleave參數值爲all,即是允許所有的處理器可以交叉訪問所有的內存,一致性內存訪問(UMA, Uniform Memory Access)方式。
numactl --interleave=all
Node Interleaving: Enable or Disable? 詳細講述了交叉訪問模式的作用。
The MySQL “swap insanity” problem and the effects of the NUMA architecture 深度分析MySQL對於swap的使用和NUMA架構對MySQL的性能影響。
NUMA(Non-Uniform Memory Access 非一致性內存訪問架構,是一種多核處理器的內存設計方案。針對每個處理器,NUMA會把全局的存儲器分爲本地內存(local memory)和非本地內存(no-local memory)。處理器訪問本地內存速度比非本地內存的速度快很多。
CPU優化
檢查CPU是否開啓了節能選項,ondemand表示處於節能狀態。[Centos 5.x]
cat /sys/devices/system/cpu/cpu1/cpufreq/scaling_governor
ondemand
ps ax | grep kndemand
2778 ? S< 0:00 [kondemand/0]
2779 ? S< 0:00 [kondemand/1]
2780 ? S< 0:00 [kondemand/2]
2781 ? S< 0:00 [kondemand/3]
2782 ? S< 0:00 [kondemand/4]
2783 ? S< 0:00 [kondemand/5]
2784 ? S< 0:00 [kondemand/6]
2785 ? S< 0:00 [kondemand/7]
上面的的進程狀態顯示8個cores均開啓了節能模式。
另外,通過/proc/cpuinfo中cpu的當前的時鐘頻率與“model name”中數字是否一致可以得知cpu是否處於節能狀態。如下,“model name”顯示的2.13GHz,而“cpu Mhz”顯示的1867.000MHz,cpu沒有達到最大的時鐘頻率,處於節能狀態。
cat /proc/cpuinfo
processor : 0
vendor_id : GenuineIntel
cpu family : 6
model : 26
model name : Intel(R) Xeon(R) CPU E5506 @ 2.13GHz
stepping : 5
cpu MHz : 1867.000
cache size : 4096 KB
Centos6.x 編譯內核的時候沒有加上cpufreq performance模塊,因此不能使用”cat /sys/devices/system/cpu/cpu1/cpufreq/scaling_governor”檢查cpu是否處於節能工作狀態。
watch grep \"cpu MHz\" /proc/cpuinfo
檢查cpu的工作頻率。
摘要
DBA的羅浮宮
Alexander Rubin,Percona