Welcome to github.com/hsfxuebao/j… , hope to help you, if you feel ok, please click on the Star

The top command is a commonly used performance analysis tool in Linux. It displays the resource usage of each process in the system in real time, similar to the Task manager in Windows. The following details how to use it. Top is a dynamic display process, that is, the current state can be continuously refreshed by the user’s keystrokes. If this command is executed in the foreground, it monopolizes the foreground until the user terminates the program. More specifically, the top command provides real-time monitoring of the state of the system processor. It displays a list of the most CPU sensitive tasks on the system. This command can be used by CPU. Sort tasks by memory usage and execution time; And many of the features of the command can be set through interactive commands or in personal customization files.

1. Command format:

Top [parameter]

2. Run the following command:

This section describes how to view information about processes that are running, including process IDS, memory usage, and CPU usage

3. Command parameters:

– b batch

-c Displays complete commands

-i Ignores the failure process

-s Indicates the confidential mode

-s Indicates the accumulation mode

-i< time > Sets the interval

-u< User name > Specifies the user name

-p< process number > Specifies the process

-n< times > Indicates the number of times that the loop is displayed

4. Example:

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): Sy us 5.9%, 3.4%, 0.0% ni, 90.4% id, wa, 0.0%, 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.2g 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 00 S 0.0 0.0 0:00.00 ksoftirqd/0 4 root rt-5 00 S 0.0 0.0 0:00.00 watchdog/0 5 root RT -5 00 S 0.0 0.0 0.0.80 Migration /1 6 root 34 19 00 S 0.0 0.0 0.0.00 ksoftirqd/1 7 root rt-5 00 S 0.0 0.0 0:00.00 watchdog/1 8 root rt-5 00 S 0.0 0.0 0:20.59 Migration /2 9 root 34 19 00 S 0.0 0.0 0:00.09 ksoftirqd/2 10 Root rt-5 00 S 0.0 0.0 0:00.00 watchdog/2 11 root rt-5 00 S 0.0 0.0 0:23.66 migration/3 12 root 34 19 00 S Ksoftirqd /3 14 root rt-5 00 S 0.0 0.0 0:20.29 watchdog/3 14 root rt-5 00 S 0.0 0.0 0:20.29 Migration /4 15 root 34 19 00 S 0.0 0.0 00.07 ksoftirqd/4 16 root rt-5 00 S 0.0 0.0 00.00 Rt-5 00 S 0.0 00 00 migration/5 18 root 34 19 00 S 0.0 00 0 30 root rt-5 00 S 00 00 00 S 00 00 0 6 Migration /6 21 root 34 19 00 S 0.0 0.0 0:00.05 Ksoftirqd /6 23 root rt-5 00 S 0.0 0.0 0:58.28 Migration /7Copy the code

Description:

Statistics area:

The first five lines are the overall statistics area of the current system. Let’s look at what each line of information means.

The first line contains the task queue information, which is the same as the result of the uptime command. The specific parameters are described as follows:

14:06:23 – The current system time

Up 70 days, 16:44 – The system has been running for 70 days, 16 hours and 44 minutes (well, I must shout that the system has not been restarted during this period!)

2 users – Currently, two users log in to the system

Load Average: 1.15, 1.42, 1.44 – The next three numbers of load Average are 1 minute, 5 minutes, and 15 minutes respectively.

The Load Average data is the number of active processes checked every five seconds and calculated by a specific algorithm. If this number is divided by the number of logical cpus, anything above 5 indicates that the system is overloaded.

The second line, Tasks – task (process), is described as follows:

The system currently has 206 processes, including 1 running, 205 sleeping, 0 stoped, and 0 zombie.

The third line, CPU status information, specific attributes are described 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 used by processes that changed their priority

90.4% ID – Percentage of idle cpus

0.0% CPU usage of WA-IO waits

0.0% HI – Percentage of hard interrupts (IRQ) used by cpus

0.2% SI – Percentage of the CPU used by Software Interrupts

Note: the CPU usage ratio is different from that of Windows. You need to understand the user space and kernel space of Linux system.

The fourth line, memory status, 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 — Amount of memory for buffers (169M)

In line 5, swap swaps partition information. The details are described as follows:

32764556k total – total switching area (32GB)

0K Used – Total amount of swap areas used (0K)

32764556k free – total free swap area (32GB)

3612636K cached – Total swap area for the buffer (3.6GB)

Remark:

In line 4, used refers to the amount of memory currently controlled by the kernel, and free refers to the amount that the kernel does not yet control. Not all kernel managed memory is in use, including memory that was used in the past and can now be reused. The kernel does not return this reusable memory to free, so on Linux there will be less and less free memory, but don’t worry about that.

If you want to calculate the available memory out of habit, here is an approximate formula: Free in line 4 + buffers in line 4 + cached in line 5. The available memory for this server is 18537836K + 169884K + 3612636K = 22GB.

For memory monitoring, in top we should always monitor the fifth line of swap partition used. If this value is constantly changing, it indicates that the kernel is constantly exchanging data between memory and swap, which is the real memory is insufficient.

Sixth line, blank line.

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

PID – ID of a process

USER – Process owner

PR – Process priority

NI – nice value. A negative value indicates a high priority and a positive value indicates a low priority

VIRT – Total amount of virtual memory used by a process, in KB. VIRT=SWAP+RES

RES – The amount of physical memory used by a process that has not been swapped out, in KB. RES=CODE+DATA

SHR – Size of shared memory, expressed in KB

S – Process status. D= uninterruptible sleep state R= Run S= sleep T= track/stop Z= zombie process

%CPU – Percentage of CPU time occupied since last update

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

TIME+ – Total CPU TIME used by the process, expressed in 1/100 second

COMMAND – Process name (COMMAND name/COMMAND line)

Other Tips:

1. Multi-u and multi-core CPU monitoring

In the Top Basic view, press the keyboard number “1” to monitor the health of each logical CPU:

Looking at the figure above, the server has 16 logical cpus, which is actually 4 physical cpus. Press the number key 1 again to return to the Top Basic view screen.

2. Highlight the running process

Press “B” (to turn highlight on/off) and top’s view changes as follows:

We noticed that the “top” process with process ID 2570 is highlighted. The top process is the only runing process shown in the second row of the view. You can turn on or off the highlighting of the running process by pressing the “y” key.

3. Sort process fields

In the following figure, The Java process whose ID is 28894 ranks first (occupying 142% of the CPU), and the Java process whose ID is 574 ranks second (occupying 16% of the CPU).

Press the keyboard “X” (to turn on/off the highlighting effect of the sequence) and the view of top changes as follows:

As you can see, the default ranking of top is “%CPU”.

4. Shift + > or Shift + < can change the order to the right or left

Below is the effect of pressing “Shift + >” once, the view is now sorted by %MEM.

Example 2: Display the complete command

Command:

top -c

Output:

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:

Example 5: Set the number of information updates

Command:

top -n 2

Output:

Description:

Indicates that the update is terminated after two updates

Example 6: Set the information update time

Command:

top -d 3

Output:

Description:

The update interval is 3 seconds

Example 7: Displays information about the specified process

Command:

top -p 574

Output:

Description:

5. Run the top interactive command

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 masked if the S option is used on the command line.

H Displays the help screen and gives a brief summary of the command

K terminates a process.

I Ignore idle and dead processes. This is an on-off command.

Q exit program

R Reprioritizes a process

S Switch to the accumulative mode

S changes the delay between refreshes (in s), converted to m s if there is a decimal. Enter a value of 0 and the system will refresh continuously. The default value is 5 seconds

F or f adds or removes items from the current display

O or O changes the order in which items are displayed

L Toggle display load average and start time information

M Displays memory information

T Displays the process and CPU status

C Switch between displaying the command name and the complete command line

M sorts by resident memory size

P Sort by percentage CPU usage

T Sort by time/cumulative time

W writes the current Settings to the ~/.toprc file

From: One Linux command per day (35) : top command