[Linux] vmstat command under Linux and Mac

1. Mito

Insert picture description here

2. Overview

vmstat is Virtual Meomory Statisticsthe abbreviation of (Virtual Memory Statistics), which can monitor the virtual memory, process and CPU activities of the operating system. It is a statistical analysis of the overall situation of the system. The disadvantage is that it is impossible to conduct an in-depth analysis of a process.

3. The difference between physical memory and virtual memory

We know that from directly 物理内存读写数据要比从硬盘读写数据要快的多, therefore, we hope that all data reading and writing are done in memory, and 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 to physical memory, there is a concept of virtual memory under Linux 虚拟内存就是为了满足物理内存的不足而提出的策略. It is a piece of logical memory that is virtualized using disk space and used as a disk for virtual memory. The space is called Swap Space.

As an extension of physical memory linux会在物理内存不足时,使用交换分区的虚拟内存,更详细的说,就是内核会将暂时不用的内存块信息写到交换空间, this way, the physical memory is released, and this piece of memory can be used for other purposes. When the original content is needed, this information will be re-read from the swap space into the physical memory.

Linux's memory management adopts a paging access mechanism. In order to ensure that physical memory can be fully utilized, the kernel will automatically exchange data blocks that are not frequently used in physical memory into virtual memory at the appropriate time. The information is retained in physical memory.

To understand the operating mechanism of Linux memory in depth, you need to know the following aspects:

First, the Linux system will perform page swap operations from time to time to keep as much free physical memory as possible. Even if nothing needs memory, Linux will swap out memory pages that are not used temporarily. This avoids waiting for the time required for the exchange.

Secondly, page swapping by Linux is conditional. Not all pages are swapped to virtual memory when not in use. The Linux kernel 最近最经常使用only swaps some infrequently used page files to virtual memory according to the "algorithm " . Sometimes we see this a linux物理内存还有很多,但是交换空间也使用了很多phenomenon: .

In fact, this is not surprising. For example, when a process that occupies a lot of memory runs, it will consume a lot of memory resources. At this time, some unused page files will be swapped into virtual memory, but later this will take up a lot of memory resources. When the process ends and a lot of memory is released, the page file that was just swapped out will not be automatically swapped into physical memory. Unless this is necessary, then the system physical memory will be much free at the moment, and the swap space is also being used. The phenomenon just mentioned has appeared. On this point, don't worry about anything, just know what is going on.

Finally, the pages of the swap space are first swapped to physical memory when they are used. If there is not enough physical memory to accommodate these pages, they will be swapped out immediately, so there may not be enough space in the virtual memory to store them. These swap pages will eventually cause problems such as fake crashes and abnormal services in Linux. Although Linux can recover itself within a period of time, the recovered system is basically unavailable.

Therefore, it is very important to properly plan and design the use of linux memory.

4. Principle of Virtual Memory

Every process running in the system needs to use memory, but not every process needs to use the memory space allocated by the system at all times. When the memory required for system operation exceeds the actual physical memory, the kernel will release some or all unused physical memory occupied by some processes, and store this part of the data on the disk until the next call of the process, and the released memory Provide to the process in need.

In Linux memory management, the above memory scheduling is mainly accomplished through "Paging" and "Swapping". The paging algorithm is to change the pages that are not used frequently in memory to disk, and keep the active pages in memory for the process to use. Swap technology is to swap the entire process, not parts of 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 found that the page is not in physical memory (because it has been Page-Out), then a page fault (Page Fault) occurs.

When the system kernel finds that there is less running memory, it uses Page-Out to free some physical memory. Although Page-Out does not happen frequently, if Page-Out happens frequently and continuously, until the kernel manages the paging time beyond the time of running the program, the system performance will drop sharply. At this time, the system has been running very slowly or has entered a suspended state, which is also called thrashing.

5. Common command display

vmstat 5  5 【在5秒时间内进行5次采样】

Insert picture description here
Field description:

Procs (process):

  r: 运行队列中进程数量
  b: 等待IO的进程数量

Memory:

  swpd: 使用虚拟内存大小
  free: 可用内存大小
  buff: 用作缓冲的内存大小
  cache: 用作缓存的内存大小

Swap:

  si: 每秒从交换区写到内存的大小
  so: 每秒写入交换区的内存大小

IO: (the size of the current Linux version block is 1024bytes)

bi: 每秒读取的块数

bo: 每秒写入的块数

system:

in: 每秒中断数,包括时钟中断。【interrupt】

cs: 每秒上下文切换数。        【count/second】

CPU (expressed as a percentage):

  us: 用户进程执行时间(user time)

  sy: 系统进程执行时间(system time)

  id: 空闲时间(包括IO等待时间),中央处理器的空闲时间 。以百分比表示。

  wa: 等待IO时间

Remarks:

  1. If r is often greater than 4, id is often less than 40, indicating that the CPU load is heavy.
  2. If bi, bo is not equal to 0 for a long time, it means that the memory is insufficient.
  3. If disk is often not equal to 0, and the queue in b is greater than 3, it means that the io performance is not good.

Linux has high stability and reliability, and 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, when maintaining a Linux system and optimizing the system, it is essential for enterprises to understand the system performance analysis tools.

Show active and inactive memory

vmstat -a 2 5 【-a 显示活跃和非活跃内存,所显示的内容除增加inact和active】

Insert picture description here
Shows the number of forks since system startup

vmstat -f 【 linux下创建进程的系统调用是fork】

Insert picture description here
Explanation: The information is obtained from the processes field in / proc / stat

View memory usage details

vmstat -s   【显示内存相关统计信息及多种系统活动数量】

Insert picture description here
Note: The information comes from / proc / meminfo, / proc / stat and / proc / vmstat

View disk read / write

vmstat -d 【查看磁盘的读写】

Insert picture description here
Note: This information mainly comes from / proc / diskstats.

View the read / write of the / dev / sda1 disk

vmstat -p /dev/sda1 【显示指定磁盘分区统计信息】

Insert picture description here

Note: This information mainly comes from / proc / diskstats.

reads:来自于这个分区的读的次数。

read sectors:来自于这个分区的读扇区的次数。

writes:来自于这个分区的写的次数。

requested writes:来自于这个分区的写请求次数。

View system slab information

vmstat -m

Note: This information mainly comes from / proc / slabinfo

slab: Since the kernel will have many small objects, these objects are constructed and destroyed very frequently, such as i-node, dentry. If these objects require a page (4kb) from the memory each time they are built, it will be very wasteful. This problem introduces a new mechanism to deal with how to allocate small storage areas in the same page frame, and slab can allocate small objects, so that there is no need to allocate page frames for each object, thereby saving space. The kernel creates and destructs some small objects very frequently, and slab buffers these small objects, which can be reused to reduce the number of memory allocations.

View process path

[root@localhost ~]# netstat -an | grep 2158

[root@localhost ~]# ll /proc/2158

Insert picture description here

cwd符号链接的是进程运行目录;

exe符号连接就是执行程序的绝对路径;

cmdline就是程序运行时输入的命令行命令;

environ记录了进程运行时的环境变量;

fd目录下是进程打开或使用的文件的符号连接。
lsof -p 2158

7.mac

No under mac linux/unix 的vmstat, only vm_stat;

(base) lcc@lcc Desktop$ vm_stat
Mach Virtual Memory Statistics: (page size of 4096 bytes)
Pages free:                              200530.
Pages active:                            478616.
for row in range(1,len(processLines)):
Pages inactive:                          452761.
Pages speculative:                        25424.
Pages throttled:                              0.
Pages wired down:                        403635.
Pages purgeable:                           9540.
"Translation faults":                 313351302.
Pages copy-on-write:                    7367819.
Pages zero filled:                    154387344.
Pages reactivated:                    113740039.
Pages purged:                           3345908.
File-backed pages:                       321220.
Anonymous pages:                         635581.
Pages stored in compressor:             2181189.
Pages occupied by compressor:            535874.
Decompressions:                        59942755.
Compressions:                          78561323.
Pageins:                               22656568.
Pageouts:                                444951.
Swapins:                               14586137.
Swapouts:                              15327080.

7.1 Python commands

#!/usr/bin/python

import subprocess
import re

# Get process info
ps = subprocess.Popen([‘ps‘, ‘-caxm‘, ‘-orss,comm‘], stdout=subprocess.PIPE).communicate()[0]
vm = subprocess.Popen([‘vm_stat‘], stdout=subprocess.PIPE).communicate()[0]

# Iterate processes
processLines = ps.split(‘\n‘)
sep = re.compile([\s]+‘)
rssTotal = 0 # kB
for row in range(1,len(processLines)):
    rowText = processLines[row].strip()
    rowElements = sep.split(rowText)
    try:
        rss = float(rowElements[0]) * 1024
    except:
        rss = 0 # ignore...
    rssTotal += rss

# Process vm_stat
vmLines = vm.split(‘\n‘)
sep = re.compile(‘:[\s]+‘)
vmStats = {}
for row in range(1,len(vmLines)-2):
    rowText = vmLines[row].strip()
    rowElements = sep.split(rowText)
    vmStats[(rowElements[0])] = int(rowElements[1].strip(‘\.‘)) * 4096

print ‘Wired Memory:\t\t%d MB‘ % ( vmStats["Pages wired down"]/1024/1024 )
print ‘Active Memory:\t\t%d MB‘ % ( vmStats["Pages active"]/1024/1024 )
print ‘Inactive Memory:\t%d MB‘ % ( vmStats["Pages inactive"]/1024/1024 )
print ‘Free Memory:\t\t%d MB‘ % ( vmStats["Pages free"]/1024/1024 )
print ‘Real Mem Total (ps):\t%.3f MB‘ % ( rssTotal/1024/1024 )

Execute as follows

(base) lcc@lcc Desktop$ ./aa.python
Wired Memory:		1558 MB
Active Memory:		1926 MB
Inactive Memory:	1817 MB
Free Memory:		700 MB
Real Mem Total (ps):	4287.605 MB
(base) lcc@lcc Desktop$
Published 1254 original articles · praised 465 · 1.59 million views

Guess you like

Origin blog.csdn.net/qq_21383435/article/details/105597501