mpstat 简介:Linux CPU 监控利器
mpstat(全称 Multiprocessor Statistics)是 Linux 系统中一款强大的 CPU 性能监控工具。它从 /proc/stat 文件中读取数据,能够精确反映 CPU 的实时运行状态。与只能提供整体 CPU 使用率的工具(如 vmstat)不同,mpstat 的核心优势在于可以针对每个独立的 CPU 核心进行详细统计。对于多核或多处理器系统而言,mpstat 是性能分析与故障排查的必备利器。无论你的服务器配备了多少物理 CPU 或超线程核心,mpstat 都能帮助你深入了解其工作负载情况。
语法格式
mpstat [-P {|ALL}] [internal [count]]
参数说明:
-P {|ALL}:指定要监控的 CPU 核心编号(从 0 到 CPU 总数减 1),或使用 ALL 查看所有核心。internal:采样间隔(单位秒)。count:采样次数,需与间隔参数同时使用。
若不提供任何参数,mpstat 将输出系统自启动以来的平均统计数据。如果指定了间隔参数,则第一行仍为累计平均值,后续每行对应每个间隔内的实时数据。
常用选项
-P:用于指定目标 CPU 编号。例如 -P 0 仅监控第一个核心,-P ALL 则监控所有核心。
使用参数
- 间隔时间:每次生成报告的时间间隔(秒)。
- 次数:总共生成的报告次数。
实际应用示例
无参数用法:显示系统启动以来的平均统计
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秒采样一次,生成所有核心的统计报告(共3次)
该命令会生成三个时间间隔的报表,并在末尾输出平均值。输出结果默认按照 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
对比有无间隔参数的效果差异
先在后台运行一个大型文件读取操作: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
带间隔参数(3秒采样10次):
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 A verage: all 9.27 0.00 16.18 0.30 1.50 7.64 0.00 65.11 2173.54
对比十分明显——不使用间隔参数时,仅显示系统启动以来的平均值,无法实时反映当前正在进行的 I/O 或 CPU 密集型活动。若要准确诊断问题,必须配合间隔参数进行动态监测。vmstat 和 iostat 等工具也遵循相同的原则。
查看多核CPU各核心实时运行状况(每2秒刷新)
mpstat 2 19: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 时间前的值。上表中的所有值可取到两位小数点。
