Introduction to % CPU in the ps command in Linux Shell

Source: Internet
Author: User

In the ps command, % CPU refers to the percentage of CPU usage time of a process. What does it mean? The man manual of PS is explained as follows:

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 ).
PS code is handled as follows:
Static int pr_pcpu (char * restrict const outbuf, const proc_t * restrict const pp) {unsigned long total_time;/* jiffies used by this process */unsigned pcpu = 0; /* scaled % CPU, 999 means 99.9% */unsigned long seconds;/* seconds of process life */total_time = PP-> utime + PP-> stime; If (include_dead_children) total_time + = (PP-> cutime + PP-> cstime); seconds = seconds_since_boot-PP-> start_time/Hertz; If (seconds) pcpu = (total_time * 1000ull/hertz) /seconds; If (pcpu> 999u) return snprintf (outbuf, colwid, "% u", pcpu/10u); Return snprintf (outbuf, colwid, "% u. % u ", pcpu/10u, pcpu % 10u );}

Where
Seconds_since_boot is obtained by subtracting the current time from the system start time. the start time is obtained by reading the btime in/proc/STAT. While
Start_time is set when a process is fork. In addition, the process time includes the user-mode running time and kernel-mode running time. In this way, the percentage indicates that the process is created to execute
The ratio of the running time of the process to that of the process within the period t during which the PS operation is performed.


If the include_dead_children option is specified in PS
Including the running time of the created but dead processes, cutime and cstime will call wait_task_zombie to accumulate when the parent process is collecting dead processes for the child process. Ratio
For example, if updatedb is executed in bash, run

PS-EO pcpu, comm, stat, PID | grep bash

And

PS-EO pcpu, comm, stat, PID | grep bash

 


Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.