linux mpstat 命令使用详解

mpstat是 Multiprocessor Statistics的缩写,是实时系统监控工具。其报告与CPU的一些统计信息,这些信息存放在/proc/stat文件中。在多CPU系统里,其不但能查看所有CPU的平均状况信息,而且能够查看特定CPU的信息。

目录

1 mpstat命令

1.1 命令格式

2 使用

2.1 每隔1s输出所有cpu的使用情况

2.2 查看软中断统计信息

2.3 查看软中断总数统计


1 mpstat命令

1.1 命令格式

mpstat [ -A ] [ -u ] [ -V ] [ -I { SUM | CPU | SCPU | ALL } ] [ -P { cpu [,...] | ON | ALL } ] [ interval [ count ] ]
OPTIONS:
-A: 等同于-u -I ALL -P ALL
-I:可以指定SUM CPU SCPU ALL四个参数,SUM表示每个处理器的中断总数,CPU表示每个核的每秒中断数量, SCPU表示每个核每秒的软中断数量,内核版本在2.6.31之后才支持。
-P: 统计的CPU编号,一般用ALL
-u: 输出列的信息
-V: 查看工具版本号

2 使用

2.1 每隔1s输出所有cpu的使用情况

$ mpstat -P ALL 1
Linux 3.10.0-693.el7.x86_64 (zhanghao)   2019年02月26日   _x86_64_  (2 CPU)

12时42分03秒 CPU  %usr  %nice  %sys %iowait  %irq  %soft %steal %guest %gnice  %idle
12时42分04秒 all  0.00  0.00  0.00  0.00  0.00  0.00  0.00  0.00  0.00 100.00
12时42分04秒  0  0.00  0.00  0.00  0.00  0.00  0.00  0.00  0.00  0.00 100.00
12时42分04秒  1  0.00  0.00  0.00  0.00  0.00  0.00  0.00  0.00  0.00 100.00

12时42分04秒 CPU  %usr  %nice  %sys %iowait  %irq  %soft %steal %guest %gnice  %idle
12时42分05秒 all  0.00  0.00  0.00  0.00  0.00  0.00  0.00  0.00  0.00 100.00
12时42分05秒  0  0.00  0.00  0.00  0.00  0.00  0.00  0.00  0.00  0.00 100.00
12时42分05秒  1  0.00  0.00  0.00  0.00  0.00  0.00  0.00  0.00  0.00 100.00

12时42分05秒 CPU  %usr  %nice  %sys %iowait  %irq  %soft %steal %guest %gnice  %idle
12时42分06秒 all  0.00  0.00  0.00  0.50  0.00  0.00  0.00  0.00  0.00  99.50
12时42分06秒  0  0.00  0.00  0.00  0.00  0.00  0.00  0.00  0.00  0.00 100.00
12时42分06秒  1  0.00  0.00  0.00  0.00  0.00  0.00  0.00  0.00  0.00 100.00

12时42分06秒 CPU  %usr  %nice  %sys %iowait  %irq  %soft %steal %guest %gnice  %idle
12时42分07秒 all  0.00  0.00  0.00  0.00  0.00  0.00  0.00  0.00  0.00 100.00
12时42分07秒  0  0.00  0.00  0.00  0.00  0.00  0.00  0.00  0.00  0.00 100.00
12时42分07秒  1  0.00  0.00  0.00  0.00  0.00  0.00  0.00  0.00  0.00 100.00

12时42分07秒 CPU  %usr  %nice  %sys %iowait  %irq  %soft %steal %guest %gnice  %idle
12时42分08秒 all  0.00  0.00  0.00  0.00  0.00  0.00  0.00  0.00  0.00 100.00
12时42分08秒  0  0.00  0.00  0.00  0.00  0.00  0.00  0.00  0.00  0.00 100.00
12时42分08秒  1  0.00  0.00  0.00  0.00  0.00  0.00  0.00  0.00  0.00 100.00

输出参数含义:

当没有参数时,mpstat则显示系统启动以后所有信息的平均值。有interval时,第一行的信息自系统启动以来的平均信息。从第二行开始,输出为前一个interval时间段的平均信息。

输出各参数含义:

参数释义从/proc/stat获得数据
CPU处理器ID 
%usr在internal时间段里,用户态的CPU时间(%),不包含 nice值为负进程usr/total*100
%nice在internal时间段里,nice值为负进程的CPU时间(%)nice/total*100
%sys在internal时间段里,核心时间(%)system/total*100
%iowait在internal时间段里,硬盘IO等待时间(%)iowait/total*100
%irq在internal时间段里,硬中断时间(%)irq/total*100
%soft在internal时间段里,软中断时间(%)softirq/total*100
%steal显示虚拟机管理器在服务另一个虚拟处理器时虚拟CPU处在非自愿等待下花费时间的百分比steal/total*100
%guest显示运行虚拟处理器时CPU花费时间的百分比guest/total*100
%gnice gnice/total*100
%idle在internal时间段里,CPU除去等待磁盘IO操作外的因为任何原因而空闲的时间闲置时间(%)idle/total*100

CPU总的工作时间:

total_cur = user + system + nice + idle + iowait + irq + softirq

total_pre = pre_user + pre_system + pre_nice + pre_idle + pre_iowait + pre_irq + pre_softirq

user = user_cur – user_pre

total = total_cur - total_pre

其中_cur 表示当前值,_pre表示interval时间前的值。上表中的所有值可取到两位小数点。

Note:
1.vmstat和mpstat 命令的差别:mpstat 可以显示每个处理器的统计,而 vmstat 显示所有处理器的统计。因此,编写糟糕的应用程序(不使用多线程体系结构)可能会运行在一个多处理器机器上,而不使用所有处理器。从而导致一个 CPU 过载,而其他 CPU 却很空闲。通过 mpstat 可以轻松诊断这些类型的问题。

2.vmstat中所有关于CPU的总结都适合mpstat。当您看到较低的 %idle 数字时,您知道出现了 CPU 不足的问题。当您看到较高的 %iowait 数字时,您知道在当前负载下 I/O 子系统出现了某些问题。

2.2 查看软中断统计信息

[root@localhost ~]# mpstat -I SCPU -P 1 3
Linux 3.10.0-693.21.1.el7.x86_64 (xxx.xxxx)        2020年11月04日  _x86_64_        (8 CPU)

06时58分47秒  CPU       HI/s    TIMER/s   NET_TX/s   NET_RX/s    BLOCK/s BLOCK_IOPOLL/s  TASKLET/s    SCHED/s  HRTIMER/s      RCU/s
06时58分50秒    1       0.00      66.33       0.00      16.33       0.00       0.00       0.00      28.33       0.00      14.33
06时58分53秒    1       0.00      72.67       0.00      16.67       0.00       0.00       0.00      24.00       0.00      16.67
06时58分56秒    1       0.00      62.67       0.00      14.00       0.00       0.00       0.00      18.00       0.00      19.67
06时58分59秒    1       0.00      70.33       0.00      18.33       0.00       0.00       0.00      17.33       0.00      34.67
06时59分02秒    1       0.00      64.33       0.00      13.33       0.00       0.00       0.00      23.67       0.00      27.67
06时59分05秒    1       0.00      69.33       0.00      14.67       0.00       0.00       0.00      18.33       0.00      32.33
06时59分08秒    1       0.00      66.00       0.00      15.67       0.00       0.00       0.00      12.33       0.00      27.00
^C
平均时间:    1       0.00      67.38       0.00      15.57       0.00       0.00       0.00      20.29       0.00      24.62
[root@localhost ~]#

2.3 查看软中断总数统计

[root@localhost ~]# mpstat -I SUM -P ALL 2
Linux 3.10.0-693.21.1.el7.x86_64 (localhost.localdomain)        2020年11月04日  _x86_64_        (8 CPU)

07时02分07秒  CPU    intr/s
07时02分09秒  all  16611.50
07时02分09秒    0    352.50
07时02分09秒    1   3479.50
07时02分09秒    2   2275.50
07时02分09秒    3   2652.50
07时02分09秒    4   2305.50
07时02分09秒    5   2335.50
07时02分09秒    6   2214.50
07时02分09秒    7   1000.50

07时02分09秒  CPU    intr/s
07时02分11秒  all  16494.00
07时02分11秒    0    328.50
07时02分11秒    1   3556.50
07时02分11秒    2   2289.50
07时02分11秒    3   2577.50
07时02分11秒    4   2304.00
07时02分11秒    5   2219.00
07时02分11秒    6   2217.50
07时02分11秒    7   1000.00

07时02分11秒  CPU    intr/s
07时02分13秒  all  16906.50
07时02分13秒    0    332.00
07时02分13秒    1   3921.50
07时02分13秒    2   2288.00
07时02分13秒    3   2593.00
07时02分13秒    4   2297.50
07时02分13秒    5   2213.50
07时02分13秒    6   2257.00
07时02分13秒    7   1000.00
^C

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值