1. 程式人生 > >Linux 程序管理 vmstat、top、pstree命令

Linux 程序管理 vmstat、top、pstree命令

vmstat命令:監控系統資源

vmstat 是 Linux 中的一個綜合性能分析工具,可以用來監控 CPU 使用、程序狀態、記憶體使用、虛擬記憶體使用、磁碟輸入/輸出狀態等資訊。vmstat 命令格式如下:
[[email protected] ~]# vmstat [重新整理延時 重新整理次數]

使用vmstat檢測,每隔1秒重新整理一次,共重新整理3次

[[email protected] ~]# vmstat 1 3
procs -----------memory---------- ---swap-- -----io---- -system-- ------cpu-----
 r  b   swpd   
free buff cache si so bi bo in cs us sy id wa st 3 0 6780 93228 40080 1174336 0 0 170 3 42 72 0 1 99 0 0 0 0 6780 93212 40088 1174332 0 0 0 12 34 61 0 0 100 0 0 0 0 6780 93212 40088 1174336 0 0 0 0 31 48 0 0 100 0 0

1) procs:程序資訊欄位:

  • -r:等待執行的程序數,數量越大,系統越繁忙。
  • -b:不可被喚醒的程序數量,數量越大,系統越繁忙。
2) memory:記憶體資訊欄位:
  • -swpd:虛擬記憶體的使用情況,單位為KB。
  • -free:空閒的記憶體容量,單位為KB。
  • -buff:緩衝的記憶體容量,單位為KB。
  • -cache:快取的記憶體容量,單位為KB。
3) swap:交換分割槽資訊欄位:
  • -si:從磁碟中交換到記憶體中資料的數量,單位為KB。
  • -so:從記憶體中交換到磁碟中資料的數量,單位為KB。
這兩個數越大,表明資料需要經常在磁碟和記憶體之間進行交換,系統性能越差。
4) io:磁碟讀/寫資訊欄位:
  • -bi:從塊裝置中讀入的資料的總量,單位是塊。
  • -bo:寫到塊裝置的資料的總量,單位是塊。
這兩個數越大,代表系統的 I/O 越繁忙。
5) system:系統資訊欄位:
  • -in:每秒被中斷的程序次數。
  • -cs:每秒進行的事件切換次數。
這兩個數越大,代表系統與介面裝置的通訊越繁忙。

6) cpu:CPU資訊欄位:
  • -us:非核心程序消耗CPU運算時間的百分比。
  • -sy:核心程序消耗CPU運算時間的百分比。
  • -id:空閒CPU的百分比。
  • -wa:等待I/O所消耗的CPU百分比。
  • -st:被虛擬機器所盜用的CPU百分比。
如果伺服器上的資源佔用率比較高,那麼使用 vmstat 命令檢視到的引數值就會比較大。我們就需要手工進行干預,如果是非正常程序佔用了系統資源,則需要判斷這些程序是如何產生的;如果是正常程序佔用了系統資源,則說明伺服器需要升級了。

dmesg命令:顯示開機時的核心檢測資訊

在系統啟動過程中,核心還需要進行一次系統檢測,這些核心檢測資訊會被記錄在記憶體當中。使用dmesg 命令就可以檢視這些核心檢測資訊。我們一般利用這個命令檢視系統的硬體資訊。dmesg 命令格式如下:

[[email protected] ~]# dmesg

檢視CPU的資訊

[[email protected] ~]# dmesg | grep CPU
[    0.000000] smpboot: Allowing 128 CPUs, 127 hotplug CPUs
[    0.000000] setup_percpu: NR_CPUS:5120 nr_cpumask_bits:128 nr_cpu_ids:128 nr_node_ids:1
[    0.000000] PERCPU: Embedded 31 pages/cpu @ffff88007c600000 s87168 r8192 d31616 u131072
[    0.000000] SLUB: HWalign=64, Order=0-3, MinObjects=0, CPUs=128, Nodes=1
[    0.000000]     RCU restricting CPUs from NR_CPUS=5120 to nr_cpu_ids=128.
[    0.000000]     Offload RCU callbacks from all CPUs
[    0.000000]     Offload RCU callbacks from CPUs: 0-127.
[    0.021791] CPU: Physical Processor ID: 0
[    0.022946] mce: CPU supports 8 MCE banks
[    0.091101] smpboot: CPU0: Intel(R) Core(TM) i5-6200U CPU @ 2.30GHz (fam: 06, model: 4e, stepping: 03)
[    0.091198] perf_event_intel: CPUID marked event: 'cpu cycles' unavailable
[    0.091199] perf_event_intel: CPUID marked event: 'instructions' unavailable
[    0.091200] perf_event_intel: CPUID marked event: 'bus cycles' unavailable
[    0.091200] perf_event_intel: CPUID marked event: 'cache references' unavailable
[    0.091201] perf_event_intel: CPUID marked event: 'cache misses' unavailable
[    0.091201] perf_event_intel: CPUID marked event: 'branch instructions' unavailable
[    0.091202] perf_event_intel: CPUID marked event: 'branch misses' unavailable
[    0.137337] Brought up 1 CPUs
[    1.383566] microcode: CPU0 sig=0x406e3, pf=0x1, revision=0xc6

檢視第一塊網絡卡的資訊

[[email protected] ~]# dmesg | grep eth0
[    2.521339] e1000 0000:02:01.0 eth0: (PCI:66MHz:32-bit) 00:0c:29:19:e9:cb
[    2.521344] e1000 0000:02:01.0 eth0: Intel(R) PRO/1000 Network Connection

free命令:檢視記憶體使用狀態

free 命令可以檢視系統記憶體和 swap 交換分割槽的使用情況,其輸出和 top 命令的記憶體部分非常相似。free 命令格式如下:

[[email protected] ~]# free [選項]

選項:

  • -b:以位元組為單位顯示;
  • -k:以 KB 為單位顯示,預設顯示;
  • -m:以 MB 為單位顯示;
  • -g:以 GB 為單位顯示;

檢視記憶體使用狀態

[[email protected] ~]# free
              total        used        free      shared  buff/cache   available
Mem:        1868660      561096       93000        5148     1214564     1114304
Swap:       2097148        6776     2090372
  •  第一行:total 是總記憶體數,used 是已經使用的記憶體數,free 是空閒的記憶體數,shared 是多個程序共享的記憶體總數,buffers 是緩衝記憶體數,cached 是快取記憶體數。預設單位是 KB。
  • 第二行:-/buffers/cache 的記憶體數,相當於第一行的 used-buffers-cached。+/buffers/cache 的記憶體數,相當於第一行的 free+buffers+cached。
  • 第三行:total 是 swap 交換分割槽的總數;used 是已經使用的 swap 交換分割槽數,free 是空閒的 swap 交換分割槽數。預設單位是 KB。

Linux檢視CPU資訊方法

CPU 的主要資訊儲存在 /proc/cpuinfo 這個檔案中,我們只要檢視這個檔案,就可以知道 CPU 的相關資訊。

[[email protected] ~]# cat /proc/cpuinfo
processor    : 0             #邏輯CPU編號
vendor_id    : GenuineIntel  #CPU製造廠商
cpu family   : 6             #產品的系列代號
model        : 78            #CPU系列代號
model name   : Intel(R) Core(TM) i5-6200U CPU @ 2.30GHz #CPU系列的名字、編號、主頻
stepping     : 3   #更新版本
microcode    : 0xc6  
cpu MHz         : 2400.005   #實際主頻
cache size      : 3072 KB    #二級快取
physical id     : 0
siblings        : 1
core id         : 0
cpu cores       : 1
apicid          : 0
initial apicid  : 0
fpu             : yes
fpu_exception   : yes
cpuid level     : 22
wp           : yes
#CPU的功能標識
flags        : fpu vme de pse tsc msr pae mce cx8 apic sep mtrr pge mca cmov pat pse36 clflush mmx fxsr sse sse2 ss syscall nx pdpe1gb rdtscp lm constant_tsc arch_perfmon nopl xtopology tsc_reliable nonstop_tsc eagerfpu pni pclmulqdq ssse3 fma cx16 pcid sse4_1 sse4_2 x2apic movbe popcnt tsc_deadline_timer aes xsave avx f16c rdrand hypervisor lahf_lm abm 3dnowprefetch arat fsgsbase tsc_adjust bmi1 avx2 smep bmi2 invpcid mpx rdseed adx smap clflushopt xsaveopt xsavec xsaves
bogomips     : 4800.01
clflush size    : 64
cache_alignment : 64
address sizes   : 43 bits physical, 48 bits virtual
power management: