欢迎光临
我们一直在努力

Linux 性能实战 | 附录:进程CPU使用率的计算

一、核心公式(工程上真正使用的)

进程 CPU 使用率必须是“两个时间点之间的差值”,公式如下:

CPU% = (Δ(utime + stime) / Δtotal_cpu_time) × NCPU × 100%

或更常见等价形式:

CPU% = Δproc_time / Δwall_time × 100%

取决于你用哪种参照系。

下面解释清楚。


二、utime / stime 是什么单位?

单位不是秒,而是:

clock ticks

转换成秒:

seconds = ticks / sysconf(_SC_CLK_TCK)

查看:

getconf CLK_TCK

通常:

100

表示:

1 tick = 10 ms


三、方法 1:最直观工程计算法(推荐)

步骤:

Step 1:读取进程 CPU 时间

cat /proc/<pid>/stat

第 14、15 字段:

utime stime

例如:

第一次:

utime = 1200
stime = 300
total = 1500

1 秒后:

utime = 1300
stime = 400
total = 1700

增量:

Δproc = 200 ticks


Step 2:换算为秒

如果 CLK_TCK = 100:

200 ticks = 2 seconds CPU time


Step 3:除以实际时间

假设采样间隔 1 秒:

CPU% = 2 / 1 × 100% = 200%

表示:

使用了 2 个 CPU 核心满载


四、为什么可以超过 100%?

因为进程可以多线程并行:

1 核满载 = 100%
2 核满载 = 200%
8 核满载 = 800%

top 的显示就是这样。


五、方法 2:用 /proc/stat 作为基准(更精确)

读取:

cat /proc/stat

第一行:

cpu user nice system idle iowait irq softirq steal …

计算:

total_cpu_time = 所有字段之和

然后:

CPU% = Δproc_time / Δtotal_cpu_time × NCPU × 100%

这是 top 的实现方式。


六、完整 Python 示例(工程级)

import time

CLK_TCK = 100

def read_proc_time(pid):
with open(f"/proc/{pid}/stat") as f:
fields = f.read().split()
utime = int(fields[13])
stime = int(fields[14])
return utime + stime

pid = 1234

t1 = read_proc_time(pid)
time.sleep(1)
t2 = read_proc_time(pid)

delta = t2 – t1

cpu_percent = delta / CLK_TCK * 100

print(cpu_percent, "%")


七、utime vs stime 的工程意义

它们回答不同问题:

字段含义判断什么问题
utime 用户态执行时间 算法 / 计算
stime 内核态执行时间 syscall / IO / kernel

八、工程诊断意义(非常重要)

情况 1:utime 高

utime >> stime

说明:

CPU 花在用户代码

原因:

  • 算法复杂
  • 死循环
  • 计算密集

情况 2:stime 高

stime >> utime

说明:

CPU 花在内核

原因:

  • write()
  • read()
  • 网络
  • 文件系统
  • 锁竞争

情况 3:CPU% 低,但进程卡

检查:

D state

说明:

在等 IO

不是 CPU 问题。


九、top 的 %CPU 本质就是这个公式

top 每隔一段时间:

读取 proc stat
计算差值
显示 %

不是瞬时值。


十、一张完整因果链图

#mermaid-svg-IeFCxSuCTm4tThe6{font-family:\”trebuchet ms\”,verdana,arial,sans-serif;font-size:16px;fill:#333;}@keyframes edge-animation-frame{from{stroke-dashoffset:0;}}@keyframes dash{to{stroke-dashoffset:0;}}#mermaid-svg-IeFCxSuCTm4tThe6 .edge-animation-slow{stroke-dasharray:9,5!important;stroke-dashoffset:900;animation:dash 50s linear infinite;stroke-linecap:round;}#mermaid-svg-IeFCxSuCTm4tThe6 .edge-animation-fast{stroke-dasharray:9,5!important;stroke-dashoffset:900;animation:dash 20s linear infinite;stroke-linecap:round;}#mermaid-svg-IeFCxSuCTm4tThe6 .error-icon{fill:#552222;}#mermaid-svg-IeFCxSuCTm4tThe6 .error-text{fill:#552222;stroke:#552222;}#mermaid-svg-IeFCxSuCTm4tThe6 .edge-thickness-normal{stroke-width:1px;}#mermaid-svg-IeFCxSuCTm4tThe6 .edge-thickness-thick{stroke-width:3.5px;}#mermaid-svg-IeFCxSuCTm4tThe6 .edge-pattern-solid{stroke-dasharray:0;}#mermaid-svg-IeFCxSuCTm4tThe6 .edge-thickness-invisible{stroke-width:0;fill:none;}#mermaid-svg-IeFCxSuCTm4tThe6 .edge-pattern-dashed{stroke-dasharray:3;}#mermaid-svg-IeFCxSuCTm4tThe6 .edge-pattern-dotted{stroke-dasharray:2;}#mermaid-svg-IeFCxSuCTm4tThe6 .marker{fill:#333333;stroke:#333333;}#mermaid-svg-IeFCxSuCTm4tThe6 .marker.cross{stroke:#333333;}#mermaid-svg-IeFCxSuCTm4tThe6 svg{font-family:\”trebuchet ms\”,verdana,arial,sans-serif;font-size:16px;}#mermaid-svg-IeFCxSuCTm4tThe6 p{margin:0;}#mermaid-svg-IeFCxSuCTm4tThe6 .label{font-family:\”trebuchet ms\”,verdana,arial,sans-serif;color:#333;}#mermaid-svg-IeFCxSuCTm4tThe6 .cluster-label text{fill:#333;}#mermaid-svg-IeFCxSuCTm4tThe6 .cluster-label span{color:#333;}#mermaid-svg-IeFCxSuCTm4tThe6 .cluster-label span p{background-color:transparent;}#mermaid-svg-IeFCxSuCTm4tThe6 .label text,#mermaid-svg-IeFCxSuCTm4tThe6 span{fill:#333;color:#333;}#mermaid-svg-IeFCxSuCTm4tThe6 .node rect,#mermaid-svg-IeFCxSuCTm4tThe6 .node circle,#mermaid-svg-IeFCxSuCTm4tThe6 .node ellipse,#mermaid-svg-IeFCxSuCTm4tThe6 .node polygon,#mermaid-svg-IeFCxSuCTm4tThe6 .node path{fill:#ECECFF;stroke:#9370DB;stroke-width:1px;}#mermaid-svg-IeFCxSuCTm4tThe6 .rough-node .label text,#mermaid-svg-IeFCxSuCTm4tThe6 .node .label text,#mermaid-svg-IeFCxSuCTm4tThe6 .image-shape .label,#mermaid-svg-IeFCxSuCTm4tThe6 .icon-shape .label{text-anchor:middle;}#mermaid-svg-IeFCxSuCTm4tThe6 .node .katex path{fill:#000;stroke:#000;stroke-width:1px;}#mermaid-svg-IeFCxSuCTm4tThe6 .rough-node .label,#mermaid-svg-IeFCxSuCTm4tThe6 .node .label,#mermaid-svg-IeFCxSuCTm4tThe6 .image-shape .label,#mermaid-svg-IeFCxSuCTm4tThe6 .icon-shape .label{text-align:center;}#mermaid-svg-IeFCxSuCTm4tThe6 .node.clickable{cursor:pointer;}#mermaid-svg-IeFCxSuCTm4tThe6 .root .anchor path{fill:#333333!important;stroke-width:0;stroke:#333333;}#mermaid-svg-IeFCxSuCTm4tThe6 .arrowheadPath{fill:#333333;}#mermaid-svg-IeFCxSuCTm4tThe6 .edgePath .path{stroke:#333333;stroke-width:2.0px;}#mermaid-svg-IeFCxSuCTm4tThe6 .flowchart-link{stroke:#333333;fill:none;}#mermaid-svg-IeFCxSuCTm4tThe6 .edgeLabel{background-color:rgba(232,232,232, 0.8);text-align:center;}#mermaid-svg-IeFCxSuCTm4tThe6 .edgeLabel p{background-color:rgba(232,232,232, 0.8);}#mermaid-svg-IeFCxSuCTm4tThe6 .edgeLabel rect{opacity:0.5;background-color:rgba(232,232,232, 0.8);fill:rgba(232,232,232, 0.8);}#mermaid-svg-IeFCxSuCTm4tThe6 .labelBkg{background-color:rgba(232, 232, 232, 0.5);}#mermaid-svg-IeFCxSuCTm4tThe6 .cluster rect{fill:#ffffde;stroke:#aaaa33;stroke-width:1px;}#mermaid-svg-IeFCxSuCTm4tThe6 .cluster text{fill:#333;}#mermaid-svg-IeFCxSuCTm4tThe6 .cluster span{color:#333;}#mermaid-svg-IeFCxSuCTm4tThe6 div.mermaidTooltip{position:absolute;text-align:center;max-width:200px;padding:2px;font-family:\”trebuchet ms\”,verdana,arial,sans-serif;font-size:12px;background:hsl(80, 100%, 96.2745098039%);border:1px solid #aaaa33;border-radius:2px;pointer-events:none;z-index:100;}#mermaid-svg-IeFCxSuCTm4tThe6 .flowchartTitleText{text-anchor:middle;font-size:18px;fill:#333;}#mermaid-svg-IeFCxSuCTm4tThe6 rect.text{fill:none;stroke-width:0;}#mermaid-svg-IeFCxSuCTm4tThe6 .icon-shape,#mermaid-svg-IeFCxSuCTm4tThe6 .image-shape{background-color:rgba(232,232,232, 0.8);text-align:center;}#mermaid-svg-IeFCxSuCTm4tThe6 .icon-shape p,#mermaid-svg-IeFCxSuCTm4tThe6 .image-shape p{background-color:rgba(232,232,232, 0.8);padding:2px;}#mermaid-svg-IeFCxSuCTm4tThe6 .icon-shape rect,#mermaid-svg-IeFCxSuCTm4tThe6 .image-shape rect{opacity:0.5;background-color:rgba(232,232,232, 0.8);fill:rgba(232,232,232, 0.8);}#mermaid-svg-IeFCxSuCTm4tThe6 .label-icon{display:inline-block;height:1em;overflow:visible;vertical-align:-0.125em;}#mermaid-svg-IeFCxSuCTm4tThe6 .node .label-icon path{fill:currentColor;stroke:revert;stroke-width:revert;}#mermaid-svg-IeFCxSuCTm4tThe6 :root{–mermaid-font-family:\”trebuchet ms\”,verdana,arial,sans-serif;}

进程运行

utime 增加

stime 增加

proc total time

两次采样求差

除以时间间隔

得到 CPU%


十一、一句话工程总结

进程 CPU 使用率,本质是“单位时间内消耗的 CPU 时间”,
utime + stime 提供了这个时间,而 CPU% 是它的变化速率。


在这里插入图片描述

赞(0)
未经允许不得转载:171主机测评 » Linux 性能实战 | 附录:进程CPU使用率的计算
分享到: 更多 (0)

评论 抢沙发

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址