Introduction to the

The more things you want to do, the more new things you will encounter. Today, let’s take a look at the Top command used to monitor server performance.

It is used to display the process dynamics, including the memory, CPU and other key status information that we often need to check. Determine which process is abnormally preempting system resources, and then decide whether to shut down the process. The top command has become a key indicator to measure the running status of the device.

The concept of RAM is physical memory that does not contain Swap space (Swap). There may be some students who don’t understand.

use

top
Copy the code

Statistics for the first five lines

Line 1 top

This line presents summary information about the system

Top-10:49:10 UP 364 days, 23:04, 1 User, load Average: 0.13, 0.11, 0.09Copy the code
  • 10:49:10: Indicates the current system time.
  • 364 days, 23:04: System has been running for 364 days, 23 minutes and 4 seconds.
  • 1 user: Indicates the number of login users. To obtain the list of current login users, use this parameterwhoThe command
  • 0.13, 0.11, 0.09: Indicates the average system load. The values are the average length of task queues within 1 minute, 5 minutes, and 15 minutes respectively.

Load Average is the average system load on a Linux server over a defined period of time, in other words, the sum of running threads and waiting threads at a given time. For multi-core processors, if the number of cores is less than or equal to 1, the system is working properly. If the value/kernel is greater than 1, the system will slow down running new tasks.

Some Application Performance Monitor (APM) software usually provides a load Average indicator. If the value is greater than the threshold set by the APM software, the load server script will be uploaded or server commands will be added to terminate the process that consumes the most CPU. This can be automated. Name one software: Site24x7.


Line 2 Tasks

Tasks collects statistics on running processes

Tasks: 196 total,   1 running, 195 sleeping,   0 stopped,   0 zombie
Copy the code
  • 196 total: The total number of processes currently running. This does not mean that the running processes have been executed.
  • 1 running, 195 sleeping, 0 stopped, 0 zombie: Running status of a process.1Three processes are executing,195Three processes are waiting for events to complete or for I/O operations to complete,0Four stopped processes,0Zombie processes.

This will involve how the system handles the process. The system marks the status of each process. If a process is performing I/O operations and the CPU is idle, the system switches to another process. In addition, if the process does not complete the task within the allowed execution time, the system still switches the execution process. This gives the user the impression of “multitasking”. So the system needs to mark the status of each process. A process has the following status flags:

  • A Runnable (R): The process in this state is executing on the CPU or exists in the run queue and can execute.
  • Interruptible sleep (S): The process in this state is waiting for the event to complete.
  • Uninterruptible sleep (D)In this case, the process is waiting for the I/O operation to complete.
  • Stopped (T): These processes can beJob control signal(for example, Ctrl + Z) stops or has been traced to stop.
  • Zombie (Z)The kernel maintains various data structures in memory to keep track of processes. A process may create multiple child processes, and they may exit while the parent process still exists. However, these data structures must be retained until the parent obtains the state of the child process. A process in which the terminated data structure remains is called a zombie.

Line 3% Cpu (s)

This line counts CPU usage.

%Cpu(s): 1.5us, 1.2SY, 0.0Ni, 92.2ID, 0.0wa, 0.0hi, 0.2Si, 0.0stCopy the code
  • 1.5 us: indicates the CPU usage of the user space.
  • 1.2 sy: indicates the percentage of CPU occupied by the kernel space.
  • 0.0 ni: indicates the CPU usage of the user process whose priority changed.
  • 92.2 id: Indicates the ratio of time the CPU stays idle. Most operating systems put the CPU in power-saving mode when idle.
  • 0.0 wa: Percentage of CPU time spent waiting for I/ OS.
  • 0.0 the hi: Indicates the CPU usage of hard interrupts (Hardware IRQ).
  • Si, 0.2 to 0.0 st: Total Time spent by the CPU serving soft interrupts, Steal Time.

Line 4 Mem

This line shows running memory (RAM) usage.

KiB Mem :  4046220 total,   206556 free,  3258284 used,   581380 buff/cache
Copy the code

Units are displayed in kilobytes (KiB) by default. Press the previous shortcut capital E to switch display units.

  • 4046220 total, 206556 free, 3258284 used: Total, free, used memory size
  • 581380 buff/cache: Disk cache. The Linux kernel also tries to reduce disk access time in various ways, so it maintains a “disk cache” in RAM, where it stores common areas of the disk.

Disk writes are stored in a “disk buffer,” and the kernel eventually writes them out to disk. Their total memory consumption is a “buff/cache” value. That may sound like a bad thing, but it’s not. The memory used by this cache will be allocated to the process as needed.


Line 5 Swap

This line is the information about swap space usage. When the memory is low, we all consider using Swap, which is to take part of the hard drive and use it as RAM.

When RAM usage nears full, less-used areas of RAM are written to Swap for later retrieval. But because disk access is slow, relying too much on Swap can hurt system performance.

KiB Swap:        0 total,        0 free,        0 used.   438744 avail Mem 
Copy the code
  • KiB Swap: 0 total, 0 free, 0 used: The size of the swap space “total”, “free”, “used”.
  • 438744 avail Mem: Size of swap space available for memory swapping.

If RAM is free, swapping with disk is not caused.

Task list area

The list of task areas is relatively simple, just look at the meaning of the header.

 PID USER      PR  NI    VIRT    RES    SHR S  %CPU %MEM     TIME+ COMMAND 
Copy the code
  • PID: indicates the process ID. It is a unique positive integer that identifies the process. We can get throughKill commandBring PID to kill process.
  • USER: Valid user name to start the process. Linux assigns one to a processReal user IDAnd aA valid user ID. The latter allows a process to perform operations on behalf of another user. (For example, a non-root user can be promoted to root to install software packages.)

Read more:

  • NIThe: NI field displays the nice value of the process, with a negative value indicating a high priority and a positive value indicating a low priority.
  • PRThe: PR field shows the scheduling priority of the process from a kernel perspective. The nice value affects the priority of the process.
  • VIRT: Total memory consumed by a process. This includes the code for the program, the data stored in memory by the process, and any memory areas of Swap, i.eInclude (RAM and Swap).
  • RES: is the memory consumed by the process in RAM(Not including Swap).
  • SHR: is the amount of memory that is shared with other processes.
  • % MEM: Percentage of total RAM memory consumed by a process.
  • S: Indicates the process status. As described earlier, processes can be in various states. This field toSingle letterForm displays process state.
  • TIME+: Total CPU time used since the process started, exact to1/100Seconds.
  • COMMAND: Process name.

Common operations

Basic operation

  • Up, down, left and right arrows: can be used to scroll a list vertically and horizontally.
  • q: Exits top or cancels user operations.
  • E: Uppercase E toggles the displayed dataunit.

Terminate the process

  • k: Pops up a prompt to kill the process.

Enter the process ID. If this parameter is left blank, the top-level process ID is selected by default. Then press Enter to prompt for SIGNAL to terminate the process. If this is left blank, the SIGTERM SIGNAL is used by default.

The SIGTERM signal allows the process to terminate normally. If you want to force termination, type SIGKILL. Signal numbers can also be used. For example, the number of SIGTERM is 15 and the number of SIGKILL is 9.

The sorting

  • M: Sort by memory usage
  • P: Sort by CPU usage
  • N: Sort by process ID
  • T: Sort by running time
  • R: Toggles the ascending sort

Display thread list

  • H: To switch threads, press again to switch back. Top displays a list of processes in its output. To list threads, press at top runtimeH.

Of course you can also execute command display

top -H
Copy the code

You’ll notice in line 2 that Tasks: becomes Threads:. Inside the Linux kernel, threads and processes use the same data structure for processing. Therefore, each thread has its own ID, state, and so on.

Displays the complete operating path

  • c: To switch to the full operating path, press again to switch back.

Of course you can also execute command display

top -c
Copy the code

On the displayThe full pathIn the case of, the top display will also be distinguishedKernel space processandUser-space process. The kernel space process is surrounded bySquare brackets []The tag.

View process startup sequence

  • V: View the child and parent of the process. You can see a hierarchical view of the startup.

Lists all processes for a user

  • u: Prompts you to enter the user name. Enter the user name Enter.

Of course you can also execute command display

top -u root # list all processes of user root
Copy the code

Change the style of statistics

  • t: Press repeatedly to change the display style of the statistics CPU on the top
  • m: Press repeatedly to change the display styles of Mem and Swap

Save Settings

  • W: Saves any changes made to the output of top. Top writes its configuration.toprcFiles in the home directory.

conclusion

There are many other Settings for top, but the above only cover a few common operations. For more operations, please run man Top on the system and see the man page.

Read more:

> www.site24x7.com/blog/load-a…

> juejin. Im/post / 684490…

> www.booleanworld.com/guide-linux…