換一個思路學Linux命令
關於top
命令的使用手冊及方法有很多,往往使用的時候我們只是看一眼,
不會就用man top
查一下或者是google
一下,這裡對top
的使用方法就不再贅述,
換一個方式,構造我們想要的top
結果
虛擬機器配置如下
No LSB modules are available. Distributor ID: Ubuntu Description:Ubuntu 16.04.5 LTS Release:16.04 Codename:xenial go version go1.6.2 linux/amd64 <!-- 檢視物理CPU個數 --> ➜~ cat /proc/cpuinfo| grep "physical id"| sort| uniq| wc -l 1 <!-- 檢視每個物理CPU中core的個數(即核數) --> ➜~ cat /proc/cpuinfo| grep "cpu cores"| uniq cpu cores: 1 <!-- 檢視邏輯CPU的個數 --> ➜~ cat /proc/cpuinfo| grep "processor"| wc -l 1
比如我們想構造一個比較耗CPU的場景,這裡用golang
程式作為示例
package main import ( "time" ) func main(){ for{ time.Sleep(1) } }
檢視top
結果
top - 22:01:26 up2:15,3 users,load average: 0.22, 0.25, 0.12 Tasks: 106 total,2 running, 104 sleeping,0 stopped,0 zombie %Cpu(s): 99.3 us,0.3 sy,0.0 ni,0.3 id,0.0 wa,0.0 hi,0.0 si,0.0 st KiB Mem :1015976 total,788612 free,46544 used,180820 buff/cache KiB Swap:1003516 total,971808 free,31708 used.815324 avail Mem PID USERPRNIVIRTRESSHR S %CPU %MEMTIME+ COMMAND 6176 ofs20083485980796 R 99.70.60:04.17 main 1 root2003790436722780 S0.00.40:01.61 systemd 2 root200000 S0.00.00:00.00 kthreadd 3 root200000 S0.00.00:05.12 ksoftirqd/0
這裡能看到golang
程式的PID也就是程序ID為:6176,CPU佔用率達99.7%之多,好可怕,這幸好是發生在虛擬機器裡面
這裡的load average
後面的三個數分別是1分鐘、5分鐘、15分鐘的CPU的負載情況。我們的配置是單CPU單核,如果這個值是1,
那CPU就是滿負荷運行了,我們這裡看到的結果是top
預設重新整理頻率是3s,load average
是一個統計值。
接下來我們構造耗記憶體的場景
package main import ( "math" ) func main(){ for{ go func(){ _ =make([]string, math.MaxInt32) }() } }
檢視top
結果
➜~ top op - 22:32:28 up2:46,3 users,load average: 0.38, 0.24, 0.19 Tasks: 107 total,3 running, 104 sleeping,0 stopped,0 zombie %Cpu(s):7.0 us, 66.9 sy,0.0 ni,0.0 id,2.3 wa,0.0 hi, 23.8 si,0.0 st KiB Mem :1015976 total,63904 free,925284 used,26788 buff/cache KiB Swap:1003516 total,820 free,1002696 used.13208 avail Mem PID USERPRNIVIRTRESSHR S %CPU %MEMTIME+ COMMAND 9882 ofs200 1861220 896372204 R 37.1 88.20:02.04 main 28 root200000 S 36.50.00:41.69 kswapd0 128 root0 -20000 S7.70.00:01.84 kworker/0:1H
記憶體只有1G,golang
程式使用了88.2%之多,必須要考慮GC
優化了
再來構造一個比較耗磁碟IO的場景,用openresty
小試一把(參考ofollow,noindex">openresty示例
)
worker_processes 1; error_log logs/error.log; events { worker_connections 2014; } http { server { listen 8080; location /{ default_type text/html; content_by_lua ' ngx.say("<p>hello, world</p>") '; } } }
在宿主機發起併發測試
siege -T -c 500000 r10 'http://127.0.0.1:18080/'
這裡在虛擬機器的8080埠對映到宿主機的18080埠
使用iotop
命令檢視磁碟IO狀況
sudo iotop Total DISK READ :0.00 B/s | Total DISK WRITE :80.24 K/s Actual DISK READ:0.00 B/s | Actual DISK WRITE:0.00 B/s TIDPRIOUSERDISK READDISK WRITESWAPINIO>COMMAND 28078 be/4 ofs0.00 B/s80.24 K/s0.00 %0.00 % nginx: worker process 1 be/4 root0.00 B/s0.00 B/s0.00 %0.00 % init 2 be/4 root0.00 B/s0.00 B/s0.00 %0.00 % [kthreadd] 3 be/4 root0.00 B/s0.00 B/s0.00 %0.00 % [ksoftirqd/0] 5 be/0 root0.00 B/s0.00 B/s0.00 %0.00 % [kworker/0:0H] 7 be/4 root0.00 B/s0.00 B/s0.00 %0.00 % [rcu_sched] 8 be/4 root0.00 B/s0.00 B/s0.00 %0.00 % [rcu_bh] 9 rt/4 root0.00 B/s0.00 B/s0.00 %0.00 % [migration/0] 10 rt/4 root0.00 B/s0.00 B/s0.00 %0.00 % [watchdog/0] 11 be/4 root0.00 B/s0.00 B/s0.00 %0.00 % [kdevtmpfs] 12 be/0 root0.00 B/s0.00 B/s0.00 %0.00 % [netns]
能看到磁碟寫入速度為: 80.24 K/s,可以看到這裡主要是nginx
程式戰勝了磁碟IO,原因是因為nginx寫入access.log的需要。