Linux运维05:mpstat命令详解

 系列专题:Linux运维入门教程 


       mpstat是Multiprocessor Statistics的缩写,是CPU实时监控工具。它会显示CPU的一些统计信息,这些信息存放在/proc/stat文件中。在多CPU系统里,mpstat命令不仅能查看所有CPU的平均状况信息,而且能够查看特定CPU的信息。mpstat最大的特点是:可以查看多核心cpu中每个计算核心的统计数据;而类似的工具vmstat只能查看系统整体cpu情况 。

        本文主要讲解mpstat的安装、命令用法,常用参数等

1. 安装

查询所属软件包

[root@devvm ~]# yum provides */mpstat
Loaded plugins: fastestmirror
Loading mirror speeds from cached hostfile
 * base: mirrors.bupt.edu.cn
 * extras: ftp.sjtu.edu.cn
 * updates: ftp.sjtu.edu.cn
pcp-4.3.2-12.el7.x86_64 : System-level performance monitoring and performance management
Repo        : base
Matched from:
Filename    : /var/lib/pcp/config/pmlogconf/tools/mpstat



pcp-4.3.2-13.el7_9.x86_64 : System-level performance monitoring and performance management
Repo        : updates
Matched from:
Filename    : /var/lib/pcp/config/pmlogconf/tools/mpstat



sysstat-10.1.5-19.el7.x86_64 : Collection of performance monitoring tools for Linux
Repo        : base
Matched from:
Filename    : /usr/bin/mpstat



sysstat-10.1.5-19.el7.x86_64 : Collection of performance monitoring tools for Linux
Repo        : @base
Matched from:
Filename    : /usr/bin/mpstat



[root@devvm ~]# 

 安装:

[root@devvm ~]# yum install sysstat -y
Loaded plugins: fastestmirror
Loading mirror speeds from cached hostfile
 * base: mirrors.bupt.edu.cn
 * extras: ftp.sjtu.edu.cn
 * updates: ftp.sjtu.edu.cn
Resolving Dependencies
--> Running transaction check
---> Package sysstat.x86_64 0:10.1.5-19.el7 will be installed
--> Processing Dependency: libsensors.so.4()(64bit) for package: sysstat-10.1.5-19.el7.x86_64
--> Running transaction check
---> Package lm_sensors-libs.x86_64 0:3.4.0-8.20160601gitf9185e5.el7 will be installed
--> Finished Dependency Resolution
 
Dependencies Resolved
 
=======================================================================================================
 Package                  Arch            Version                                  Repository     Size
=======================================================================================================
Installing:
 sysstat                  x86_64          10.1.5-19.el7                            base          315 k
Installing for dependencies:
 lm_sensors-libs          x86_64          3.4.0-8.20160601gitf9185e5.el7           base           42 k
 
Transaction Summary
=======================================================================================================
Install  1 Package (+1 Dependent package)
 
Total download size: 357 k
Installed size: 1.2 M
Downloading packages:
(1/2): lm_sensors-libs-3.4.0-8.20160601gitf9185e5.el7.x86_64.rpm                |  42 kB  00:00:00     
(2/2): sysstat-10.1.5-19.el7.x86_64.rpm                                         | 315 kB  00:00:00     
-------------------------------------------------------------------------------------------------------
Total                                                                  1.1 MB/s | 357 kB  00:00:00     
Running transaction check
Running transaction test
Transaction test succeeded
Running transaction
  Installing : lm_sensors-libs-3.4.0-8.20160601gitf9185e5.el7.x86_64                               1/2 
  Installing : sysstat-10.1.5-19.el7.x86_64                                                        2/2 
  Verifying  : sysstat-10.1.5-19.el7.x86_64                                                        1/2 
  Verifying  : lm_sensors-libs-3.4.0-8.20160601gitf9185e5.el7.x86_64                               2/2 
 
Installed:
  sysstat.x86_64 0:10.1.5-19.el7                                                                       
 
Dependency Installed:
  lm_sensors-libs.x86_64 0:3.4.0-8.20160601gitf9185e5.el7                                              
 
Complete!
[root@devvm ~]# 

2. 用法

语法:

mpstat [-P {|ALL}] [internal [count]]

参数说明

参数解释
-P {|ALL}表示监控哪个CPU,例如mpstat -P 0    mpstat -P 7   mpstat -P ALL
internal相邻的两次采样的间隔时间
count采样的次数,count只能和delay一起使用
当没有参数时,mpstat只显示系统所有信息的平均值,有internal参数时,第一行的信息是自系统启动以来的平均信息,从第二行开始,输出为前一个internal时间段的平均信息

3. 实践

3.1 默认使用方式

查看CPU整体的运行状况,每秒更新1次;

[root@devvm ~]# mpstat 1   #没有-P {|ALL}参数时,默认显示CPU整体运行状况
Linux 4.14.81.bm.15-amd64 (n227-080-096)     10/04/21     _x86_64_    (8 CPU)

11:05:37     CPU    %usr   %nice    %sys %iowait    %irq   %soft  %steal  %guest  %gnice   %idle
11:05:38     all    0.50    0.00    1.51    0.00    0.00    0.00    0.13    0.00    0.00   97.86
11:05:39     all    0.75    0.00    1.76    0.00    0.00    0.00    0.13    0.00    0.00   97.36
11:05:40     all    0.25    0.00    1.76    0.00    0.00    0.00    0.13    0.00    0.00   97.86
11:05:41     all    0.63    0.00    1.51    0.00    0.00    0.00    0.25    0.00    0.00   97.62
11:05:42     all    1.00    0.00    2.63    0.00    0.00    0.00    0.13    0.00    0.00   96.24

3.2 监控每个CPU

[root@devvm ~]#  mpstat -P ALL 1     #有-P {|ALL}参数时,会显示全部CPU核心的运行状况
Linux 4.14.81.bm.15-amd64 (n227-080-096)     10/04/21     _x86_64_    (8 CPU)

11:07:11     CPU    %usr   %nice    %sys %iowait    %irq   %soft  %steal  %guest  %gnice   %idle
11:07:12     all    0.76    0.00    2.15    0.00    0.00    0.00    0.13    0.00    0.00   96.97
11:07:12       0    0.00    0.00    4.00    0.00    0.00    0.00    1.00    0.00    0.00   95.00
11:07:12       1    0.00    0.00    1.01    0.00    0.00    0.00    0.00    0.00    0.00   98.99
11:07:12       2    0.00    0.00    3.03    0.00    0.00    0.00    0.00    0.00    0.00   96.97
11:07:12       3    1.01    0.00    2.02    0.00    0.00    0.00    0.00    0.00    0.00   96.97
11:07:12       4    3.03    0.00    2.02    0.00    0.00    0.00    0.00    0.00    0.00   94.95
11:07:12       5    0.00    0.00    3.06    0.00    0.00    0.00    0.00    0.00    0.00   96.94
11:07:12       6    1.00    0.00    1.00    0.00    0.00    0.00    0.00    0.00    0.00   98.00
11:07:12       7    0.00    0.00    0.00    0.00    0.00    0.00    0.00    0.00    0.00  100.00

11:07:12     CPU    %usr   %nice    %sys %iowait    %irq   %soft  %steal  %guest  %gnice   %idle
11:07:13     all    0.38    0.00    1.13    0.00    0.00    0.00    0.38    0.00    0.00   98.11
11:07:13       0    0.99    0.00    1.98    0.00    0.00    0.00    0.99    0.00    0.00   96.04
11:07:13       1    0.00    0.00    2.04    0.00    0.00    0.00    0.00    0.00    0.00   97.96
11:07:13       2    0.00    0.00    2.02    0.00    0.00    0.00    0.00    0.00    0.00   97.98
11:07:13       3    0.00    0.00    0.00    0.00    0.00    0.00    1.01    0.00    0.00   98.99
11:07:13       4    1.00    0.00    1.00    0.00    0.00    0.00    0.00    0.00    0.00   98.00
11:07:13       5    0.00    0.00    1.00    0.00    0.00    0.00    1.00    0.00    0.00   98.00
11:07:13       6    0.00    0.00    1.00    0.00    0.00    0.00    1.00    0.00    0.00   98.00
11:07:13       7    0.00    0.00    0.00    0.00    0.00    0.00    0.00    0.00    0.00  100.00

3.3 字段含义

字段解释
%usr在internal时间段里,用户态占用CPU的时间(%),不包含nice值为负的进程
%nice在internal时间段里,nice值为负的进程占用CPU的时间(%) 
%sys在internal时间段里,内核态占用CPU的时间(%)
%iowait在internal时间段里,等待IO占用CPU的时间(%)
%irq在internal时间段里,硬中断占用CPU的时间(%)
%soft在internal时间段里,软中断占用CPU的时间(%)
%guestCPU处理虚拟进程花费的时间开销
%idle在internal时间段里,空闲CPU的时间(%)

4. 总结

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

       2.vmstat中所有关于CPU的总结都适合mpstat。当您看到较低的 %idle 数字时,就知道出现了 CPU 不足的问题。当您看到较高的 %iowait 数字时,就知道在当前负载下等待IO严重。。

  • 5
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

YoungerChina

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值