美文网首页
cpu使用率分析 2022-10-20

cpu使用率分析 2022-10-20

作者: 9_SooHyun | 来源:发表于2022-10-19 19:28 被阅读0次

参考 https://zhuanlan.zhihu.com/p/479517115 https://www.idnt.net/en-US/kb/941772

cpu信息是如何统计的

统计CPU使用情况是在 时钟中断处理程序 中完成的

如何查看cpu信息

Linux的CPU使用率信息可以通过 /proc/stat 文件计算得到。/proc/stat 包含了所有CPU活动的信息,该文件中所有值都从系统启动开始累计,单位为jiffies

jiffies代表时间。它的长度随硬件平台的不同而不同
系统里定义了一个常数HZ,代表每秒最小时间间隔的数目。jiffies=1/HZ (s)。Intel平台jiffies是1/100秒,这就是系统所能分辨的最小时间间隔了。每个CPU时间片,jiffies都要加1
CPU的利用率就是用执行用户态+系统态的jiffies除以总的jifffies表示

[root@VM-165-116-centos ~]# cat /proc/stat | grep 'cpu'
cpu  944763272 3864 503300776 29969001825 6509965 0 174204777 0 0 0
cpu0 117588876 536 62824428 3736353788 907537 0 43247913 0 0 0
cpu1 116871553 383 62479216 3738905475 924950 0 34253198 0 0 0
cpu2 116449394 414 60407450 3755828232 911656 0 15593531 0 0 0
cpu3 116549849 512 62345427 3750497005 1111537 0 17713716 0 0 0
cpu4 120043262 514 63885646 3745474312 591465 0 17964564 0 0 0
cpu5 119308900 620 63792076 3747990842 538122 0 16212062 0 0 0
cpu6 119079964 490 63746220 3747124511 604055 0 15002422 0 0 0
cpu7 118871470 392 63820310 3746827657 920639 0 14217368 0 0 0
//   user      nice  system idle       iowait irq softirq steal guest ?

如何解读cat /proc/stat | grep 'cpu'

  • 当 CPU 在执行用户程序,
    • 如果进程的 nice 值小于等于0,那么增加到 CPU 统计结构的 user 字段中
    • 如果进程的 nice 值大于0,那么将会增加到 CPU 统计结构的 nice 字段中

user:从系统启动开始累计到当前时刻,用户态的CPU时间,不包含nice值为正的进程
nice:从系统启动开始累计到当前时刻,nice值为正的进程所占用的CPU时间

"nice" generally refers to to the priority of a process. (More positive values are lower priority, more negative values are higher priority). On a CPU utilization graph or report, the "nice" CPU percentage is the % of CPU time occupied by user level processes with a positive nice value (lower scheduling priority -- see man nice for details). man nice for more.

  • 当 CPU 在执行内核程序,
    system:从系统启动开始累计到当前时刻,内核态时间
    irq:从系统启动开始累计到当前时刻,硬中断时间。Time spent serving hardware interrupts. See the description of the intr line for more details. since 2.6.0
    softirq:从系统启动开始累计到当前时刻,软中断时间。Time spent serving software interrupts. since 2.6.0

  • 当 CPU 处于idle进程执行时间,
    idle:从系统启动开始累计到当前时刻,除IO等待时间以外其它等待时间
    iowait:从系统启动开始累计到当前时刻,IO等待时间。Time spent waiting for I/O to completed. This is considered idle time too. since 2.5.41

  • additional
    steal:Time stolen by other operating systems running in a virtual environment. since 2.6.11
    guest:Time spent for running a virtual CPU or guest OS under the control of the kernel. since 2.6.24

在Linux/Unix下,CPU利用率分为用户态,系统态和空闲态,分别表示CPU处于用户态执行的时间,系统内核执行的时间,和空闲系统进程执行的时间。平时所说的CPU利用率是指:CPU执行非系统空闲进程的时间 / CPU总的执行时间

cpu_total=(user+nice)+(system+irq+softirq)+(idle+iowait)
cpu_used=(user+nice)+(system+irq+softirq)
cpu_used_percent=cpu_used/cpu_total

相关文章

网友评论

      本文标题:cpu使用率分析 2022-10-20

      本文链接:https://www.haomeiwen.com/subject/dynuzrtx.html