VMStat Unleashed: The Complete Guide to Linux System Performance Monitoring

vmstat (Virtual Memory Statistics) is a powerful command-line tool that provides information about system processes, memory, paging, block IO, traps, and CPU activity. This guide will help you understand and effectively use vmstat for system monitoring and troubleshooting.

Basic Usage

vmstat [options] [delay [count]]

Example:

vmstat 1 5  # Report every 1 second for 5 times
vmstat -S M # Display in megabytes

Output Fields Explained

Procs (Processes)

  • r: Number of processes waiting for run time
  • b: Number of processes in uninterruptible sleep

Memory

  • swpd: Amount of virtual memory used
  • free: Amount of idle memory
  • buff: Amount of memory used as buffers
  • cache: Amount of memory used as cache

Swap

  • si: Amount of memory swapped in from disk (/s)
  • so: Amount of memory swapped out to disk (/s)

IO

  • bi: Blocks received from a block device (blocks/s)
  • bo: Blocks sent to a block device (blocks/s)

System

  • in: Number of interrupts per second
  • cs: Number of context switches per second

CPU (percentages)

  • us: Time spent running user code
  • sy: Time spent running kernel code
  • id: Time spent idle
  • wa: Time spent waiting for IO
  • st: Time stolen from a virtual machine

Common Options

OptionDescription
-aDisplay active and inactive memory
-fDisplay number of forks since boot
-mDisplay slabinfo
-nDisplay header only once
-sDisplay event counters and memory stats
-dDisplay disk statistics
-pShow disk partition utilization
-SChange unit size (K, M, G)

Common Use Cases

  1. Monitor System Load

    vmstat 1
    
  2. Check Memory Usage

    vmstat -s
    
  3. Monitor Disk Activity

    vmstat -d
    
  4. Display Statistics in Megabytes

    vmstat -S M
    

Troubleshooting Scenarios

High CPU Usage

  • Watch the us and sy columns
  • High us: User processes consuming CPU
  • High sy: System/kernel consuming CPU

Memory Issues

  • Monitor freebuff, and cache
  • Watch si and so for excessive swapping
  • High swap activity indicates memory pressure

IO Problems

  • Check bi and bo for disk activity
  • High wa indicates IO bottleneck
  • Monitor r and b for process queue

Best Practices

  1. Regular Monitoring

    • Run vmstat periodically to establish baseline
    • Keep historical data for comparison
    • Use with other tools (iostat, top, sar)
  2. Performance Tuning

    • Monitor trends over time
    • Correlate with application behavior
    • Use appropriate intervals for monitoring
  3. Resource Planning

    • Track memory usage patterns
    • Monitor swap usage
    • Plan capacity based on trends

Tips and Tricks

  1. Continuous Monitoring

    vmstat 5 | tee vmstat.log
    
  2. Quick Memory Check

    vmstat -s | grep -i "memory"
    
  3. Disk Performance

    vmstat -d | sort -k 3 -n -r
    
  • top: Interactive process viewer
  • iostat: IO statistics
  • sar: System activity reporter
  • free: Memory usage display
  • dstat: Versatile resource statistics tool

Additional Resources

  • Man pages: man vmstat
  • Procfs documentation: /proc/vmstat
  • System monitoring guides
  • Performance tuning documentation

Common Issues and Solutions

  1. High Swap Usage

    • Check memory usage patterns
    • Consider adding more RAM
    • Adjust swappiness
  2. IO Bottlenecks

    • Monitor disk queue length
    • Check for slow devices
    • Consider IO scheduling
  3. CPU Saturation

    • Identify resource-heavy processes
    • Check for runaway processes
    • Consider load balancing

Remember that vmstat is most effective when used in conjunction with other monitoring tools and as part of a comprehensive system monitoring strategy.

Comments

Popular posts from this blog

IOTop: Master Linux Disk I/O Monitoring

A Beginner's Guide to Network Management with NMCLI