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.