The Linux top command is detailed

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

A detailed explanation of the Linux top command usage
First, describe the meanings of some of the fields in top:

Virt:virtual Memory usage Virtual RAM
1. The process "required" virtual memory size, including the library, code, data, etc. used by the process
2, if the process to apply for 100m of memory, but the actual use of only 10m, then it will grow 100m, rather than the actual use of the volume

Res:resident Memory Usage resident RAM
1. The memory size currently used by the process, but does not include swap out
2. Sharing with other processes
3, if the application of 100m of memory, the actual use of 10m, it only grows 10m, and virt opposite
4, about the memory consumption of the library, it only statistics the size of the loaded library file memory

Shr:shared Memory Shared
1, in addition to the shared memory of the process itself, also includes the shared memory of other processes
2, although the process uses only a few shared library functions, but it contains the entire size of the shared library
3. Calculate the physical memory size formula that a process occupies: res–shr
4, swap out, it will be lowered

DATA
1, the memory occupied by the data. If top is not displayed, press the F key to display it.
2, the real data space required by the program is really in operation to use.

In the

Top run, you can control how processes are displayed through the internal commands of top. The internal command is as follows:
S-Change the screen update frequency
L – Turn off or turn on the first section of the first line top information
T-close or open the first part second line the next row of Tasks and the third row Cpus information representation
M – close or open the first part of the four-line Mem and The representation of the five-line Swap information
N – the list of processes in the order of the PID
P – the list of processes in the order of CPU utilization
M – the list of processes in the order of memory footprint
H – Display Help
N – Settings in the process list The number of processes displayed
Q – Exit top
S-Change the screen update period

Ordinal column name meaning
A PID process ID
b PPID Parent Process ID
C ruser Real User name
D UID Process Owner's user ID
e username of user process Owner
Group name of the F group Process owner
The terminal name of the G TTY boot process. Processes that are not started from the terminal are displayed as?
H PR-Priority
I NI nice value. Negative values indicate high priority, positive values indicate low priority
The last CPU used by J P is meaningful only in a multi-CPU environment
K%cpu CPU time consumption percentage last updated to current
The total CPU time, in seconds, used by the timing process
The total CPU time used by the M time+ process, in units 1/100 seconds
n the percentage of physical memory used by the%MEM process
o The total amount of virtual memory used by the VIRT process, in kilobytes. Virt=swap+res
The P swap process uses the size of the virtual memory, which is swapped out, in kilobytes.
The size of the physical memory, in kilobytes, that the Q RES process uses and has not been swapped out. Res=code+data
R code executable code occupies the physical memory size, in kilobytes
The amount of physical memory that is used outside of the S data executable code (data segment + stack), in kilobytes
T SHR shared memory size, in kilobytes
U Nflt page Error count
V NDRT the number of pages that have been modified in the last write to now.
W S Process state. (d= non-disruptive sleep status, r= run, s= sleep, t= track/Stop, z= zombie process)
x command name/command line
Y Wchan If the process is sleeping, the system function name in sleep is displayed
Z Flags task Flag, reference sched.h

By default, only the more important PID, USER, PR, NI, VIRT, RES, SHR, S,%cpu,%MEM, time+, and COMMAND columns are displayed. You can change the display by using the following shortcut keys.

The F key allows you to select what is displayed. Press the F key to display a list of columns, press A-Z to show or hide the corresponding column, and then press ENTER to confirm.
Press the O key to change the order in which the columns are displayed. A-Z in the lower case moves the corresponding column to the right, while the uppercase A-Z moves the corresponding column to the left. Finally, press ENTER to confirm.
Press the uppercase F or O key, and then press A-Z to sort the process by the appropriate column. The uppercase R key can reverse the current sort.

Top How to use:

Use format:

Top [-] [d] [P] [Q] [C] [C] [s] [s] [n]

Parameter description:

D: Specifies the time interval between each two screen information refresh. Of course the user can use the s interactive command to change it.

P: Monitor only the state of a process by specifying the monitoring process ID.

Q: This option will cause top to refresh without any delay. If the calling program has Superuser privileges, top will run at the highest possible priority.

S: Specifies the cumulative mode.

S: Causes the top command to run in Safe mode. This removes the potential danger of interactive commands.

I: Make top not show any idle or zombie processes.

C: Displays the entire command line instead of just displaying the command name.

Common Command Description:

Ctrl+l: Erase and rewrite screen

K: Terminates a process. The user will be prompted for the process PID to be terminated and what signal needs to be sent to the process. The normal termination process can use a 15 signal, and if not, use signal 9 to force end the process. The default value is signal 15. This command is masked in safe mode.

I: Ignore idle and zombie processes. This is a switch-type command.

Q: Exit the program

R: Reschedule the priority level of a process. The user is prompted to enter the process PID that needs to be changed and the process priority value that needs to be set. Entering a positive value lowers the priority and, conversely, it gives the process a higher priority. The default value is 10.

S: Switch to cumulative mode.

S: Change the delay time between two refreshes. The user will be prompted to enter a new time in S. If there are decimals, it is converted into M S. Enter a value of 0 and the system will refresh continuously, the default value is 5 S. It is important to note that if you set too small a time, it is likely to cause a constant refresh, so it is too late to see the display, and the system load will be greatly increased.

F or F: Add or remove items from the current display.

O or O: Change the order in which items are displayed

L: Toggle display of average load and start time information.

M: Toggles display memory information.

T: Toggles display of process and CPU status information.

C: Toggle display command name and full command line.

M: Sorts based on the size of the resident memory.

P: Sorts according to the percentage size of CPU usage.

T: Sort by Time/cumulative time.

W: Writes the current settings to the ~/.TOPRC file.

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 usage, the programmer basically knows this command, but it is strange to use it very few people, such as the top monitoring view of the memory value of the meaning of a lot of distortion.

This article covers the meaning of various data in the top view and the sorting of the fields of each process (task) in the view through top monitoring of a running Web server.

Enter the top command

1.1 System run time and average load:

The top of the top command displays output similar to the uptime command
These fields display:

Current time
Time the system has been running
Number of currently logged-in users
The average load in the corresponding last 5, 10, and 15 minutes.
You can use the ' l ' command to toggle the display of uptime.

21:45:11-Current system time
0 days, 4:54-the system has been running for 4 hours and 54 minutes (not restarted during this period)
2 users-currently has 2 users logged into the system
The three numbers behind the load average:0.24, 0.15, 0.19-load average are 5-minute, 10-minute, 15-minute loads respectively.
The load average data is the number of active processes that are checked every 5 seconds and then calculated by a particular algorithm. If this number is divided by the number of logical CPUs, the result above 5 indicates that the system is overloaded.

1.2 Tasks:
2.
3.tasks-task (process), the system now has a total of 144 processes, of which there are 1 in the running, 143 in hibernation (sleep), 0 in the stoped state, and 0 in the zombie State (zombie).

The second line shows a summary of the task or process. The process can be in a different state. This shows the total number of processes. In addition, there are the number of running, sleeping, stopping, zombie processes (zombies are the state of a process). These process summary information can be toggled with ' t ' Display

1.3 CPU Status:
2.
This shows the percentage of CPU time in different modes, which represent the different CPU time:
US, User: CPU time for running (non-prioritized) users process
Sy,system: CPU time for running kernel processes
Ni,niced: CPU time of the user process that has the adjusted priority run
Wa,io wait: CPU time to wait for IO to complete
Hi: CPU time to handle hardware interrupts
Si: CPU time to handle software interrupts
ST: this virtual machine is hypervisor to steal the CPU time (if you are currently in a VM under a hypervisor, in fact, Hypervisor also consumes a portion of CPU processing time).
You can toggle the display using the ' t ' command.

1.3% us-The percentage of CPU consumed by user space.
1.0% sy-The percentage of CPU consumed by the kernel space.
0.0% ni-The percentage of CPU that has changed the priority of the process
97.3% id-Idle CPU percentage
0.0% Wa-io waiting for CPU percentage
0.3% hi-Hard Interrupt (Hardware IRQ)% of CPU occupied
0.0% si-Soft Interrupt (software interrupts)% of CPU occupied
In this case the CPU usage ratio is different from the Windows concept, and if you don't understand the user space and kernel space, it needs to be recharged.

1.4 Memory Usage:

The next two lines show memory usage, a bit like the ' free ' command. The first line is physical memory usage, and the second line is virtual memory usage (swap space).

The physical memory is displayed as follows: All available memory, used memory, free memory, buffered memory. Similarly: The swap section shows: all, used, idle, and buffered swap space.

The memory display can be toggled with the ' m ' command.

509248k total-Total Physical memory (509M)
495964k used-Total Memory in use (495M)
13284k free-Total free memory (13M)
25364k buffers-Cache Memory (25M)

Swap swap partition
492536k total-Swap Area total (492M)
Total swap area used by 11856k used-(11M)
480680k free-Total Idle swap area (480M)
202224k cached-Buffer Swap area total (202M)

The point here is that this data cannot be understood in the memory concept of Windows, which is "dangerous" if you press windows: The total amount of memory in 8G is only 530M of available memory. The memory management of Linux has its particularity, the complex point needs a book to explain, here is simply to say the point and our traditional concept (Windows) is different.

The total amount of memory in use in row four (used) refers to the amount of memory that is now controlled by the system kernel, and the total amount of free memory that the kernel has not included in its control range. The memory that is included in kernel management is not always in use, but also includes the memory that has been used in the past that can now be reused, and the kernel does not return these reusable memory to free, so there is less memory on Linux, but don't worry about it.

If you are accustomed to calculating the number of available memory, here is an approximate formula: The fourth line of Free + fourth row buffers + fifth row of cached, press this formula for this server's available memory:

13284+25364+202224 = 240M.

For memory monitoring, in the top we have to monitor the fifth line swap partition used, if this value is constantly changing, indicating that the kernel is constantly in memory and swap data exchange, which is really not enough memory.

Line Six is a blank line
1.5 status monitoring of each process (Task):

PID: Process ID, unique identifier of the process

User: The actual username of the process owner.

PR: The scheduling priority of the process. Some values of this field are ' RT '. This means that these processes run in a real-time state.

NI: The nice value of the process (priority). The smaller the value means the higher the priority. Negative values indicate high priority, positive values indicate low priority

VIRT: The virtual memory used by the process. The total amount of virtual memory used by the process, in kilobytes. Virt=swap+res

RES: The size of the memory resident. Resident memory is the non-exchangeable physical memory size used by the task. The size, in kilobytes, of the physical memory that the process used and was not swapped out. Res=code+data

SHR:SHR is the shared memory used by the process. Shared memory size, in kilobytes

S: This is the state of the process. It has the following different values:

D-Non-interruptible sleep state.
R – Running state
S-Sleep state
T-is tracked or stopped
Z – Zombie State
%CPU: The percentage of CPU time that has been used by the task since the last time it was updated.

%MEM: Percentage of available physical memory used by the process.

Time+: The total CPU time to be used since the start of the task, exactly 1% seconds.

Command: Run the commands used by the process. Process name (command name/command line)

There are also many outputs that are not displayed by default, and they can display page faults, valid group and group IDs, and other more information for a process.

Interactive commands

2.1 ' H ': Help
Can I use H or? Displays the Help menu for the interactive command.

2.2 ' <ENTER> ' or ' <SPACE> ': Refresh the display
The top command refreshes the display by default at a specific interval (3 seconds). To refresh manually, the user can enter a carriage return or a space.

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

Top View 02

Observe that the server has 16 logical CPUs, which are actually 4 physical CPUs.

Process Field Sort
By default, when Top is reached, the processes are sorted by CPU consumption, the Java process with process ID 14210 in top view 01 is ranked first (CPU occupied 100%), and the Java process with process ID 14183 is ranked second (CPU consumption 12%). You can change the sort field by keyboard instructions, for example, to monitor which process consumes the most mem, I use the following method:

    1. Tap the keyboard "B" (Turn on/off the highlight effect) and the top view changes as follows:

      Top View 03

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

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

      Top View 04

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

    1. With SHIFT + > or SHIFT + <, you can change the row sequence to the right or left by pressing SHIFT + >:

      Top View 05

The view is now sorted according to%mem.

Change the Process display field

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

      Top View 06

Here is a list of all the process fields that can be displayed in the top basic view, with "" and the fields marked as uppercase are visible,and fields with lowercase letters are not displayed. If you want to display the "CODE" and "DATA" two fields in the base view, you can tap the "R" and "s" Keys:

Top View 07

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

      Top View 08

Additions to the 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, where top monitoring has a lot of limitations. This server runs the WebSphere cluster, there are two node services, is "Top view 01" in the eldest, the old 22 Java processes, the top command is the smallest monitoring unit of the process, so I do not see the number of Java threads and customer connections, And these two indicators are very important indicators of Java Web Services, usually I use PS and netstate two commands to supplement the top of the shortcomings.
Copy the code code as follows:

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

Copy the code code as follows:

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

The above two commands, can change the parameters of grep, to achieve more detailed monitoring requirements.
Under the guidance of the idea of "everything is a file" in the Linux system, the running state of all processes can be obtained by file. system root/proc, each digital subdirectory name is the PID of the running process, into any process directory, through which files or directories to observe the process of the various operational indicators, such as the task directory is used to describe the thread of the process, Therefore, you can also get the number of threads running in a process by using the following method (PID refers to the process ID):

Copy the code code as follows:

Ls/proc/pid/task | Wc-l

There is also a command pmap in Linux to output the state of the process memory that can be used to analyze the thread stack:

Copy the code code as follows:

Pmap PID

The Linux top command is detailed

Related Article

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.