Article From:https://www.cnblogs.com/lanyangsh/p/9062129.html

cpu”

ps CPU”

man pageThe definition given in this paper is:

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:The time of using CPU in the process life cycle
  • realtime:Process life cycle
  • %cpu = cputime/realtime*100, It’s impossible to reach 100%

top CPU”

man page Definition:

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.

By default, 3S refreshes the screen once, that is, the use of CPU in 3S.

psStarting from the process starts, the average occupancy rate is calculated, and top is counted from the last refresh. Usually a few seconds can be considered as instantaneous. Moreover, the sum of occupancy rate of CPU by default is not 100%, but the x100% of top, so sometimes a process occupies more than100%.

topThe%CPU field in the command indicates the ratio of the CPU time used by a process to the total CPU time (that is the interval time) in a fixed interval. [the meaning of the CPU field in the resource manager of the Window operating system is the same].

process proc parameter definition

$ 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

Among them, 14,15,16.17, the 21 meanings are as follows:

  • utime=1587 The operation time of the task in user state is jiffies.
  • stime=1 The duration of the task in nuclear psychology is jiffies.
  • cutime=0 The cumulative waited-for time of the task has been running in user state at jiffies.
    (The time consumed by the user state of the subprocess)
  • cstime=0 The cumulative waited-for time of the task has been running in the nuclear state of mind at jiffies.
    (The time of the kernel state consumption of the subprocess

  • start_time The start time of the process (relative to the boot time of the system) is jiffies.

ps The time field in the command represents the CPU time,

$ ps –p 1222 –otime

How does this time be calculated?

By comparing the data with /proc/pid/stat, we can get the CPU time of PS.

time=utime + systime

Then the%CPU=time/ process lifecycle of PS.

cpu_time”

Usually, the process takes up CPU time cpu_time in two ways:

A way

utime+stime+uctime+sctime

The 4 parameters can be obtained as follows:

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

That is,

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

Another way
Only consider the use time of the process itself, regardless of the use time of the sub processes.

utime+stime

That is,

cpu_time = (utime+stime)/HZ

HZ”

HZThe number of 1s vibration (tick number).

You can use the command to return the number of ticks per second HZ

$ getconf CLK_TCK

The total answer divided by HZ is the time in seconds.

reference

/proc/pid/statMeaning of each field
https://www.linuxidc.com/Linux/2010-12/30589.htm

How to get the time that the process has run
http://smilejay.com/2012/05/get_process_time/

ps And the CPU of the top command
https://unix.stackexchange.com/questions/58539/top-and-ps-not-showing-the-same-cpu-result

cpuOccupancy rate
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

Leave a Reply

Your email address will not be published. Required fields are marked *