CPU 架構SMP/NUMA,調優node
SMP:全稱是「對稱多處理」(Symmetrical Multi-Processing)技術 。ios
是指在一個計算機上聚集了一組處理器(多CPU),各CPU之間共享內存以及總線。nginx
弱點:CPU變多後,可是內存和內存控制器只有一個,CPU是經過內存控制器訪問內存的,因此多個CPU對內存控制器就會產生競爭,爲了不競爭就出現了NUMA架構。c++
NUMA:Non Uniform Memory Accessshell
各個CPU有本身專用的內存(學名叫node),可是也能夠訪問別的CPU的專業內存,這時性能就會降低。ubuntu
Linux下NUMA相關的命令微信
numastat:查看節點的狀態。架構
能夠看到本身CPU下的進程命中了本身的內存(node)多少次,沒命中多少次,若是沒命中的次數多了怎麼辦?就要強制把這進程綁定到本身的CPU上。性能
<font color=red>經典的應用場景:把nginx的worker進程綁定到numa架構下的特定的CPU上,性能會大幅度提高。</font>學習
numactl:能夠實現把進程綁定到特定的CPU上
可是,當機器從新啓動後,綁定就失效了。如何解決呢,使用numad
numad:守護進程
numademo
$ taskset -p -c 0,1,2-4,5,9 1234
上面命令的意思:把進程ip爲1234的進程,綁定到0號,1號,2號,3號,4號,5號,9號CPU上。
這只是個例子,通常都綁定到1個CPU上,可是當系統重啓後,還須要從新綁定,由於pid也變了。
Nginx比較厲害,能夠配置哪一個worker綁定到哪一個CPU,事先寫到配置nginx的配置文件裏。
解決辦法:假設有6個CPU,系統啓動時,只讓2個CPU執行內核的指令,其他4個不讓執行內核指令。
編輯/etc/default/grub 文件,在 quiet splash 後面加上 isolcpus=2,3。回到終端執行update-grub 。其將自動依照剛纔編輯的配置文件(/etc/default/grub)生成爲引導程序準備的配置文件(/boot/grub/grub.cfg)
修改/proc/irq/<irq_num>/smp_affinity文件
$ echo cpu_mask > /proc/irq/<irq_num>/smp_affinity
cpu_mask:用比特位表示。
0001:表明1號CPU 0011:表明1號和2號CPU 0101:表明1號和3號CPU
sar -q 使用sar以前要配置一下 1,修改:/etc/default/sysstat, 將 ENABLED=「false「 改成ENABLED=「true「 2,執行:sudo /etc/init.d/sysstat restart top w uptime wmstat 1 5 下面的是查看CPU的使用率 mpstat 1 2 sar -P 1 2 iostat -c 1 cat /proc/stat
命令iostat -c 1的截圖,含義查看CPU的佔用率
ys:~$ iostat -c 1 Linux 4.15.0-20-generic (ys-VirtualBox) 2019年09月27日 _x86_64_ (1 CPU) avg-cpu: %user %nice %system %iowait %steal %idle 0.26 0.03 0.08 0.05 0.00 99.57 avg-cpu: %user %nice %system %iowait %steal %idle 0.00 0.00 0.00 0.00 0.00 100.00 avg-cpu: %user %nice %system %iowait %steal %idle 0.00 0.00 0.00 0.00 0.00 100.00 avg-cpu: %user %nice %system %iowait %steal %idle 0.00 0.00 0.00 0.00 0.00 100.00
命令【dstat】比較強大,能夠直觀的獲得以下信息:
--top-bio show most expensive block I/O process 顯示最消耗blockI/O的進程 --top-bio-adv show most expensive block I/O process (incl. pid and other stats) 顯示最消耗blockI/O的進程 --top-childwait show process waiting for child the most 顯示等待子進程時間最長的父進程 --top-cpu show most expensive CPU process 顯示最消耗CPU的進程 --top-cpu-adv show most expensive CPU process (incl. pid and other stats) 顯示最消耗CPU的進程 --top-cputime show process using the most CPU time (in ms) 顯示最消耗CPU時間片的進程 --top-cputime-avg show process with the highest average timeslice (in ms) 顯示最消耗CPU時間片的進程 --top-int show most frequent interrupt 顯示最常常發生的中斷信號 --top-io show most expensive I/O process 顯示最消耗I/O的進程 --top-io-adv show most expensive I/O process (incl. pid and other stats) 顯示最消耗I/O的進程 --top-latency show process with highest total latency (in ms) 顯示等待時間最長的進程 --top-latency-avg show process with the highest average latency (in ms) 顯示等待時間最長的進程 --top-mem show process using the most memory 顯示使用內存最多的進程
命令【sar -w 1(秒爲單位)】比較強大,能夠直觀的獲得進程在指定秒數裏的平均切換次數
<font color="green">
</font>