计算CPU利用率

/proc/stat

1
2
cat /proc/stat | grep "cpu " | \
awk '{for(i = 2; i <= NF; i++) {cpu += $i} printf "%d\n", cpu}'

/proc/<pid>/stat

1
cat /proc/<pid>/stat | awk '{printf "%d\n", $14 + $15 + $16 + $17}'

$$
\mathrm{cpu\ utilization} = \frac{c2 - c1}{t2 - t1}
$$

参考资料

1.https://www.kernel.org/doc/html/latest/filesystems/proc.html#miscellaneous-kernel-statistics-in-proc-stat


----------本文结束感谢您的阅读----------
坚持原创技术分享,您的支持将鼓励我继续创作!