CPU 使用率

CPU 使用率

CPU 使用率(CPU Utilization)是指在一段时间内,处理器用于执行任务的时间占总时间的比例。它反映了 CPU 的繁忙程度,通常以百分比表示。

1 基本概念

  1. 用户态(User Mode)CPU 使用率

    • CPU 在用户态(用户空间)运行用户应用程序的时间。
    • 例如,当你运行一个程序(如文本编辑器、浏览器等)时,CPU 会在用户态处理这些任务。
  2. 系统态(System Mode)CPU 使用率

    • CPU 在内核态(内核空间)执行系统调用、驱动程序等操作的时间。
    • 例如,文件读写操作、网络请求处理等属于内核态。
  3. 空闲态(Idle)CPU 使用率

    • CPU 处于空闲状态,没有任务要执行的时间。
    • 这个数值越大,表示 CPU 越空闲。
  4. 等待 I/O(I/O Wait)

    • CPU 等待输入输出(如硬盘、网络)的时间。这种情况下,CPU 可能是空闲的,但由于等待 I/O 完成而无法进行其他任务。
  5. 中断时间(IRQ & SoftIRQ)

    • CPU 处理硬件中断(IRQ)和软件中断(SoftIRQ)的时间。

高 CPU 使用率的含义

如何查看 CPU 使用率

2 如何查看CPU时间

cat /proc/stat 查看开机以来的系统CPU时间 /proc/stat

The amount of time, measured in units of USER_HZ
(1/100ths of a second on most architectures, use
sysconf(SC_CLK_TCK) to obtain the right value),
that the system ("cpu" line) or the specific CPU
("cpu_N
" line) spent in various states

单位为时间数量,一般是 1/100 秒,具体值根据 USER_HZ 确定。

3 CPU 使用率的计算方法

总CPU使用率(开机以来)

CPU 使用率=1空闲时间总 CPU 时间

一段时间内的平均CPU使用率

平均 CPU 使用率=1空闲时间new空闲时间old总 CPU 时间new总 CPU 时间old

4 获得CPU使用率的几种方法

  1. top命令Pasted image 20240919184300.png|600
    1可以看每个CPU单独的数据 linux-top
  2. htop 命令 Pasted image 20240919184348.png|600
    优势:可以响应鼠标点击事件,滚动查看,查看进程树
  3. vmstat 1 每秒输出一次 vmstat reports information about processes, memory, paging, block IO, traps, disks and cpu activity
  4. mpstat 或者 mpstat -u 1 10 获取一段时间内的CPU使用率

mpstat命令输出

Linux 6.8.0-44-generic (dy-turbo-vm) 	09/19/2024 	_aarch64_	(8 CPU)

10:52:27 AM  CPU    %usr   %nice    %sys %iowait    %irq   %soft  %steal  %guest  %gnice   %idle
10:52:27 AM  all    0.02    0.00    0.05    0.01    0.00    0.00    0.00    0.00    0.00   99.91
  1. sar 1 5 此命令每秒采集一次数据,连续采集 5 次。

sar命令输出

Linux 6.8.0-44-generic (dy-turbo-vm) 	09/19/2024 	_aarch64_	(8 CPU)

10:54:49 AM     CPU     %user     %nice   %system   %iowait    %steal     %idle
10:54:50 AM     all      0.12      0.00      0.12      0.00      0.00     99.75
10:54:51 AM     all      0.00      0.00      0.25      0.00      0.00     99.75
10:54:52 AM     all      0.00      0.00      0.12      0.00      0.00     99.88
10:54:53 AM     all      0.00      0.00      0.12      0.00      0.00     99.88
10:54:54 AM     all      0.00      0.00      0.00      0.00      0.00    100.00
Average:        all      0.03      0.00      0.13      0.00      0.00     99.85
  1. psps -eo pid,ppid,cmd,%mem,%cpu --sort=-%cpu | head
  2. cat /proc/stat 通过读取CPU时间,自行计算CPU使用率
  3. 查看指定进程的CPU使用情况: linux-pidstat

本文总阅读量