卡饭网 > 其他 > 正文

Linux mpstat命令的方法(实时系统监控工具)

来源:本站整理 作者:梦在深巷 时间:2018-02-17 11:17:14

简介

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

mpstat命令指令主要用于多CPU环境下,它显示各个可用CPU的状态系你想。这些信息存放在/proc/stat文件中。在多CPUs系统里,其不但能查看所有CPU的平均状况信息,而且能够查看特定CPU的信息。

语法

mpstat(选项)(参数)

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

选项

-P:指定CPU编号。

参数

间隔时间:每次报告的间隔时间(秒);

次数:显示报告的次数。

实例

当mpstat不带参数时,输出为从系统启动以来的平均值。

mpstat
Linux 2.6.9-5.31AXsmp (builder.redflag-linux.com) 12/16/2005
09:38:46 AM CPU %user %nice %system %iowait %irq %soft %idle intr/s
09:38:48 AM all 23.28 0.00 1.75     0.50 0.00 0.00 74.47 1018.59

每2秒产生了2个处理器的统计数据报告:

下面的命令可以每2秒产生了2个处理器的统计数据报告,一共产生三个interval 的信息,然后再给出这三个interval的平均信息。默认时,输出是按照CPU 号排序。第一个行给出了从系统引导以来的所有活跃数据。接下来每行对应一个处理器的活跃状态。

mpstat -P ALL 2 3
Linux 2.6.18-164.el5 (server.sys.com)    01/04/2010
09:34:20 PM CPU   %user   %nice    %sys %iowait    %irq   %soft %steal   %idle    intr/s
09:34:22 PM all    0.00    0.00    0.00    0.00    0.00    0.00    0.00 100.00   1001.49
09:34:22 PM    0    0.00    0.00    0.50    0.00    0.00    0.00    0.00   99.50   1001.00
09:34:22 PM    1    0.00    0.00    0.00    0.00    0.00    0.00    0.00 100.00      0.00

比较带参数和不带参数的mpstat的结果:

在后台开一个2G的文件

cat 1.img &

然后在另一个终端运行mpstat命令

mpstat
Linux 2.6.18-164.el5 (server.sys.com)    01/04/2010
10:17:31 PM CPU   %user   %nice    %sys %iowait    %irq   %soft %steal   %idle    intr/s
10:17:31 PM all    0.07    0.02    0.25    0.21    0.01    0.04    0.00   99.40   1004.57
mpstat
Linux 2.6.18-164.el5 (server.sys.com)    01/04/2010
10:17:35 PM CPU   %user   %nice    %sys %iowait    %irq   %soft %steal   %idle    intr/s
10:17:35 PM all    0.07    0.02    0.25    0.21    0.01    0.04    0.00   99.39   1004.73
mpstat 3 10
Linux 2.6.18-164.el5 (server.sys.com)    01/04/2010
10:17:55 PM CPU   %user   %nice    %sys %iowait    %irq   %soft %steal   %idle    intr/s
10:17:58 PM all   13.12    0.00   20.93    0.00    1.83    9.80    0.00   54.32   2488.08
10:18:01 PM all   10.82    0.00   19.30    0.83    1.83    9.32    0.00   57.90   2449.83
10:18:04 PM all   10.95    0.00   20.40    0.17    1.99    8.62    0.00   57.88   2384.05
10:18:07 PM all   10.47    0.00   18.11    0.00    1.50    8.47    0.00   61.46   2416.00
10:18:10 PM all   11.81    0.00   22.63    0.00    1.83   11.98    0.00   51.75   2210.60
10:18:13 PM all    6.31    0.00   10.80    0.00    1.00    5.32    0.00   76.58   1795.33
10:18:19 PM all    1.75    0.00    3.16    0.75    0.25    1.25    0.00   92.85   1245.18
10:18:22 PM all   11.94    0.00   19.07    0.00    1.99    8.29    0.00   58.71   2630.46
10:18:25 PM all   11.65    0.00   19.30    0.50    2.00    9.15    0.00   57.40   2673.91
10:18:28 PM all   11.44    0.00   21.06    0.33    1.99   10.61    0.00   54.56   2369.87
Average:     all    9.27    0.00   16.18    0.30    1.50    7.64    0.00   65.11   2173.54

上两表显示出当要正确反映系统的情况,需要正确使用命令的参数。vmstat和iostat 也需要注意这一问题。

查看多核CPU核心的当前运行状况信息, 每2秒更新一次

mpstat  219:45:12 CPU %usr %nice %sys %iowait %irq %soft %steal %guest %idle
19:45:14 all 0.04 0.00 0.00 0.00 0.00 0.00 0.00 0.00 99.96
19:45:16 all 0.00 0.00 0.00 0.03 0.00 0.00 0.00 0.00 99.97
19:45:18 all 0.00 0.07 0.07 0.00 0.00 0.00 0.00 0.00 99.87

如果要看每个cpu核心的详细当前运行状况信息,输出如下:

mpstat  -P ALL 2

19:43:58     CPU    %usr   %nice    %sys %iowait    %irq   %soft  %steal  %guest   %idle
19:43:59     all    0.00    0.00    0.04    0.00    0.00    0.00    0.00    0.00   99.96
19:43:59       0    0.00    0.00    0.00    0.00    0.00    0.00    0.00    0.00  100.00
19:43:59       1    0.00    0.00    0.00    0.00    0.00    0.00    0.00    0.00  100.00
.......
19:43:59      13    0.99    0.00    0.00    0.00    0.00    0.00    0.00    0.00   99.01
19:43:59      14    0.00    0.00    0.00    0.00    0.00    0.00    0.00    0.00  100.00
19:43:59      15    0.00    0.00    0.00    0.00    0.00    0.00    0.00    0.00  100.00

字段的含义如下

%user      在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
%idle      在internal时间段里,CPU除去等待磁盘IO操作外的因为任何原因而空闲的时间闲置时间(%) (idle/total)*100

计算公式如下

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时间前的值。上表中的所有值可取到两位小数点。

好了,这篇文章就介绍到这了,希望大家以后多多支持我们。

相关推荐