系列专题: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的时间(%) |
%guest | CPU处理虚拟进程花费的时间开销 |
%idle | 在internal时间段里,空闲CPU的时间(%) |
4. 总结
1. mpstat和vmstat命令的差别:mpstat可以显示每个CPU的统计信息,而vmstat只显示所有CPU的统计信息。因此,编写糟糕的应用程序(不使用多线程体系结构)可能会运行在一个多处理器机器上,而不使用所有处理器。从而导致一个 CPU 过载,而其他 CPU 却很空闲。通过 mpstat 可以轻松诊断这些类型的问题。
2.vmstat中所有关于CPU的总结都适合mpstat。当您看到较低的 %idle 数字时,就知道出现了 CPU 不足的问题。当您看到较高的 %iowait 数字时,就知道在当前负载下等待IO严重。。