Description of % CPU in PS command in LinuxShell

Source: Internet
Author: User
Article Title: Description of % CPU in PS command in LinuxShell. Linux is a technology channel of the IT lab in China. Includes basic categories such as desktop applications, Linux system management, kernel research, embedded systems, and open source.

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 theprocess has been running (cputime/realtime ratio),expressed as a percentage. It will not add up to 100%unless you are lucky. (alias pcpu).
     

This is the case in ps code.

  

     
      static int pr_pcpu(char *restrict const outbuf, const proc_t *restrict const pp){unsigned long long total_time; /* jiffies used by this process */unsigned pcpu = 0; /* scaled %cpu, 999 means 99.9% */unsigned long 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);}
     

 

Here, seconds_since_boot is obtained by subtracting the current time from the system start time, And the start time is obtained by reading the btime in/proc/stat. 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 the ratio of the running time of the process to T from the process being created to the execution of ps operations.

If the include_dead_children option is specified in ps, the time of the process also includes the running time of the process it created but has already died, cutime and cstime call wait_task_zombie to accumulate when the parent process is a child process. For example, execute updatedb in bash and run

Ps-eo pcpu, comm, stat, pid | grep bash

And

Ps-eo pcpu, comm, stat, pid | grep bash

The percentage of the latter is greater.

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.