Detailed explanation of Linux top command

Reprinted from: https://www.cnblogs.com/dragonsuc/p/5512797.html
View multi-core CPU commands

mpstat -P ALL and sar -P ALL 
instructions: sar -P ALL > aaa.txt redirects the output to the file aaa.txt

 

top command

It is often used to monitor the system status of Linux, such as the use of cpu and memory. Programmers basically know this command, but it is strange that very few people can use it well. For example, the meaning of the memory value in the top monitoring view is Lots of misunderstandings.
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 the sorting of the fields of each process (task) in the view.

 

top enter view

 

    The first line:
    10:08:45 — the current system time is
    10 days, 3:05 — the system has been running for 10 days, 3 hours and 5 minutes (without restarting during this period)
    1 users — there is currently 1 user logged in to the system
    load average : 0.00, 0.00, 0.00 — The three numbers after the load average are the load conditions for 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 135 processes, of which 1 is running, 134 are sleeping (sleep), 0 are in the stopped state, and 0 are in the zombie state (zombie). indivual.

    The third line: cpu status
    0.3% us - the percentage of CPU occupied by user space.
    0.0% sy — The percentage of CPU used by kernel space.
    0.0% ni — 99.7% of CPU occupied by processes whose priorities have changed
    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
    3808060k total — total physical memory (4GB)
    3660048k used — total memory in use (3.6GB)
    148012k free — total free memory (148M)
    359760k buffers — cached memory (359M

    ) Five lines: swap swap partition
    4184924k total - total swap area (4G)
    0k used - total used swap area (0M)
    4184924k free - total free swap area (4G)
    2483956k cached - total buffered swap area (2483M)

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 these reusable memory to free, so free memory on linux will be Less and less, but don't worry about it.

If you calculate the number of available memory out of habit, here is an approximate calculation formula: free in the fourth line + buffers in the fourth line + cached in the fifth line, according to this formula, the available memory of this server: 148M+259M+2483M = 2990M.

For memory monitoring, in the top, we need to monitor the used of the swap partition of the fifth row at all times. If this value is constantly changing, it means that the kernel is constantly exchanging data between memory and swap, which is 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 usage percentage since last update
    %MEM - physical memory percentage
    used by process TIME+ - CPU used by 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:

  

Looking at the picture above, the server has 4 logical CPUs, which is actually 1 physical CPU.

If you do not press 1, the average value of all CPUs is displayed in the top view.

Process field sorting

When entering the top by default, each process is sorted according to the occupancy of the CPU. In [top view 01], the java process with process ID 14210 is ranked first (CPU occupancy is 100%), and the java process with process ID 14183 is ranked first. In the 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:

    

We found that the "top" process with a process id of 12363 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 view of top changes as follows:
You can see that the default sorting column of 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:

 

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:

 

 

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:

 

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

 

 

 

Supplement to top command

The top command is the preferred command for system monitoring on Linux, but sometimes it fails to meet our requirements. For example, the current server, top monitoring has great limitations. This server runs a websphere cluster and has two node services, namely the eldest and 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 the number of network client connections:

netstat -n | grep tcp | grep listen port | wc -l

The above two commands can change the parameters of grep to achieve more detailed monitoring requirements.

Under the guidance of the Linux system "everything is a file", the running status of all processes can be obtained by using files. In the system root directory /proc, the name of each digital subdirectory is the PID of the running process. Entering any process directory, you can observe various running indicators of the process through the files or directories in it. For example, the task directory is used to Describes the threads in the process, so you can also get the number of threads running in a process by the following method (PID refers to the process ID):

ls /proc/PID/task | wc -l
There is also a command pmap in linux to output the status of the process memory, which can be used to analyze the thread stack:

 pmap PID

Everyone is familiar with Linux, you can use the top command to view the memory, CPU and other information of all processes. In addition, there are other commands that can get more detailed information, such as process related

cat /proc/your_PID/status  

Get the PID of the process by top or ps -ef | grep 'process name'. This command can provide information such as process status, number of file handles, memory usage, etc.
Memory related
    vmstat -s -S M  
This can view the report containing each item of memory, through -S M or -S k you can specify the viewing unit, the default is kb. Combined with the watch command, you can see the dynamic change report.

Also available cat /proc/meminfo  

Depends on the cpu configuration information is available

cat /proc/cpuinfo  

It can display information such as the number of CPU cores, clock frequency, CPU model and so on.

To check cpu fluctuations, especially on multi-core machines, you can use

mpstat -P ALL 10 

该命令可间隔10秒钟采样一次CPU的使用情况,每个核的情况都会显示出来,例如,每个核的idle情况等。
只需查看均值的,可用
    iostat -c 
IO相关
    iostat -P ALL  
该命令可查看所有设备使用率、读写字节数等信息。

另外,htop ,有时间可以用一下。

 

 

Linux查看物理CPU个数、核数、逻辑CPU个数

# 总核数 = 物理CPU个数 X 每颗物理CPU的核数

# 总逻辑CPU数 = 物理CPU个数 X 每颗物理CPU的核数 X 超线程数

 

# 查看物理CPU个数

cat /proc/cpuinfo| grep "physical id"| sort| uniq| wc -l

 

# 查看每个物理CPU中core的个数(即核数)

cat /proc/cpuinfo| grep "cpu cores"| uniq

 

# 查看逻辑CPU的个数

cat /proc/cpuinfo| grep "processor"| wc -l

 

 查看CPU信息(型号)
cat /proc/cpuinfo | grep name | cut -f2 -d: | uniq -c

Guess you like

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