Detailed explanation of top command for Linux performance analysis
Overview
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. Its function is similar to the task manager in Windows systems.
The top command can dynamically display the load of the system. This article will introduce all aspects of the top command in detail.
Common parameters
The top command supports some parameters. The following are some commonly used parameters:
- -d: Set refresh frequency.
- -n: Set the number of refreshes.
- -p: Display information about the specified process.
- -u: Display process information of the specified user.
- -o: Specify the sorting field.
- -h: Display help information.
Display process information refreshed every 2 seconds and only 10 times:
top -d 2 -n 10
Display the process information of the specified user root:
top -u root
Display information for the specified process ID 123:
top -p 123
Sort by memory usage:
top -o %MEM
top command content
Usually The output of the top
command is as follows. It contains many aspects of data, such as CPU, memory, system tasks, etc.
top - 10:32:42 up 38 min, 2 users, load average: 0.00, 0.00, 0.00
Tasks: 237 total, 1 running, 236 sleeping, 0 stopped, 0 zombie
%Cpu(s): 0.3 us, 0.4 sy, 0.0 ni, 99.1 id, 0.0 wa, 0.1 hi, 0.1 si, 0.0 st
MiB Mem : 3635.0 total, 3229.8 free, 441.0 used, 170.1 buff/cache
MiB Swap: 2072.0 total, 2072.0 free, 0.0 used. 3194.0 avail Mem
PID USER PR NI VIRT RES SHR S %CPU %MEM TIME+ COMMAND
1461 root 20 0 7252 3672 3280 S 0.7 0.1 0:00.03 bash
1621 root 20 0 10700 4360 3476 R 0.7 0.1 0:00.05 top
1477 root 20 0 0 0 0 I 0.3 0.0 0:00.01 kworker/0:0-events
1 root 20 0 103372 12748 9724 S 0.0 0.3 0:02.18 systemd
2 root 20 0 0 0 0 S 0.0 0.0 0:00.04 kthreadd
3 root 0 -20 0 0 0 I 0.0 0.0 0:00.00 rcu_gp
4 root 0 -20 0 0 0 I 0.0 0.0 0:00.00 rcu_par_gp
5 root 0 -20 0 0 0 I 0.0 0.0 0:00.00 slub_flushwq
6 root 0 -20 0 0 0 I 0.0 0.0 0:00.00 netns
8 root 0 -20 0 0 0 I 0.0 0.0 0:00.00 kworker/0:0H-events_highpri
10 root 0 -20 0 0 0 I 0.0 0.0 0:00.00 kworker/0:1H-events_highpri
11 root 0 -20 0 0 0 I 0.0 0.0 0:00.00 mm_percpu_wq
13 root 20 0 0 0 0 I 0.0 0.0 0:00.00 rcu_tasks_kthre
Let's look at the data of the top command line by line.
The first line actually represents the system startup time and the average load of the system.
top - 10:32:42 up 38 min, 2 users, load average: 0.00, 0.00, 0.00
top - 10:32:42 up 38 min
means that the current system time is10:32:42
, and the machine has been started38 min
. 2 users
means there are currently two users logged in. load average: 0.00, 0.00, 0.00
represents the average load of the system in the last 5, 10 and 15 minutes.
It should be noted here that the average load does not represent the CPU usage. Simply put, average load means that the system is in an operational statethe average number of processes in the uninterruptible state, that is,the average number of active processes< a i=8>.
The so-called runnable state and uninterruptible state refer to:
- Runnable status®: Process status that is running on the CPU or waiting for the CPU, as above;
- Uninterruptible state (D): Uninterruptible refers to some processes that are in the core process of the kernel. If blindly interrupted, it will cause unpredictable consequences. For example, if a process that is writing to the disk is blindly interrupted, it may cause inconsistencies in reading and writing. The problem.
Therefore, it is emphasized again that load average is not directly related to CPU usage, and its value can be greater than 100.
In fact, the display data in the first line has the same function as the commanduptime
.
The second line displays a summary ofprocess status.
Processes can be in different states. The number of processes in various states is displayed here, such as the number of running tasks, sleeping tasks, stopped tasks, zombie processes, etc.
In the display below, the system has a total of 237 processes, of which 1 process is in the running state, 236 processes are in the sleeping state, 0 processes are in the stop state, and 0 processes are in the zombie process state.
Tasks: 237 total, 1 running, 236 sleeping, 0 stopped, 0 zombie
The third line is a more critical line. When locating problems online, you will often pay attention to this point.
%Cpu(s): 0.3 us, 0.4 sy, 0.0 ni, 99.1 id, 0.0 wa, 0.1 hi, 0.1 si, 0.0 st
The meaning of each parameter here is as follows:
- us, user: CPU percentage of user space
- sy, system: percentage of CPU occupied by kernel space
- ni, niced: CPU percentage occupied by user processes with adjusted priorities
- id, idle: percentage of idle CPU
- wa, IO wait: CPU time used to wait for IO to complete
- hi: CPU time spent processing hardware interrupts
- si: CPU time to handle software interrupts
- st: The CPU time of this virtual machine stolen by the hypervisor (Annotation: If the vm is currently under a hypervisor, the hypervisor actually consumes part of the CPU processing time).
The fourth part is about the data of the memory part. This contains two lines,
The first line is the physical memory usage, and the second line is about the swap partition usage.
MiB Mem : 3635.0 total, 3229.8 free, 441.0 used, 170.1 buff/cache
MiB Swap: 2072.0 total, 2072.0 free, 0.0 used. 3194.0 avail Mem
The meaning of the first line is as follows:
- 3635.0 total — total physical memory (3265MB)
- 3229.8 free — Total amount of free memory (3229.8MB)
- 441.0 used — Total memory in use (441.0MB)
- 170.1 buff/cache — memory size occupied by block buffer + page cache (169M)
Add here the content of buff/cache. Before Linux 2.4, page cache and buffer cache is two independent caches, starting with Linux 2.4page cache and buffer cache was unified.
The meaning of the second line is as follows:
- 2072.0 total - total amount of swap partitions
- 2072.0 free - Total number of free swap partitions
- 0.0 used - used swap partition size
- 3194.0 avail Mem: This value is an estimate of the system and represents the amount of physical memory available to start a new program (excluding swap space)
The fifth part is aboutdata for each process:
PID USER PR NI VIRT RES SHR S %CPU %MEM TIME+ COMMAND
1461 root 20 0 7252 3672 3280 S 0.7 0.1 0:00.03 bash
1621 root 20 0 10700 4360 3476 R 0.7 0.1 0:00.05 top
1477 root 20 0 0 0 0 I 0.3 0.0 0:00.01 kworker/0:0-events
1 root 20 0 103372 12748 9724 S 0.0 0.3 0:02.18 systemd
2 root 20 0 0 0 0 S 0.0 0.0 0:00.04 kthreadd
3 root 0 -20 0 0 0 I 0.0 0.0 0:00.00 rcu_gp
Below the horizontal listing of system properties and status, processes are displayed in columns. Different columns represent different attributes explained below.
- 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 are running in real time.
- NI: Nice value (priority) of the process. The value of NI is in [-20, 19]. The smaller the value, the higher the priority.
- VIRT: Virtual memory used by the process. The default is KB.
- RES: resident memory size. Resident memory is the size of non-swapped physical memory used by the task, and the default is KB.
- SHR: SHR is the shared memory used by the process, the default is KB.
- S: This is the status of the process. It has the following different values:
- D – Uninterruptible sleep state.
- R - running state
- S – sleep state
- T – Tracked or Stopped
- Z – Zombie state
- CPU: The percentage of CPU time used by the task since the last update.
- MEM: Percentage of available physical memory used by the process.
- TIME: The total CPU time used since the task was started until now, accurate to one hundredth of a second.
- COMMAND: The command used to run the process.
skills
Multi-core CPU monitoring
In the basic view of top, press the number 1 to monitor the status of each logical CPU. For example, in the example below, my virtual machine contains 4 CPUs, and the running status of the 4 CPUs is displayed here.
top - 13:35:11 up 3:40, 2 users, load average: 0.00, 0.02, 0.00
Tasks: 234 total, 1 running, 233 sleeping, 0 stopped, 0 zombie
%Cpu0 : 0.3 us, 0.7 sy, 0.0 ni, 98.7 id, 0.0 wa, 0.3 hi, 0.0 si, 0.0 st
%Cpu1 : 0.7 us, 0.3 sy, 0.0 ni, 98.7 id, 0.0 wa, 0.0 hi, 0.3 si, 0.0 st
%Cpu2 : 0.3 us, 0.7 sy, 0.0 ni, 99.0 id, 0.0 wa, 0.0 hi, 0.0 si, 0.0 st
%Cpu3 : 0.3 us, 0.3 sy, 0.0 ni, 99.3 id, 0.0 wa, 0.0 hi, 0.0 si, 0.0 st
MiB Mem : 3635.0 total, 3188.5 free, 461.1 used, 209.5 buff/cache
MiB Swap: 2072.0 total, 2072.0 free, 0.0 used. 3173.9 avail Mem
Switch memory units
- Switch the memory unit of the task area
In the basic view of top, press the letter e to switch the unit of the memory size occupied by each task, switching in a loop from KB, MB, GB, TB, PB to EB. The following switches to MB as the unit:
top - 14:45:06 up 4:59, 1 user, load average: 0.02, 0.01, 0.00
Tasks: 228 total, 2 running, 226 sleeping, 0 stopped, 0 zombie
%Cpu(s): 0.3 us, 0.3 sy, 0.0 ni, 99.2 id, 0.0 wa, 0.1 hi, 0.1 si, 0.0 st
MiB Mem : 3635.0 total, 3193.4 free, 454.6 used, 211.3 buff/cache
MiB Swap: 2072.0 total, 2072.0 free, 0.0 used. 3180.4 avail Mem
PID USER PR NI VIRT RES SHR S %CPU %MEM TIME+ COMMAND
94996 root 20 0 10.4m 4.3m 3.4m R 0.7 0.1 0:00.05 top
1712 root 20 0 0.0m 0.0m 0.0m I 0.3 0.0 0:35.48 kworker/0:0-events
1 root 20 0 165.1m 12.5m 9.6m S 0.0 0.3 1:38.13 systemd
2 root 20 0 0.0m 0.0m 0.0m S 0.0 0.0 0:00.05 kthreadd
3 root 0 -20 0.0m 0.0m 0.0m I 0.0 0.0 0:00.00 rcu_gp
4 root 0 -20 0.0m 0.0m 0.0m I 0.0 0.0 0:00.00 rcu_par_gp
- Switch the memory unit of the summary area
Using the upper and lower letters E, the memory unit of the summary area can be switched cyclically from KB, MB, GB, TB, PB to EB.
top - 14:54:18 up 5:08, 1 user, load average: 0.02, 0.02, 0.00
Tasks: 227 total, 2 running, 225 sleeping, 0 stopped, 0 zombie
%Cpu(s): 0.3 us, 0.4 sy, 0.0 ni, 99.1 id, 0.0 wa, 0.1 hi, 0.1 si, 0.0 st
KiB Mem : 3722236 total, 3268096 free, 467500 used, 216364 buff/cache
KiB Swap: 2121724 total, 2121724 free, 0 used. 3254736 avail Mem
top - 14:54:34 up 5:08, 1 user, load average: 0.02, 0.02, 0.00
Tasks: 227 total, 1 running, 226 sleeping, 0 stopped, 0 zombie
%Cpu(s): 0.4 us, 0.4 sy, 0.0 ni, 99.0 id, 0.0 wa, 0.0 hi, 0.1 si, 0.0 st
MiB Mem : 3635.0 total, 3186.9 free, 461.2 used, 211.3 buff/cache
MiB Swap: 2072.0 total, 2072.0 free, 0.0 used. 3173.8 avail Mem
top - 14:54:44 up 5:08, 1 user, load average: 0.01, 0.02, 0.00
Tasks: 227 total, 1 running, 226 sleeping, 0 stopped, 0 zombie
%Cpu(s): 0.0 us, 0.0 sy, 0.0 ni,100.0 id, 0.0 wa, 0.0 hi, 0.0 si, 0.0 st
GiB Mem : 3.5 total, 3.1 free, 0.4 used, 0.2 buff/cache
GiB Swap: 2.0 total, 2.0 free, 0.0 used. 3.1 avail Mem
Limit the number of display tasks
The shortcut key n is used to limit the number of tasks to be displayed, 0 means no limit. The default is to display all (more than one page needs to be turned).
After entering n here, enter 5 again to choose to display only 5 records.
top - 14:47:34 up 5:01, 1 user, load average: 0.05, 0.03, 0.00
Tasks: 227 total, 1 running, 226 sleeping, 0 stopped, 0 zombie
%Cpu(s): 1.5 us, 1.5 sy, 0.0 ni, 97.0 id, 0.0 wa, 0.0 hi, 0.0 si, 0.0 st
MiB Mem : 3635.0 total, 3187.3 free, 460.8 used, 211.3 buff/cache
MiB Swap: 2072.0 total, 2072.0 free, 0.0 used. 3174.2 avail Mem
Maximum tasks = 0, change to (0 is unlimited)5
PID USER PR NI VIRT RES SHR S %CPU %MEM TIME+ COMMAND
1 root 20 0 169040 12832 9800 S 0.0 0.3 1:38.14 systemd
2 root 20 0 0 0 0 S 0.0 0.0 0:00.05 kthreadd
Displayed as follows:
top - 14:48:16 up 5:02, 1 user, load average: 0.02, 0.02, 0.00
Tasks: 228 total, 1 running, 227 sleeping, 0 stopped, 0 zombie
%Cpu(s): 0.3 us, 0.5 sy, 0.0 ni, 99.0 id, 0.0 wa, 0.1 hi, 0.1 si, 0.0 st
MiB Mem : 3635.0 total, 3190.8 free, 457.2 used, 211.3 buff/cache
MiB Swap: 2072.0 total, 2072.0 free, 0.0 used. 3177.8 avail Mem
PID USER PR NI VIRT RES SHR S %CPU %MEM TIME+ COMMAND
1386 root 20 0 84320 22744 19808 S 0.3 0.6 0:00.09 smbd
1514 root 20 0 7252 3696 3304 S 0.3 0.1 0:51.33 bash
90783 root 20 0 0 0 0 I 0.3 0.0 0:00.02 kworker/2:2-mm_percpu_wq
95906 root 20 0 10700 4356 3472 R 0.3 0.1 0:00.05 top
1 root 20 0 169040 12832 9800 S 0.0 0.3 1:38.14 systemd
Only display tasks for specified users
The shortcut key u represents the user to be filtered, and you can add ! prefix to represent the reverse condition. You can enter UID or username, and press Enter directly to cancel this filtering condition.
top - 14:49:30 up 5:03, 1 user, load average: 0.00, 0.02, 0.00
Tasks: 229 total, 2 running, 227 sleeping, 0 stopped, 0 zombie
%Cpu(s): 0.5 us, 0.4 sy, 0.0 ni, 98.9 id, 0.0 wa, 0.1 hi, 0.1 si, 0.0 st
MiB Mem : 3635.0 total, 3190.0 free, 458.0 used, 211.3 buff/cache
MiB Swap: 2072.0 total, 2072.0 free, 0.0 used. 3177.0 avail Mem
Which user (blank for all) root
PID USER PR NI VIRT RES SHR S %CPU %MEM TIME+ COMMAND
46 root 20 0 0 0 0 S 0.3 0.0 0:00.28 kcompactd0
1454 root 20 0 19112 7260 5176 S 0.3 0.2 0:33.76 sshd
1514 root 20 0 7252 3696 3304 S 0.3 0.1 0:51.54 bash
Sort by CPU usage
After running the top command, type the capital letter P to sort the CPU usage of tasks. There are two ways:
- With the uppercase keyboard open, just press p
- To open the uppercase keyboard, shift+p.
top - 15:01:01 up 5:15, 1 user, load average: 0.02, 0.03, 0.00
Tasks: 228 total, 2 running, 226 sleeping, 0 stopped, 0 zombie
%Cpu(s): 2.8 us, 2.8 sy, 0.0 ni, 94.4 id, 0.0 wa, 0.0 hi, 0.0 si, 0.0 st
MiB Mem : 3635.0 total, 3189.3 free, 458.8 used, 211.3 buff/cache
MiB Swap: 2072.0 total, 2072.0 free, 0.0 used. 3176.2 avail Mem
PID USER PR NI VIRT RES SHR S %CPU %MEM TIME+ COMMAND
727 root 20 0 26128 9688 8596 S 7.4 0.3 0:00.16 systemd-journal
99654 root 20 0 10700 4356 3476 R 3.7 0.1 0:00.74 top
1 root 20 0 169040 12832 9800 S 0.0 0.3 1:38.14 systemd
2 root 20 0 0 0 0 S 0.0 0.0 0:00.05 kthreadd
3 root 0 -20 0 0 0 I 0.0 0.0 0:00.00 rcu_gp
Sort tasks by memory usage
After running the top command, type the capital letter M to sort the CPU usage of tasks. There are two ways:
- With the uppercase keyboard open, just press m
- To open the uppercase keyboard, shift+m.
top - 15:02:25 up 5:16, 1 user, load average: 0.00, 0.02, 0.00
Tasks: 229 total, 1 running, 228 sleeping, 0 stopped, 0 zombie
%Cpu(s): 0.4 us, 0.4 sy, 0.0 ni, 99.0 id, 0.0 wa, 0.1 hi, 0.1 si, 0.0 st
MiB Mem : 3635.0 total, 3187.3 free, 460.8 used, 211.3 buff/cache
MiB Swap: 2072.0 total, 2072.0 free, 0.0 used. 3174.2 avail Mem
PID USER PR NI VIRT RES SHR S %CPU %MEM TIME+ COMMAND
1386 root 20 0 84320 22744 19808 S 0.0 0.6 0:00.09 smbd
813 root 20 0 257068 21184 16536 S 0.0 0.6 0:01.22 NetworkManager
872 root 20 0 72880 17140 14664 S 0.0 0.5 0:00.62 nmbd
1 root 20 0 169040 12832 9800 S 0.0 0.3 1:38.14 systemd
1436 root 20 0 18764 12060 10248 S 0.0 0.3 0:00.03 sshd
1433 root 20 0 18756 12036 10184 S 0.0 0.3 0:00.04 sshd
View help in top interactive mode
In interactive mode, type h to view help information: