How to check the CPU and memory usage of Linux on SAIL-IMX6
How to check the memory occupancy under Linux method 1:
We can use a combination of the following commands to do this. In this method, we use a combination of free and awk commands to obtain memory usage.
To get the memory usage that does not contain a percentage symbol:
# free-t | awk'NR = = 2 {print "CurrentMemory Utilization is:" $3 Universe 2100}'
Or
# free-t | awk 'FNR = = 2 {print "Current Memory Utilization is:" $3 Universe 2100}'
Current Memory Utilization is: 18.0433
To get the memory usage that contains the percentage symbol and retains two decimal places:
# free-t | awk'NR = 2 {printf ("Current Memory Utilization is:% .2f%"), $3pm
Or
# free-t | awk 'FNR = 2 {printf ("Current Memory Utilization is:% .2f%"), $3pm
Current Memory Utilization is: 18.06%
Typing the free command will better explain:
Root@imx6qdlsolo:~# free
Total used free shared buff/cache available
Mem: 1024780 185096 783012 1036 56672 769256
Swap: 0 0 0
Details are as follows:
Free: is a standard command for viewing memory usage under Linux.
Awk: a powerful command specifically for text data processing.
FNR = = 2: this command gives the number of lines for each input file. It is basically used to pick out a given row (for this case, it selects the line with line number 2)
NR = = 2: this command gives the total number of rows processed. It is basically used to filter the given row (for this case, it selects the line with line number 2)
$3Universe 100: this command divides column 3 by column 2 and multiplies the result by 100.
Printf: this command is used to format and print data.
% .2f%: by default, it retains a floating-point number of 6 places after printing the decimal point. Use the following format to constrain decimal places.
How to check the memory occupancy rate under Linux method 2:
We can use a combination of the following commands to do this. In this approach, we use a combination of free, grep, and awk commands to obtain memory usage.
To get the memory usage that does not contain a percentage symbol:
# free-t | grep Mem | awk'{print "Current Memory Utilization is:" $3max 2mm 100}'
Current Memory Utilization is: 18.071
To get the memory usage that contains the percentage symbol and retains two decimal places:
Free-t | grep Mem | awk' {printf ("Current Memory Utilization is:% .2f%"), $3prime
Current Memory Utilization is: 18.07%
How to check the occupancy rate of CPU under Linux method 1:
We can do this using a combination of the following commands. In this approach, we use a combination of top, print, and awk commands to obtain CPU occupancy.
If you are showing multiple CPU in the output, you need to use the following method.
# top-b-N1 | grep ^% Cpu
% Cpu (s): 0.1 us, 0.4 sy, 0.0 ni, 99.4 id, 0.0 wa, 0.0 hi, 0.0 si, 0.0 st
To get the CPU occupancy that does not contain a percentage symbol:
# top-b-N1 | grep ^% Cpu | awk' {cpu+=$9} END {print "Current CPU Utilization is:" 100-cpu/NR}'
Current CPU Utilization is: 100
To get the CPU occupancy that contains the percentage sign and retains two decimal places:
# top-b-N1 | grep ^% Cpu | awk' {cpu+=$9} END {printf ("Current CPU Utilization is:% .2f%"), 100-cpu/NR}'
Current CPU Utilization is: 100.005%
How to check the occupancy rate of CPU under Linux method 2:
We can do this using a combination of the following commands. In this approach, we use a combination of top, print/printf, and awk commands to obtain CPU occupancy.
If all the CPU cases are shown together in a single output, then you need to use the following method.
# top-b-N1 | grep ^% Cpu
% Cpu (s): 0.1 us, 0.4 sy, 0.0 ni, 99.5 id, 0.0 wa, 0.0 hi, 0.0 si, 0.0 st
To get the CPU occupancy that does not contain a percentage symbol:
# top-b-N1 | grep ^% Cpu | awk'{print "Current CPU Utilization is:" 100Mexico 8}'
Current CPU Utilization is: 0.5
To get the CPU occupancy that contains the percentage sign and retains two decimal places:
# top-b-N1 | grep ^% Cpu | awk' {printf ("Current CPU Utilization is:% .2f%"), 100ml} 8}'
Current CPU Utilization is: 0.50%
Here are some details:
Top: a very good command for viewing processes running on the current Linux system.
-b: option allows the top command to switch to batch mode. It is very useful when you run the top command from the local system to the remote system.
-N1: iterations.
^% Cpu: filter lines that start with% CPU.
Awk: a powerful command specifically used for text data processing.
Cpu+=$9: for each row, add column 9 to the variable cpu.
Printf: this command is used to format and print data.
% .2f%: by default, it retains a floating point of 6 places after printing the decimal point. Use the following format to limit the number of decimal places.
100-cpu/NR: finally prints out the average occupancy of CPU, which is subtracted by 100 and divided by the number of lines.
I believe that friends must have mastered several methods, if there are other convenient ways, you can communicate together in the comment area.