Linux vmstat Command with Practical Examples

LinuxLinuxBeginner
Practice Now

Introduction

In this lab, you will learn about the Linux vmstat command, a powerful tool for monitoring system performance. The vmstat command provides a detailed overview of various system resources, including CPU, memory, and disk I/O. You will understand the different columns of the vmstat output and how to use the command to analyze system performance. Additionally, you will explore how to run vmstat with different options and time intervals to get a comprehensive understanding of your system's behavior.

Linux Commands Cheat Sheet


Skills Graph

%%%%{init: {'theme':'neutral'}}%%%% flowchart RL linux(("`Linux`")) -.-> linux/SystemInformationandMonitoringGroup(["`System Information and Monitoring`"]) linux/SystemInformationandMonitoringGroup -.-> linux/ps("`Process Displaying`") linux/SystemInformationandMonitoringGroup -.-> linux/free("`Memory Reporting`") subgraph Lab Skills linux/ps -.-> lab-422998{{"`Linux vmstat Command with Practical Examples`"}} linux/free -.-> lab-422998{{"`Linux vmstat Command with Practical Examples`"}} end

Understanding the vmstat Command

In this step, you will learn about the vmstat command in Linux, which is a powerful tool for monitoring system performance. The vmstat command provides a detailed overview of various system resources, including CPU, memory, and disk I/O.

First, let's start by running the vmstat command without any arguments:

$ vmstat
procs -----------memory---------- ---swap-- -----io---- -system-- ------cpu-----
 r  b   swpd   free   buff  cache   si   so    bi    bo   in   cs us sy id wa st
 1  0      0 1877028  16308 1359812    0    0     0     0    0    0  0  0 100  0  0

The output of the vmstat command is divided into several columns, each providing information about a specific system resource:

  • procs: Displays the number of processes in various states (running, blocked, etc.)
  • memory: Provides information about memory usage, including free, used, and cached memory
  • swap: Displays information about swap space usage
  • io: Shows disk I/O statistics, including blocks read and written
  • system: Displays system-level statistics, such as interrupts and context switches
  • cpu: Provides CPU utilization statistics, including user, system, idle, and wait time

To get a more detailed understanding of the vmstat output, let's run the command with the -a option, which displays additional memory-related information:

$ 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
 1  0      0 1877028 1294456  65356    0    0     0     0    0    0  0  0 100  0  0

In this output, you can see the inact (inactive) and active memory columns, which provide more insight into memory usage.

The vmstat command also supports various time intervals and iteration counts. For example, to display system statistics every 2 seconds for 5 times, you can use the following command:

$ vmstat 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
 1  0      0 1877028  16308 1359812    0    0     0     0    0    0  0  0 100  0  0
 0  0      0 1877028  16308 1359812    0    0     0     0  367  688  0  0 100  0  0
 0  0      0 1877028  16308 1359812    0    0     0     0  369  682  0  0 100  0  0
 0  0      0 1877028  16308 1359812    0    0     0     0  373  696  0  0 100  0  0
 0  0      0 1877028  16308 1359812    0    0     0     0  367  688  0  0 100  0  0

This output will display system statistics every 2 seconds, for a total of 5 iterations.

The vmstat command provides a wealth of information about your system's performance, and understanding how to interpret its output is crucial for effective system monitoring and troubleshooting.

Monitoring System Performance with vmstat

In this step, you will learn how to use the vmstat command to monitor various aspects of your system's performance, including CPU, memory, and disk I/O.

Let's start by monitoring the CPU utilization of your system. The vmstat command provides detailed information about CPU usage, including the percentage of time spent in user mode, system mode, idle, and wait states.

To monitor CPU utilization, run the following command:

$ vmstat 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 1877028  16308 1359812    0    0     0     0  367  688  0  0 100  0  0
 0  0      0 1877028  16308 1359812    0    0     0     0  369  682  0  0 100  0  0
 0  0      0 1877028  16308 1359812    0    0     0     0  373  696  0  0 100  0  0
 0  0      0 1877028  16308 1359812    0    0     0     0  367  688  0  0 100  0  0
 0  0      0 1877028  16308 1359812    0    0     0     0  367  688  0  0 100  0  0

In the output, the "cpu" section shows the percentage of time the CPU spent in user mode (us), system mode (sy), idle (id), and waiting for I/O (wa). In this example, the CPU is mostly idle (100%).

Next, let's monitor memory usage. The vmstat command provides information about the amount of free, used, and cached memory, as well as swap space usage.

To monitor memory usage, run the following command:

$ vmstat
procs -----------memory---------- ---swap-- -----io---- -system-- ------cpu-----
 r  b   swpd   free   buff  cache   si   so    bi    bo   in   cs us sy id wa st
 1  0      0 1877028  16308 1359812    0    0     0     0    0    0  0  0 100  0  0

In the output, the "memory" section shows the amount of free memory (free), used memory (used), and cached memory (cache). The "swap" section shows the amount of used and free swap space (swpd and free).

Finally, let's monitor disk I/O. The vmstat command provides information about the number of blocks read from and written to disk.

To monitor disk I/O, run the following command:

$ vmstat
procs -----------memory---------- ---swap-- -----io---- -system-- ------cpu-----
 r  b   swpd   free   buff  cache   si   so    bi    bo   in   cs us sy id wa st
 1  0      0 1877028  16308 1359812    0    0     0     0    0    0  0  0 100  0  0

In the output, the "io" section shows the number of blocks read from disk (bi) and written to disk (bo).

By using the vmstat command, you can effectively monitor various aspects of your system's performance and identify potential bottlenecks or issues.

Analyzing CPU, Memory, and Disk I/O with vmstat

In this step, you will learn how to analyze the CPU, memory, and disk I/O metrics provided by the vmstat command to identify potential performance issues in your system.

Let's start by analyzing the CPU utilization:

$ vmstat 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 1877028  16308 1359812    0    0     0     0  367  688  0  0 100  0  0
 0  0      0 1877028  16308 1359812    0    0     0     0  369  682  0  0 100  0  0
 0  0      0 1877028  16308 1359812    0    0     0     0  373  696  0  0 100  0  0
 0  0      0 1877028  16308 1359812    0    0     0     0  367  688  0  0 100  0  0
 0  0      0 1877028  16308 1359812    0    0     0     0  367  688  0  0 100  0  0

In the "cpu" section, you can see that the CPU is mostly idle (100%), which indicates that the system is not under heavy load.

Next, let's analyze the memory usage:

$ vmstat
procs -----------memory---------- ---swap-- -----io---- -system-- ------cpu-----
 r  b   swpd   free   buff  cache   si   so    bi    bo   in   cs us sy id wa st
 1  0      0 1877028  16308 1359812    0    0     0     0    0    0  0  0 100  0  0

The "memory" section shows that the system has a large amount of free memory (1,877,028 kB) and a significant amount of cached memory (1,359,812 kB). This indicates that the system has enough memory resources available and is not experiencing any memory-related performance issues.

Finally, let's analyze the disk I/O:

$ vmstat
procs -----------memory---------- ---swap-- -----io---- -system-- ------cpu-----
 r  b   swpd   free   buff  cache   si   so    bi    bo   in   cs us sy id wa st
 1  0      0 1877028  16308 1359812    0    0     0     0    0    0  0  0 100  0  0

The "io" section shows that there are no blocks being read from or written to disk (bi and bo are both 0). This suggests that the system is not experiencing any significant disk I/O activity.

By analyzing the CPU, memory, and disk I/O metrics provided by the vmstat command, you can identify potential performance bottlenecks in your system and take appropriate actions to optimize its performance.

Summary

In this lab, you learned about the vmstat command in Linux, which is a powerful tool for monitoring system performance. You started by understanding the basic usage of the vmstat command and the various columns of information it provides, including CPU, memory, and disk I/O statistics. You then explored how to use the -a option to display additional memory-related information, and how to set time intervals and iteration counts to monitor system statistics over time. The key learning points covered in this lab were understanding the vmstat command, monitoring system performance with vmstat, and analyzing CPU, memory, and disk I/O using the vmstat tool.

Linux Commands Cheat Sheet

Other Linux Tutorials you may like