Home > php教程 > PHP开发 > body text

linux command: top command

高洛峰
Release: 2016-12-13 11:59:35
Original
1744 people have browsed it

The top command is a commonly used performance analysis tool under Linux. It can display the resource usage of each process in the system in real time, similar to the Windows Task Manager. The following describes how to use it in detail. top is a dynamic display process, that is, the current status can be continuously refreshed through the user's keystrokes. If this command is executed in the foreground, it will occupy the foreground exclusively until the user terminates the program. To be more precise, the top command provides real-time control of the system Processor status monitoring. It will display a list of the most CPU "sensitive" tasks in the system. This command can sort tasks by CPU usage, memory usage and execution time; and many features of this command can be accessed through interactive commands or Make settings in the personal customization file.

1. Command format:

top [parameter]

2. Command function:

Display relevant information about the process currently being executed by the system, including process ID, memory usage, CPU usage, etc.

3. Command parameters:

-b Batch processing

-c Display complete treatment command

-I Ignore the failure process

-s Confidential mode

-S Accumulation mode

-i

-u Specify username

-p Specify process

-n Number of times to cycle through


4. Usage examples:

Example 1: Display process information

Command:

top

Output:

[root@TG1704 log]# top
top - 14:06:23 up 70 days, 16:44,  2 users,  load average: 1.25, 1.32, 1.35
Tasks: 206 total,   1 running, 205 sleeping,   0 stopped,   0 zombie
Cpu(s):  5.9%us,  3.4%sy,  0.0%ni, 90.4%id,  0.0%wa,  0.0%hi,  0.2%si,  0.0%st
Mem:  32949016k total, 14411180k used, 18537836k free,   169884k buffers
Swap: 32764556k total,        0k used, 32764556k free,  3612636k cached

  PID USER      PR  NI  VIRT  RES  SHR S %CPU %MEM    TIME+  COMMAND                                                                
28894 root      22   0 1501m 405m  10m S 52.2  1.3   2534:16 java                                                                   
18249 root      18   0 3201m 1.9g  11m S 35.9  6.0 569:39.41 java                                                                   
 2808 root      25   0 3333m 1.0g  11m S 24.3  3.1 526:51.85 java                                                                   
25668 root      23   0 3180m 704m  11m S 14.0  2.2 360:44.53 java                                                                   
  574 root      25   0 3168m 611m  10m S 12.6  1.9 556:59.63 java                                                                   
 1599 root      20   0 3237m 1.9g  11m S 12.3  6.2 262:01.14 java                                                                   
 1008 root      21   0 3147m 842m  10m S  0.3  2.6   4:31.08 java                                                                   
13823 root      23   0 3031m 2.1g  10m S  0.3  6.8 176:57.34 java                                                                   
28218 root      15   0 12760 1168  808 R  0.3  0.0   0:01.43 top                                                                    
29062 root      20   0 1241m 227m  10m S  0.3  0.7   2:07.32 java                                                                   
    1 root      15   0 10368  684  572 S  0.0  0.0   1:30.85 init                                                                   
    2 root      RT  -5     0    0    0 S  0.0  0.0   0:01.01 migration/0                                                            
    3 root      34  19     0    0    0 S  0.0  0.0   0:00.00 ksoftirqd/0                                                            
    4 root      RT  -5     0    0    0 S  0.0  0.0   0:00.00 watchdog/0                                                             
    5 root      RT  -5     0    0    0 S  0.0  0.0   0:00.80 migration/1                                                            
    6 root      34  19     0    0    0 S  0.0  0.0   0:00.00 ksoftirqd/1                                                            
    7 root      RT  -5     0    0    0 S  0.0  0.0   0:00.00 watchdog/1                                                             
    8 root      RT  -5     0    0    0 S  0.0  0.0   0:20.59 migration/2                                                            
    9 root      34  19     0    0    0 S  0.0  0.0   0:00.09 ksoftirqd/2                                                            
   10 root      RT  -5     0    0    0 S  0.0  0.0   0:00.00 watchdog/2                                                             
   11 root      RT  -5     0    0    0 S  0.0  0.0   0:23.66 migration/3                                                            
   12 root      34  19     0    0    0 S  0.0  0.0   0:00.03 ksoftirqd/3                                                            
   13 root      RT  -5     0    0    0 S  0.0  0.0   0:00.00 watchdog/3                                                             
   14 root      RT  -5     0    0    0 S  0.0  0.0   0:20.29 migration/4                                                            
   15 root      34  19     0    0    0 S  0.0  0.0   0:00.07 ksoftirqd/4                                                            
   16 root      RT  -5     0    0    0 S  0.0  0.0   0:00.00 watchdog/4                                                             
   17 root      RT  -5     0    0    0 S  0.0  0.0   0:23.07 migration/5                                                            
   18 root      34  19     0    0    0 S  0.0  0.0   0:00.07 ksoftirqd/5                                                            
   19 root      RT  -5     0    0    0 S  0.0  0.0   0:00.00 watchdog/5                                                             
   20 root      RT  -5     0    0    0 S  0.0  0.0   0:17.16 migration/6                                                            
   21 root      34  19     0    0    0 S  0.0  0.0   0:00.05 ksoftirqd/6                                                            
   22 root      RT  -5     0    0    0 S  0.0  0.0   0:00.00 watchdog/6                                                             
   23 root      RT  -5     0    0    0 S  0.0  0.0   0:58.28 migration/7
Copy after login

Description:

Statistical information area:

The first five lines are the overall statistical information area of ​​the current system situation. Below we look at the specific meaning of each line of information.

The first line, the task queue information, is the same as the execution result of the uptime command. The specific parameter description is as follows:

14:06:23 — Current system time

up 70 days, 16:44 — The system has been running for 70 days 16 hours and 44 minutes (the system has not been restarted during this period!)

2 users — There are currently 2 users logged into the system

load average: 1.15, 1.42, 1.44 — The three numbers after load average are 1 minute each , 5 minutes, 15 minutes of load conditions.

Load average data is a value calculated by checking the number of active processes every 5 seconds and then calculating it according to a specific algorithm. If this number is divided by the number of logical CPUs, a result higher than 5 indicates that the system is overloaded.

The second line, Tasks - tasks (processes), the specific information is as follows:

The system now has a total of 206 processes, of which 1 is running, 205 are sleeping, and 0 are in stopped state , there are 0 zombies in zombie status.

The third line, cpu status information, the specific attribute description is as follows:

5.9%us - the percentage of CPU occupied by user space.

3.4% sy — The percentage of CPU occupied by kernel space.

0.0% ni — The percentage of CPU occupied by processes that have changed priority

90.4% id — The percentage of idle CPU

0.0% wa — The percentage of CPU occupied by IO waiting

0.0% hi — Hardware IRQ occupied The percentage of CPU

0.2% si - The percentage of CPU occupied by software interrupts

Note: The CPU usage ratio here is different from the windows concept. You need to understand the relevant knowledge of the Linux system user space and kernel space!

The fourth line, memory status, the specific information is as follows:

32949016k total — total physical memory (32GB)

14411180k used — total memory in use (14GB)

18537836k free — total free memory (18GB) )

169884k buffers - the amount of cached memory (169M)

The fifth line, swap partition information, the specific information is as follows:

32764556k total - the total amount of swap area (32GB)

0k used - the total used swap area Amount (0K)

32764556k free - Total free swap area (32GB)

3612636k cached - Total buffered swap area (3.6GB)


Remarks:

The total memory used in the fourth line The amount (used) 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 included in its control. Not all the memory managed by the kernel is in use, but also includes memory that has been used in the past and can now be reused. The kernel does not return these reusable memories to free, so free memory on Linux will Less and less, but don’t worry about it.

If you calculate the 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: 18537836k + 169884k +3612636k = about 22GB.

For memory monitoring, in top, we must always monitor the used of the swap partition in the fifth line. If this value is constantly changing, it means that the kernel is constantly exchanging data between memory and swap. This means that the real memory is not enough.

The sixth line is an empty line.

The seventh line below: status monitoring of each process (task), the item column information is as follows:

PID — process id

USER — process owner

PR — process priority

NI — nice value. Negative values ​​represent high priority, positive values ​​represent low priority

VIRT — The total amount of virtual memory used by the process, in kb. VIRT=SWAP+RES

RES — The size of the physical memory used by the process that has not been swapped out, in kb. RES=CODE+DATA

SHR — Shared memory size, unit kb

S — Process status. D=Uninterruptible sleep state R=Run S=Sleep T=Track/Stop Z=Zombie process

%CPU — The percentage of CPU time occupied since the last update

%MEM — The percentage of physical memory used by the process

TIME+ — Total CPU time used by the process, unit 1/100 second

COMMAND — Process name (command name/command line)


Other usage tips:

1. Multi-U multi-core CPU monitoring

Basic on top In the view, press the keyboard number "1" to monitor the status of each logical CPU:

linux command: top command

Observe the picture above, the server has 16 logical CPUs, which are actually 4 physical CPUs. Press the numeric key 1 again to return to the top basic view interface.

2. Highlight the currently running process

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

linux command: top command

We found that the "top" process with process ID 2570 was When highlighted, the top process is the only running process displayed on the second line of the view. You can turn off or turn on the highlighting effect of running processes by tapping the "y" key.

3. Process field sorting

When entering top by default, each process is sorted according to CPU usage. In the figure below, the java process with process ID 28894 is ranked first (cpu usage 142%), and the process ID is The java process with 574 ranked second (cpu occupied 16%).

linux command: top command

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

linux command: top command

You can see that the default sort column of top is "%CPU".


4. You can change the sorting column to the right or left by "shift + >" or "shift +

The picture below is the effect of pressing "shift + >" once. The view is now as follows %MEM to sort.

linux command: top command

Example 2: Display the complete command

Command:

top -c

Output:

linux command: top command

Description:


Example 3: Display program information in batch mode

Command:

top -b

Output:

Description:


Example 4: Display program information in cumulative mode

Command:

top -S

Output:

Description:


Examples 5: Set the number of information updates

Command:

top -n 2

Output:

Description:

means to terminate the update display after updating twice


Example 6: Set the information update time

Command:

top -d 3

Output:

Explanation:

means the update cycle is 3 seconds


Example 7: Display the specified process information

Command:

top -p 574

Output:

linux command: top command

Instructions:

5.top interactive commands


Some interactive commands that can be used during the execution of the top command. These commands are single-letter, and some of them may be blocked if the s option is used on the command line.

h Display the help screen and give some short command summary instructions

k Terminate a process.

i Ignore idle and zombie processes. This is a switch command.

q Exit the program

r Rearrange the priority level of a process

S Switch to cumulative mode

s Change the delay time between two refreshes (unit is s), if there is a decimal, convert it to m s. Enter a value of 0 and the system will refresh continuously. The default value is 5 seconds.

f or F. Add or delete items from the current display.

o or O. Change the order of displayed items.

l. Toggle display of average load and startup time information.

m Switch to display memory information

t Switch to display process and CPU status information

c Switch to display command name and complete command line

M Sort by resident memory size

P Sort by CPU usage percentage

T Sort by time/accumulated time

W Write current settings~/ .toprc file


Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Recommendations
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template