Linux Performance Optimization: CPU performance analysis tool — vmstat

Time:2022-1-24

Blog:Blog Garden personal

Vmstat is the abbreviation of virtual memory statistics, which can monitor the virtual memory, process and CPU activity of the operating system. It is to make statistics on the overall situation of the system. The deficiency is that it is unable to make an in-depth analysis of a process.

Syntax format:

vmstat [options] [delay [count]]

delay: refresh interval. If not specified, only one result is displayed.

count: refresh times. If the refresh times are not specified, but the refresh time interval is specified, the refresh times are infinite.

Parameter description

parameter explain
-a Displays active and inactive memory
-f Displays the number of forks since the system was started
-m Display slabinfo
-n Each field name is displayed only once at the beginning
-s Displays memory related statistics and the number of various system activities
-d Displays disk related statistics
-p Displays statistics for the specified disk partition
-S Displays in the specified units. The parameters are k, K, m and m, representing 1000, 1024, 1000000 and 1048576 bytes respectively. The default unit is K (1024 bytes)
-V Display vmstat version information

Output information description

Category:

  • Procs: process
  • Memory: memory
  • Swap: virtual memory
  • IO: disk IO
  • System: system kernel
  • CPU: percentage of CPU time
procs
project explain
r The number of tasks waiting to be executed. When this value exceeds the number of CPUs, a CPU bottleneck will appear
b The number of processes in non interrupted sleep state. Greater than 0 indicates that the process is blocked
memory
project explain
swpd The size unit of the swap in use is K. if it is greater than 0, it indicates that there is insufficient physical memory
free Free physical memory space
buff Used buff size to buffer reads and writes of block devices
cache Used cache size, file system cache
inact Inactive memory size
active Active memory size
swap
project explain
si Swap memory usage and transfer from disk to memory
so Swap memory usage, from memory to disk
io
project explain
bi The total amount of data read from the block device (read disk) (KB / s). If this value is greater than 0, it indicates that the physical memory is insufficient or the memory is leaked
bo The data (write disk) (KB / s) written to the block device. If this value is greater than 0, it indicates that the physical memory is insufficient or the memory is leaked
system

The larger these two values are, the more CPU time consumed by the kernel will be seen.

project explain
in Number of interrupts generated per second
cs Number of context switches generated per second
cpu
project explain
us Percentage of CPU time consumed by the user process. When the value of us is high, it indicates that the user process consumes more CPU time, but if it is used for more than 50% for a long time, it is time to consider optimizing the program algorithm or accelerating it
sy The percentage of CPU time consumed by the kernel process, including the time consumed in system, IRQ and softirq states. When the value of SY is high, it indicates that the system kernel consumes more CPU resources, which is not a benign performance. We should check the reason
id Percentage of total CPU time consumed by the system idle
wa Percentage of CPU time consumed by IO wait. When the value of Wa is high, it indicates that the IO wait is serious, which may be caused by a large number of random accesses to the disk, or the bandwidth bottleneck of the disk (block operation)
st Time stolen from a virtual machine. Prior to Linux 2.6.11, unknown.

Example

Without any parameters:

[[email protected] ~]# vmstat
procs -----------memory---------- ---swap-- -----io---- --system-- -----cpu-----
 r  b   swpd   free   buff  cache   si   so    bi    bo   in   cs us sy id wa st
 0  0 18536908 381556  58920 35857652    0    0     2     6    0    0  2  2 96  1  0

The above example shows the average of the statistics recorded since the system was started. The CPU column shows that the CPU has 2% of the time for executing user application code, 2% of the time for system code, and the rest 96% of the time is idle.

Refresh the output results every 2 seconds, with a total of 10 data:

[[email protected] ~]# vmstat 2 10 |tee vmstat.log
procs -----------memory---------- ---swap-- -----io---- --system-- -----cpu-----
 r  b   swpd   free   buff  cache   si   so    bi    bo   in   cs us sy id wa st
 2  3 18244556 320756  58848 37127060    0    0     2     6    0    0  2  2 96  1  0	
 2  2 18233548 319008  58856 37107488 4546    0  4554    64 35098 28464  5  5 84  6  0	
 2  1 18233548 365572  58864 36950716    8    0     8   640 40116 27700  6  7 83  5  0	
 1  2 18233292 321304  58880 37012480  134    0   134    92 35978 33730  5  2 88  6  0	
 1  1 18233292 374516  58880 36972972   12    0    12   106 31706 25987  3  2 90  5  0	
 4  2 18233292 302484  58888 37047228    8    0    10    32 26781 24285  5  3 87  5  0	
 1  2 18233292 371552  58904 36977100   12    0    12    78 30176 25186  5  3 87  5  0	
 3  0 18233292 307768  58912 37015744   24    0    24    42 36149 26812  1  5 89  5  0	
 0  3 18233036 377984  58920 36941756  194    0   212    64 35131 26675  2  3 90  5  0	
 3  2 18233036 567952  58920 36795824   22    0    22     0 30918 25781  2  2 90  6  0

In the above example, the first set of data isCPU ticks, shows the CPU time since the system was started. Here “tick” is a time unit. Although the reduced vmstat output shows only four CPU States – US, sy, ID, and Wa, the distribution of all CPU ticks is shown here.

Recommended Today

IOS memory partition

#Stack area It is a continuous data structure expanded from high address to low address, which is used to store the values of local variables and function parameters #Heap area Programmers need to manage and release the data structure domain expanded by low address like high address and discontinuous memory area #Global area Global variables […]