温馨提示×

怎么使用centos cpustat

小樊
42
2026-07-09 03:56:40
栏目: 智能运维

在 CentOS 系统中,cpustat 通常是 sysstat 工具包的一部分,用于查看 CPU 的使用情况。下面是详细的使用说明,从安装到常见用法。


一、安装 cpustat(如果未安装)

1. 检查是否已安装

which cpustat

2. 安装 sysstat

sudo yum install -y sysstat

CentOS 7/8 默认仓库中都有 sysstat


二、基本用法

1. 查看 CPU 实时统计

cpustat

输出示例:

  CPU  %usr  %nice  %sys  %iowait  %irq  %soft  %steal  %guest  %gnice  %idle
  all   5.2   0.0   1.3     0.2    0.0    0.1     0.0     0.0     0.0   93.2

字段说明:

  • %usr:用户态 CPU 使用率
  • %sys:内核态 CPU 使用率
  • %idle:空闲 CPU
  • %iowait:IO 等待占用的 CPU

2. 指定采样间隔和次数

cpustat 1 5

含义:

  • 1 秒 采样一次
  • 共采样 5 次

三、常见实用参数

1. 显示所有 CPU(每个核心)

cpustat -c

或(不同版本可能):

mpstat -P ALL

2. 显示指定 CPU

cpustat -C 0

3. 显示汇总信息

cpustat -u

4. 持续监控(类似 top)

watch -n 1 cpustat

四、和 mpstat 的关系(很重要)

在很多 CentOS 系统中:

  • cpustat 实际上是 mpstat 的别名或简化版
  • 更常用的是:
mpstat
mpstat 1
mpstat -P ALL 1

示例:

mpstat -P ALL 1 3

五、排查 CPU 问题的常见用法

1. 查看是否 IO 导致 CPU 等待

mpstat 1

关注 %iowait


2. 查看哪个核心压力最大

mpstat -P ALL 1

3. 查看中断对 CPU 的影响

mpstat -I ALL

六、常见问题

❓ cpustat 命令不存在?

✅ 解决方案:

sudo yum install sysstat

❓ 数据和 top 不一样?

  • cpustat/mpstat 统计的是 CPU 时间占比
  • top 是实时快照,略有差异

七、总结常用命令

# 安装
sudo yum install -y sysstat

# 基本使用
cpustat
cpustat 1 5

# 每个 CPU 单独显示
mpstat -P ALL 1

# 持续监控
watch -n 1 cpustat

如果你愿意,我也可以:

  • ✅ 帮你分析具体的 cpustat 输出
  • ✅ 教你如何用 sar 做历史 CPU 分析
  • ✅ 结合 top / htop / pidstat 一起诊断高 CPU 问题

0