一、内存分析之vmstat
命令
1.一些基础知识
- vmstat的全称是
Report virtual memory statistics
,报告虚拟内存统计信息报告。 - 生成的第一份报告是自上一重启到现在的平均值。但是不管是哪种类型的报告,进程和内存信息都是实时的。
- 后面生成的报告是从上一次采样到现在的平均值。
2. vmstat的语法格式
- vmstat [options] [delay [count]]
- options:选项
- delay:刷新间隔。(两次采样的时间间隔)
- count:采样的次数
二、vmstat报告内容字段说明
[root@tysonscloud ~]# vmstat
procs -----------memory---------- ---swap-- -----io---- -system-- ------cpu-----
r b swpd free buff cache si so bi bo in cs us sy id wa st
2 0 0 308836 142120 1153132 0 0 3 25 20 6 1 0 99 0 0
2.1 process部分
- r
- The number of runnable processes (running or waiting for run time).
- CPU中处于running状态的进程的数量。
- 类似于top命令中的load average。当这个值超过了CPU的数量时CPU的性能会出现瓶颈。
- b
- The number of processes in uninterruptible sleep.
- 表示处于阻塞状态的进程数量。
2.2 memory部分
- swpd
- the amount of virtual memory used.
- 已经使用到的虚拟内存大小。
- 当这个值大于0时说明此时物理内存可能不足。
- free
- the amount of idle memory.
- 空闲物理内存大小。
- buff
- the amount of memory used as buffers.
- 物理内存中用作缓冲的空间的大小。
- 是与特定的块设备相关的,包括文件系统的的元数据(数据、文件权限)的缓存以及正在进行的页面。(毕竟是从文件系统里出来的)
- cache
- the amount of memory used as cache.
- 物理内存中用作缓存的空间的大小。
- 仅仅包含停放的文件数据。
2.3 swap部分
- si
- Amount of memory swapped in from disk (/s).
- 每秒钟从硬盘读取到交换空间的大小。
- so
- Amount of memory swapped to disk (/s).
- 每秒钟从交换空间交换到硬盘的大小。
2.4 io部分
- bi
- Blocks received from a block device (blocks/s).
- 每秒从块设备接收到的块的数量。
- 块设备是指系统上的磁盘和其他块设备(光盘、U盘)。
- bo
- Blocks sent to a block device (blocks/s).
- 每秒发送到块设备进行写入的块的数量。
2.5 system部分
- in:
- The number of interrupts per second, including the clock.
- 每秒钟CPU中断数,包括时钟中断。
- cs:The number of context switches per second.
- 每秒钟切换上下文的次数。
- 上下文切换是指CPU从一个进程或线程切换到另一个进程或线程。
- 发生上下文切换的场景:程序调用系统函数(会把代码调入到内核空间)、进程和线程的的切换。
- 该值越小越好,数值越大意味着花了更多的时间进行上下文切换。
2.6 cpu部分
These are percentages of total CPU time.这里所示的都是CPU时间的百分比。
- us
- Time spent running non-kernel code. (user time, including nice time)
- 处于用户进程所消耗CPU的时间百分比。
- sy
- Time spent running kernel code. (system time)
- 处于系统进程所消耗CPU的时间百分比。
- 若该值过大,意味着IO操作频繁(系统调用频繁)。
- id
- Time spent idle. Prior to Linux 2.5.41, this includes IO-wait time.
- CPU处于空闲的时间百分比。(包括IO-WAIT状态)
- wa
- Time spent waiting for IO. Prior to Linux 2.5.41, included in idle.
- CPU处于等待IO的时间百分比,也就是IO-wait的时间百分比。
- st
- Time stolen from a virtual machine. Prior to Linux 2.6.11, unknown.
三、vmstat
报告中各字段的参考值
- procs部分r多则运行的进程多,系统较繁忙。
- bi/bo:大文件读写时10M以内不用担心,小文件读写时2M以内正常。
- cpu部分us若大于50,则需考虑优化。
- cpu部分wa若大于30(IO等待所占用的CPU时间超过了30%)则说明IO等待问题严重。
- cpu部分id若大于50,则需考虑优化。
四、vmstat
选项说明
以下内容主要对常用部分进行了翻译。部分参数未翻译。
4.1 delay
- The delay between updates in seconds. If no delay is specified,only one report is printed with the average values since boot.
- 两次采样的时间间隔。
4.2 count
- Number of updates. In absence of count, when delay is defined,default is infinite.
- 采样的次数。
4.3 -a, --active
- Display active and inactive memory, given a 2.5.41 kernel or bet‐ter.
- 显示active(活跃内存)或inactive(非活跃内存),而非显示buff(缓冲)和cache(缓存)。
4.4 -f, --forks
- The -f switch displays the number of forks since boot. This includes the fork, vfork, and clone system calls, and is equivalent to the total number of tasks created. Each process is represented by one or more tasks, depending on thread usage. This display does not repeat.
- 显示自从上一次开机(boot)以来所进行的task的数量。
4.5 -m, --slabs
- Displays slabinfo.
- 查看缓存分配器。
- 缓存分配器的作用是:替linux内核为**临时对象(任务、设备结构、结点)**分配内存。
[root@tysonscloud ~]# vmstat -m
Cache Num Total Size Pages
isofs_inode_cache 12 12 640 12
ext4_groupinfo_4k 420 420 136 30
………………………………
- Cache:缓存名称。
- num:当前活动对象的数量。
- 可用对象的总数。
- 每个对象的总数。
- 至少含有一个活动对象的页的总数。
4.6 -n, --one-header
- Display the header only once rather than periodically.
- 使用vmstat命令时,头行信息(字段名称)只显示一次,默认开启了该选项。
4.7 -s, --stats
- Displays a table of various event counters and memory statistics.This display does not repeat.
- 显示内存相关的统计信息以及多种系统活动数量。
[root@tysonscloud ~]# vmstat -s
1883844 K total memory
274440 K used memory
984324 K active memory
501096 K inactive memory
208348 K free memory
152232 K buffer memory
1248824 K swap cache
0 K total swap
0 K used swap
0 K free swap
1213580 non-nice user cpu ticks
472 nice user cpu ticks
423139 system cpu ticks
154582764 idle cpu ticks
422203 IO-wait cpu ticks
0 IRQ cpu ticks
6358 softirq cpu ticks
0 stolen cpu ticks
4024011 pages paged in
39315924 pages paged out
0 pages swapped in
0 pages swapped out
205309783 interrupts
401484760 CPU context switches
1548578290 boot time
2407854 forks
4.8 -d, --disk
- Report disk statistics (2.5.70 or above required).
- 显示磁盘相关统计信息
[root@tysonscloud ~]# vmstat -d
disk- ------------reads------------ ------------writes----------- -----IO------
total merged sectors ms total merged sectors ms cur sec
vda 236001 357 8047394 3025341 5291625 3765029 78638536 80981917 0 5522
sr0 88 0 628 46 0 0 0 0 0 0
- Reads部分
- total:已经完成的总读数。
- merged:分组读取数。
- sectors:已经完成读的扇区数。
- ms:读取消耗的毫秒数。
- Writes部分 类似
- IO部分
- cur:正在进行中的IO数(current)
- sec:IO消耗的秒数
4.9 -D, --disk-sum
- Report some summary statistics about disk activity.
4.10 -p, --partition device
- Detailed statistics about partition (2.5.70 or above required).
- 显示磁盘分区统计信息。
[root@tysonscloud ~]# df -h
Filesystem Size Used Avail Use% Mounted on
/dev/vda1 50G 3.9G 43G 9% /
devtmpfs 911M 0 911M 0% /dev
tmpfs 920M 24K 920M 1% /dev/shm
tmpfs 920M 340K 920M 1% /run
tmpfs 920M 0 920M 0% /sys/fs/cgroup
tmpfs 184M 0 184M 0% /run/user/0
[root@tysonscloud ~]# vmstat -p /dev/vda1
vda1 reads read sectors writes requested writes
235900 8044778 5247903 78642384
- reads:发给该分区的读取操作总数。
- read sectors:发给该分区的读取操作的总扇区数。
- writes:发给该分区的写入操作总数。
- requested writes:写入到该分区的写入请求总数。
4.11 -S, --unit character
- Switches outputs between 1000 (k), 1024 (K), 1000000 (m), or 1048576 (M) bytes. Note this does not change the swap (si/so) or block (bi/bo) fields.
- 通过该选项去指定显示的统计信息的单位大小。
[root@tysonscloud ~]# vmstat -S k
procs -----------memory---------- ---swap-- -----io---- -system-- ------cpu-----
r b swpd free buff cache si so bi bo in cs us sy id wa st
2 0 0 212787 156094 1279053 0 0 3 25 21 10 1 0 99 0 0
[root@tysonscloud ~]# vmstat -S K
procs -----------memory---------- ---swap-- -----io---- -system-- ------cpu-----
r b swpd free buff cache si so bi bo in cs us sy id wa st
2 0 0 207660 152436 1249076 0 0 3 25 21 10 1 0 99 0 0
[root@tysonscloud ~]# vmstat -S M
procs -----------memory---------- ---swap-- -----io---- -system-- ------cpu-----
r b swpd free buff cache si so bi bo in cs us sy id wa st
2 0 0 202 148 1219 0 0 3 25 21 10 1 0 99 0 0
4.12 -t, --timestamp
- Append timestamp to each line
- 给每行统计信息添加一个时间戳
[root@tysonscloud ~]# vmstat -t
procs -----------memory---------- ---swap-- -----io---- -system-- ------cpu----- -----timestamp-----
r b swpd free buff cache si so bi bo in cs us sy id wa st CST
2 0 0 207804 152464 1249104 0 0 3 25 21 10 1 0 99 0 0 2019-02-14 20:31:52
4.13 -w, --wide
- Wide output mode (useful for systems with higher amount of memory,where the default output mode suffers from unwanted column break‐age). The output is wider than 80 characters per line.
- 统计信息显示的宽度。
4.14 -V, --version
- Display version information and exit.
4.15 -h, --help
- Display help and exit.