若是你的系统运行在多处理器芯片上,你可使用 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 范畴的应用处理将受到影响