概述
mpstat是MultiProcessor Statistics的缩写,是实时系统监控工具。其报告与CPU的一些统计信息,这些信息存放在/proc/stat文件中。在多CPUs系统里,其不但能查看所有CPU的平均状况信息,而且能够查看特定CPU的信息。下面只介绍mpstat与CPU相关的参数,mpstat的语法如下:
mpstat [-P {|ALL}] [internal [count]]
参数的含义:
参数 | 说明 |
---|---|
-P | {|ALL} 表示监控哪个CPU, cpu在[0,cpu个数-1]中取值 |
internal | 相邻的两次采样的间隔时间 |
count | 采样的次数 |
count只能和delay一起使用 当没有参数时,mpstat则显示系统启动以后所有信息的平均值。有interval时,第一行的信息自系统启动以来的平均信息。
从第二行开始,输出为前一个interval时间段的平均信息。与CPU有关的输出的含义如下:
结果的含义
参数 | 说明 |
---|---|
CPU | 处理器ID |
user | 用户态的CPU时间(%),不包含 nice值为负 进程 (usr/total)*100 |
nice | nice值为负进程的CPU时间(%) (nice/total)*100 |
system | 核心时间(%) (system/total)*100 |
iowait | 硬盘IO等待时间(%) (iowait/total)*100 |
irq | 硬中断时间(%) (irq/total)*100 |
soft | 软中断时间(%) (softirq/total)*100 |
idle | CPU除去等待磁盘IO操作外的因为任何原因而空闲的时间闲置时间(%)(idle/total)*100 |
intr/s | 在internal时间段里,每秒CPU接收的中断的次数intr/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时间前的值。上表中的所有值可取到两位小数点。
可以从/proc/stat获得数据。
常用方法
- 粗略模式:当mpstat不带参数时,输出为从系统启动以来的平均值。
~ # mpstat
Linux 4.9.37 ((none)) 10/25/20 _armv7l_ (2 CPU)
11:58:10 CPU %usr %nice %sys %iowait %irq %soft %steal %guest %idle
11:58:10 all 54.51 0.00 33.18 0.00 0.00 2.02 0.00 0.00 10.30
- 定时模式:加上interval和count参数,如果想一直输出,count参数可以不加
比如:定时查看所有CPU,mpstat -P ALL 1 3
或者mpstat -P ALL 2
定时查看CPU1mpstat -P 1 1 3
或者mpstat -P 1 1
~ # mpstat -P ALL 1 3
Linux 4.9.37 ((none)) 10/25/20 _armv7l_ (2 CPU)
12:00:44 CPU %usr %nice %sys %iowait %irq %soft %steal %guest %idle
12:00:45 all 54.23 0.00 36.32 0.00 0.00 2.99 0.00 0.00 6.47
12:00:45 0 39.39 0.00 43.43 0.00 0.00 5.05 0.00 0.00 12.12
12:00:45 1 69.70 0.00 29.29 0.00 0.00 0.00 0.00 0.00 1.01
12:00:45 CPU %usr %nice %sys %iowait %irq %soft %steal %guest %idle
12:00:46 all 55.84 0.00 30.96 0.00 0.00 1.52 0.00 0.00 11.68
12:00:46 0 39.18 0.00 38.14 0.00 0.00 3.09 0.00 0.00 19.59
12:00:46 1 71.29 0.00 24.75 0.00 0.00 0.99 0.00 0.00 2.97
12:00:46 CPU %usr %nice %sys %iowait %irq %soft %steal %guest %idle
12:00:47 all 56.57 0.00 34.34 0.00 0.00 1.52 0.00 0.00 7.58
12:00:47 0 41.41 0.00 41.41 0.00 0.00 3.03 0.00 0.00 14.14
12:00:47 1 72.00 0.00 27.00 0.00 0.00 0.00 0.00 0.00 1.00
~ # mpstat -P 1 1
Linux 4.9.37 ((none)) 10/25/20 _armv7l_ (2 CPU)
12:03:33 CPU %usr %nice %sys %iowait %irq %soft %steal %guest %idle
12:03:34 1 70.59 0.00 26.47 0.00 0.00 0.00 0.00 0.00 2.94
12:03:34 CPU %usr %nice %sys %iowait %irq %soft %steal %guest %idle
12:03:35 1 67.33 0.00 29.70 0.00 0.00 0.00 0.00 0.00 2.97
12:03:35 CPU %usr %nice %sys %iowait %irq %soft %steal %guest %idle
12:03:36 1 66.67 0.00 30.30 0.00 0.00 1.01 0.00 0.00 2.02