對于一個 Linux 服務(wù)器來說,cpu 的主頻以及占用率,內(nèi)存的大小以及占用率,磁盤 I/O 速率和網(wǎng)卡 I/O 速率極大地影響著服務(wù)器的性能。在 Linux 系統(tǒng)下,開發(fā)者提供了/proc 文件系統(tǒng)來提供系統(tǒng)相關(guān)的進程信息
1.查看 cpu 主頻和占用率
Linux 下 CPU 的主頻有兩種獲取方式,一種是通過讀取 /proc/cpuinfo 文件,另外一種是利用 CPU 主頻的計算原理編寫程序獲取。此處只介紹前一種方式。即使用命令 cat /proc/cpuinfo 。從下方圖片可以看到,本人的電腦是雙核 CPU,主頻都為 2.60GHz。
Lance# cat /proc/cpuinfo processor : 0 vendor_id : GenuineIntel cpu family : 6 model : 23 model name : Pentium(R) Dual-Core CPU E5300 @ 2.60GHz stepping : 10 microcode : 0xa0b cpu MHz : 1200.000 cache size : 2048 KB physical id : 0 siblings : 2 core id : 0 cpu cores : 2 apicid : 0 initial apicid : 0 fpu : yes fpu_exception : yes cpuid level : 13 wp : yes flags : fpu vme de pse tsc msr pae mce cx8 apic sep mtrr pge mca cmov pat pse36 clflush dts acpi mmx fxsr sse sse2 ss ht tm pbe syscall nx lm constant_tsc arch_perfmon pebs bts rep_good nopl cpuid aperfmperf pni dtes64 monitor ds_cpl vmx est tm2 ssse3 cx16 xtpr pdcm xsave lahf_lm pti tpr_shadow vnmi flexpriority dtherm bugs : cpu_meltdown spectre_v1 spectre_v2 bogomips : 5186.86 clflush size : 64 cache_alignment : 64 address sizes : 36 bits physical, 48 bits virtual power management: processor : 1 vendor_id : GenuineIntel cpu family : 6 model : 23 model name : Pentium(R) Dual-Core CPU E5300 @ 2.60GHz stepping : 10 microcode : 0xa0b cpu MHz : 1200.000 cache size : 2048 KB physical id : 0 siblings : 2 core id : 1 cpu cores : 2 apicid : 1 initial apicid : 1 fpu : yes fpu_exception : yes cpuid level : 13 wp : yes flags : fpu vme de pse tsc msr pae mce cx8 apic sep mtrr pge mca cmov pat pse36 clflush dts acpi mmx fxsr sse sse2 ss ht tm pbe syscall nx lm constant_tsc arch_perfmon pebs bts rep_good nopl cpuid aperfmperf pni dtes64 monitor ds_cpl vmx est tm2 ssse3 cx16 xtpr pdcm xsave lahf_lm pti tpr_shadow vnmi flexpriority dtherm bugs : cpu_meltdown spectre_v1 spectre_v2 bogomips : 5186.86 clflush size : 64 cache_alignment : 64 address sizes : 36 bits physical, 48 bits virtual power management:
Linux 下 CPU 的使用狀態(tài)可分為用戶態(tài)、系統(tǒng)態(tài)以及空閑態(tài)。CPU 占用率可以用如下公式計算
CPU占用率 = CPU執(zhí)行非系統(tǒng)空閑進程時間/CPU執(zhí)行總時間
/proc/stat 文件對 CPU 的使用情況進行了實時的記錄,如下圖所示:
Lance# cat /proc/stat cpu 525228 16439 390034 139932703 224352 0 2010 0 0 0 cpu0 265224 6822 197250 70017169 60063 0 74 0 0 0 cpu1 260004 9617 192784 69915534 164289 0 1936 0 0 0 intr 126884204 43 2 0 0 0 0 0 1 0 3 0 0 4 0 0 0 124 0 2 1404563 0 0 0 0 955106 514 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 ctxt 193899569 btime 1536131324 processes 268172 procs_running 1 procs_blocked 0 softirq 101783843 10 36600014 116255 1002291 1058479 0 263 35993177 0 27013354
其中首行的 10 個參數(shù)含義如下:
user nice system idle iowait irq softirq steal guest guest_nice
user:用戶態(tài)的CPU時間
nice:低優(yōu)先級程序所占用的用戶態(tài)的cpu時間。
system:系統(tǒng)態(tài)的CPU時間
idle:CPU空閑的時間
iowait:等待IO響應(yīng)的時間
irq:處理硬件中斷的時間
softirq:處理軟中斷的時間
steal: 處理其他所花的時間
guest:運行時間為客戶操作系統(tǒng)下的虛擬CPU控制
guest_nice:訪客的低優(yōu)先級程序所占用的用戶態(tài)的cpu時間
余下的參數(shù)中:
intr:第一個參數(shù)表示的是自系統(tǒng)啟動以來,發(fā)生的所有中斷的次數(shù);其他的數(shù)對應(yīng)一個特定中斷發(fā)生的次數(shù)
ctxt: cpu自系統(tǒng)啟動以來發(fā)生的上下文交換次數(shù)
btime: 系統(tǒng)啟動到現(xiàn)在的時間 UTC 秒
processes: 表示系統(tǒng)啟動以來創(chuàng)建的進程個數(shù)
procs_running: 當(dāng)前運行進程的個數(shù)
procs_blocked: 當(dāng)前被阻塞的進程個數(shù)
softirq: 顯示軟中斷情況
要計算 CPU 在某段時間內(nèi)的占用率,可以分別記錄下 t1 和 t2 時刻的 CPU 總的狀態(tài)和空閑狀態(tài),因此:
CPU占用率 = 1 – ( idle2 – idle1 ) / (total2 – total1)
其中 total 表示 10 個參數(shù)之和。
2. 查看內(nèi)存大小及占用率
/proc/meminfo 記錄了內(nèi)存信息。
Lance# cat /proc/meminfo MemTotal: 4003752 kB MemFree: 517464 kB MemAvailable: 3144816 kB Buffers: 257152 kB Cached: 2289308 kB SwapCached: 0 kB Active: 1455916 kB Inactive: 1487740 kB Active(anon): 277376 kB Inactive(anon): 174624 kB Active(file): 1178540 kB Inactive(file): 1313116 kB Unevictable: 0 kB Mlocked: 0 kB SwapTotal: 6253564 kB SwapFree: 6253564 kB Dirty: 8 kB Writeback: 0 kB AnonPages: 395168 kB Mapped: 207872 kB Shmem: 54808 kB Slab: 460364 kB SReclaimable: 425200 kB SUnreclaim: 35164 kB KernelStack: 5376 kB PageTables: 22064 kB NFS_Unstable: 0 kB Bounce: 0 kB WritebackTmp: 0 kB CommitLimit: 8255440 kB Committed_AS: 2553704 kB VmallocTotal: 34359738367 kB VmallocUsed: 0 kB VmallocChunk: 0 kB HardwareCorrupted: 0 kB AnonHugePages: 2048 kB ShmemHugePages: 0 kB ShmemPmdMapped: 0 kB CmaTotal: 0 kB CmaFree: 0 kB HugePages_Total: 0 HugePages_Free: 0 HugePages_Rsvd: 0 HugePages_Surp: 0 Hugepagesize: 2048 kB DirectMap4k: 113216 kB DirectMap2M: 4044800 kB
其中:
MemTotal:表示系統(tǒng)可支配內(nèi)存
MemAvailable:表示應(yīng)用程序可用的內(nèi)存
因此,內(nèi)存占用率可以用如下公式計算:
3.查看磁盤 I/O占用率
安裝 sysstat 工具,它的子工具 iostat 可以顯示 I/O 占用率。%util 參數(shù)表示 1s內(nèi)用于 I/O 請求所用時間的比例,%util 值越高,I/O 占用率越高,可直接使用這個參數(shù)來代表 I/O 占用率
Lance# iostat -x Linux 4.13.0-32-generic (Lance) 2018年09月13日 _x86_64_ (2 CPU) avg-cpu: %user %nice %system %iowait %steal %idle 0.37 0.01 0.28 0.16 0.00 99.17 Device r/s w/s rkB/s wkB/s rrqm/s wrqm/s %rrqm %wrqm r_await w_await aqu-sz rareq-sz wareq-sz svctm %util loop0 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 21.41 0.00 0.00 2.44 0.00 20.89 0.00 loop1 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 44.62 0.00 0.00 1.69 0.00 11.38 0.00 loop2 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 19.93 0.00 0.00 9.65 0.00 12.31 0.00 loop3 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 41.81 0.00 0.00 14.23 0.00 18.88 0.00 loop4 0.01 0.00 0.01 0.00 0.00 0.00 0.00 0.00 6.61 0.00 0.00 1.12 0.00 0.50 0.00 loop5 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 1.60 0.00 0.00 0.00 sda 0.24 0.52 3.45 14.18 0.17 0.56 41.05 51.90 3.56 31.99 0.02 14.40 27.51 4.51 0.34
4. 查看網(wǎng)絡(luò)帶寬使用率
/proc/net/dev 文件中記錄了網(wǎng)絡(luò)相關(guān)的使用情況,其中分別記錄了發(fā)送和接收的數(shù)據(jù)包總數(shù)、收發(fā)的錯誤包數(shù)以及收發(fā)的丟失包數(shù)等。取單位時間內(nèi)發(fā)送和接 收 的 數(shù) 據(jù) 包 總 包 數(shù) (totalPackets ), 除 以 網(wǎng) 絡(luò) 的 基 本 帶 寬 ( bandWidth ),
pnet = 100%*totalPackets/bandWidth
Lance# cat /proc/net/dev Inter-| Receive | Transmit face |bytes packets errs drop fifo frame compressed multicast|bytes packets errs drop fifo colls carrier compressed lo: 2729345 46942 0 0 0 0 0 0 2729345 46942 0 0 0 0 0 0 enp2s0: 558026941 723202 0 11733 0 0 0 105753 23827630 275738 0 0 0 0 0 0
bytes: 接口發(fā)送或接收的總字節(jié)數(shù)
packets: 接口發(fā)送或接收的總數(shù)據(jù)包數(shù)
errs: 有設(shè)備驅(qū)動程序檢測到的發(fā)送或者接收錯誤的總數(shù)
drop: 設(shè)備驅(qū)動程序丟棄的數(shù)據(jù)包總數(shù)
fifo: FIFO 緩沖區(qū)錯誤的數(shù)量
frame: 分組幀錯誤的數(shù)量
colls: 接口上檢測到的沖突數(shù)
compressed: 設(shè)備驅(qū)動程序發(fā)送或接收的壓縮數(shù)據(jù)包數(shù)
carrier: 由設(shè)備驅(qū)動程序檢測到的載波損耗的數(shù)量
multicast: 設(shè)備驅(qū)動程序發(fā)送或接收的多播幀數(shù)