Swiftorial Logo
Home
Swift Lessons
Matchups
CodeSnaps
Tutorials
Career
Resources

Using vmstat - Linux System Monitoring

Introduction

The vmstat command in Linux is a valuable tool for monitoring system performance. It provides detailed information about processes, memory, paging, block IO, traps, and CPU activity. This tutorial will guide you through using vmstat from start to finish, with detailed explanations and examples.

Getting Started with vmstat

To begin using vmstat, open your terminal and simply type:

vmstat

This will display a snapshot of various system statistics.

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  81720  44092  105316    0    0     1     1    0    1  0  0 100  0  0
                

Understanding vmstat Output

The output of vmstat can be divided into six sections:

  • Procs: Displays the number of processes waiting for runtime and processes in uninterruptible sleep.
  • Memory: Shows the amount of memory free, used as buffers, and used as cache.
  • Swap: Indicates the amount of memory swapped in from disk and out to disk.
  • IO: Displays the blocks received from and sent to a block device.
  • System: Shows the number of interrupts per second and context switches per second.
  • CPU: Presents the percentage of CPU time spent in user mode, system mode, idle, waiting for IO, and stolen from a virtual machine.

Running vmstat with Intervals

You can run vmstat with intervals to monitor the system continuously. For example, to display statistics every 2 seconds, use:

vmstat 2

This will update the statistics every 2 seconds until you stop it with Ctrl+C.

Specifying the Number of Updates

You can also specify the number of updates you want. For example, to display the statistics every 2 seconds for 5 updates, use:

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  81720  44092  105316    0    0     1     1    0    1  0  0 100  0  0
 0  0      0  81720  44092  105316    0    0     0     0  100  200  1  2 97  0  0
 0  0      0  81720  44092  105316    0    0     0     0  120  180  0  1 99  0  0
 0  0      0  81720  44092  105316    0    0     0     0  110  170  0  0 100  0  0
 0  0      0  81720  44092  105316    0    0     0     0  130  190  0  1 99  0  0
                

Monitoring Specific Metrics

You can use vmstat options to monitor specific metrics:

  • Memory: vmstat -s provides detailed memory statistics.
  • Disk: vmstat -d displays disk statistics.
  • Slabs: vmstat -m shows slabinfo (kernel caches).
vmstat -s
       16384 K total memory
       12472 K used memory
        8172 K active memory
        4300 K inactive memory
        3912 K free memory
        2000 K buffer memory
        3160 K swap cache
        8192 K total swap
           0 K used swap
        8192 K free swap
           0 non-nice user cpu ticks
           0 nice user cpu ticks
           0 system cpu ticks
           0 idle cpu ticks
           0 IO-wait cpu ticks
           0 IRQ cpu ticks
           0 softirq cpu ticks
           0 stolen cpu ticks
           0 pages paged in
           0 pages paged out
           0 pages swapped in
           0 pages swapped out
           0 interrupts
           0 CPU context switches
           0 boot time
           0 forks
                

Conclusion

The vmstat command is a powerful tool for monitoring various system performance metrics in Linux. By understanding and utilizing its features, you can gain valuable insights into your system's behavior and troubleshoot performance issues effectively.