用/proc/stat计算cpu的占用率

<!-- /* Font Definitions */ @font-face {font-family:宋体; panose-1:2 1 6 0 3 1 1 1 1 1; mso-font-alt:SimSun; mso-font-charset:134; mso-generic-font-family:auto; mso-font-pitch:variable; mso-font-signature:3 135135232 16 0 262145 0;} @font-face {font-family:"/@宋体"; panose-1:2 1 6 0 3 1 1 1 1 1; mso-font-charset:134; mso-generic-font-family:auto; mso-font-pitch:variable; mso-font-signature:3 135135232 16 0 262145 0;} /* Style Definitions */ p.MsoNormal, li.MsoNormal, div.MsoNormal {mso-style-parent:""; margin:0cm; margin-bottom:.0001pt; text-align:justify; text-justify:inter-ideograph; mso-pagination:none; font-size:10.5pt; mso-bidi-font-size:12.0pt; font-family:"Times New Roman"; mso-fareast-font-family:宋体; mso-font-kerning:1.0pt;} a:link, span.MsoHyperlink {color:blue; text-decoration:underline; text-underline:single;} a:visited, span.MsoHyperlinkFollowed {color:purple; text-decoration:underline; text-underline:single;} pre {margin:0cm; margin-bottom:.0001pt; mso-pagination:widow-orphan; font-size:12.0pt; font-family:宋体; mso-bidi-font-family:宋体;} /* Page Definitions */ @page {mso-page-border-surround-header:no; mso-page-border-surround-footer:no;} @page Section1 {size:595.3pt 841.9pt; margin:72.0pt 90.0pt 72.0pt 90.0pt; mso-header-margin:42.55pt; mso-footer-margin:49.6pt; mso-paper-source:0; layout-grid:15.6pt;} div.Section1 {page:Section1;} -->


http://blog.csdn.net/nineday/archive/2007/12/11/1928847.aspx

Linux下,CPU利用率分为用户态,系统态和空闲态,分别表示CPU处于用户态执行的时间,系统内核执行的时间,和空闲系统进程执行的时间,三者之和就是CPU的总时间,当没有用户进程、系统进程等需要执行的时候,CPU就执行系统缺省的空闲进程。从平常的思维方式理解的话,CPU的利用率就是非空闲进程占用时间的比例,即CPU执行非空闲进程的时间 / CPU总的执行时间。

Linux系统中,CPU时间的分配信息保存在/proc/stat文件中,利用率的计算应该从这个文件中获取数据。文件的头几行记录了每个CPU的用户态,系统态,空闲态等状态下分配的时间片(单位是Jiffies),这些数据是从CPU加电到当前的累计值。常用的监控软件就是利用/proc/stat里面的这些数据来计算CPU的利用率的。

不同版本的linux /proc/stat文件内容不一样,以Linux 2.6来说,/proc/stat文件的内容如下:

 

cpu 2032004 102648 238344 167130733 758440 15159 17878 0

cpu0 1022597 63462 141826 83528451 366530 9362 15386 0

cpu1 1009407 39185 96518 83602282 391909 5796 2492 0

intr 303194010 212852371 3 0 0 11 0 0 2 1 1 0 0 3 0 11097365 0 72615114 6628960 0 179 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0

ctxt 236095529

btime 1195210746

processes 401389

procs_running 1

procs_blocked 0

 

第一行的数值表示的是CPU总的使用情况,所以我们只要用第一行的数字计算就可以了。下表解析第一行各数值的含义:

参数

解析(单位:jiffies

user (2032004)

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

nice (102648)

从系统启动开始累计到当前时刻,nice值为负的进程所占用的CPU时间

system (238344)

从系统启动开始累计到当前时刻,核心时间

idle (167130733)

从系统启动开始累计到当前时刻,除IO等待时间以外其它等待时间

iowait (758440)

从系统启动开始累计到当前时刻,IO等待时间

irq (15159)

从系统启动开始累计到当前时刻,硬中断时间

softirq (17878)

从系统启动开始累计到当前时刻,软中断时间

 

 

因为/proc/stat中的数值都是从系统启动开始累计到当前时刻的积累值,所以需要在不同时间点t1t2取值进行比较运算,当两个时间点的间隔较短时,就可以把这个计算结果看作是CPU的即时利用率。

 

CPU的即时利用率的计算公式:

CPUt1t2时间段总的使用时间 = ( user2+ nice2+ system2+ idle2+ iowait2+ irq2+ softirq2) - ( user1+ nice1+ system1+ idle1+ iowait1+ irq1+ softirq1)

CPUt1t2时间段空闲使用时间 = (idle2 - idle1)

CPUt1t2时间段即时利用率 =  1 - CPU空闲使用时间 / CPU总的使用时间

 

这些值是谁,什么时候记录的呢?

每次timer的中断就会记录一次,记录在struct cpu_usage_stat 里,实现在timer_tick ->update_process_times里。
那么它的精度就是HZ,如果HZ100,就意味着每S记录100次。这个精度当然是不高的,而且容易出错,下面是在Documentation/cpu-load.txt中的一个例子:
  time line between two timer interrupts
 |--------------------------------------|
 ^                                    ^
 |_ user appA begins working          |
                                      |_ user appA goes to sleep
结果这个A的动作没有被记录下来,这一S有可能被记录到其他的头上。如果你做的程序正好是那个其他,你就会抱怨说,这真是一陀屎呀。
那么有没有高精度的记录呢?
有,但是要自己写,就算你用oprofile之类的,他的原理也是用timer_interrupt记录的,你可以用其他的高精度timer,但是,频繁的中断会把系统弄死。所以要自己写,假设有一个高精度的硬件counter,好像x86下的TimeStamp Counter
cpu_idle 里记录idle的时间,在asm_do_IRQ里记录处理irq的时间,在context_switch记录进入了那个process,以及时间,在__do_softirq里记录处理softirq的时间,把这些东西记录在一块全局数组里。
 

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值