想看操作系统每颗CPU的使用情况,在网上查找的时候发现了这个命令,于是写这篇文章就当学习记录,方便以后用到时查看。
命令概述:
mpstat - Report processors related statistics。这是摘抄Linux man手册的内容,这个命令是干啥的从这一句解释来看简洁明了。翻译成汉语,就是一台服务器上的所有处理器(通常说的CPU)相关的统计信息。这个命令可以看到每颗CPU的使用情况,这正好能满足我的需求。
语法解释:
mpstat [ -A ] [ -u ] [ -V ] [ -I { SUM | CPU | SCPU | ALL } ] [ -P { cpu [,...] | ON | ALL } ] [ interval [ count ] ]
该命令使用也比较简单,相关的参数比较少,以下分别对每个参数做详细说明:
参数值 | 说明 |
-A | 相当于指定了-u -I ALL -P ALL |
-u | 效果跟直接执行mpstat一样,显示CPU的平均使用信息 |
-V | 输出版本号 |
-I | 统计中断的信息 |
-P | 统计每颗CPU的使用状态 |
interval | 结果隔几秒输出一次 |
count | 结果输出的总数 |
常用命令
以下命令表示每5秒输出所有CPU的使用情况,一共输出3次。
[root@dr19crac1 ~]# mpstat -P ALL 5 3
Linux 3.10.0-957.el7.x86_64 (dr19crac1) 01/09/2024 _x86_64_ (4 CPU)
11:32:43 AM CPU %usr %nice %sys %iowait %irq %soft %steal %guest %gnice %idle
11:32:48 AM all 4.79 0.00 3.42 0.11 0.00 0.16 0.00 0.00 0.00 91.53
11:32:48 AM 0 5.70 0.00 3.59 0.21 0.00 0.21 0.00 0.00 0.00 90.30
11:32:48 AM 1 4.46 0.00 4.25 0.21 0.00 0.21 0.00 0.00 0.00 90.87
11:32:48 AM 2 5.25 0.00 2.73 0.21 0.00 0.21 0.00 0.00 0.00 91.60
11:32:48 AM 3 3.97 0.00 2.72 0.00 0.00 0.00 0.00 0.00 0.00 93.31
^C
Average: CPU %usr %nice %sys %iowait %irq %soft %steal %guest %gnice %idle
Average: all 4.79 0.00 3.42 0.11 0.00 0.16 0.00 0.00 0.00 91.53
Average: 0 5.70 0.00 3.59 0.21 0.00 0.21 0.00 0.00 0.00 90.30
Average: 1 4.46 0.00 4.25 0.21 0.00 0.21 0.00 0.00 0.00 90.87
Average: 2 5.25 0.00 2.73 0.21 0.00 0.21 0.00 0.00 0.00 91.60
Average: 3 3.97 0.00 2.72 0.00 0.00 0.00 0.00 0.00 0.00 93.31
输出的结果中,%usr,%sys,%iowait,%idle都是一些常见的指标,此处不做多余介绍。
名称 | 定义 | 个人理解 |
%nice | Show the percentage of CPU utilization that occurred while executing at the user level with nice priority. | 展示设定用户优先级的CPU使用率 |
%irq | Show the percentage of time spent by the CPU or CPUs to service hardware interrupts. | 展示硬中断占用CPU的百分比 |
%soft | Show the percentage of time spent by the CPU or CPUs to service software interrupts. | 展示软中断占用CPU的百分比 |
%steal | Show the percentage of time spent in involuntary wait by the virtual CPU or CPUs while the hypervisor was servicing another virtual processor. | 展示虚拟服务器服务于其他虚拟处理器时花费的非自愿等待占用CPU的百分比 |
%guest | Show the percentage of time spent by the CPU or CPUs to run a virtual processor. | 显示CPU运行虚拟处理器所花费的时间百分比 |
%gnice | Show the percentage of time spent by the CPU or CPUs to run a niced guest. | 显示CPU运行nice客户所花费的时间百分比 |