Linux Top command usage details _unix Linux

Source: Internet
Author: User
Tags sleep cpu usage java web

View multi-core CPU commands
Mpstat-p All and sar-p all

Description:sar-p all > Aaa.txt redirect output to file Aaa.txt

The top command is often used to monitor Linux system conditions, such as CPU, memory use, the programmer basically know the command, but it is strange that there are few people can use it well, such as the top monitor view of the memory value of the meaning of a lot of misunderstanding.

This article describes the meaning of the various data in the top view, including the sorting of the fields of each process (task) in the view, through a top monitor screenshot of a running Web server.

Top Entry View

Top View 01
"Top View 01" is the basic view that just entered the top, and we'll combine this view to explain the meaning of each data.

Copy Code code as follows:

First line:
10:01:23-Current system time
126 Day, 14:29-the system has been running for 126 days 14 hours and 29 minutes (no reboot during this period)
2 users-currently has 2 user login system
Load average:1.15, 1.42, 1.44-load average the following three numbers are 1 minutes, 5 minutes, 15 minutes of loading.

The load average data is the number of active processes that are checked every 5 seconds, and then the values calculated by a specific algorithm. If this number is divided by the number of logical CPUs, the result is higher than 5, indicating that the system is overloaded.
Copy Code code as follows:

Second line:
tasks-task (process), the system is now a total of 183 processes, of which 1 are running, 182 in hibernation (sleep), stoped state of 0, zombie State (zombie) has 0.

Copy Code code as follows:

Third line: CPU state
6.7% the percentage of CPU that us-user space consumes.
0.4% sy-The percentage of CPU consumed by the kernel space.
0.0% ni-Percentage of CPU per process that has changed priority
92.9% id-Idle CPU percent
0.0% Wa-io% of CPU waiting to be consumed
0.0% hi-Hard Interrupt (Hardware IRQ)% of CPU usage
0.0% si-Soft Interrupt (Software interrupts) Percentage of CPU utilization

The CPU usage ratio here is different from the Windows concept, and if you don't understand user space and kernel space, you need to recharge it.
Copy Code code as follows:

Line four: Memory status
8306544k total-Total Physical memory (8GB)
7775876k used-Total Memory in use (7.7GB)
530668k free-Total free memory (530M)
79236k buffers-Cache Memory (79M)

Copy Code code as follows:

Line five: Swap swap partition
Total 2031608k total-swap area (2GB)
Total number of swap areas used by 2556k used-(2.5M)
2029052k free-Free Swap area total (2GB)
4231276k cached-Buffer Total swap area (4GB)

To illustrate here is not to use the concept of Windows memory to understand this data, if the Windows way this server "dangerous": 8G of total Memory only 530M of available memory. The memory management of Linux has its particularity, complex point need a book to explain, here is simply point and our traditional concept (Windows) difference.

The total amount of memory in use (used) in line four refers to the amount of memory currently controlled by the system kernel, and the total amount of free memory is the number of cores that have not yet been included in its control range. Not all of the memory included in the kernel is in use, including memory that has been used in the past that can be reused now, and the kernel does not return these reusable memory back to free, so there will be fewer free memory on Linux, but don't worry about it.

If you calculate the amount of available memory out of habit, here's an approximate formula: line fourth free + fourth line buffers + fifth line cached, press this formula for this server's available memory: 530668+79236+4231276 = 4.7GB.

For memory monitoring, in the top we are constantly monitoring the fifth line of swap partition used, if this value is constantly changing, the kernel is constantly in memory and swap data exchange, this is the real memory is not enough.

Copy Code code as follows:

Line Six is a blank line

Copy Code code as follows:

Line seventh: Status monitoring for each process (Task)
pid-Process ID
user-Process Owner
pr-Process Priority
Ni-nice value. A negative value indicates a high priority, and a positive value indicates a low priority
Total amount of virtual memory used by the virt-process, in kilobytes. Virt=swap+res
The amount of physical memory that the res-process uses, not swapped out, in kilobytes. Res=code+data
shr-shared memory size, per kb
S-process state. d= non-disruptive sleep state r= run s= sleep t= trace/stop z= zombie Process
%cpu-percentage of CPU time that was last updated to current
Percentage of physical memory used by the%mem-process
Total CPU time used by the time+-process, in units 1/100 seconds
command-Process Name (command name/command line)

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

Top View 02

In the view above, the server has 16 logical CPUs, which are actually 4 physical CPUs.

Process field Sorting
When you go to top by default, processes are sorted by CPU usage, and Java processes with a process ID of 14210 in top view 01 are ranked first (CPU occupies 100%), and process ID 14183 Java processes are ranked second (CPU occupies 12%). You can change the sort fields by keyboard commands, such as monitoring which process takes up the most mem, and I use the following methods generally:

1. Tapping the keyboard "B" (turn on/off brightening effect), the view of top changes as follows:

Top View 03

We found that the "top" process with a process ID of 10704 was highlighted, and the top process was the only running state (runing) that was displayed in the second row of the view, and you could turn off or turn on the highlight effect of the running process by tapping the "Y" key.

2. Tapping the keyboard "X" (The highlight effect on/off the row sequence), the top view changes as follows:

Top View 04

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

3. Through SHIFT + > or SHIFT + <, you can change the order of the column to the right or left, and the following illustration is a "SHIFT + >" Effect chart:

Top View 05

The view is now sorted by%mem.

Change the Process display field

1. Tap the "F" key and top into another view, where you can arrange the display fields in the basic view:

Top View 06

This lists all the process fields that can be displayed in the top base view, and fields that have "*" and are marked as uppercase are visible, and fields that do not have "*" and are lowercase letters are not displayed. If you want to display the "CODE" and "DATA" two fields in the basic view, you can click the "R" and "s" Keys:

Top View 07

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

Top View 08

Add to Top command
The top command is the preferred command for system monitoring on Linux, but sometimes it does not reach our requirements, such as the current server, top monitoring has great limitations. This server runs the WebSphere cluster, has two node services, is "Top view 01" in the eldest, the old 22 Java process, the top command of the smallest unit of monitoring is the process, so I do not see the number of Java threads and customer connections, These two metrics are a very important metric for Java Web Services, and I usually use PS and netstate two commands to complement top's deficiencies.

Copy Code code as follows:

To monitor the number of Java threads:
ps-elf | grep java | Wc-l

Copy Code code as follows:

Monitor the number of network client connections:
Netstat-n | grep TCP | grep Listening Port | Wc-l

The above two commands can change grep parameters to achieve more detailed monitoring requirements.
In the Linux system "Everything is a file" under the guidance of the idea, all the running state of the process can be used to obtain files. system root in/proc, the name of each number subdirectory is the PID of the running process, into any process directory, through which files or directories to observe the running of the process, such as the task directory is used to describe the process threads, So you can also get the number of threads running in a process (PID refers to the process ID) in the following ways:
Copy Code code as follows:

Ls/proc/pid/task | Wc-l

There is also a command pmap in Linux to output the status of the process memory that can be used to analyze the thread stack:
Copy Code code as follows:

Pmap PID

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.