top command usage

top command usage
top command is often used to monitor the system status of linux. It is a common performance analysis tool, which can display the resource occupation of each process in the system in real time.

How to use top top [-d number] | top [-bnp]

Parameter interpretation:

-d: Number represents the number of seconds and represents the interval between the page displayed by the top command to be updated once. The default is 5 seconds- b: Execute top in batch mode- n: Used in conjunction with - B, it indicates that the output result of the top command needs to be performed several times- p: Specify a specific pid process number for observation.

On the page displayed by the top command, you can also enter the following keys to perform the corresponding functions (pay attention to case sensitivity):

?: Display the commands that can be input in top P: display in order of CPU used resources M: display in order of memory used resources N: display in order of pid T: display in order of accumulated time used by the process k: give a signal to a pid. It can be used to kill the process r: re customize a nice value (i.e. priority) for a pid q: exit top (you can also exit top with ctrl+c).

Meaning of top output parameters
The following is a screenshot of performance testing using the top command:

 

 

 

 

1, top 5 lines of Statistics
Line 1: Top - 05:43:27, up 4:52, 2 users, load average: 0.58, 0.41, 0.30
The first line is the task queue information, and its parameters are as follows:

content meaning
05:43:27 Indicates the current time
up 4:52 The format of system running time is: minutes
2 users Number of currently logged in users
load average: 0.58, 0.41, 0.30 System load, that is, the average length of the task queue. The three values are the average values from 1 minute, 5 minutes and 15 minutes ago to now.
load average: if this number is divided by the number of logical CPU s, when the result is higher than 5, it indicates that the system is overloaded.

Line 2: Tasks: 159 total, 1 running, 158 sleeping, 0 stopped, 0 zombie
Line 3:% Cpu(s): 37.0 us, 3.7 sy, 0.0 ni, 59.3 id, 0.0 wa, 0.0 hi, 0.0 si, 0.0 st
Section 2 and 3 information about processes and CPU s
When there are multiple CPU s, these contents may exceed two lines. The parameters are as follows:

content meaning
159 total Total number of processes
1 running Number of running processes
158 sleeping Number of sleep processes
0 stopped Number of processes stopped
0 zombie Number of zombie processes
37.0 us Percentage of CPU occupied by user space
3.7 sy Percentage of CPU occupied by kernel space
0.0 ni Percentage of CPU used by processes with changed priority in user process space
59.3 id Percentage of idle CPU
0.0 wa Percentage of CPU time waiting for input and output
0.0 hi Percentage of CPU occupied by Hardware IRQ
0.0 si Percentage of CPU occupied by Software Interrupts
0.0 st

Line 4: KiB Mem: 1530752 total, 1481968 used, 48784 free, 70988 buffers
Line 5: KIB swap: 3905532 total, 267544 used, 3637988 free 617312 cached Mem
Memory behavior information
Its parameters are as follows:

content meaning
KiB Mem: 1530752 total Total physical memory
1481968 used Total physical memory used
48784 free Total free memory
70988 buffers(buff/cache) Amount of memory used as kernel cache
KiB Swap: 3905532 total Total exchange area
267544 used Total number of swap areas used
3637988 free k free
617312 cached Mem Total number of swap buffers.
3156100 avail Mem Represents the amount of physical memory available for the next allocation of the process
The total number of buffer swap areas mentioned last above is explained here. The so-called total number of buffer swap areas, that is, the contents in the memory are swapped out to the swap area and then swapped into the memory, but the used swap area has not been overwritten. This value is the size of the swap area in which these contents already exist in the memory. When the corresponding memory is swapped out again, there is no need to write to the swap area.

There is an approximate formula for calculating the amount of available memory:
free in line 4 + buffers in line 4 + cached in line 5

2, Process information
Listing meaning
PID Process id
PPID Parent process id
RUSER Real user name
UID User id of the process owner
USER User name of the process owner
GROUP Group name of the process owner
TTY The name of the terminal that started the process. Processes that are not started from the terminal are displayed as?
PR priority
NI nice value. Negative values indicate high priority and positive values indicate low priority
P The last CPU used only makes sense in a multi CPU environment
%CPU CPU time percentage since last update
TIME Total CPU time used by the process, in seconds
TIME+ Total CPU time used by the process, in 1 / 100 second
%MEM Percentage of physical memory used by the process
VIRT The total amount of virtual memory used by the process, in kb. VIRT=SWAP+RES
SWAP The size of the virtual memory used by the process, in kb
RES The size of physical memory used by the process and not swapped out, in kb. RES=CODE+DATA
CODE Physical memory occupied by executable code, in kb
DATA Physical memory occupied by parts other than executable code (data segment + stack), unit: kb
SHR Shared memory size in kb
nFLT Number of page errors
nDRT The number of pages that have been modified since the last write.
S Process status. D = non interruptible sleep state R = running s = sleep T = tracking / stopping Z = zombie process
COMMAND Command name / command line
WCHAN If the process is sleeping, the system function name in sleep is displayed
Flags Task flag
other
When entering top by default, the processes are sorted according to the CPU usage.

1. In the top basic view, press the keyboard number "1" to monitor the status of each logical CPU:


2. Tap the keyboard 'b' (turn on and off the highlighting effect) the top view is transformed as follows:

 

PID 16283 is the only running process in the current top view. You can also tap the keyboard 'y' to turn on or off the highlighting effect of running processes.

3. Tap the keyboard 'x' (turn on / off the highlighting effect of the row sequence), and the top view changes as follows:

 

You can see that the sorting is now based on "% CPU". You can change the sorting sequence around "Shift + >" or "Shift + <".

4. Change process display field

In the top basic view, click "f" to enter another view, where you can edit the display fields in the basic view:

Use the up and down keys to select an option, and press the spacebar to decide whether to display this option in the basic view.

 

 

top command is a commonly used performance analysis tool under Linux. It can display the resource occupation of each process in the system in real time, which is similar to the task manager of Windows.

Top displays the current process and other conditions of the system. It is a dynamic display process, that is, the current state can be refreshed continuously by pressing the user's key If the command is executed in the foreground, it will monopolize the foreground until the user terminates the program To be more precise, the top command provides real-time status monitoring of the system processor It will display the list of the most "sensitive" tasks of the CPU in the system This command can be used by CPU Sorting tasks by memory usage and execution time; Moreover, many features of the command can be set through interactive commands or in personal customization files  

Its usage is described in detail below.

Parameter meaning

top - 01:06:48 up  1:22,  1 user,  load average: 0.06, 0.60, 0.48
Tasks: 29 total, 1 running, 28 sleeping, 0 stopped, 0 zombie
Cpu(s): 0.3% us, 1.0% sy, 0.0% ni, 98.7% id, 0.0% wa, 0.0% hi, 0.0% si
Mem: 191272k total, 173656k used, 17616k free, 22052k buffers
Swap: 192772k total, 0k used, 192772k free, 123988k cached

PID USER PR NI VIRT RES SHR S %CPU %MEM TIME+ COMMAND
1379 root 16 0 7976 2456 1980 S 0.7 1.3 0:11.03 sshd
14704 root 16 0 2128 980 796 R 0.7 0.5 0:02.72 top
1 root 16 0 1992 632 544 S 0.0 0.3 0:00.90 init
2 root 34 19 0 0 0 S 0.0 0.0 0:00.00 ksoftirqd/0
3 root RT 0 0 0 0 S 0.0 0.0 0:00.00 watchdog/0


The first five lines of the statistical information area are the statistical information of the whole system. The first line is the task queue information, which is the same as the execution result of uptime command. The contents are as follows:

01:06:48    current time 
up 1:22 System running time in the format of:branch
1 user Number of currently logged in users
load average: 0.06, 0.60, 0.48 System load, that is, the average length of the task queue. The three values are the average values from 1 minute, 5 minutes and 15 minutes ago to now.

 

Second and third, the information of the behavior process and CPU. When there are multiple CPUs, these contents may exceed two lines. The contents are as follows:

total Total number of processes
running Number of running processes
sleeping Number of sleep processes
stopped Number of processes stopped
zombie Number of zombie processes
Cpu(s): 
0.3% us User space occupation CPU percentage
1.0% sy Kernel space CPU percentage
0.0% ni Occupied by processes with changed priority in user process space CPU percentage
98.7% id free CPU percentage
0.0% wa Waiting for input and output CPU Time percentage
0.0%hi: Hardware CPU Interrupt occupancy percentage
0.0%si: Soft interrupt occupancy percentage
0.0%st: Virtual machine occupancy percentage

 

 

The last two lines are memory information. The contents are as follows:

Mem:
191272k total Total physical memory
173656k used Total physical memory used
17616k free Total free memory
22052k buffers Amount of memory used as kernel cache
Swap:
192772k total Total exchange area
0k used Total number of swap areas used
192772k free k free
123988k cached Total number of swap buffers,The contents in the memory are swapped out to the swap area and then swapped into the memory, but the used swap area has not been overwritten. This value is the size of the swap area where these contents already exist in the memory,When the corresponding memory is swapped out again, there is no need to write to the swap area.

 

The detailed information of each process is displayed below the statistical information area of the process information area. First, let's understand the meaning of each column.

Serial number column name    meaning
a PID process id
b PPID Parent process id
c RUSER Real user name
d UID User of the process owner id
e USER User name of the process owner
f GROUP Group name of the process owner
g TTY The name of the terminal that started the process. Processes that are not started from the terminal are displayed as ?
h PR priority
i NI nice Value. Negative values indicate high priority and positive values indicate low priority
j P Last used CPU,Only in many CPU Meaningful in the environment
k %CPU Last updated to now CPU Time occupancy percentage
l TIME Used by the process CPU Total time in seconds
m TIME+ Used by the process CPU Total time, unit 1/100 second
n %MEM Percentage of physical memory used by the process
o VIRT Total amount of virtual memory used by the process, in kb. VIRT=SWAP+RES
p SWAP The size, unit, of the virtual memory used by the process kb.
q RES The size of physical memory used by the process and not swapped out, unit: kb. RES=CODE+DATA
r CODE Physical memory occupied by executable code, unit kb
s DATA Parts other than executable code(Data segment+Stack)Size of physical memory occupied, unit kb
t SHR Shared memory size in kb
u nFLT Number of page errors
v nDRT The number of pages that have been modified since the last write.
w S Process status(D=A state of uninterrupted sleep,R=function,S=sleep,T=track/stop it,Z=Zombie process)
x COMMAND Command name/command line
y WCHAN If the process is sleeping, the system function name in sleep is displayed
z Flags Task flag, reference sched.h
 

By default, only the important PID, USER, PR, NI, VIRT, RES, SHR, S,% CPU,% MEM, TIME +, COMMAND columns are displayed. You can change the display by using the shortcut keys below.  

Change the display content. You can select the displayed content by pressing the f # key. Press f to display the list of columns. Press a-z to display or hide the corresponding columns. Finally, press enter to confirm.  
Press the # key to change the display order of the columns. Press z-a to move the lowercase column to the left and z-a to the right. Finally, press enter to confirm.  
Press the uppercase # F # or # O # key, and then press a-z to sort the processes according to the corresponding columns. The uppercase R , key can reverse the current sorting.


Command use

top use format

top [-] [d] [p] [q] [c] [C] [S] [s]  [n]


Parameter description

d Specify the time interval between two screen information refreshes. Of course, users can use s Interactive commands to change it. 
p Monitor processes by specifying ID To monitor only the status of a process.
q This option will make top Refresh without any delay. If the caller has superuser privileges, then top Will run at the highest possible priority.
S Specify cumulative mode
s send top The command runs in safe mode. This removes the potential danger of interactive commands.
i send top No idle or dead processes are displayed.
c Displays the entire command line, not just the command name


Other practical commands
The following describes some interactive commands that can be used during the execution of the top command. From the perspective of use, mastering these commands is more important than mastering the options. These commands are single letter. If the s option is used in the command line option, some of them may be masked.

Ctrl+L Erase and rewrite the screen. 
h perhaps? Display the help screen and give some brief command summary instructions.
k Terminate a process. The user will be prompted to enter the process to be terminated PID,And what signals need to be sent to the process. The general termination process can use 15 signals; If it cannot end normally, use signal 9 to force the end of the process. The default value is signal 15. This command is masked in safe mode.
i Ignore idle and dead processes. This is an on-off command.
q Exit the program.
r Reprioritize a process. The system prompts the user to enter the process to be changed PID And the process priority value to be set. Entering a positive value will lower the priority, and vice versa will give the process a higher priority. The default value is 10.
S Switch to cumulative mode.
s Change the delay time between refreshes. The system will prompt the user to enter a new time in s. If there are decimals, convert them into m s. Enter a value of 0 and the system will refresh continuously. The default value is 5 s. It should be noted that if the time is set too small, it is likely to cause continuous refresh, so it is impossible to see the display at all, and the system load will be greatly increased.
f perhaps F Add or remove items from the current display.
o perhaps O Change the order in which items are displayed.
l Switch to display average load and start-up time information.
m Toggles the display of memory information.
t Toggle display process and CPU Status information.
c Toggles the display of the command name and the full command line.
M Sort according to the size of resident memory.
P according to CPU Sort using percentage size.
T According to time/Sort by cumulative time.
W Write current settings to~/.toprc File. This is written top Recommended methods for configuration files.

 

Common operations attached:

top   //Explicit resource usage of all processes every 5 seconds
top -d 2 //The resource usage of all processes is explicitly displayed every 2 seconds
top -c //The resource usage of the process is displayed every 5 seconds, and the command line parameters of the process are displayed (only the process name by default)
top -p 12345 -p 6789//The resource occupancy of the two processes with pid of 12345 and pid of 6789 is displayed every 5 seconds
top -d 2 -c -p 123456 //The resource usage of the process whose pid is 12345 is displayed every 2 seconds, and the command line parameters of the process are explicitly displayed

Added by Markto on Sun, 06 Mar 2022 15:50:50 +0200