博客园  :: 首页  :: 新随笔  :: 联系 :: 订阅 订阅  :: 管理

proc stat

Posted on 2017-07-05 11:06  bw_0927  阅读(116)  评论(0)    收藏  举报

http://blog.csdn.net/tenfyguo/article/details/7476306

 

/proc/cpuinfo文件

说明:以下只解释对我们计算Cpu使用率有用的相关参数。

         参数                               解释

processor (0)                       cpu的一个物理标识

结论1:可以通过该文件根据processor出现的次数统计cpu的逻辑个数(包括多核、超线程)。

CPU_Num=$(cat /proc/cpuinfo | grep 'processor' | wc | awk '{print $1}')
echo $CPU_Num

 

/proc/stat文件

         该文件包含了所有CPU活动的信息,该文件中的所有值都是从系统启动开始累计到当前时刻。不同内核版本中该文件的格式可能不大一致,以下通过实例来说明数据该文件中各字段的含义。

实例数据:2.6.24-24版本上的

 

fjzag@fjzag-desktop:~$cat /proc/stat

cpu 38082 627 27594 89390812256 581 895 0 0

cpu022880 472 16855 430287 10617 576 661 0 0

cpu115202 154 10739 463620 1639 4 234 0 0

intr120053 222 2686 0 1 1 0 5 0 3 0 0 0 47302 0 0 34194 29775 0 5019 845 0 0 0 0 00 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 00 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 00 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 00 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 00 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

ctxt1434984

btime1252028243

processes8113

procs_running1

procs_blocked0

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

参数         解析(单位:jiffies)

(jiffies是内核中的一个全局变量,用来记录自系统启动一来产生的节拍数,在Linux中,一个节拍大致可理解为操作系统进程调度的最小时间片,不同linux内核可能值有不同,通常在1ms到10ms之间)

user (38082)      从系统启动开始累计到当前时刻,处于用户态的运行时间,不包含 nice值为负的进程。

system (27594) 从系统启动开始累计到当前时刻,处于核心态的运行时间。

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

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

iowait (12256)  从系统启动开始累计到当前时刻,IO等待时间(since 2.5.41)。

irq (581)             从系统启动开始累计到当前时刻,硬中断时间(since 2.6.0-test4)。

softirq (895)     从系统启动开始累计到当前时刻,软中断时间(since2.6.0-test4)

stealstolen(0)    which is the time spent in otheroperating systems when running in a virtualized environment(since 2.6.11)。

guest(0)              whichis the time spent running a virtual CPU  for  guest operating systems under the control ofthe Linux kernel(since 2.6.24)。

 

结论2

1,总的cpu时间totalCpuTime = user + nice+ system + idle + iowait + irq + softirq + stealstolen +  guest;

2,cpu使用率的计算:

    • 通过/proc/stat文件采集并计算CPU总使用率及单个核使用率。以cpu0为例,算法如下:
    • 1. cat /proc/stat | grep ‘cpu0’得到cpu0的信息
    • 2. cpuTotal1=user+nice+system+idle+iowait+irq+softirq
    • 3. cpuUsed1=user+nice+system+irq+softirq
    • 4. sleep 15秒
    • 5. 再次cat /proc/stat | grep ‘cpu0’得到cpu的信息
    • 6. cpuTotal2=user+nice+system+idle+iowait+irq+softirq
    • 7. cpuUsed2=user+nice+system+irq+softirq
    • 8. 得到cpu0 在15秒内的单核利用率:(cpuUsed2 – cpuUsed1) * 100 / (cpuTotal2 – cpuTotal1)   //百分数
    • 相当于使用top –d 15命令,把user、nice、system、irq、softirq五项的使用率相加。

 

主要问题:

1.      不同内核版本/proc/stat文件格式不大一致。/proc/stat文件中第一行为总的cpu使用情况。

各个版本都有的4个字段: user、nice、system、idle

2.5.41版本新增字段:iowait

2.6.0-test4新增字段:irq、softirq

2.6.11新增字段:stealstolen:                    which is thetime spent in other operatingsystems whenrunning in a virtualized environment

2.6.24新增字段:guest:                              whichis the time spent running a virtual CPU  for  guest operating systems under the control ofthe Linux kernel

2./proc/pid/task目录是Linux 2.6.0-test6之后才有的功能。 

3.关于出现cpu使用率为负的情况,目前想到的解决方案是如果出现负值,连续采样计算cpu使用率直到为非负。 

4.有些线程生命周期较短,可能在我们采样期间就已经死掉了.