Introduction
The top command is a commonly used performance analysis tool under Linux. It can display the resource occupancy status of each process in the system in real time, similar to the Windows task manager.
top displays the current process and other status of the system. It is a dynamic display process, that is, the current status can be continuously refreshed through the user's keystrokes. If the command is executed in the foreground, it will occupy the foreground until the user terminates the program. More precisely The ,top command provides real-time monitoring of the system's processor status. It will display a list of the most "sensitive" tasks for the CPU in the system. The command can sort tasks by CPU usage, memory usage, and execution time; and the command's Many features can be set via interactive commands or in custom files.
How to use it 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 overall statistical information of the system. The first line is the task queue information, which is the same as the execution result of the uptime command. Its contents are as follows:
01 : 06 : 48Current time up 1 : 22 System running time, the format is hour: minute 1 user The number of currently logged in users load average: 0.06 , 0.60 , 0.48 The system load 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 the present.
The second and third behaviors are information about the process and CPU. These may exceed two lines when there are multiple CPUs. The content is as follows:
total total number of processes running the number of running processes number of processes sleeping stopped Number of processes stopped zombie number of zombie processes Cpu(s): 0.3 % us The percentage of CPU occupied by user space 1.0 % sy The percentage of CPU occupied by kernel space 0.0 % ni The percentage of CPU occupied by processes whose priorities have changed in user process space 98.7 % id The percentage of idle CPU 0.0 % wa The percentage of CPU time waiting for input and output 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 content is 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 swap area 0k used total swap area used 192772k free total free swap area 123988k The total amount of swap area buffered by cached, the content in the memory is 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 amount of content that already exists in the memory. The size of the swap area, when the corresponding memory is swapped out again, it is not necessary to write to the swap area.
The lower part of the statistics area of the process information area shows the detailed information of each process. 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 id of the process owner e USER username of the process owner f GROUP the group name of the process owner g TTY Terminal name from which the process was started. Processes not started from a terminal are displayed as ? h PR priority i NI nice value. Negative values indicate high priority, positive values indicate low priority j P Last CPU used, only meaningful in a multi-CPU environment k %CPU The percentage of CPU time occupied by the last update to the present l TIME The total CPU time used by the process, in seconds m TIME+ The total CPU time used by the process, in 1/100 seconds n %MEM The percentage of physical memory used by the process o The total amount of virtual memory used by the VIRT process, in kb. VIRT=SWAP+RES p The size of the virtual memory used by the SWAP process, in kb. q The size of the physical memory used by the RES process and not swapped out, in kb. RES=CODE+DATA r CODE The size of the physical memory occupied by the executable code, in kb s DATA The size of the physical memory occupied by the part other than the executable code (data segment + stack), in kb t SHR Shared memory size, in kb u nFLT page fault count v The number of pages modified since the last write by nDRT. w S Process state (D=uninterruptible sleep state, R=running, S=sleep, T=trace/stop, Z=zombie process) x COMMAND command name/command line y WCHAN If the process is sleeping, display the name of the sleeping system function z Flags task flags, refer to sched.h
By default, only the more important PID, USER, PR, NI, VIRT, RES, SHR, S, %CPU, %MEM, TIME+, COMMAND columns are displayed. The display content can be changed by the shortcut keys below.
Change the display content The content of the display can be selected by the f key. After pressing the f key, a list of columns will be displayed, press az to show or hide the corresponding column, and finally press the Enter key to confirm.
Press the o key to change the display order of the columns. Press lowercase az to move the corresponding column to the right, and uppercase AZ to move the corresponding column to the left. Finally press Enter to confirm.
Press the uppercase F or O key, then az to sort the processes by the corresponding column. The uppercase R key reverses the current sorting.
command usage
top uses the format
top [-] [d] [p] [q] [c] [C] [S] [s] [n]
Parameter Description
d Specifies the time interval between each refresh of the screen information. Of course the user can use the s interactive command to change it. p Monitor only the status of a process by specifying the monitor process ID. q This option will make top refresh without any delay. If the calling program has superuser privileges, then top will run with the highest possible priority. S Specifies the accumulation mode s makes the top command run in safe mode. This removes the potential danger posed by interactive commands. i makes top not show any idle or zombie processes. c displays the entire command line instead of just the command name
Other useful commands
The following describes some interactive commands that can be used during the execution of the top command. From a usage point of view, mastering these commands is more important than mastering the options. These commands are all single letter, if the s option is used in the command line options, some of these commands may be blocked.
Ctrl+L Erase and rewrite the screen. h or ? Displays a help screen giving some brief command summaries. k terminates a process. The system will prompt the user to enter the PID of the process that needs to be terminated, and what kind of signal needs to be sent to that process. The general termination process can use the 15 signal; if it cannot be terminated normally, use the signal 9 to force the termination of the process. The default value is signal 15. This command is blocked in safe mode. i Ignore idle and zombie processes. This is a switch command. q Quit the program. r Reschedules the priority level of a process. The system prompts the user to input the process PID that needs to be changed and the process priority value that needs to be set. Entering a positive value will make the priority lower, otherwise it will make the process have a higher priority. The default value is 10. S Switch to accumulation mode. s Changes the delay time between refreshes. The system will prompt the user to enter a new time in seconds. If there is a decimal, it is converted to ms. If you enter a value of 0, 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 that it is too late to see the display clearly, and the system load will be greatly increased. f or F Add or remove items from the current display. o or O Change the order of displayed items. l Toggle to display the average load and startup time information. m Toggles display of memory information. t Toggles display of process and CPU status information. c Toggles showing the command name and the full command line. M is sorted by resident memory size. P is sorted by CPU usage percentage size. T is sorted by time/cumulative time. W writes the current settings to the ~/.toprc file. This is the recommended way to write top configuration files.
With common operations:
top // Display the resource usage of all processes every 5 seconds top -d 2 // Display the resource usage of all processes every 2 seconds top -c // Display the resource usage of all processes every 5 seconds, And display the command line parameters of the process (only the process name by default) top -p 12345 -p 6789 // Display the resource usage of the two processes with pid 12345 and pid 6789 every 5 seconds top -d 2 -c -p 123456 // Display the resource usage of the process whose pid is 12345 every 2 seconds, and specify the command line parameters started by the process