How to view the CPU and MEM information of a process? If the process has been running for a long time, how can we determine the real CPU utilization? Then ps and top commands can properly solve your problem.
** 1. ps command**
Let's first look at the ps command. You can view the CPU, MEM and other information of the process through ps -aux:
USER PID %CPU %MEM VSZ RSS TTY STAT START TIME COMMANDroot 1 0.0 0.3 191808 3764 ? Ss 2018 2:44 /usr/lib/systemd/systemd --switched-root --system --deserialize 21root 2 0.0 0.0 0 0 ? S 2018 0:02 [kthreadd]root 3 0.0 0.0 0 0 ? S 2018 5:23 [ksoftirqd/0]root 5 0.0 0.0 0 0 ? S< 2018 0:00 [kworker/0:0H]root 7 0.0 0.0 0 0 ? S 2018 0:00 [migration/0]root 22056 0.1 2.0 750304 20340 ? Ssl 9 July 3:07 /usr/local/cloudmonitor/CmsGoAgent.linux-amd64......#The following contents are omitted Here we take it alone PID For 22056 CmsGoAgent Process CPU and MEM Information. Command: ps -o %cpu %,mem -p 22056
[16210504@izuf60jasqavbxb9efockpz ~]$ ps -o %cpu,%mem -p 22056%CPU %MEM 0.1 2.0
It can be seen from the above results that the result is successfully taken out, but the result is actually inaccurate. The instructions for viewing ps statistics by using the man ps command are as follows:
%cpu %CPU 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). %mem %MEM ratio of the process's resident set size to the physical memory on the machine, expressed as a percentage. (alias pmem).
We can see that the ps command counts the CPU by dividing the CPU usage time by the total running time (cputime/realtime)
ratio), which may lead to high CPU utilization at a certain time. However, because the process has been running for a long time, the actual calculated value is still very small, which affects the judgment of the result.
** 2. top command**
Usually, we focus on the continuous changes of CPU, that is, we need the slice information of CPU. At this time, if we want timely statistics, we need to use the top command:
[16210504@izuf60jasqavbxb9efockpz ~]$ toptop - 23:03:54 up 316 days, 13:52, 13 users, load average: 0.00, 0.01, 0.05Tasks: 159 total, 1 running, 113 sleeping, 45 stopped, 0 zombie%Cpu(s): 0.3 us, 0.7 sy, 0.0 ni, 99.0 id, 0.0 wa, 0.0 hi, 0.0 si, 0.0 stKiB Mem : 1016396 total, 434728 free, 133020 used, 448648 buff/cacheKiB Swap: 0 total, 0 free, 0 used. 710848 avail Mem PID USER PR NI VIRT RES SHR S %CPU %MEM TIME+ COMMAND22056 root 20 0 750304 20472 5612 S 0.3 2.0 3:23.00 CmsGoAgent.linu 1 root 20 0 191808 3764 1436 S 0.0 0.4 2:44.29 systemd 2 root 20 0 0 0 0 S 0.0 0.0 0:02.10 kthreadd 3 root 20 0 0 0 0 S 0.0 0.0 5:23.84 ksoftirqd/0 5 root 0 -20 0 0 0 S 0.0 0.0 0:00.00 kworker/0:0H 7 root rt 0 0 0 0 S 0.0 0.0 0:00.00 migration/0 8 root 20 0 0 0 0 S 0.0 0.0 0:00.00 rcu_bh 9 root 20 0 0 0 0 S 0.0 0.0 42:39.12 rcu_sched
First filter out the CPU and MEM information of the CmsGoAgent process with PID 22056. Command: top -b -p 22056 -d 1
-b : Batch mode, you can export the output to the next process-p : Print a process-d 1 : Print every 1 second
[16210504@izuf60jasqavbxb9efockpz ~]$ top -b -p 22056 -d 1top - 23:14:52 up 316 days, 14:03, 10 users, load average: 0.00, 0.01, 0.05Tasks: 1 total, 0 running, 1 sleeping, 0 stopped, 0 zombie%Cpu(s): 2.5 us, 2.5 sy, 0.0 ni, 94.9 id, 0.1 wa, 0.0 hi, 0.0 si, 0.0 stKiB Mem : 1016396 total, 444484 free, 122968 used, 448944 buff/cacheKiB Swap: 0 total, 0 free, 0 used. 721340 avail Mem PID USER PR NI VIRT RES SHR S %CPU %MEM TIME+ COMMAND22056 root 20 0 750304 20472 5612 S 0.0 2.0 3:24.04 CmsGoAgent.linu top - 23:14:53 up 316 days, 14:03, 10 users, load average: 0.00, 0.01, 0.05Tasks: 1 total, 0 running, 1 sleeping, 0 stopped, 0 zombie%Cpu(s): 0.0 us, 0.0 sy, 0.0 ni,100.0 id, 0.0 wa, 0.0 hi, 0.0 si, 0.0 stKiB Mem : 1016396 total, 444484 free, 122968 used, 448944 buff/cacheKiB Swap: 0 total, 0 free, 0 used. 721340 avail Mem PID USER PR NI VIRT RES SHR S %CPU %MEM TIME+ COMMAND22056 root 20 0 750304 20472 5612 S 0.0 2.0 3:24.04 CmsGoAgent.linu...... 16210504@izuf60jasqavbxb9efockpz ~]$ top -p 22056 -d 1top - 23:18:05 up 316 days, 14:07, 10 users, load average: 0.13, 0.04, 0.05Tasks: 1 total, 0 running, 1 sleeping, 0 stopped, 0 zombie%Cpu(s): 1.0 us, 0.0 sy, 0.0 ni, 99.0 id, 0.0 wa, 0.0 hi, 0.0 si, 0.0 stKiB Mem : 1016396 total, 444360 free, 123004 used, 449032 buff/cacheKiB Swap: 0 total, 0 free, 0 used. 721304 avail Mem PID USER PR NI VIRT RES SHR S %CPU %MEM TIME+ COMMAND22056 root 20 0 750304 20472 5612 S 0.0 2.0 3:24.37 CmsGoAgent.linu ... [16210504@izuf60jasqavbxb9efockpz ~]$
example:
Use the top command to complete the requirements of performance data, CPU and MEM information:
_ Idea: Count the CPU and MEM changes of the process 10 times and output the average value.
Expected effect:
CPU MEM0.1 2.00.1 2.1... # 10 times in total-----0.13 2.4 # Here is the average _ **realization:**_
Print the process information every 1 second for a total of 10 times, and filter out the lines containing only the process information: Command: top -b -p 22056 -n 10 -d 1 | grep 22056
[16210504@izuf60jasqavbxb9efockpz ~]$ top -b -p 22056 -n 10 -d 1 | grep 2205622056 root 20 0 750304 20476 5616 S 0.0 2.0 3:26.70 CmsGoAgent.linu22056 root 20 0 750304 20476 5616 S 0.0 2.0 3:26.70 CmsGoAgent.linu22056 root 20 0 750304 20476 5616 S 0.0 2.0 3:26.70 CmsGoAgent.linu......# A total of 10 lines are printed
Use the awk command to take out the 9th and 10th information respectively, corresponding to CPU and MEM, and use the begin command to print the header information first.
Command: Top - B - P 22056 - N 10 - D 1 | grep 22056 | awk begin {print "CPU%", "MEM%"}
[16210504@izuf60jasqavbxb9efockpz ~]$ top -b -p 22056 -n 10 -d 1 | grep 22056 | awk 'BEGIN{print "CPU%","MEM%"}{print $9,$10}'CPU% MEM%
Now there is a phenomenon that the information after awk slicing is not printed in real time. After about 10 seconds, all the information is printed together as follows:
[16210504@izuf60jasqavbxb9efockpz ~]$ top -b -p 22056 -n 10 -d 1 | grep 22056 | awk 'BEGIN{print "CPU%","MEM%"}{print $9,$10}'CPU% MEM%#After 10 seconds, all messages are printed together 0.0 2.01.0 2.01.0 2.00.0 2.00.0 2.00.0 2.00.0 2.00.0 2.00.0 2.00.0 2.00.0 2.0 Here is another question: grep and awk In consideration of efficiency, a batch of data will be cached and then output to standard output, which will not be printed even if the matching conditions are met. If you want to obtain real-time information, you need to use it grep --line-buffered,send grep Output directly without caching information:
In addition, because the average value must be calculated at last, the data must be stored in a variable every time
Command: Top - B - P 22056 - N 10 - D 1 | grep 22056 -- line buffered | awk 'begin {print "CPU%", "MEM%"} {print $9, $10} {CPU + = $9; MEM + = $10}'
[16210504@izuf60jasqavbxb9efockpz ~]$ top -b -p 22056 -n 10 -d 1 | grep 22056 --line-buffered | awk 'BEGIN{print "CPU%","MEM%"}{print $9,$10}{cpu+=$9;mem+=$10}'CPU% MEM%0.0 2.00.0 2.00.0 2.01.0 2.00.0 2.00.0 2.00.0 2.00.0 2.00.0 2.00.0 2.0 Finally, we use awk of END,To output the separator, calculate the final average and print it out(Among them NR Indicates the total number of rows) Command: top -b -p 22056 -n 10 -d 1 | grep 22056 --line-buffered | awk 'BEGIN{print "CPU%","MEM%"}{print $9,$10}{cpu+=$9;mem+=$10}END{print "------";print cpu/NR,mem/NR}'
_ Final result:_
[16210504@izuf60jasqavbxb9efockpz ~]$ top -b -p 22056 -n 10 -d 1 | grep 22056 --line-buffered | awk 'BEGIN{print "CPU%","MEM%"}{print $9,$10}{cpu+=$9;mem+=$10}END{print "------";print cpu/NR,mem/NR}'CPU% MEM%0.0 2.00.0 2.00.0 2.01.0 2.00.0 2.00.0 2.00.0 2.01.0 2.00.0 2.00.0 2.0------0.2 2 You can also print the average value information and use OFS Specify how tabs format output: Command: top -b -p 22056 -n 20 -d 1 | grep --line-buffered 22056 | awk 'BEGIN{OFS="\t";print"cpu","mem","avgc","avgm"}{c+=$9;m+=$10;print $9, $10,c/NR,m/NR}'
[16210504@izuf60jasqavbxb9efockpz ~]$ top -b -p 22056 -n 20 -d 1 | grep --line-buffered 22056 | awk 'BEGIN{OFS="\t";print "cpu","mem","avgc","avgm"}{c+=$9;m+=$10;print $9,$10,c/NR,m/NR}'cpu mem avgc avgm0.0 2.0 0 20.0 2.0 0 20.0 2.0 0 20.0 2.0 0 20.0 2.0 0 20.0 2.0 0 20.0 2.0 0 20.0 2.0 0 22.0 2.0 0.222222 20.0 2.0 0.2 20.0 2.0 0.181818 20.0 2.0 0.166667 20.0 2.0 0.153846 20.0 2.0 0.142857 20.0 2.0 0.133333 20.0 2.0 0.125 20.0 2.0 0.117647 20.0 2.0 0.111111 20.0 2.0 0.105263 20.0 2.0 0.1 2 ** _3.
Come to Hogwarts test and development society to learn more advanced technologies of software testing and test development. The knowledge points include web automated testing, app automated testing, interface automated testing, test framework, performance testing, security testing, continuous integration / continuous delivery / DevOps, test left, test right, precision testing, test platform development, test management, etc, The course technology covers bash, pytest, junit, selenium, appium, postman, requests, httprunner, jmeter, jenkins, docker, k8s, elk, sonarqube, Jacobo, JVM sandbox and other related technologies, so as to comprehensively improve the technical strength of test and development engineers
QQ communication group: 484590337
The official account TestingStudio
Click for more information