ps 和 top 的cpu的区别

cpu的计算

ps cpu的定义

man page中给出的定义:

cpu utilization of the process in "##.#" format. Currently, it is the CPU time used divided by the time the process has been running (cputime/realtime ratio), expressed as a percentage. It will not add up to 100% unless you are lucky. (alias pcpu).

  • cputime:进程生命周期中使用cpu的时间
  • realtime:进程生命周期
  • %cpu = cputime/realtime*100, 不可能达到100%

top cpu的定义

man page 定义:

The task's share of the elapsed CPU time since the last screen update, expressed as a percentage of total CPU time. In a true SMP environment, if 'Irix mode' is Off, top will operate in 'Solaris mode' where a task's cpu usage will be divided by the total number of CPUs. You toggle 'Irix/Solaris' modes with the 'I' interactive command.

默认情况下是3s刷新一次屏幕,也就是3s内的cpu使用情况。

ps是从进程启动就开始计算,是平均的占用率;而top是从上次刷新开始算的,一般几秒钟一刷,可以认为是即时的。而且top默认cpu的占用率的和并不是100%,而是核数x100%,所以有时会有一个进程占用超过100%的情况。

top命令中的%CPU字段表示:在一个固定的间隔时间内,某个进程使用的CPU时间占总CPU时间(即这段间隔时间)的比值。[在Window操作系统下的资源管理器中的CPU字段含义也是如此]

手动计算top命令中的%CPU字段

  • 利用ps
    确定一个间隔时间,在间隔时间的开始处,执行ps命令,获取某个进程在开始处已经使用的CPU时间;在间隔时间的结束处,执行ps命令,获取某个进程在结束处已经使用的CPU时间。
    间隔时间内进程使用的CPU时间=结束处使用的CPU时间-开始处使用的CPU时间
    %CPU=间隔时间内进程使用的CPU时间*100/CPU总时间(即间隔时间长度)
  • 利用/proc下的数据
    ps命令的数据来自于/proc目录下的文件,因而如果直接使用/proc下的数据也是可以实现“手动计算top命令中的%CPU字段”的目标的

进程proc参数定义

$ cat /proc/28433/stat
28433 
(happy-agent) 
S 
1 ppid
28395 pgrp
28395 session
0 tty
-1 tpgid
4202496 
80653 
91801047 
0 
0 
96 utime
94 ctime
2932 uctime
4477 sctime
20 
0 
10 
0 
2219421267   start time 
181817344 1887 18446744073709551615 1 1 0 0 0 0 0 3 2143420156 18446744073709551615 0 0 17 31 0 0 0 
0 
0

其中,第14,15,16.17, 21个参含义如下:

  • utime=1587 该任务在用户态运行的时间,单位为jiffies
  • stime=1 该任务在核心态运行的时间,单位为jiffies
  • cutime=0 累计的该任务的所有的waited-for进程曾经在用户态运行的时间,单位为jiffies
    (子进程用户态消耗的时间)
  • cstime=0 累计的该任务的所有的waited-for进程曾经在核心态运行的时间,单位为jiffies
    (子进程内核态消耗的时间)

  • start_time 进程的启动时间(相对于系统开机时刻), 单位为jiffies

ps 命令中的time字段表示占用cpu时间,

$ ps –p 1222 –otime

这个时间是如何计算的呢?

通过与/proc/pid/stat中数据的比对,可以得出ps的cpu时间

time=utime + systime

则ps的%CPU=time/进程生命周期。

使用proc数据计算进程CPU百分比

计算公式

%CPU=cpu_time/interval

cpu_time的计算

通常,进程占用cpu时间cpu_time有两种方式:

一种方式

utime+stime+uctime+sctime

这个4个参数可以通过如下方式得到:

$ cat /proc/28433/stat | cut -d" " -f 14,15,16,17
498 402 12752 19498

cpu_time = (utime+stime+uctime+sctime)/HZ

另一种方式
只考虑进程自身的使用时间,不考虑子进程的使用时间。

utime+stime

cpu_time = (utime+stime)/HZ

HZ的计算

HZ表示1s 振动次数(滴答数)。

可使用命令返回每秒钟的滴答数HZ

$ getconf CLK_TCK

总滴答数除以HZ就是,就是以秒为单位的时间

时间间隔interval的计算

可以使用开机启动时间计算间隔:

interval = uptime1 – uptime2

而系统启动时间,单位就是秒

$ cat /proc/uptime  | cut -d" " -f 1
22289498.57

参考

/proc/pid/stat各字段含义
https://www.linuxidc.com/Linux/2010-12/30589.htm

如何获取进程已经运行的时间
http://smilejay.com/2012/05/get_process_time/

ps 和top命令的cpu
https://unix.stackexchange.com/questions/58539/top-and-ps-not-showing-the-same-cpu-result

cpu占用率
http://www.samirchen.com/linux-cpu-performance/

https://stackoverflow.com/questions/16726779/how-do-i-get-the-total-cpu-usage-of-an-application-from-proc-pid-stat

https://segmentfault.com/a/1190000008567845

猜你喜欢

转载自www.cnblogs.com/lanyangsh/p/9062129.html
今日推荐