当前位置:   article > 正文

Linux 内存之vmstat

vmstat

前言

NAME
       vmstat - Report virtual memory statistics
  • 1
  • 2

vmstat 报告有关processes,memory, paging,block IO,traps,disks 和 cpu activity的信息。这些报告旨在帮助识别系统瓶颈,Linux vmstat并不将自己视为正在运行的进程。
vmstat用来观测系统整体的性能情况,并不能观测单个进程,单个进程观测可以用pidstat。

一、vmstat简介

虚拟内存统计命令vmstat提供系统内存运行状况的高级视图,包括当前可用内存和分页统计信息。

delay:更新时间间隔,以秒为单位。如果没有指定延迟,则只打印一个报告,其中包含自引导以来的平均值。
count:更新次数。在没有指定count的情况下,当定义了delay时,count默认值则为无限大。

vmstat每两秒显示一次,显示五次:

[root@localhost ~]# vmstat -w 2 5
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      1100516        17264      5916084    0    0     1     1    4    2   2   1  97   0   0
 0  0            0      1100292        17264      5916084    0    0     0     0  729 3516   5   2  93   0   0
 1  0            0      1100268        17264      5916084    0    0     0     4  825 3379   5   2  93   0   0
 1  0            0      1100020        17264      5916084    0    0     0     0  831 3464   6   2  92   0   0
 0  0            0      1099872        17264      5916084    0    0     0     2  908 3519   5   2  93   0   0
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8

我们可以看到第一行数据的与其他行的数据差异较大(比如 in 和 cs 这两个数据),查看man手册说明:

 The  first report produced gives averages since the last reboot.  Additional reports give information on a sampling period of length delay.  The process and memory reports are instantaneous in either case.
  • 1

第一行数据是系统启动以来的平均值,其他行才是你在运行 vmstat 命令时,设置的间隔时间的平均值。另外,进程和内存的报告内容都是即时数值。

1.1 processes

Procs
    r: The number of runnable processes (running or waiting for run time).
    b: The number of processes in uninterruptible sleep.
  • 1
  • 2
  • 3

r 表示 处于运行态的进程,正在执行或者正在运行队列中等待运行。
通过ps或top显示为 R 状态(Running 或 Runnable)的进程。

b 表示 不可中断的处于睡眠态的进程,进程正在睡眠(阻塞),等待某些条件的达成。但是与 interruptible sleep 的进程不一样的是,该进程在等待条件的过程中,不会对信号做出任何响应。
通过ps或top显示为D状态的进程就是处于uninterruptible sleep的进程,该类型进程通常是等待硬件设备的 I/O 响应等重要的操作,或者持有一个信号量。
比如,当一个进程向磁盘读写数据时,为了保证数据的一致性,在得到磁盘回复前,它是不能被其他进程或者中断打断的,这个时候的进程就处于不可中断状态。如果此时的进程被打断了,就容易出现磁盘数据与进程数据不一致的问题。
不可中断状态是系统对进程和硬件设备的一种保护机制。

1.2 memory

(1)

 swpd: the amount of of swapped-out memory.
 free: the amount of idle memory (Free available memory).
 buff: the amount of memory used as buffers (buffer cache).
 cache: the amount of memory used as cache (page cache).
  • 1
  • 2
  • 3
  • 4

buff/cache:系统中的空闲内存(free memory)在系统启动后,如果空闲内存(free memory)较多,会有一部分用来当作缓存,以提高系统性能。这部分缓存可以在需要的时候(free memory不够的时候)释放出来供应用程序使用。

缓存(buff/cache)占用实际的物理内存,通常缓存的是磁盘上的数据,用来减少对磁盘I/O的操作,把对磁盘的访问变成对物理内存的访问,应该缓存经常访问的数据(热数据),而不是不常访问的数据(冷数据)。

(2)

Swap
    si: Amount of memory swapped in from disk (/s).
    so: Amount of memory swapped to disk (/s).
  • 1
  • 2
  • 3

如果 the si and so 持续非零,则系统处于内存不足的状态,内存的一些数据(最近没有访问的数据,非活跃的数据)正在交换到 swap device(Swap分区,匿名页被交换到Swap分区) 或者 file(file-backed pages会被直接写入到文件中)中。

anonymous pages和file-backed pages:
在这里插入图片描述
Linux一个进程使用的内存分为2种:
file-backed pages(有文件背景的页面,比如代码段、比如read/write方法读写的文件、比如mmap读写的文件;他们有对应的硬盘文件,因此如果要交换,可以直接和硬盘对应的文件进行交换),此部分页面进page cache。
anonymous pages(匿名页,如stack,heap,CoW后的数据段等;他们没有对应的硬盘文件,因此如果要交换,只能交换到虚拟内存-swapfile或者Linux的swap硬盘分区),此部分页面,如果系统内存不充分,可以被swap到swapfile或者硬盘的swap分区。
具体可参考:swappiness=0究竟意味着什么?

如果系统开启了Swap分区,在物理内存不够用时,操作系统会从物理内存中把部分暂时不被使用的数据转移到交换分区,从而为当前运行的程序留出足够的物理内存空间。通常生产环境不会开启Swap分区,会影响性能。

查看交换分区:

[root@localhost ~]# free -m
              total        used        free      shared  buff/cache   available
Mem:           7705         837        1502         138        5365        6275
Swap:          7935           0        7935
  • 1
  • 2
  • 3
  • 4
NAME
       swapon, swapoff - enable/disable devices and files for paging and swapping

[root@localhost ~]# swapon
NAME      TYPE      SIZE USED PRIO
/dev/dm-1 partition 7.8G   0B   -2
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
[root@localhost ~]# cat /proc/swaps
Filename                                Type            Size    Used    Priority
/dev/dm-1                               partition       8126460 0       -2
  • 1
  • 2
  • 3
[root@localhost ~]# cat /etc/fstab
......
/dev/mapper/centos-swap swap                    swap    defaults        0 0
  • 1
  • 2
  • 3

swappiness用于设置向交换分区写页面的活跃程度:

[root@localhost ~]# cat /proc/sys/vm/swappiness
20
  • 1
  • 2

swappiness越大,越倾向于回收匿名页;swappiness越小,越倾向于回收file-backed的页面。当然,它们的回收方法都是一样的LRU算法。
swappiness = 0,当内存不足时,只回收file-backed的页面,不回收匿名页。
swappiness = 1,进行最少量的匿名页交换,而不禁用交换。
swappiness=100,当内存不足时的时候,表示积极的使用swap分区,并且把内存上的数据及时的搬运到swap空间里面。
swappiness的值为 0 到 100。

1.3 block IO

IO
    bi: Blocks received from a block device (blocks/s).
    bo: Blocks sent to a block device (blocks/s).
  • 1
  • 2
  • 3

所有 linux blocks 目前都是1024字节。

1.4 System

System
    in: The number of interrupts per second, including the clock.
    cs: The number of context switches per second.
  • 1
  • 2
  • 3

其中 in 代表系统每秒发生中断的次数,包括时钟中断(是否包括了系统软中断)。

其中 cs 代表系统每秒上下文切换的次数,显示了系统总体的上下文切换情况。
上下文切换包括:进程上下文切换、线程上下文切换以及中断上下文切换(是否包括了系统调用上下文切换)。

man 手册中指出:没有统计系统调用的数目,cs应该没有包括系统调用上下文切换的次数。

Does not tabulate the block io per device or count the number of system calls.
  • 1

1.5 cpu activity

CPU
    These are percentages of total CPU time.
    us: Time spent running non-kernel code.  (user time, including nice time)
    sy: Time spent running kernel code.  (system time)
    id: Time spent idle.
    wa: Time spent waiting for IO.
    st: Time stolen from a virtual machine.
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7

二、使用步骤

2.1 vmstat -a

-a, --active
              Display active and  inactive memory
  • 1
  • 2
[root@localhost ~]# 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
 0  0      0 1543828 1254616 4325384    0    0     1     1    1    5  2  1 97  0  0
  • 1
  • 2
  • 3
  • 4

inact 是 the page cache 中 inactive memory 的数目。
active 是 the page cache 中 active memory 的数目。

2.2 vmstat -f

-f, --forks
  • 1
[root@localhost ~]# vmstat -f
      1688718 forks
  • 1
  • 2

显示自系统引导(boot)以来的fork数量。这包括fork、vfork和clone系统调用,相当于创建的任务总数。每个进程由一个或多个任务表示,具体取决于线程的使用情况。

2.3 vmstat -m

-m, --slabs
       Displays slabinfo.
  • 1
  • 2
[root@localhost ~]# vmstat -m
Cache                       Num  Total   Size  Pages
fuse_request                 80     80    400     20
fuse_inode                   21     21    768     21
nf_conntrack_ffff970adafd8000      0      0    320     25
nf_conntrack_ffffffff9dd11640   1200   1200    320     25
rpc_inode_cache              25     25    640     25
fat_inode_cache             432    682    720     22
fat_cache                  2346   2346     40    102
kvm_vcpu                      0      0  13056      2
xfs_dqtrx                     0      0    528     31
xfs_dquot                     0      0    488     16
xfs_ili                   95208  95208    168     24
xfs_inode                118541 118541    960     17
xfs_efd_item                 95    209    416     19
xfs_log_ticket              792    792    184     22
bio-3                       407    500    320     25
i915_dependency               0      0     64     64
i915_request                  0      0    576     28
drm_i915_gem_object         787   1218    768     21
kcopyd_job                    0      0   3312      9
dm_uevent                     0      0   2608     12
dm_rq_target_io               0      0    136     30
ip6_dst_cache               306    306    448     18
RAWv6                       104    104   1216     26
UDPLITEv6                     0      0   1216     26
UDPv6                       104    104   1216     26
tw_sock_TCPv6                 0      0    256     16
TCPv6                        60     60   2112     15
cfq_queue                     0      0    232     17
bsg_cmd                       0      0    312     26
mqueue_inode_cache           36     36    896     18
hugetlbfs_inode_cache        78     78    608     26
configfs_dir_cache           46     46     88     46
dquot                         0      0    256     16
kioctx                      112    112    576     28
userfaultfd_ctx_cache         0      0    192     21
pid_namespace                14     14   2200     14
posix_timers_cache          608    608    248     16
UDP-Lite                      0      0   1088     30
flow_cache                    0      0    144     28
UDP                         120    120   1088     30
tw_sock_TCP                 240    320    256     16
TCP                          64     64   1984     16
dax_cache                    21     21    768     21
blkdev_queue                 52     52   2456     13
blkdev_ioc                  195    195    104     39
user_namespace               68     68    480     17
sock_inode_cache           1208   1300    640     25
fsnotify_mark_connector  145872 146880     24    170
net_namespace                24     24   5248      6
shmem_inode_cache          1979   2256    680     24
Acpi-State                 2943   3417     80     51
task_delay_info             900    900    112     36
taskstats                    96     96    328     24
proc_inode_cache          89554  90336    656     24
Cache                       Num  Total   Size  Pages
sigqueue                    225    275    160     25
bdev_cache                   76     76    832     19
kernfs_node_cache         30202  30566    120     34
mnt_cache                 13601  14217    384     21
inode_cache               24273  24273    592     27
dentry                   421617 421617    192     21
iint_cache                    0      0    128     32
avc_xperms_node            1606   1606     56     73
avc_node                  18274  18368     72     56
selinux_inode_security   241622 245004     40    102
buffer_head              629616 629616    104     39
vm_area_struct            31754  32346    216     18
mm_struct                   260    260   1600     20
files_cache                 300    300    640     25
signal_cache                506    532   1152     28
sighand_cache               353    405   2112     15
task_xstate                 780    780   1088     30
task_struct                 558    637   4160      7
anon_vma                  10598  11679     80     51
shared_policy_node         3393   3655     48     85
numa_policy                  31     31    264     31
radix_tree_node           43204  43204    584     28
idr_layer_cache             375    375   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               0      0    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
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                 84    108   8192      4
kmalloc-4096                701    744   4096      8
kmalloc-2048                945   1040   2048     16
kmalloc-1024               5819   7920   1024     16
kmalloc-512                1974   4496    512     16
kmalloc-256               12207  13792    256     16
kmalloc-192                8287   8379    192     21
kmalloc-128                5374   6496    128     32
kmalloc-96                10878  10878     96     42
kmalloc-64               350630 353536     64     64
kmalloc-32               136946 138752     32    128
kmalloc-16                73134  75520     16    256
kmalloc-8                 87552  87552      8    512
kmem_cache_node             320    320     64     64
kmem_cache                  144    144    256     16
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78
  • 79
  • 80
  • 81
  • 82
  • 83
  • 84
  • 85
  • 86
  • 87
  • 88
  • 89
  • 90
  • 91
  • 92
  • 93
  • 94
  • 95
  • 96
  • 97
  • 98
  • 99
  • 100
  • 101
  • 102
  • 103
  • 104
  • 105
  • 106
  • 107
  • 108
FIELD DESCRIPTION FOR SLAB MODE
       cache: Cache name
       num: Number of currently active objects
       total: Total number of available objects
       size: Size of each object
       pages: Number of pages with at least one active object
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6

2.4 vmstat -s

-s, --stats
  • 1

显示各种事件计数器和内存统计信息。

[root@localhost ~]# vmstat -s
      7890812 K total memory
       856064 K used memory
      4648752 K active memory
      1388784 K inactive memory
      1101524 K free memory
        17264 K buffer memory
      5915960 K swap cache
      8126460 K total swap
            0 K used swap
      8126460 K free swap
     15121809 non-nice user cpu ticks
        15133 nice user cpu ticks
      5119922 system cpu ticks
    690449365 idle cpu ticks
        57602 IO-wait cpu ticks
            0 IRQ cpu ticks
         5756 softirq cpu ticks
            0 stolen cpu ticks
      3828326 pages paged in
      7187387 pages paged out
            0 pages swapped in
            0 pages swapped out
    582941861 interrupts
   2668486471 CPU context switches
   1667480478 boot time
      1858539 forks
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27

2.5 vmstat -d/D

-d, --disk
       Report disk statistics
  • 1
  • 2
[root@localhost ~]# vmstat -d
disk- ------------reads------------ ------------writes----------- -----IO------
       total merged sectors      ms  total merged sectors      ms    cur    sec
sda    50328   1098 5830005  346513 251291  81504 13311417 8074414      0   1322
dm-0   50608      0 5753849  362039 294970      0 13273320 29031220      0   1321
dm-1      91      0    4928    2075      0      0       0       0      0      1
dm-2      94      0    6729    1398     17      0    4440     119      0      1
sdb     3536  29173  386910   21114      2      0       2      18      0      6
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
FIELD DESCRIPTION FOR DISK MODE
   Reads
       total: Total reads completed successfully
       merged: grouped reads (resulting in one I/O)
       sectors: Sectors read successfully
       ms: milliseconds spent reading

   Writes
       total: Total writes completed successfully
       merged: grouped writes (resulting in one I/O)
       sectors: Sectors written successfully
       ms: milliseconds spent writing

   IO
       cur: I/O in progress  (正在进行的I/O操作)
       s: seconds spent for I/O
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
-D, --disk-sum
       Report some summary statistics about disk activity.
  • 1
  • 2
[root@localhost ~]# vmstat -D
            5 disks
            4 partitions
       104657 total reads
        30271 merged reads
     11982421 read sectors
       733139 milli reading
       546326 writes
        81506 merged writes
     26589723 written sectors
     37106137 milli writing
            0 inprogress IO
         2652 milli spent IO
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13

三、vmstat 数据来源

[root@localhost ~]# strace -e trace=open vmstat
......
open("/proc/meminfo", O_RDONLY)         = 3
open("/proc/stat", O_RDONLY)            = 4
open("/proc/vmstat", O_RDONLY)          = 5
......
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6

参考资料

https://zhuanlan.zhihu.com/p/444321207
swappiness=0究竟意味着什么?

极客时间:Linux 性能优化实战
极客时间:Linux 内核技术实战课
Systems.Performance.Enterprise.and.the.Cloud.2nd.Edition

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/从前慢现在也慢/article/detail/76533
推荐阅读
相关标签
  

闽ICP备14008679号