ps 计算CPU利用率原理

ps -o pcpu  pid    可以查询进程pid所占用的CPU利用率。

 

top 计算CPU的利用率:

top计算进程cpu利用率:  http://www.samirchen.com/linux-cpu-performance/
         cat /proc/stat   //查看总的CPU情况
          cat /proc/[pid]/stat   //查看进程的CPU情况
         
CPUTime1 = user1 + nice1 + system1 + idle1 + iowait1 + irq1 + softirq1 + stealstolen1 + guest1;
CPUTime2 = user2 + nice2 + system2 + idle2 + iowait2 + irq2 + softirq2 + stealstolen2 + guest2;
totalCPUTime = CPUTime2 – CPUTime1;        
processTime1 = utime1 + stime1 + cutime1 + cstime1;
processTime2 = utime2 + stime2 + cutime1 + cstime2;
processTime = processTime2 – processTime1;
processCPUUse = processTime / totalCPUTime;

 

ps 计算CPU利用率是通过读取进程的统计数据计算出来的。

总的公式:源码参考自procps-3.2.7/ps/output.c : pr_pcpu() 

进程CPU利用率=  进程在CPU上的运行时间 / 进程运行经过的系统时间。

   进程运行经过的时间= 系统当前时间 - 进程启动时的时间

系统当前时间= uptime,   读/proc/uptime

进程启动时的时间= start_time , 读/proc/pid/stat 

进程在CPU上运行的时间= utime+stime+cutime+cstime;  读/proc/pid/stat         

(注:/proc/pid/stat下的 start_time, utime,stime, cutime,cstime的单位不是秒,要转化成秒,需要除以 sysconf(_SC_CLK_TCK))

下面是详细说明:

1,/proc/[pid]/stat   -- 进程的统计信息。

cat /proc/16891/stat
16891 (cpu) R 16783 16891 16783 34817 18308 4202496 206 0  0  0 837758 10  0  0  20 0  1 0  154386088 3805184 89 2628956160 4194304 4196196 140734028402976 18446744073709551615 4195742 0  0 0 0 0 0 0 17 4 0 0 0 0 0
(详细格式可以man proc 找到)
1,pid %d      进程(包括轻量级进程,即线程)号
2,comm %s     应用程序或命令的名字。
3,state %c    任务的状态  "RSDZTW": R is running,
                            S : 可中断的睡眠状态 , 
 D :不可中断睡眠(等IO),  
 Z : zombie, 
 T :is traced or stopped (on a signal), 
 W :is paging.  
4,ppid %d     父进程ID。
5,pgrp %d     进程组ID。
6,session %d  该任务所在的会话组 ID。
7,tty_nr %d   该任务的 tty 终端的设备号,INT(34817/256)= 主设备号,(34817-主设备号)= 次设备号。
The controlling  terminal  of  the  process.   (   主设备号为8到15bit, &0xff00, 次设备号0到7bit,20到31bit. &0xffff00ff
The minor device  number  is contained in the combination of bits 31
                          to 20 and 7 to 0; the major device number is in bits 15 t0 8.)
8,tpgid %d    终端的进程组号,当前运行在该任务所在终端的前台任务(包括 shell 应用程序)的 PID。
The  ID of the foreground process group of the controlling
                          terminal of the process.


9,flags %u (%lu before Linux 2.6.22)   进程标志位,查看该任务的特性
                           For  bit  meanings,see  the  PF_* defines in <linux/sched.h>.  

10,minflt %lu  该任务不需要从硬盘拷数据而发生的缺页(次缺页)的次数
The number of minor faults the process has made which have
                          not required loading a memory page from disk.


11,cminflt %lu 累计的该任务的所有的 waited-for 进程曾经发生的次缺页的次数目
The  number  of minor faults that the process's waited-for
                          children have made.


12,majflt %lu  The number of major faults the process has made which have required loading a memory page from disk.


13,cmajflt %lu The  number  of major faults that the process's waited-for children have made.


14,utime %lu   进程在用户态运行的时间,单位是clock(除以sysconf(_SC_CLK_TCK),转成秒)  
Amount of time that this process  has  been  scheduled  in
                          user   mode,   measured   in   clock   ticks   (divide  by
                          sysconf(_SC_CLK_TCK).    This   includes    guest    time,
                          guest_time  (time spent running a virtual CPU, see below),
                          so that applications that are not aware of the guest  time
                          field do not lose that time from their calculations.


15,stime %lu  进程在内核态运行的时间,单位是clock,   

16,cutime %ld  
      Amount  of  time  that  this process's waited-for children
                          have been scheduled in user mode, measured in clock  ticks
                          (divide  by  sysconf(_SC_CLK_TCK).   (See  also times(2).)
                          This includes guest time, cguest_time (time spent  running
                          a virtual CPU, see below).
17,cstime %ld  Amount  of  time  that  this process's waited-for children
                          have been scheduled in  kernel  mode,  measured  in  clock
                          ticks (divide by sysconf(_SC_CLK_TCK).

18,priority %ld
19, nice %ld
20,  num_thread %ld
21,  itrealvalue %ld
22, starttime %llu (was %lu before Linux 2.6)
...

 

 

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值