Vmstat is short for VirtualMeomoryStatistics (virtual memory statistics). it monitors virtual memory, processes, and CPU activity of the operating system.
He makes statistics on the overall situation of the system. The disadvantage is that he cannot conduct in-depth analysis on a process. The vmstat tool provides a low-cost system performance observation method. Because vmstat itself is a low-cost tool, on a very high-load server, you need to view and monitor the health of the system, and you can still use vmstat to output results in the control window. Before learning the vmstat command, let's take a look at the information about the physical memory and virtual memory in the Linux system.
Differences between physical memory and virtual memory:
We know that reading and writing data directly from the physical memory is much faster than reading and writing data from the hard disk. Therefore, we hope that all data can be read and written in the memory, while the memory is limited, this introduces the concept of physical memory and virtual memory.
Physical memory is the memory size provided by the system hardware and is the real memory. compared with physical memory, there is also a concept of virtual memory in linux, virtual memory is a strategy proposed to meet the shortage of physical memory. it is a logical memory virtualized by the disk Space. the disk Space used as the virtual memory is called the Swap Space ).
As an extension of the physical memory, linux uses the virtual memory of swap partitions when the physical memory is insufficient. in more details, the kernel will write the memory block information that is not used for the moment to the swap space, in this way, the physical memory has been released, and this memory can be used for other purposes. when you need to use the original content, this information will be re-read from the swap space into the physical memory.
Linux Memory Management adopts a paging access mechanism. to ensure that the physical memory can be fully utilized, when appropriate, the kernel automatically exchanges infrequently used data blocks in the physical memory to the virtual memory, and retains frequently used information to the physical memory.
To learn more about the linux memory running mechanism, you need to know the following aspects:
First, the Linux system will perform page switching from time to maintain as much free physical memory as possible. even if there is no need for memory, Linux will swap out memory pages that are temporarily unavailable. This avoids waiting for the switching time.
Second, linux provides page swap conditions. not all pages are switched to the virtual memory when not in use. linux kernel uses the "most recently used" algorithm, swap some infrequently used page files to the virtual memory. sometimes we can see that there is still a lot of linux physical memory, but the swap space is also used. In fact, this is not surprising. for example, a process that occupies a large amount of memory needs to consume a lot of memory resources, and some infrequently used page files will be exchanged to the virtual memory, however, when the process that occupies a lot of memory resources ends and releases a lot of memory, the page files that have just been exchanged will not be automatically exchanged into the physical memory unless necessary, now the physical memory of the system will be much idle and the swap space will be used. You don't have to worry about this. you just need to know what it is.
Finally, the pages in the swap space will be first exchanged to the physical memory during use. if there is not enough physical memory to accommodate these pages, they will be immediately exchanged, there may not be enough space in the virtual memory to store these swap pages, which will eventually lead to issues such as false crashes and service exceptions in linux. Although linux can be restored by itself within a period of time, however, the recovered system is basically unavailable.
Therefore, it is very important to reasonably plan and design the linux memory usage.
Virtual memory principle:
Every process running in the system needs to use the memory, but not every process needs to use the memory space allocated by the system every moment. When the memory required to run the system exceeds the actual physical memory, the kernel will release some or all of the physical memory occupied by some processes, store this part of data on the disk until the next Call of the process, and provide the released memory to the process in need.
In Linux memory management, the above memory scheduling is mainly completed through "page Paging" and "Swapping switching. The paging algorithm is used to change pages that are not frequently used in the memory to disks and keep the active pages in the memory for the process to use. The switching technology is to swap the entire process, not some pages, to the disk.
The Page writing process is called Page-Out, and the Page re-returning from the disk to the memory is called Page-In. When the kernel requires a Page but finds that this Page is not in the physical memory (because it has already been Page-Out), a Page error (Page Fault) occurs ).
When the system kernel finds that the amount of memory that can run is low, it releases some physical memory through Page-Out. Page-Out management does not occur frequently. However, if Page-out occurs frequently until the kernel management Page time exceeds the running program time, the system performance decreases sharply. At this time, the system is running very slowly or in the paused state. this state is also called thrashing)
1. command format:
Copy codeThe code is as follows:
Vmstat [-a] [-n] [-S unit] [delay [count]
Vmstat [-s] [-n] [-S unit]
Vmstat [-m] [-n] [delay [count]
Vmstat [-d] [-n] [delay [count]
Vmstat [-p disk partition] [-n] [delay [count]
Vmstat [-f]
Vmstat [-V]
2. command functions:
Used to display virtual memory information
3. command parameters:
-A: Displays active and inactive memory.
-F: displays the number of Forks since system startup.
-M: displays slabinfo
-N: the field names are displayed only once at the beginning.
-S: displays memory-related statistics and the number of various system activities.
Delay: refresh interval. If this parameter is not specified, only one result is displayed.
Count: Number of refreshes. If you do not specify the number of refreshes, but specify the interval, the number of refreshes is infinite.
-D: displays disk statistics.
-P: displays statistics of specified disk partitions.
-S: displays data in a specified unit. Parameters include k, K, m, and M, representing 1000, 1024, 1000000, and 1048576 bytes respectively ). The default unit is K (1024 bytes)
-V: displays the vmstat version.
4. example:
Instance 1: displays the virtual memory usage
Command: vmstat
Output:
Copy codeThe code is as follows:
[Root @ localhost ~] # Vmstat 5 6
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 0 3029876 199616 0 0 0 2 2 0 0 690980 0
0 0 0 3029752 199616 690980 0 0 41 1009 39 0 100 0 0
0 0 0 3029752 199616 0 0 0 3 690980 36 0 0 1004 0
0 0 0 3029752 199616 0 0 0 4 690980 36 0 0 1004 0
0 0 0 3029752 199616 0 0 0 6 690980 33 0 0 1003 0
0 0 0 3029752 199616 0 0 0 5 690980 33 0 0 1003 0
Vmstat command output details:
Field description:
Procs (process ):
R: number of processes in the running queue
B: number of processes waiting for IO
Memory (Memory ):
Swpd: virtual memory size used
Free: available memory size
Buff: memory size used as a buffer
Cache: memory size used as the cache
Swap:
Si: size of memory written from swap zone per second
So: memory size written to swap zone per second
IO: (the size of the Linux block is 1024 bytes)
Bi: Number of read blocks per second
Bo: number of data blocks written per second
System:
In: The number of interrupts per second, including clock interruptions.
Cs: Number of context switches per second.
CPU (expressed in percentage ):
Us: user time)
Sy: system process execution time (system time)
Id: idle time (including IO wait time), the idle time of the central processor. Expressed as a percentage.
Wa: Wait for IO time
Note: If r is often greater than 4 and the id is often less than 40, the cpu load is heavy. If pi and po are not equal to 0 for a long time, the memory is insufficient. If the disk is often not equal to 0 and the queue in B is greater than 3, the io performance is poor. In addition to high stability and reliability, Linux has good scalability and scalability. it can be adjusted for different applications and hardware environments to optimize the best performance that meets the needs of current applications. Therefore, it is vital for enterprises to understand system performance analysis tools when maintaining and optimizing Linux systems.
Command: vmstat 5 5
Sampling is performed five times in five seconds. A summary of the data to reflect the real system conditions.
Instance 2: Displays active and inactive memory
Command: vmstat-a 2 5
Output:
Copy codeThe code is as follows:
[Root @ localhost ~] # Vmstat-a 2 5
Procs ----------- memory ---------- --- swap -- ----- io ---- system -- ----- cpu ------
R B swpd free inact active si so bi bo in cs us sy id wa st
0 0 0 3029752 387728 0 0 0 2 2 0 0 513008 0
0 0 0 3029752 387728 513076 0 0 0 1005 34 0 0 100 0
0 0 0 3029752 387728 0 0 0 22 513076 36 0 0 1004 0
0 0 0 3029752 387728 0 0 0 513076 33 0 0 1004 0 0
0 0 0 3029752 387728 0 0 0 513076 32 0 0 1003 0 0
[Root @ localhost ~] #
Note:
When the-a option is used to display active and inactive memory, the displayed content is the same as that in example 1 except inact and active.
Field description:
Memory (Memory ):
Inact: inactive memory size (displayed when the-a option is used)
Active: active memory size (displayed when the-a option is used)
Instance 3: View how many times the system has fork
Command: vmstat-f
Output:
Copy codeThe code is as follows:
[Root @ SCF1129 ~] # Vmstat-f
12744849 forks
[Root @ SCF1129 ~] #
Note:
This data is obtained from the processes field in/proc/stat.
Instance 4: view the memory usage details
Command: vmstat-s
Output:
Copy codeThe code is as follows:
[Root @ localhost ~] # Vmstat-s
4043760 total memory
1013884 used memory
513012 active memory
387728 inactive memory
3029876 free memory
199616 buffer memory
690980 swap cache
6096656 total swap
0 used swap
6096656 free swap
83587 non-nice user cpu ticks
132 nice user cpu ticks
278599 system cpu ticks
913344692 idle cpu ticks
814550 IO-wait cpu ticks
10547 IRQ cpu ticks
21261 softirq cpu ticks
0 stolen cpu ticks
310215 pages paged in
14254652 pages paged out
0 pages swapped in
0 pages swapped out
288374745 interrupts
146680577 CPU context switches
1351868832 boot time
367291 forks
Note:
The information is from/proc/meminfo,/proc/stat, and/proc/vmstat.
Instance 5: View disk read/write
Command: vmstat-d
Output:
Copy codeThe code is as follows:
[Root @ localhost ~] # Vmstat-d
Disk------------- reads ------------ writes ----------- ----- IO ------
Total merged sectors MS cur sec
Ram0 0 0 0 0 0 0 0 0 0 0
Ram1 0 0 0 0 0 0 0 0 0 0 0
Ram2 0 0 0 0 0 0 0 0 0 0 0
Ram3 0 0 0 0 0 0 0 0 0 0 0
Ram4 0 0 0 0 0 0 0 0 0 0
Ram5 0 0 0 0 0 0 0 0 0 0 0
Ram6 0 0 0 0 0 0 0 0 0 0 0
Ram7 0 0 0 0 0 0 0 0 0 0 0
Ram8 0 0 0 0 0 0 0 0 0 0 0
Ram9 0 0 0 0 0 0 0 0 0 0 0
Ram10 0 0 0 0 0 0 0 0 0 0 0
Ram11 0 0 0 0 0 0 0 0 0 0 0
Ram12 0 0 0 0 0 0 0 0 0 0 0
Ram13 0 0 0 0 0 0 0 0 0 0 0
Ram14 0 0 0 0 0 0 0 0 0 0 0
Ram15 0 0 0 0 0 0 0 0 0 0
Sda 33381 6455 615407 63224 2068111 1495416 28508288 15990289 0 10491
Hdc 0 0 0 0 0 0 0 0 0 0 0
Fd0 0 0 0 0 0 0 0 0 0 0
Md0 0 0 0 0 0 0 0 0 0 0
[Root @ localhost ~] #
Note:
This information mainly comes from/proc/diskstats.
Merged: indicates a merge write/read request. Generally, the system merges multiple connected/adjacent read/write requests to perform the operation.
Instance 6: view the read/write operations on the/dev/sda1 disk.
Command: vmstat-p/dev/sda1
Output:
Copy codeThe code is as follows:
[Root @ SCF1129 ~] # Df
File System 1 K-block used available % mount point
/Dev/sda3 1119336548 27642068 1034835500 3%/tmpfs 32978376 0 32978376 0%/dev/shm
/Dev/sda1 1032088 59604 920056 7%/boot
[Root @ SCF1129 ~] # Vmstat-p/dev/sda1
Sda1 reads read sectors writes requested writes
18607 4249978 6 48 [root @ SCF1129 ~] # Vmstat-p/dev/sda3
Sda3 reads read sectors writes requested writes
429350 35176268 28998789 980301488 [root @ SCF1129 ~] #
Note:
This information mainly comes from/proc/diskstats.
Reads: The number of reads from this partition.
Read sectors: the number of read sectors from this partition.
Writes: number of writes from this partition.
Requested writes: number of write requests from this partition.
Instance 7: view the slab information of the system
Command: vmstat-m
Output:
Copy codeThe code is as follows:
[Root @ localhost ~] # Vmstat-m
Cache Num Total Size Pages
Ip_conntrack_expect 0 0 136 28
Ip_conntrack 3 13 304 13
Ip_fib_alias 11 59 64 59
Ip_fib_hash 11 59 64 59
AF_VMCI 0 0 960 4
Bio_map_info 100 105 1064 7
Dm_mpath 0 0 1064 7
Jbd_4kb 0 0 4096 1
Dm_uevent 0 0 2608 3
Dm_tio 0 0 24 144
Dm_io 0 0 48 77
Scsi_cmd_cache 10 10 384 10
Sgpool-128 32 32 4096 1
Sgpool-64 32 32 2048 2
Sgpool-32 32 32 1024 4
Sgpool-16 32 32 512 8
Sgpool-8 45 256 15
Scsi_io_context 0 0 112 34
Ext3_inode_cache 51080 51105 760 5
Ext3_xattr 36 88 88 44
Journal_handle 18 144 24 144
Journal_head 56 80 96 40
Revoke_table 4 202 16 202
Revoke_record 0 0 32 112
Uhci_urb_priv 0 0 56 67
UNIX 13 33 704 11
Flow_cache 0 0 128 30
Msi_cache 33 59 64 59
Cfq_ioc_pool 14 90 128 30
Cfq_pool 12 90 216 18
Crq_pool 16 96 80 48
Deadline_drq 0 0 80 48
As_arq 0 0 96 40
Mqueue_inode_cache 1 4 896 4
Isofs_inode_cache 0 0 608 6
Hugetlbfs_inode_cache 1 7 576 7
Cache Num Total Size Pages
Ext2_inode_cache 0 0 720 5
Ext2_xattr 0 0 88 44
Dnotify_cache 0 0 40 92
Dquot 0 0 256 15
Eventpoll_pwq 3 53 72 53
Eventpoll_epi 3 20 192 20
Inotify_event_cache 0 0 40 92
Inotify_watch_cache 1 53 72 53
Kioctx 0 0 320 12
Kiocb 0 0 256 15
Fasync_cache 0 0 24 144
Shmem_inode_cache 254 290 768 5
Posix_timers_cache 0 0 128 30
Uid_cache 0 0 128 30
Ip_mrt_cache 0 0 128 30
Tcp_bind_bucket 3 112 32 112
Inet_peer_cache 0 0 128 30
Secpath_cache 0 0 64 59
Xfrm_dst_cache 0 0 384 10
Ip_dst_cache 5 10 384 10
Arp_cache 1 15 256 15
RAW 5 5 768 5
UDP 5 10 768 5
Tw_sock_TCP 0 0 192 20
Request_sock_TCP 0 0 128 30
TCP 4 5 1600 5
Blkdev_ioc 14 118 64 59
Blkdev_queue 20 30 1576 5
Blkdev_requests 13 42 272 14
Biovec-256 7 4096 1
Biovec-128 7 8 2048 2
Biovec-64 7 8 1024 4
Biovec-16 7 15 256 15
Biovec-4 7 59 64 59
Biovec-1 23 202 16 202
Bio 270 270 128 30
Utrace_engine_cache 0 0 64 59
Cache Num Total Size Pages
Utrace_cache 0 0 64 59
Sock_inode_cache 33 48 640 6
Skbuff_fclone_cache 7 7 512 7
Skbuff_head_cache 319 390 256 15
File_lock_cache 1 22 176 22
Acpi-Operand 4136 4248 64 59
Acpi-ParseExt 0 0 64 59
Acpi-Parse 0 0 40 92
Acpi-State 0 0 80 48
Acpi-Namespace 2871 2912 32 112
Delayacct_cache 81 295 64 59
Taskstats_cache 4 53 72 53
Proc_inode_cache 1427 1440 592 6
Sigqueue 0 160 24
Radix_tree_node 13166 13188 536 7
Bdev_cache 23 24 832 4
Sysfs_dir_cache 5370 5412 88 44
Mnt_cache 26 30 256 15
Inode_cache 2009 2009 560 7
Dentry_cache 60952 61020 216 18
Filp 479 1305 256 15
Names_cache 3 3 4096 1
Avc_node 14 53 72 53
Selinux_inode_security 994 1200 80 48
Key_jar 2 20 192 20
Idr_layer_cache 74 77 528 7
Buffer_head 164045 164800 96 40
Mm_struct 51 56 896 4
Vm_area_struct 1142 1958 176 22
Fs_cache 35 177 64 59
Files_cache 36 55 768 5
Signal_cache 72 162 832 9
Sighand_cache 68 84 2112 3
Task_struct 76 80 1888 2
Anon_vma 458 864 24 144
Pid 83 295 64 59
Shared_policy_node 0 0 48 77
Cache Num Total Size Pages
Numa_policy 37 144 24 144
Size-131072 (DMA) 0 0 131072 1
Size-131072 0 0 131072 1
Size-65536 (DMA) 0 0 65536 1
Size-65536 1 65536 1
Size-32768 (DMA) 0 0 32768 1
Size-32768 2 2 32768 1
Size-16384 (DMA) 0 0 16384 1
Size-16384 5 5 16384 1
Size-8192 (DMA) 0 0 8192 1
Size-8192 7 7 8192 1
Size-4096 (DMA) 0 0 4096 1
Size-4096 110 111 4096 1
Size-2048 (DMA) 0 0 2048 2
Size-2048 602 602 2048 2
Size-1024 (DMA) 0 0 1024 4
Size-1024 344 352 1024 4
Size-512 (DMA) 0 0 512 8
Size-512 433 480 512 8
Size-256 (DMA) 0 0 256 15
Size-256 1139 1155 256 15
Size-128 (DMA) 0 0 128 30
Size-64 (DMA) 0 0 64 59
Size-64 5639 5782 64 59
Size-32 (DMA) 0 0 32 112
Size-128 801 930 128 30
Size-32 3005 3024 32 112
Kmem_cache 137 137 2688 1
This set of information comes from/proc/slabinfo.
Slab: Because the kernel has many small objects, the structures of these objects are frequently destroyed, such as I-node and dentry. if these objects require a page (4 KB) to the memory each time they are built ), in fact, there are only a few bytes, which will be very wasteful. to solve this problem, a new mechanism is introduced to deal with how to allocate a small storage area in the same page box, slab can allocate small objects so that no page boxes are allocated for each object, saving space. the kernel frequently creates analysis structures for some small objects, slab caches these small objects and can be reused to reduce the number of memory allocations.