Detailed explanation of the usage of Linux top command

Top command is often used to monitor Linux system status, such as cpu, memory usage.

Exit the top interface command: q

View multi-core CPU commands
mpstat -P ALL and sar -P ALL 

Description: sar -P ALL > aaa.txt redirect the output content to the file aaa.txt This article describes the meaning of various data in the top view through the top monitoring screenshot of a running WEB server, and also includes each process in the view ( task) fields.

top enter view

top view 01
[top view 01] is the basic view that just entered the top. Let's combine this view to explain the meaning of each data.
The first line:
10:01:23 — the current system time is
126 days, 14:29 — the system has been running for 126 days, 14 hours and 29 minutes (without restarting during this period)
2 users — there are currently 2 users logged in to the system
load average : 1.15, 1.42, 1.44 — The three numbers after the load average are the load conditions of 1 minute, 5 minutes, and 15 minutes, respectively.


The load average data is to check the number of active processes every 5 seconds, and then calculate the value according to a specific algorithm. If this number is divided by the number of logical CPUs, a result above 5 indicates that the system is overloaded.

 


The second line:
Tasks - tasks (processes), the system now has a total of 183 processes, of which 1 is running, 182 are sleeping (sleep), 0 are in the stopped state, and 0 are in the zombie state (zombie). indivual.

 

 


The third line: cpu status
6.7% us - the percentage of CPU occupied by user space.
0.4% sy — The percentage of CPU used by kernel space. 0.0% ni - 92.9%
of CPU occupied by processes that have changed priority id - 0.0% of idle CPU wa - 0.0% of CPU occupied by IO waiting hi - 0.0% of CPU occupied by hardware IRQs si - soft Percentage of CPU usage by Software Interrupts




The CPU usage ratio here is different from the windows concept. If you don't understand user space and kernel space, you need to charge.

 


Fourth line: memory status
8306544k total — total physical memory (8GB)
7775876k used — total memory in use (7.7GB)
530668k free — total free memory (530M)
79236k buffers — cached memory (79M)


Fifth line: swap swap partition
2031608k total - total swap area (2GB)
2556k used - total swap area used (2.5M)
2029052k free - total free swap area (2GB)
4231276k cached - total buffered swap area ( 4GB)


What I want to explain here is that these data cannot be understood with the memory concept of windows. If the server is "dangerous" in the way of windows: the total amount of memory of 8G is only 530M of available memory. The memory management of Linux has its particularity, and the complexity needs a book to explain, here is just a brief point of difference from our traditional concept (windows).

 

The total amount of memory in use (used) in the fourth line refers to the amount of memory currently controlled by the system kernel, and the total amount of free memory (free) is the amount that the kernel has not yet brought under its control. The memory managed by the kernel is not necessarily in use, and it also includes the memory that has been used in the past and can now be reused. The kernel does not return the memory that can be reused to free, so free memory will be on linux . Less and less, but don't worry about it.

If you calculate the amount of available memory out of habit, here is an approximate calculation formula: free in the fourth row + buffers in the fourth row + cached in the fifth row, according to this formula, the available memory of this server: 530668+79236+4231276 = 4.7GB.

For memory monitoring, we need to monitor the used of the swap partition of the fifth row in the top at all times. If this value is constantly changing, it means that the kernel is constantly exchanging data between memory and swap, which means that the real memory is not enough.

 


The sixth line is blank

 

 


Below the seventh line: status monitoring
PID of each process (task) - process id
USER - process owner
PR - process priority
NI - nice value. Negative values ​​indicate high priority, positive values ​​indicate low priority
VIRT - the total amount of virtual memory used by the process, in kilobytes. VIRT=SWAP+RES
RES — The size of the physical memory used by the process and not swapped out, in kb. RES=CODE+DATA
SHR — Shared memory size in kb
S — Process status. D=uninterruptible sleep state R=running S=sleep T=trace/stop Z=zombie process
%CPU - CPU time percentage since last update
%MEM - percentage of physical memory used by the
process TIME+ - CPU used by the process Total time, unit 1/100 second
COMMAND — process name (command name/command line)


Multi-U multi-core CPU monitoring
In the top basic view, press the number "1" on the keyboard to monitor the status of each logical CPU:

 

top view 02

Looking at the diagram above, the server has 16 logical CPUs, which are actually 4 physical CPUs.

Process field sorting
By default, when entering the top, each process is sorted according to the CPU occupancy. In [top view 01], the Java process with process ID 14210 is ranked first (CPU occupancy is 100%), and the process ID is 14183. The java process came in second (12% cpu usage). You can change the sorting field through keyboard commands. For example, if you want to monitor which process occupies the most MEM, my general usage is as follows:

1. Press "b" on the keyboard (to turn on/off the highlighting effect), the top view changes as follows:

top view 03

We found that the "top" process with the process id of 10704 was highlighted. The top process is the only running process displayed in the second line of the view. You can close or open the running process by pressing the "y" key. highlighting effect.

2. Press "x" on the keyboard (to turn on/off the highlighting effect of the sorting column), the top view changes as follows:

top view 04

As you can see, the default sort column for top is "%CPU".

3. By "shift + >" or "shift + <", you can change the sorting column to the right or left. The following picture is the effect of pressing "shift + >" once:

top view 05

Views are now sorted by %MEM.

Change the progress display field

1. Hit the "f" key, top to enter another view, where you can arrange the display fields in the basic view:

top view 06

All process fields that can be displayed in the top basic view are listed here. Fields marked with "*" and uppercase letters are displayable, and fields without "*" and lowercase letters are not displayed. If you want to display the "CODE" and "DATA" fields in the basic view, you can do this by hitting the "r" and "s" keys:

top view 07

2. "Enter" returns to the basic view, you can see two more fields "CODE" and "DATA":

top view 08

Supplement to the top command The
top command is the preferred command for system monitoring on Linux, but sometimes it cannot meet our requirements. For example, the current server has great limitations in top monitoring. This server runs a websphere cluster and has two node services, namely the eldest and the second java processes in [top view 01]. The smallest unit of monitoring of the top command is the process, so I can't see the number of java threads I care about. And the number of customer connections, and these two indicators are very important indicators of java web services, usually I use the ps and netstate commands to supplement the lack of top.


Monitor the number of java threads:
ps -eLf | grep java | wc -l

 

Monitor network client connections:
netstat -n | grep tcp | grep listening port | wc -l

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324144105&siteId=291194637