1, Introduction
vmstat (full English Name: virtual memory statistics) under Linux, the abbreviation of virtual memory statistics, can monitor the overall operation status of the operating system, such as virtual memory, process, CPU activity, I/O and so on.
The disadvantage of vmstat is that it cannot deeply analyze a process. The vmstat tool provides a low overhead way to observe system performance. Because vmstat itself is a low overhead tool, you need to view and monitor the health of the system on very high load servers. You can still use vmstat to output results in the control window.
2, Format description
vmstat [options] [delay [count]] vmstat [parameter] Usage: vmstat [options] [delay [count]] Options: -a, --active active/inactive memory -f, --forks number of forks since boot -m, --slabs slabinfo -n, --one-header do not redisplay header -s, --stats event counter statistics -d, --disk disk statistics -D, --disk-sum summarize disk statistics -p, --partition <dev> partition specific statistics -S, --unit <char> define display unit -w, --wide wide output -t, --timestamp show timestamp -h, --help display this help and exit -V, --version output version information and exit
3, Option description
-a Displays active and inactive memory -f Displays the total number of processes created since startup. -m display slab information -n Header information is displayed only once -s Displays event counters and memory status in tabular form -d Displays disk related statistics -p Displays statistics for the specified disk partition -S Displays in the specified units. Parameters have k ,K ,m ,M ,Represents 1000, 1024, 1000000 and 1048576 bytes respectively( byte). The default unit is K(1024 bytes) -V display vmstat Version information 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.
4, Command function
Monitor the overall operation status of the operating system, such as virtual memory, process, CPU activity and I/O.
5, Common usage
5.1 display active and inactive memory
# vmstat -a procs -----------memory---------- ---swap-- -----io---- -system-- ------cpu----- r b swpd free inact active si so bi bo in cs us sy id wa st 1 0 0 328332 2438756 4577156 0 0 0 10 0 0 0 0 99 0 0 #Statistics are made every 1 second # vmstat 1 #Once every 1 second, 5 times in total # vmstat -a 1 5 procs -----------memory---------- ---swap-- -----io---- -system-- ------cpu----- r b swpd free inact active si so bi bo in cs us sy id wa st 1 0 0 329084 2438724 4575624 0 0 0 10 0 0 0 0 99 0 0 0 0 0 328812 2438732 4575328 0 0 0 0 1068 1560 0 0 99 0 0 0 0 0 328456 2438732 4575776 0 0 0 0 848 1370 0 0 100 0 0 0 0 0 328456 2438732 4575776 0 0 0 76 683 1148 0 0 100 0 0 0 0 0 328812 2438728 4575384 0 0 0 72 1564 2719 0 0 99 0 0 Field description Procs(Process: r: Number of processes in the run queue b: wait for IO Number of processes Memory(Memory: swpd: Use virtual memory size free: Available memory size buff: Size of memory used as buffer cache: Memory size used as cache Swap: si: The size of writes from swap to memory per second so: The amount of memory written to the swap per second IO: (current Linux The size of the version block is 1024 bytes) bi: Number of blocks read per second bo: Number of blocks written per second system: in: Number of interrupts per second, including clock interrupts. cs: Number of context switches per second. CPU((in percentage): us: User process execution time(user time) sy: System process execution time(system time) id: free time(include IO waiting time),CPU idle time. Expressed as a percentage. wa: wait for IO time Note: if r Often greater than 4, and id Often less than 40, indicating cpu The load is heavy. If pi,po Long term is not equal to 0, indicating insufficient memory. If disk Often not equal to 0, and b If the queue in is greater than 3, it indicates that io Poor performance. Linux With high stability and reliability, it has good scalability and scalability. It can be adjusted for different applications and hardware environments to optimize the best performance to meet the needs of current applications. Therefore, enterprises are maintaining Linux It is very important to understand the system performance analysis tools when tuning the system.
5.2 displays the total number of processes created after startup
# vmstat -f 70291088 forks #Note: this data is obtained from the processes field in / proc/stat
5.3 viewing the slab information of the system
# vmstat -m Cache Num Total Size Pages nf_conntrack_1 450 450 320 25 isofs_inode_cache 75 75 640 25 ext4_groupinfo_4k 420 420 136 30 ext4_inode_cache 155824 156080 1024 16 ext4_xattr 184 184 88 46 ext4_free_data 2560 2560 64 64 ext4_allocation_context 128 128 128 32 ext4_io_end 1960 1960 72 56 ext4_extent_status 27527 84966 40 102 jbd2_journal_handle 340 340 48 85 jbd2_journal_head 1044 1044 112 36 jbd2_revoke_table_s 256 256 16 256 jbd2_revoke_record_s 4096 4096 32 128 ip6_dst_cache 90 90 448 18 RAWv6 52 52 1216 26 UDPLITEv6 0 0 1216 26 UDPv6 104 104 1216 26 tw_sock_TCPv6 80 80 256 16 TCPv6 60 60 2112 15 cfq_queue 0 0 232 17 bsg_cmd 0 0 312 26 Cache Num Total Size Pages mqueue_inode_cache 18 18 896 18 hugetlbfs_inode_cache 26 26 608 26 configfs_dir_cache 184 184 88 46 dquot 272 272 256 16 kioctx 112 112 576 28 userfaultfd_ctx_cache 0 0 192 21 dio 75 75 640 25 pid_namespace 0 0 2200 14 posix_timers_cache 64 64 248 16 UDP-Lite 0 0 1088 30 flow_cache 0 0 144 28 UDP 120 120 1088 30 tw_sock_TCP 304 352 256 16 TCP 491 544 1984 16 dax_cache 21 21 768 21 blkdev_integrity 0 0 112 36 blkdev_queue 39 39 2496 13 blkdev_ioc 312 312 104 39 user_namespace 0 0 488 16 dmaengine-unmap-128 240 240 1088 30 sock_inode_cache 682 1050 640 25 Cache Num Total Size Pages fsnotify_mark_connector 3060 3060 24 170 net_namespace 6 6 5248 6 shmem_inode_cache 1187 1224 680 24 Acpi-State 1286 1683 80 51 task_delay_info 1080 1080 112 36 taskstats 96 96 328 24 proc_inode_cache 7205 17760 672 24 sigqueue 400 400 160 25 bdev_cache 38 38 832 19 kernfs_node_cache 16830 16830 120 34 mnt_cache 189 189 384 21 inode_cache 9858 10746 592 27 dentry 776220 776370 192 21 iint_cache 0 0 128 32 avc_xperms_node 3650 3650 56 73 avc_node 3584 3584 72 56 selinux_inode_security 11526 11526 40 102 buffer_head 1134212 1195467 104 39 vm_area_struct 17299 17496 216 18 mm_struct 304 340 1600 20 fs_cache 320 320 64 64 Cache Num Total Size Pages files_cache 475 475 640 25 signal_cache 560 560 1152 28 sighand_cache 253 300 2112 15 task_xstate 627 627 832 19 task_struct 279 315 4208 7 cred_jar 1533 1533 192 21 anon_vma 9154 9588 80 51 pid 2493 3168 128 32 shared_policy_node 95025 102765 48 85 numa_policy 31 31 264 31 radix_tree_node 87727 87808 584 28 idr_layer_cache 255 255 2112 15 dma-kmalloc-8192 0 0 8192 4 dma-kmalloc-4096 0 0 4096 8 dma-kmalloc-2048 0 0 2048 16 dma-kmalloc-1024 0 0 1024 16 dma-kmalloc-512 32 32 512 16 dma-kmalloc-256 0 0 256 16 dma-kmalloc-128 0 0 128 32 dma-kmalloc-64 0 0 64 64 dma-kmalloc-32 0 0 32 128 Cache Num Total Size Pages dma-kmalloc-16 0 0 16 256 dma-kmalloc-8 0 0 8 512 dma-kmalloc-192 0 0 192 21 dma-kmalloc-96 0 0 96 42 kmalloc-8192 45 56 8192 4 kmalloc-4096 152 168 4096 8 kmalloc-2048 534 624 2048 16 kmalloc-1024 1353 1472 1024 16 kmalloc-512 1007 1152 512 16 kmalloc-256 3049 3936 256 16 kmalloc-192 77229 77427 192 21 kmalloc-128 1991 2048 128 32 kmalloc-96 2685 3570 96 42 kmalloc-64 155391 156096 64 64 kmalloc-32 1920 1920 32 128 kmalloc-16 4608 4608 16 256 kmalloc-8 5120 5120 8 512 kmem_cache_node 192 192 64 64 kmem_cache 128 128 256 16 #Note: this group of information comes from / proc/slabinfo. slab:Because there are many small objects in the kernel, these object structures are destroyed very frequently, such as i-node,dentry,If these objects ask for a page from memory each time they are built(4kb),In fact, there are only a few bytes, which will be very wasteful. In order to solve this problem, a new mechanism is introduced to deal with how to allocate small storage areas in the same page box slab Small objects can be assigned,In this way, there is no need to allocate page boxes for each object, thus saving space. The kernel creates and destructs some small objects very frequently, slab Buffer these small objects,Can be reused,Reduce the number of memory allocations.
5.4 header information is displayed only once
# vmstat -n
5.5 display event counters and memory status in tabular form
# vmstat -s 8008656 K total memory 2198412 K used memory 4572020 K active memory 2438476 K inactive memory 332064 K free memory 212700 K buffer memory 5265480 K swap cache 0 K total swap 0 K used swap 0 K free swap 23236842 non-nice user cpu ticks 4704 nice user cpu ticks 22491780 system cpu ticks 8123700891 idle cpu ticks 2666825 IO-wait cpu ticks 0 IRQ cpu ticks 274843 softirq cpu ticks 0 stolen cpu ticks 4790784 pages paged in 786384320 pages paged out 0 pages swapped in 0 pages swapped out 2665979789 interrupts 2264646031 CPU context switches 1616497272 boot time 70292589 forks #Note: the information comes from / proc/meminfo,/proc/stat and / proc/vmstat respectively.
5.6 viewing disk read / write
# vmstat -d #Once every 1 second, 5 times in total # vmstat -d 1 5 disk- ------------reads------------ ------------writes----------- -----IO------ total merged sectors ms total merged sectors ms cur sec vda 261581 944 9569132 5360844 92451702 59872533 1572785921 474771528 0 29522 sr0 124 0 12436 67 0 0 0 0 0 0 vda 261581 944 9569132 5360844 92451702 59872533 1572785921 474771528 0 29522 sr0 124 0 12436 67 0 0 0 0 0 0 vda 261581 944 9569132 5360844 92451720 59872534 1572786145 474771556 0 29522 sr0 124 0 12436 67 0 0 0 0 0 0 vda 261581 944 9569132 5360844 92451722 59872597 1572786665 474771568 0 29522 sr0 124 0 12436 67 0 0 0 0 0 0 vda 261581 944 9569132 5360844 92451722 59872597 1572786665 474771568 0 29522 sr0 124 0 12436 67 0 0 0 0 0 0
5.7 display the specified hard disk partition status
# vmstat -p /dev/vda1 vda1 reads read sectors writes requested writes 261523 9557868 91469637 1572779937
6, Expand - manage memory and virtual memory
We know that reading and writing data directly from physical memory is much faster than reading and writing data from hard disk. Therefore, we hope that all data reading and writing are completed in memory, and the memory is limited, which leads to the concept of physical memory and virtual memory.
Physical memory is the size of memory provided by the system hardware. It is real memory. Compared with physical memory, there is also a concept of virtual memory under Linux. Virtual memory is a strategy proposed to meet the shortage of physical memory. It is a piece of logical memory virtualized by using disk space. The disk space used as virtual memory is called Swap Space.
As an extension of the physical memory, Linux will use the virtual memory of the swap partition when the physical memory is insufficient. More specifically, the kernel will write the temporarily unused memory block information to the swap space. In this way, the physical memory is released and this memory can be used for other purposes. When the original content is needed, This information is re read from the swap space into physical memory.
The memory management of Linux adopts the paging access mechanism. In order to ensure that the physical memory can be fully utilized, the kernel will automatically exchange the infrequently used data blocks in the physical memory to the virtual memory at an appropriate time, and retain the frequently used information to the physical memory.
To deeply understand the memory operation mechanism of Linux, you need to know the following aspects:
First, the Linux system will exchange pages from time to time to keep as much free physical memory as possible. Even if there is nothing to need memory, Linux will exchange temporarily unused memory pages. This avoids the time required to wait for the exchange.
Secondly, page exchange in Linux is conditional. Not all pages are exchanged to virtual memory when not in use. The Linux kernel is most frequently used recently "The algorithm only exchanges some infrequently used page files into virtual memory. Sometimes we see such a phenomenon: there is still a lot of physical memory in Linux, but the exchange space is also used. In fact, this is not surprising. For example, when a process that occupies a lot of memory runs, it needs to consume a lot of memory resources. At this time, there will be some infrequently used page files Pieces are exchanged into virtual memory, but later, when the process that occupies a lot of memory resources ends and releases a lot of memory, the page files just exchanged will not be automatically exchanged into physical memory. Unless it is necessary, the physical memory of the system will be much idle at the moment, and the exchange space is also being used, which leads to the phenomenon mentioned just now At this point, don't worry about anything, just know what's going on.
Finally, the pages in the swap space will be swapped to the physical memory first. If there is not enough physical memory to hold these pages at this time, they will be swapped out immediately. In this way, there may not be enough space in the virtual memory to store these swap pages, which will eventually lead to problems such as false crash and service exception in Linux, Although Linux can recover itself for a period of time, the recovered system is basically unavailable.
Therefore, it is very important to reasonably plan and design the use of Linux memory.
Virtual memory principle:
Each process running in the system needs to use memory, but not every process needs to use the memory space allocated by the system all the time. When the memory required for system operation exceeds the actual physical memory, the kernel will release some or all of the unused physical memory occupied by some processes, store this part of data on disk until the next call of the process, and provide the released memory to the processes in need.
In Linux memory management, the above memory scheduling is mainly completed through "page paging" and "switching switching". Paging algorithm is to replace the recently unused pages in memory to disk, and keep the active pages in memory for the process to use. Exchange technology is to exchange the whole process, not part of the pages, to disk.
The process of writing pages to disk is called page out, and the process of returning pages from disk to memory is called page in. When the kernel needs a page, but it is found that the page is not in physical memory (because it has been page out), a Page Fault occurs.
When the system kernel finds that the runnable memory becomes less, it will release part of the physical memory through page out. Although page out does not occur frequently, if page out occurs frequently, the system performance will decline sharply until the kernel manages pages for more than the time of running the program. At this time, the system has been running very slowly or has entered a pause state, which is also called thrashing.