1.mpstat 工具的使用
如果你的系统运行在多处理器芯片上,你可以使用 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
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 空闲时间的百分比 |
3. 案例学习: 未充分使用的处理量
在这个例子中,为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
4. 结论
监控 CPU 性能由以下几个部分组成:
-
检查system的运行队列,以及确定不要超出每个处理器3个可运行状态线程的限制.
-
确定CPU 利用率中user/system比例维持在70/30
-
当CPU 开销更多的时间在system mode,那就说明已经超负荷并且应该尝试重新调度优先级
-
当I/O 处理得到增长,CPU 范畴的应用处理将受到影响
转载于:https://blog.51cto.com/liubao0312/1031615