Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

Detailed explanation of linux top command

2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >

Share

Shulou(Shulou.com)06/02 Report--

The top command is mainly used to view information about the process, and it also provides system average load, cpu information and memory information. The following screenshot shows the information provided by default by the top command:

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 7 days represents the total elapsed time since the system was last booted.

1 user indicates that there is only one logged-in user in the current system.

Load average: 0.01,0.04,0.00 represents the average load of the system, and the last three numbers represent the average load of the last minute, the last five minutes, and the last 15 minutes, respectively.

The lowercase letter I controls whether the system average load information is displayed.

Task information summary

In linux systems, processes and threads 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 total number of processes on the current system.

1 running indicates that there is 1 process running on the current system.

269 sleeping indicates that there are 269 dormant processes in the current system.

0 stopped indicates that the number of processes stopped 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 CPU:

There are a total of eight fields, which are the main basis for us to understand the CPU load, which we will introduce one by one.

Us

The percentage of CPU time consumed by the process in the user address space. Shell programs, compilers of various languages, database applications, web servers, and various desktop applications are all processes running in the user's address space. If these programs are not in the idle state, 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 to be used by all processes are handled by the Linux kernel. When a process in user mode (user address space) needs to use system resources, such as allocating some memory, or performing IO operations, or creating a child process, it will run in kernel state (kernel address space). In fact, the process scheduler that determines whether the process will be run at the next moment runs in the kernel state. For the design of the operating system, the less time spent on kernel state, the better. In practice, there is a typical situation that makes sy larger, that is, a large number of IO operations, so we need to focus on it when investigating IO-related problems.

Ni

Ni is an acronym for nice, and the priority of the user state of the process can be adjusted by the nice value. The ni shown here represents the CPU time consumed by a process that has adjusted the nice value. If no process on the system has adjusted the nice value, then ni is displayed as 0.

Id

The percentage of CPU in the idle state. In general, us + ni + id should be close to 100%.

Wa

The time CPU waits for disk IO operations. Compared with the processing speed of CPU, disk IO operation is very slow. There are many such operations, for example, CPU needs to wait for the result of a disk read and write operation after starting a disk read and write operation. CPU can only be idle until the disk read and write operation is complete. When calculating the average load of the system, the Linux system will also take into account the time it takes for CPU to wait for IO operations, so when we see that the average load of the system is too high, we can use wa to determine whether the performance bottleneck of the system is caused by too many IO operations.

Hi & si

These two values represent the time it takes for the system to process interrupts. Interrupts are divided into hard interrupts and soft interrupts. Hi represents the time spent processing hard interrupts, and si represents the time spent processing soft interrupts. Hard interrupt is an interrupt message sent to CPU by hard disk, network card and other hardware devices. When CPU receives the interrupt message, it needs to be handled properly (consuming CPU time). A soft interrupt is an interrupt issued by a program that eventually executes the corresponding handler (consuming CPU time).

St

St makes sense only when Linux is running as a virtual machine. It represents the time that the virtual machine waits for the CPU resource (the virtual machine is assigned to the virtual CPU, and when the real CPU is needed, the real CPU may be running other virtual tasks, so you need to wait).

The lowercase letter t controls whether task information summary and CPU information are displayed. Yes, it controls whether two lines of information are displayed.

Memory information

The memory information contains two lines, memory and swap space:

The output of this part of the top command is basically the same as that of the free command, which has been described in detail in the article "linux free command" and will not be repeated here.

Control display unit

The top command displays the memory size in K units by default, which is maddening. Fortunately, we can toggle the display units of the memory information area by capitalizing E (note that E cannot control the memory units in the task area). The following figure shows the memory size in GB:

The lowercase m controls whether memory information is displayed.

Task detail

Below the memory information is a blank line (actually the area that interacts with the user), and below the blank line is the task details area:

By default, 12 columns of data are displayed here, all of which we are more concerned about. Let's introduce them one by one below.

PID represents the process ID.

USER represents the valid user name of the process owner. To put it simply, the process is started with which user rights. For example, in the figure above, two processes are started by the user nick, one by the user prometheus, and the other by the root user.

PR represents the priority of process execution, and the value of PR is the priority of process execution seen from the perspective of the Linux kernel.

The process execution priority seen by NI from the user's perspective. Notice in the figure above that the two processes with a NI value of-20 both have a PR value of 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 amount of shared memory used by the process.

S represents the current state of the process. The S values are as follows:

D uninterruptible sleep state (uninterruptible sleep)

R running status (running)

S sleep state (sleeping)

T track or stop status (traced or stopped)

Z Zombie status (zombie)

% CPU represents the percentage of CPU used by the process.

% MEM represents the percentage of memory used by the process.

TIME+ represents the cumulative CPU time used by the process.

COMMAND represents the program that runs the process.

In general, this information is sufficient, but if you want more information, you can try to add more columns. Press the lowercase f to enter the configuration interface of the task information:

Here you can select the columns to display, and you can configure which column to sort.

The problem of displaying memory size in units also exists in the task details area, and the default unit is KB. To change its units, you need to use the lowercase letter e to switch, for example, I can switch it to MB:

It looks much more intuitive this way!

Top is a very complex command, and the above introduction is only scratching the surface. Even so, you can do a lot of things with it! If you want more detailed information, please refer to the user manual of top. Let's move on to some common use cases.

Display details of multiple CPU cores

No matter how many CPU cores there are in the system, the default CPU information always outputs one line, that is, the combined data of all the cores. Can you view the individual data of each CPU core? The answer is yes. Press the number 1 on the keyboard to switch between different views:

Sort processes by a column

Enter the sorting interface by lowercase f, select a column, sort by lowercase's', and then exit.

The strange thing is that the default main interface does not show which column is sorted! You can use the lowercase letter x to bold the currently sorted columns:

Can you see that the font of the% CPU column is bold? It's not obvious, but I can barely see it.

There are also predefined commands that can directly sort by a column, such as the uppercase letter M by the% MEM column, the uppercase N by the PID column, the uppercase P by the% CPU column, and the uppercase T by the TIME+ column.

M% MEM

N PID

P% CPU

T TIME+

The result of the reverse sort is a common requirement, and the capital letter R can reverse the result of the current sort.

Displays the complete command executed by the process

The default COMMAND column shows only the name of the program and does not contain the path to the program. Sometimes it is convenient to see the full path of the program. You can toggle the display mode of the COMMAND column by lowercase c:

Not only the complete path of the program, but also the parameters of the startup program are shown!

Hide the process of idle

When we investigate problems, we always want to find the busy process in the fastest way. But the top command lists all the processes, which requires us to scan lines of process information through our dim old eyes. Fortunately, we can use the lowercase I to control whether the process in the idle state is displayed! After using this command, you will find that the world is so clean!

Show only the process of a user

If you want to see processes started with certain user privileges, you can use the lowercase letter u. This will prompt you to enter the user's name, and after you enter the user's name, press enter:

The user name entered by the author in the image above is nick. Pressing enter will filter out all processes started with the user's nick privileges.

Configuration file for the top command

The top command has a configuration file, which means that the configuration you modify through the command can be saved. The command to save the configuration is an uppercase W. After you change the configuration of the top command, press the uppercase W, then exit the top command and execute the top command again, while your changes are still in effect.

Help documentation

Help documents will always be our good friends, the lowercase letter h or? You can open the help documentation for the top command. Don't be surprised, the document is a little long!

The above is the whole content of this article, I hope it will be helpful to your study, and I also hope that you will support it.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report