The top command is primarily used to view information about the process, and it also provides the system's average load, CPU information, and memory information. The following shows the information that the top command provides by default:
Average system load
The first line in the output of the top command is the average load of the system, which is the same as the output of the uptime command:
13:05:49 represents the current time of the system.
Up to 7 days indicates the total elapsed time after the last boot of the system.
1 User indicates that there is only one logged-on user in the current system.
Load average:0.01, 0.04, 0.00 represents the average load of the system, the last three numbers represent the last minute system average load, the last five minutes of the system average load, and the last 15 minutes of the system average load.
The lowercase letter I can control whether the system load information is displayed.
Summary of Task information
In Linux systems, process and line Cheng are generally referred to as tasks. The second line of information is a statistic of all the tasks in the current system:
tasks:270 Total represents the current number of processes in the system.
1 Running indicates that there are 1 running processes in the current system.
269 Sleeping indicates that there are 269 dormant processes in the current system.
0 stopped indicates that the number of processes in the stop state is 0.
0 Zombie indicates that the number of processes in the zombie state is 0.
CPU Information
The third line shows the usage of the CPU:
Here is a total of eight fields, is the main basis for us to understand the CPU load, we introduce each one.
us
The percentage of CPU time that the process consumes in the user's address space. Like shell programs, compilers for various languages, database applications, Web servers, and various desktop applications are all processes that run in the user address space. If these programs are not idle, then the vast majority of CPU time is running in the user state.
Sy
The percentage of CPU time that the process consumes in the kernel address space. The system resources that are used by all processes are handled by the Linux kernel. When a process in the user-state (user address space) needs to use the system's resources, such as allocating some memory, or performing an IO operation, or creating a sub-process, the kernel State (kernel address space) runs. In fact, the process scheduler that determines whether the process will run at the next moment is running in the kernel state. For the design of the operating system, the less time it takes to consume the kernel state, the better. There is a typical case in practice that makes SY larger, which is a large number of IO operations, so you need to focus on it when you investigate IO-related issues.
Ni
NI is the abbreviation for nice and can adjust the priority of the process user state with the nice value. The NI shown here represents the CPU time consumed by processes that have adjusted the nice value. If no process in the system has been adjusted to the nice value, then NI is displayed as 0.
Id
The percentage of the CPU in idle state. In general, the US + ni + ID should be close to 100%.
Wa
The time that the CPU waits for disk IO operations. The disk IO operation is very slow compared to the processing speed of the CPU. There are a number of such operations, such as: After the CPU starts a disk read and write operation, it needs to wait for the disk read and write operation results. The CPU can only be idle until the disk read-write operation is complete. The Linux system calculates the CPU's time to wait for IO operations when computing the average load, so when we see the average system load is too high, it can be determined by WA that the performance bottleneck of the system is not due to excessive IO operations.
Hi & Si
These values represent the time that the system processed the interrupt consumption. Interrupts are divided into hard interrupts and soft interrupts, hi indicates the time it takes to process the hard interrupt consumption, and SI represents the time it takes to process the soft interrupt. A hard interrupt is an interrupt message sent to the CPU by a hardware device such as a hard disk or network card, which needs to be properly processed (CPU time is consumed) when the CPU receives the interrupt message. A soft interrupt is an interrupt made by a program that eventually executes the corresponding handler (CPU time is consumed).
St
Only Linux is meaningful when it is running as a virtual runtime. It represents the time that the virtual machine waits for CPU resources (virtual CPUs are assigned to it, and when real CPUs are needed, it is possible that the real CPU is running other VM tasks, so it needs to wait).
The small Letter T can control whether the task information summary and CPU information are displayed. Yes, it can control whether two lines of information are displayed.
Memory Information
The memory information contains two lines of content, memory and swap space:
The output of this part of the top command is basically the same as the output of the free command, which I have described in detail in the article "Linux free Command", which I will not repeat here.
Control display Units
The top command displays memory size by default in K, which is a very crazy one. Fortunately, we can switch the display units of the memory information area by the capital Letter E (Note that E cannot control the memory units in the task area), showing the memory size in gigabytes:
The small letter M can control whether memory information is displayed.
Task Details
Memory information The following is a blank line (in fact, the area that interacts with the user), and the following empty line is the task detail area:
By default, this will show 12 columns of data, all of which we are more concerned about to carry out the relevant information, below we are one of the introduction.
The PID represents the process ID.
User represents a valid user name for the process owner. In short, it is the process that starts with which user rights. For example, there are two processes that user Nick started, and one is user Prometheus initiated, and the others are root user initiated.
The PR represents the priority of the process execution, and the PR value is the priority of the process execution as seen from the Linux kernel's perspective.
the process execution priority that NI sees from the user's perspective. Note that the median NI value is 20 of two processes, and their PR value is 0.
VIRT represents the amount of virtual memory used by the process.
RES represents the amount of physical memory used by the process.
SHR represents the size of the shared memory used by the process.
S represents the current state of the process. There are several S values:
D non-disruptive sleep state (uninterruptible sleep)
R running State (running)
S sleep State (sleeping)
T Track or stop state (traced or stopped)
Z Zombie Status (zombie)
%cpu represents the percentage of the CPU that the process uses.
%MEM indicates the percentage of memory used by the process.
time+ represents the CPU time that the process accumulates.
COMMAND indicates the program that the process corresponds to running.
In general, this information is sufficient, but if you want more information, you can try adding more columns. Press the lowercase letter F to enter the configuration interface for the task information:
Here you can select the columns to display, and you can configure which column to sort.
Unit problems that show memory size also exist in the task detail area, and the default units are KB. To change the unit you need to use the lowercase letter e to switch, for example I can switch it to MB:
It looks a lot more intuitive!
Top is a very complex command, and the content described above is just some fur. Even so, you can use it to do a lot of things! If you would like more detailed information, please refer to the User guide of top. We'll cover some of the common use cases next.
Show details for multiple CPU cores
Regardless of the number of CPU cores in the system, the default CPU information is always output one line, which is the combined data of all the cores combined. Can you see the individual data for each CPU core? The answer is yes, yes. You can switch between different views by pressing the number 1 on your keyboard:
sort the process in a column
Press the lowercase letter F to enter the Sort Settings screen, select a column, specify the sort by lowercase ' s ', and then exit.
Oddly, the default main interface does not show which column is sorted! You can use the lowercase letter x to display the currently sorted column in bold:
can you see the font for the%CPU column is bold? It's not too obvious, but I can barely see it.
There are also predefined commands that can be used to sort functions directly in a column, such as capital letter M sorted by%MEM column, uppercase N in PID column, uppercase P in%CPU column, and capital letter T sorted by time+ column.
M%MEM
N PID
P%cpu
T time+
The result of reversing the sort is a common requirement, and the uppercase R can invert the results of the current order.
show complete commands for process execution
The default COMMAND column displays only the name of the program and does not include the path to the program. Sometimes it is convenient to see the full path of the program. You can switch the display mode of the COMMAND column by using the lowercase letter C:
Not only the full path of the program, even the parameters of the startup program are displayed!
hide the Idle process
When we investigate the problem, we always want to find the busiest process in the quickest way. But the top command would list all the processes, which would require us to scan a line of process information through Dim's old eye. Fortunately, we can use the lowercase letter I to control whether the process is displayed in the Idle state! After using this command you will find the world is so refreshing!
Show only a user's process
If you want to see a process that starts with a user right, you can use the lowercase letter U. This will prompt you to enter the name of the user and, after you enter the user name, press ENTER:
I entered the user named Nick, press ENTER after the filter out all the user Nick permissions to start the process.
configuration file for top command
The top command has a configuration file, which means that the configuration you modify with the command can be saved. The command to save the configuration is a capital letter W. When you have modified the configuration of the top command, press the capital letter W, then exit the top command and execute the top command again, and your changes are still working.
Help Documentation
Help documents are always our best friends, lowercase h or yes? You can open the Help document for the top command. Don't be too surprised, the documentation is a bit long yo!
Linux Top command