search
HomeSystem TutorialLINUXHow to use vmstat command under Linux

How to use vmstat command under Linux

Feb 14, 2024 pm 11:09 PM
linuxlinux tutoriallinux systemlinux commandshell scriptGetting started with linuxlinux learning

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:良许Linux教程网. If there is any infringement, please contact admin@php.cn delete
How does hardware compatibility differ between Linux and Windows?How does hardware compatibility differ between Linux and Windows?Apr 23, 2025 am 12:15 AM

Linux and Windows differ in hardware compatibility: Windows has extensive driver support, and Linux depends on the community and vendors. To solve Linux compatibility problems, you can manually compile drivers, such as cloning RTL8188EU driver repository, compiling and installing; Windows users need to manage drivers to optimize performance.

What are the differences in virtualization support between Linux and Windows?What are the differences in virtualization support between Linux and Windows?Apr 22, 2025 pm 06:09 PM

The main differences between Linux and Windows in virtualization support are: 1) Linux provides KVM and Xen, with outstanding performance and flexibility, suitable for high customization environments; 2) Windows supports virtualization through Hyper-V, with a friendly interface, and is closely integrated with the Microsoft ecosystem, suitable for enterprises that rely on Microsoft software.

What are the main tasks of a Linux system administrator?What are the main tasks of a Linux system administrator?Apr 19, 2025 am 12:23 AM

The main tasks of Linux system administrators include system monitoring and performance tuning, user management, software package management, security management and backup, troubleshooting and resolution, performance optimization and best practices. 1. Use top, htop and other tools to monitor system performance and tune it. 2. Manage user accounts and permissions through useradd commands and other commands. 3. Use apt and yum to manage software packages to ensure system updates and security. 4. Configure a firewall, monitor logs, and perform data backup to ensure system security. 5. Troubleshoot and resolve through log analysis and tool use. 6. Optimize kernel parameters and application configuration, and follow best practices to improve system performance and stability.

Is it hard to learn Linux?Is it hard to learn Linux?Apr 18, 2025 am 12:23 AM

Learning Linux is not difficult. 1.Linux is an open source operating system based on Unix and is widely used in servers, embedded systems and personal computers. 2. Understanding file system and permission management is the key. The file system is hierarchical, and permissions include reading, writing and execution. 3. Package management systems such as apt and dnf make software management convenient. 4. Process management is implemented through ps and top commands. 5. Start learning from basic commands such as mkdir, cd, touch and nano, and then try advanced usage such as shell scripts and text processing. 6. Common errors such as permission problems can be solved through sudo and chmod. 7. Performance optimization suggestions include using htop to monitor resources, cleaning unnecessary files, and using sy

What is the salary of Linux administrator?What is the salary of Linux administrator?Apr 17, 2025 am 12:24 AM

The average annual salary of Linux administrators is $75,000 to $95,000 in the United States and €40,000 to €60,000 in Europe. To increase salary, you can: 1. Continuously learn new technologies, such as cloud computing and container technology; 2. Accumulate project experience and establish Portfolio; 3. Establish a professional network and expand your network.

What is the main purpose of Linux?What is the main purpose of Linux?Apr 16, 2025 am 12:19 AM

The main uses of Linux include: 1. Server operating system, 2. Embedded system, 3. Desktop operating system, 4. Development and testing environment. Linux excels in these areas, providing stability, security and efficient development tools.

Does the internet run on Linux?Does the internet run on Linux?Apr 14, 2025 am 12:03 AM

The Internet does not rely on a single operating system, but Linux plays an important role in it. Linux is widely used in servers and network devices and is popular for its stability, security and scalability.

What are Linux operations?What are Linux operations?Apr 13, 2025 am 12:20 AM

The core of the Linux operating system is its command line interface, which can perform various operations through the command line. 1. File and directory operations use ls, cd, mkdir, rm and other commands to manage files and directories. 2. User and permission management ensures system security and resource allocation through useradd, passwd, chmod and other commands. 3. Process management uses ps, kill and other commands to monitor and control system processes. 4. Network operations include ping, ifconfig, ssh and other commands to configure and manage network connections. 5. System monitoring and maintenance use commands such as top, df, du to understand the system's operating status and resource usage.

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Atom editor mac version download

Atom editor mac version download

The most popular open source editor

Dreamweaver Mac version

Dreamweaver Mac version

Visual web development tools

PhpStorm Mac version

PhpStorm Mac version

The latest (2018.2.1) professional PHP integrated development tool

mPDF

mPDF

mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),

EditPlus Chinese cracked version

EditPlus Chinese cracked version

Small size, syntax highlighting, does not support code prompt function