Top View process status

1. Introduction to top command

I believe that every operation and maintenance personnel has encountered something like a sudden surge in server load. When encountering this situation, everyone's first reaction must be to log in to the server and type the top command to see the load average. In the Linux operating system, top is the most frequently used command with relatively complete information. It provides continuously updated overview information for all running processes and system loads, including system load, CPU utilization distribution, memory usage, and each Information such as the resource usage of each process. Today’s article will tell you how to understand the top command.

2. Analysis of the output results of the top command

After executing the top command, the following content will appear by default: Insert image description here
The top command outputs a lot of parameters. We need to look at the real server load together with other parameters. The running results can be divided into two parts: the first
part is the first 5 lines, which is the overall system Statistical information;
the second part is the process information starting from line 8. We will explain it line by line from top to bottom.

2.1. Overall system statistical information

first row:

top - 16:20:38 up 12 days,  5:24,  2 users,  load average: 0.04, 0.03, 0.05
Field illustrate
top current time
up How long has the machine been running?
users Current number of logged in users
load average System load, which 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 line:

Tasks: 127 total,   1 running, 126 sleeping,   0 stopped,   0 zombie
Field illustrate
Tasks:356 total Total number of processes in the system
2 running The number of running processes. The more running here, the greater the pressure on the server will naturally be.
354 sleeping Number of sleeping processes
0 stopped How to stop the number of processes
0 zombie Number of zombie processes. If it is not 0, you need to manually check the zombie process

The third row:

%Cpu(s):  0.3 us,  0.7 sy,  0.0 ni, 99.0 id,  0.0 wa,  0.0 hi,  0.0si,  0.0 st
Field illustrate
%Cpu(s): 0.1%us The percentage of user space in the CPU (such as shell programs, compilers of various languages, various applications, web servers and various desktop applications are all processes running in the user address space. If these programs are not in the idle state, then most Most of the CPU time is running in user mode)
0.1% and The percentage of kernel space in the CPU (all system resources used by processes are handled by the Linux kernel. For the design of the operating system, the less time spent in the kernel state should be as little as possible. In practice, there is a typical The situation will make sy larger, that is a large number of IO operations, so you need to focus on it when investigating IO related issues)
It is 0.0% The priority of the user process space has been changed (ni is the abbreviation of nice. You can adjust the priority of the process user state through the nice value. The ni displayed here represents the CPU time consumed by the process that has adjusted the nice value. If no process in the system has been adjusted If the nice value is exceeded, then ni will be displayed as 0)
99.7% id Idle CPU usage
0.1% of Percentage of CPU time waiting for input and output (Compared with the processing speed of the CPU, disk IO operations are very slow. There are many such operations. For example, after the CPU starts a disk read and write operation, it needs to wait for the result of the disk read and write operation. . Before the disk read and write operations are completed, the CPU can only be in an idle state. When calculating the average load of the system, the Linux system will also include the time the CPU waits for IO operations, so when we see that the average load of the system is too high, we can pass wa to determine whether the system's performance bottleneck is caused by excessive IO operations.
0.0% hi Percentage occupied by hard interrupts (hard interrupts are interrupt messages sent to the CPU by hardware devices such as hard disks and network cards. When the CPU receives the interrupt message, it needs to perform appropriate processing (consuming CPU time).
0.1% and Soft interrupt occupation percentage (soft interrupt is an interrupt issued by the program, and the corresponding processing program will eventually be executed, consuming CPU time)
0.0% st st (steal time) means the percentage of virtual time, which is the percentage of time that the virtual CPU waits for the actual CPU when there is a virtual machine
The fourth line:
KiB Mem : 1863012 total, 1286408 free,  216532 used, 360072 buff/cache
Field content
KiB Mem: 9280308 total The total amount of physical memory in KB
211208 free The amount of free physical memory.
5943424 used The amount of physical memory used
3126676 buff/cache The amount of memory used as kernel cache

The fifth line:

KiB Swap: 5242876 total, 7999484 free,     0 used. 1468240 avail Mem

What is displayed is the usage of the swap partition, so if the swap memory function is not turned off when reading the top command, you need to pay more attention. If the value keeps changing, it means that the memory is really not enough.

Field content
KiB Swap:0 total Total amount of swap partition (virtual memory)
0 free Size of free swap partition
0 used Size of swap partition used
2928776 avail mem Total amount of buffered swap area

2.2. Process information

PID    USER    PR  NI  VIRT    RES   SHR   S  %CPU  %MEM     TIME+  COMMAND            
21829  root    20   0  0       0     0     S   0.7  0.6   129:53.91  java
22559  root    20   0  158920  5628  4268  S   0.3  9.2   139:42.81  java
22598  root    20   0  162112  2208  1540  S   0.3  0.1   0:04.68    fluentd
PID  	进程id
USER	进程所有者的用户名
PR	   	优先级
NI		nice值,负值表示高优先级,正值表示低优先级
VIRT	进程使用的虚拟内存总量,单位kb。VIRT=SWAP+RES
RES		进程使用的、未被换出的物理内存大小,单位kb。RES=CODE+DATA
SHR		共享内存大小,单位kb
S		进程状态。D=不可中断的睡眠状态 R=运行 S=睡眠 T=跟踪/停止 Z=僵尸进程
%CPU	上次更新到现在的CPU时间占用百分比
%MEM	进程使用的物理内存百分比
TIME+	进程使用的CPU时间总计,单位1/100秒
COMMAND	命令名/命令行

By default, only the more important PID, USER, PR, NI, VIRT, RES, SHR, S, %CPU, %MEM, TIME+, COMMAND columns are displayed, as well as some parameters, such as:

PPID	父进程id
GROUP   进程所有者的组名
SWAP:	进程使用的虚拟内存中被换出的大小
CODE	可执行代码占用的物理内存大小,单位kb
DATA	可执行代码以外的部分(数据段+栈)占用的物理内存大小,单位kb
nFLT	页面错误次数
nDRT	最后一次写入到现在,被修改过的页面数。
WCHAN	若该进程在睡眠,则显示睡眠中的系统函数名
Flags	任务标志

3. Use of top command

3.1 Change display content

You can edit the displayed content by pressing the f key. After pressing the f key, the following picture will appear:
Insert image description here
According to the above picture:
① It is currently sorted according to the %CPU column
② You can select it by using the up and down keys
③ Press the right key to select the entire column, and then press the up and down keys to move the display position of the entire column, move forward or backward, and press Enter to confirm
④ Press the space bar to display or hide the column, with it displayed, without it it will not be displayed
⑤ Press The s key can set the current column as the sorting column
⑥ Press the q key to exit

3.2 top common parameters

The format of the top command is:

top [选项]
----------------------------------
top命令常用的选项参数:
选项	功能
-d	指定每两次屏幕信息刷新之间的时间间隔,如希望每秒刷新一次,则使用:top -d 1
-p	通过指定PID来仅仅监控某个进程的状态
-S	指定累计模式
-s	使top命令在安全模式中运行。这将去除交互命令所带来的潜在危险
-i	使top不显示任何闲置或者僵死的进程
-c	显示整个命令行而不只是显示命令名

For example:

 每隔3秒显式所有进程的资源占用情况
top -d 1  每隔1秒显式所有进程的资源占用情况
top -c    每隔3秒显式进程的资源占用情况,并显示进程的命令行参数(默认只有进程名)
top -p 28820 -p 38830   每隔3秒显示pid是28820和pid是38830的两个进程的资源占用情况
top -d 2 -c -p 69358  每隔2秒显示pid是69358的进程的资源使用情况,并显式该进程启动的命令行参数

3.3 top interactive commands

By default, when entering top, the processes are sorted according to CPU usage.

[1] After typing top, press the number "1" on the keyboard to monitor the status of each logical CPU: Insert image description here
[2] After typing top, enter u, and then enter the user name, you can view the corresponding user process;

Insert image description here
[3] After typing top, the top command displays the memory size in K by default. We can switch the display unit of the memory information area by using the capital letter E. As follows, press E to switch to MB and press E to switch to GB
Insert image description here
:
Insert image description here

【4】After typing top, enter h to enter the help document of the top command to learn more about the usage of top.
Insert image description here
Top is of course our most common command to check system status. There are many other commands, such as vmstat, w, uptime, and iostat, which are all commonly used commands.

Guess you like

Origin blog.csdn.net/QQ657205470/article/details/128937863