5

mpstat 使用介绍和输出参数详解

 2 years ago
source link: https://wsgzao.github.io/post/mpstat/
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

mpstat 相比 top 来说使用频率未必很高,甚至还可能略低于 vmstat,top 进入交互式界面后,按 1 显示所有 cpu 的负载,结果和 mpstat 基本一致,本文对 mpstat 使用做下简单的记录,方便回顾输出参数的含义。

mpstat 使用介绍和输出参数详解

2019 年 06 月 29 日 - 初稿

阅读原文 - https://wsgzao.github.io/post/mpstat/

扩展阅读


mpstat 简介

mpstat 是 Multiprocessor Statistics 的缩写,是实时系统监控工具。其报告与 CPU 的一些统计信息,这些信息存放在 /proc/stat 文件中。在多 CPU 系统里,其不但能查看所有 CPU 的平均状况信息,而且能够查看特定 CPU 的信息。

mpstat 语法

# mpstat 属于 sysstat
yum provides mpstat
Loaded plugins: fastestmirror
Loading mirror speeds from cached hostfile
* base: mirror.vodien.com
* epel: download.nus.edu.sg
* extras: mirror.vodien.com
* updates: mirror.vodien.com
sysstat-10.1.5-17.el7.x86_64 : Collection of performance monitoring tools for Linux
Repo : base
Matched from:
Filename : /usr/bin/mpstat

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


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

其中,各参数含义如下:

参数 含义 -P {cpu l ALL} 表示监控哪个 CPU, cpu 在 [0,cpu 个数 - 1] 中取值 internal 相邻的两次采样的间隔时间 count 采样的次数,count 只能和 delay 一起使用

使用 mpstat 命令

1. 直接使用 mpstat 命令:

mpstat

Linux 3.10.0-862.el7.x86_64 07/04/2019 _x86_64_ (32 CPU)

11:45:19 AM CPU %usr %nice %sys %iowait %irq %soft %steal %guest %gnice %idle
11:45:19 AM all 0.39 0.00 0.19 0.00 0.00 0.03 0.00 0.00 0.00 99.39

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

2. 使用 mpstat -P ALL 5 2 命令

mpstat -P ALL 5 2

Linux 3.10.0-862.el7.x86_64 07/04/2019 _x86_64_ (32 CPU)

11:44:11 AM CPU %usr %nice %sys %iowait %irq %soft %steal %guest %gnice %idle
11:44:16 AM all 0.38 0.00 0.21 0.00 0.00 0.03 0.00 0.00 0.00 99.38
11:44:16 AM 0 0.20 0.00 0.20 0.00 0.00 0.00 0.00 0.00 0.00 99.60
11:44:16 AM 1 0.20 0.00 0.20 0.00 0.00 0.00 0.00 0.00 0.00 99.60

表示每 5 秒产生一个报告,总共产生 2 个。

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

输出参数含义

当没有参数时,mpstat 则显示系统启动以后所有信息的平均值。有 interval 时,第一行的信息自系统启动以来的平均信息。从第二行开始,输出为前一个 interval 时间段的平均信息。

输出各参数含义:

参数 释义 从 /proc/stat 获得数据 CPU 处理器 ID

%usr 在 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 %steal 显示虚拟机管理器在服务另一个虚拟处理器时虚拟 CPU 处在非自愿等待下花费时间的百分比 steal/total*100 %guest 显示运行虚拟处理器时 CPU 花费时间的百分比 guest/total*100 %gnice

gnice/total*100 %idle 在 internal 时间段里,CPU 除去等待磁盘 IO 操作外的因为任何原因而空闲的时间闲置时间(%) idle/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 时间前的值。上表中的所有值可取到两位小数点。

Note:

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

  2. vmstat 中所有关于 CPU 的总结都适合 mpstat。当您看到较低的 % idle 数字时,您知道出现了 CPU 不足的问题。当您看到较高的 % iowait 数字时,您知道在当前负载下 I/O 子系统出现了某些问题。


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK