proc文件系统
/proc文件系统是一个伪文件系统,它只存在内存当中,而不占用外存空间。它以文件系统的方式为内核与进程提供通信的接口。用户和应用程序可以通过/proc得到系统的信息,并可以改变内核的某些参数。由于系统的信息,如进程,是动态改变的,所以用户或应用程序读取/proc目录中的文件时,proc文件系统是动态从系统内核读出所需信息并提交的。
/proc目录中有一些以数字命名的目录,它们是进程目录。系统中当前运行的每一个进程在/proc下都对应一个以进程号为目录名的目录/proc/pid,它们是读取进程信息的接口。此外,在Linux 2.6.0-test6以上的版本中/proc/pid目录中有一个task目录,/proc/pid/task目录中也有一些以该进程所拥有的线程的线程号命名的目录/proc/pid/task/tid,它们是读取线程信息的接口。
cat /proc/stat
cpu 57549862 34941 89952946 3407364372 431719 0 276431 0 0 0
cpu0 2808675 2844 4922495 101984525 127137 0 31830 0 0 0
cpu1 2939852 900 5343730 102187826 9503 0 13459 0 0 0
cpu2 3028333 950 5421249 102120648 8609 0 10052 0 0 0
cpu3 3206453 1798 5652116 101699634 7545 0 15362 0 0 0
cpu4 2948933 1814 4649947 101576634 7481 0 67787 0 0 0
输出解释
cpu[]
CPU 以及CPU0、CPU1每行的每个参数意思(以第一行为例)为:
user (57549862 ) 从系统启动开始累计到当前时刻,用户态的CPU时间(单位:jiffies) ,不包含 nice值为负进程。1jiffies=0.01秒
nice (34941 ) 从系统启动开始累计到当前时刻,nice值为负的进程所占用的CPU时间(单位:jiffies)
system (89952946 ) 从系统启动开始累计到当前时刻,核心时间(单位:jiffies)
idle (3407364372 ) 从系统启动开始累计到当前时刻,除硬盘IO等待时间以外其它等待时间(单位:jiffies)
iowait (431719 ) 从系统启动开始累计到当前时刻,硬盘IO等待时间(单位:jiffies) ,
irq (0 ) 从系统启动开始累计到当前时刻,硬中断时间(单位:jiffies)
softirq (276431 ) 从系统启动开始累计到当前时刻,软中断时间(单位:jiffies)
stealstolen(0) which is the time spent in other operating systems when running in a virtualized environment(since 2.6.11)
guest(0) which is the time spent running a virtual CPU for guest operating systems under the control of the Linux kernel(since 2.6.24)
CPU 总时间totalCpuTime = user + nice + system + idle + iowait + irq + softirq + stealstolen +guest
CPU在t1到t2时间段总的使用时间 = ( user2+ nice2+ system2+ idle2+ iowait2+ irq2+ softirq2) - ( user1+ nice1+ system1+ idle1+ iowait1+ irq1+ softirq1)
CPU在t1到t2时间段空闲使用时间 = (idle2 - idle1)
CPU在t1到t2时间段即时利用率 = 1 - CPU空闲使用时间 / CPU总的使用时间