Home  >  Article  >  System Tutorial  >  How to use vmstat command under Linux

How to use vmstat command under Linux

WBOY
WBOYforward
2024-02-14 23:09:02795browse

vmstat is the abbreviation of virtual memory statistics. It is a very useful monitoring tool under Linux. In addition to memory, it also provides additional information such as block IO and CPU time

grammar

The execution of the vmstat command does not require special permissions and can be executed by ordinary users. Its syntax is as follows

vmstat [options] [delay [count]]

delay Indicates the data update interval, in seconds. If this value is not specified, it indicates the average time since the system started, and the result is only output once at this time

count represents the number of output times. If this value is not specified, but the value of delay is specified, it means unlimited times

Result field description

Enter vmstat on the command line and press Enter, the result will be output once

[root@cghost22 ~]# vmstat
procs -----------memory---------- ---swap-- -----io---- -system-- ------cpu-----
 r  b   swpd   free   buff  cache   si   so    bi    bo   in   cs us sy id wa st
 3  0      0 991324    932 537336    0    0     0     0    3    4  0  0 100  0  0
[root@cghost22 ~]#

There are many fields in the results. The following table lists the detailed description of each field

Field illustrate
#r The number of runnable processes, including running and ready states
b Number of processes in uninterruptible sleep state
swpd Virtual memory usage
free Free memory
buff Amount of memory used as buffer
cache Amount of memory used as cache
si The amount of memory swapped from disk
so Amount swapped out from memory to disk
bi Blocks received from block device, unit: blocks/second
bo Blocks sent to block device, unit: blocks/second
in Number of interrupts per second, including clock interrupts
cs Context switches per second
us User mode execution time
sy Kernel mode execution time
id CPU idle time
wa Waiting time for IO
st Time stolen from the virtual machine

The second row in the tableThe number of processes in the uninterruptible sleep state, the uninterruptible here refers to the state that a process enters when executing certain system calls. In this state , the process is blocked and cannot be interrupted until the system call is completed

The field results are divided into several parts according to color, from top to bottom: process information, memory information, IO information, system interrupt and context, CPU time

The default unit of the value in the memory information is KB (1024 kbytes). The CPU time field does not represent a specific time, but a percentage of the total CPU time

Common options

Options illustrate
-a Show active and inactive memory
-f The number of forks since system startup, including system calls such as fork, vfork and clone
-s Display system event counts and memory statistics
-d Report disk statistics
-D Statistical active disk information
-p Details of the specified partition
-t Append a column of time display
-S Display according to the specified byte unit
-w The results are displayed in wide mode
-V vmstat version

常见用法

vmstat 命令主要用于识别系统的瓶颈,统计数据的时候,它不会包含自身进程

  • 按照指定时间间隔和次数输出
[root@cghost22 ~]$ vmstat 2 10
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  12552 148356 234324 3382956    0    0     1    21    4    4  2  2 97  0  0
 1  0  12552 148264 234324 3382980    0    0     0     0 14974 27478  3  2 96  0  0
 1  0  12552 148232 234324 3382984    0    0     0    14 14384 27181  3  2 96  0  0
 0  0  12552 148376 234332 3383052    0    0     0   204 14197 26812  4  2 94  0  0
 0  0  12552 148512 234332 3383088    0    0     0     4 14398 27155  3  2 95  0  0
 0  0  12552 147892 234332 3383128    0    0     0   210 15515 28802  3  2 95  0  0
 1  0  12552 148388 234332 3383156    0    0     0     0 15147 28042  3  2 95  0  0
 0  0  12552 148264 234332 3383168    0    0     0     4 14380 27395  3  1 96  0  0
 0  0  12552 148264 234336 3383216    0    0     0   198 14430 27008  3  1 95  0  0
 2  0  12552 148140 234336 3383252    0    0     0     6 14233 27161  3  2 95  0  0

2 表示每隔 2 秒输出一次结果,10 表示总共输出 10 次,10 次之后程序自动结束

  • 修改内存显示单位

输出的结果中,内存数据的单位默认是 KB,可以通过 -S 选项调整显示的单位,有下面几种单位可供选择

注意:-S 选项对 si、 so 字段无效

k    # 1000 bytes
K    # 1024 bytes
m    # 1000 * 1000 bytes
M    # 1024 * 1024 bytes
Linux 下如何使用 vmstat 命令

上图中第一个结果中内存数据显示单位是 KB , 第二个结果中单位是 MB,将第一个结果对应字段的数值除以 1024 就得到了第二个结果

  • 活跃内存和非活跃内存
Linux 下如何使用 vmstat 命令

inact 是非活跃内存,active 是活跃内存

活跃内存是进程在使用的内存,非活跃内存是未运行进程的内存

  • 系统启动以来 fork 的数量

这里的 fork 数量包括 fork、vfork 以及 clone 等系统调用

[root@cghost22 ~]# vmstat -f
        12714 forks
[root@cghost22 ~]# vmstat -f
        12715 forks
[root@cghost22 ~]# vmstat -f
        12716 forks

我们每次在控制台执行一次命令,系统就会 fork 一个新的进程来执行命令,比如像上面的例子,每执行一次 vmstat -f 命令,系统就会 fork 一个新进程

这个选项还可以用于统计某个操作消耗多少次 fork 调用,只需要在操作前后各执行一次 vmstat -f 命令,比较两次结果的差值即可

  • 每行追加一列时间

追加一列时间显示,有助于比较一段时间内的结果

Linux 下如何使用 vmstat 命令
  • 按照宽模式显示

vmstat 结果中的某些字段的数字有时会比较长,而且跟字段名的位置有偏差, 不太适合人类的观看习惯,-w 选项可以按照宽模式显示数据,使结果看起来更直观,下图是分别未使用宽模式和使用了宽模式的一个对比

Linux 下如何使用 vmstat 命令
  • 统计磁盘信息
Linux 下如何使用 vmstat 命令

磁盘信息主要分三个方面:读、写、IO ,读和写以毫秒为单位,IO以秒为单位

读
    total:     成功读取的总数
    merged:    分组读取(产生一个 IO)
    sectors:   成功读取的扇区数
    ms:        读取花费的毫秒
    
写
    total:      成功写入的总数
    merged:     分组写入(产生一个 IO)
    sectors:    成功写入的扇区数
    ms:         写花费的毫秒
    
IO
    cur:    正在进行的IO
    s:      IO花费的秒数
  • 指定磁盘分区信息
Linux 下如何使用 vmstat 命令

上图中,输出结果显示 sda3 分区设备的信息,它们包括:读计数、读取的扇区数,写计数,分区写请求总数

The above is the detailed content of How to use vmstat command under Linux. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:lxlinux.net. If there is any infringement, please contact admin@php.cn delete