若是你的系統運行在多處理器芯片上,你可使用 mpstat 命令來監控每一個獨立的芯片.Linux 內核視雙核處理器爲2 CPU’s,所以一個雙核處理器的雙內核就報告有4 CPU’s 可用.
mpstat 命令給出的CPU 利用率統計值大體和 vmstat 一致,可是 mpstat 能夠給出基於單個處理器的統計值.
# mpstat –P ALL 1
Linux 2.4.21-20.ELsmp (localhost.localdomain) 05/23/2006
05:17:31 PM CPU %user %nice %system %idle intr/s
05:17:32 PM all 0.00 0.00 3.19 96.53 13.27
05:17:32 PM 0 0.00 0.00 0.00 100.00 0.00
05:17:32 PM 1 1.12 0.00 12.73 86.15 13.27
05:17:32 PM 2 0.00 0.00 0.00 100.00 0.00
05:17:32 PM 3 0.00 0.00 0.00 100.00 0.00
2. vmstat 工具的使用
vmstat 工具提供了一種低開銷的系統性能觀察方式.由於 vmstat 自己就是低開銷工具,在很是高負荷的服務器上,你須要查看並監控系統的健康狀況,在控制窗口仍是可以使用vmstat 輸出結果.這個工具運行在2種模式下:average 和 sample 模式.sample 模式經過指定間隔時間測量狀態值.這個模式對於理解在持續負荷下的性能表現,頗有幫助.下面就是
vmstat 運行1秒間隔的示例:
# vmstat 1
procs ———–memory———- —swap– —–io—- –system– —-cpu—-
r b swpd free buff cache si so bi bo in cs us sy id wa
0 0 104300 16800 95328 72200 0 0 5 26 7 14 4 1 95 0
0 0 104300 16800 95328 72200 0 0 0 24 1021 64 1 1 98 0
0 0 104300 16800 95328 72200 0 0 0 0 1009 59 1 1 98 0
表 2.1. The vmstat CPU statistics
Field |
Description |
r |
The amount of threads in the run queue. These are threads that are runnable, but the CPU is not available to execute them. 當前運行隊列中線程的數目.表明線程處於可運行狀態,但CPU 還未能執行. |
b |
This is the number of processes blocked and waiting on IO requests to finish. 當前進程阻塞並等待IO 請求完成的數目 |
in |
This is the number of interrupts being processed. 當前中斷被處理的數目 |
cs |
This is the number of context switches currently happening on the system. 當前kernel system中,發生上下文切換的數目 |
us |
This is the percentage of user CPU utilization. CPU 利用率的百分比 |
sys |
This is the percentage of kernel and interrupts utilization. 內核和中斷利用率的百分比 |
wa |
This is the percentage of idle processor time due to the fact that ALL runnable threads are blocked waiting on IO. 全部可運行狀態線程被阻塞在等待IO 請求的百分比 |
id |
This is the percentage of time that the CPU is completely idle. CPU 空閒時間的百分比 |
在這個例子中,爲4 CPU核心可用.其中2個CPU 主要處理進程運行(CPU 0 和1).第3個核心處理全部內核和其餘系統功能(CPU 3).第4個核心處於idle(CPU 2).
使用 top 命令能夠看到有3個進程差很少徹底佔用了整個CPU 核心.
# top -d 1
top - 23:08:53 up 8:34, 3 users, load average: 0.91, 0.37, 0.13
Tasks: 190 total, 4 running, 186 sleeping, 0 stopped, 0 zombie
Cpu(s): 75.2% us, 0.2% sy, 0.0% ni, 24.5% id, 0.0% wa, 0.0% hi, 0.0%
si
Mem: 2074736k total, 448684k used, 1626052k free, 73756k buffers
Swap: 4192956k total, 0k used, 4192956k free, 259044k cached
PID USER PR NI VIRT RES SHR S %CPU %MEM TIME+ COMMAND
15957 nobody 25 0 2776 280 224 R 100 20.5 0:25.48 php
15959 mysql 25 0 2256 280 224 R 100 38.2 0:17.78 mysqld
15960 apache 25 0 2416 280 224 R 100 15.7 0:11.20 httpd
15901 root 16 0 2780 1092 800 R 1 0.1 0:01.59 top
1 root 16 0 1780 660 572 S 0 0.0 0:00.64 init
# mpstat –P ALL 1
Linux 2.4.21-20.ELsmp (localhost.localdomain) 05/23/2006
05:17:31 PM CPU %user %nice %system %idle intr/s
05:17:32 PM all 81.52 0.00 18.48 21.17 130.58
05:17:32 PM 0 83.67 0.00 17.35 0.00 115.31
05:17:32 PM 1 80.61 0.00 19.39 0.00 13.27
05:17:32 PM 2 0.00 0.00 16.33 84.66 2.01
05:17:32 PM 3 79.59 0.00 21.43 0.00 0.00
05:17:32 PM CPU %user %nice %system %idle intr/s
05:17:33 PM all 85.86 0.00 14.14 25.00 116.49
05:17:33 PM 0 88.66 0.00 12.37 0.00 116.49
05:17:33 PM 1 80.41 0.00 19.59 0.00 0.00
05:17:33 PM 2 0.00 0.00 0.00 100.00 0.00
05:17:33 PM 3 83.51 0.00 16.49 0.00 0.00
05:17:33 PM CPU %user %nice %system %idle intr/s
05:17:34 PM all 82.74 0.00 17.26 25.00 115.31
05:17:34 PM 0 85.71 0.00 13.27 0.00 115.31
05:17:34 PM 1 78.57 0.00 21.43 0.00 0.00
05:17:34 PM 2 0.00 0.00 0.00 100.00 0.00
05:17:34 PM 3 92.86 0.00 9.18 0.00 0.00
05:17:34 PM CPU %user %nice %system %idle intr/s
05:17:35 PM all 87.50 0.00 12.50 25.00 115.31
05:17:35 PM 0 91.84 0.00 8.16 0.00 114.29
05:17:35 PM 1 90.82 0.00 10.20 0.00 1.02
05:17:35 PM 2 0.00 0.00 0.00 100.00 0.00
05:17:35 PM 3 81.63 0.00 15.31 0.00 0.00
你也可使用 ps 命令經過查看 PSR 這列,檢查哪一個進程在佔用了哪一個CPU.
# while :; do ps -eo pid,ni,pri,pcpu,psr,comm | grep ‘mysqld’; sleep 1;
done
PID NI PRI %CPU PSR COMMAND
15775 0 15 86.0 3 mysqld
PID NI PRI %CPU PSR COMMAND
15775 0 14 94.0 3 mysqld
PID NI PRI %CPU PSR COMMAND
15775 0 14 96.6 3 mysqld
PID NI PRI %CPU PSR COMMAND
15775 0 14 98.0 3 mysqld
PID NI PRI %CPU PSR COMMAND
15775 0 14 98.8 3 mysqld
PID NI PRI %CPU PSR COMMAND
15775 0 14 99.3 3 mysqld
監控 CPU 性能由如下幾個部分組成:
-
檢查system的運行隊列,以及肯定不要超出每一個處理器3個可運行狀態線程的限制.
-
肯定CPU 利用率中user/system比例維持在70/30
-
當CPU 開銷更多的時間在system mode,那就說明已經超負荷而且應該嘗試從新調度優先級
-
當I/O 處理獲得增加,CPU 範疇的應用處理將受到影響