How to monitor system performance with vmstat
How to Monitor System Performance with vmstat
Table of Contents
- [Introduction](#introduction)
- [Prerequisites](#prerequisites)
- [Understanding vmstat](#understanding-vmstat)
- [Basic vmstat Usage](#basic-vmstat-usage)
- [Interpreting vmstat Output](#interpreting-vmstat-output)
- [Advanced vmstat Options](#advanced-vmstat-options)
- [Practical Examples and Use Cases](#practical-examples-and-use-cases)
- [Monitoring Strategies](#monitoring-strategies)
- [Common Issues and Troubleshooting](#common-issues-and-troubleshooting)
- [Best Practices](#best-practices)
- [Integration with Other Tools](#integration-with-other-tools)
- [Conclusion](#conclusion)
Introduction
System performance monitoring is crucial for maintaining healthy Linux systems, identifying bottlenecks, and ensuring optimal resource utilization. The `vmstat` (Virtual Memory Statistics) command is one of the most powerful and versatile tools available for monitoring system performance in real-time. This comprehensive guide will teach you how to effectively use vmstat to monitor CPU usage, memory consumption, I/O activity, and system processes.
By the end of this article, you'll understand how to interpret vmstat output, identify performance issues, implement monitoring strategies, and troubleshoot common system problems using this essential Linux utility.
Prerequisites
Before diving into vmstat monitoring, ensure you have:
- Linux System Access: Root or sudo privileges for comprehensive monitoring
- Basic Command Line Knowledge: Familiarity with terminal operations
- System Administration Understanding: Basic knowledge of Linux system concepts
- vmstat Installation: Most Linux distributions include vmstat by default
To verify vmstat availability:
```bash
which vmstat
vmstat --version
```
If vmstat is not installed, install it using your distribution's package manager:
```bash
Ubuntu/Debian
sudo apt-get install procps
CentOS/RHEL/Fedora
sudo yum install procps-ng
or
sudo dnf install procps-ng
```
Understanding vmstat
What is vmstat?
vmstat is a system monitoring tool that provides detailed information about:
- Virtual memory usage
- CPU utilization
- I/O statistics
- System processes
- Kernel activity
How vmstat Works
vmstat reads data from the `/proc` filesystem, specifically:
- `/proc/meminfo` - Memory information
- `/proc/stat` - CPU and system statistics
- `/proc/vmstat` - Virtual memory statistics
The tool calculates averages and rates based on system counters, providing both instantaneous snapshots and continuous monitoring capabilities.
Basic vmstat Usage
Simple vmstat Execution
The most basic vmstat command displays system statistics since boot:
```bash
vmstat
```
Sample Output:
```
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 0 1048576 65536 524288 0 0 45 12 150 300 15 5 78 2 0
```
Continuous Monitoring
For real-time monitoring, specify an interval (in seconds):
```bash
Update every 2 seconds
vmstat 2
Update every 5 seconds, 10 times total
vmstat 5 10
```
Key Command Syntax
```bash
vmstat [options] [interval] [count]
```
- interval: Time between updates (seconds)
- count: Number of updates to display
- options: Various formatting and display options
Interpreting vmstat Output
Understanding vmstat output is crucial for effective system monitoring. Let's break down each column:
Process Information (procs)
| Column | Description |
|--------|-------------|
| r | Number of runnable processes (running or waiting for CPU) |
| b | Number of processes in uninterruptible sleep (blocked) |
Performance Indicators:
- High `r` values indicate CPU contention
- High `b` values suggest I/O bottlenecks
Memory Statistics (memory)
| Column | Description |
|--------|-------------|
| swpd | Virtual memory used (KB) |
| free | Idle memory (KB) |
| buff | Memory used as buffers (KB) |
| cache | Memory used as cache (KB) |
Analysis Tips:
- Low `free` memory isn't necessarily problematic if `cache` is high
- High `swpd` with active swapping indicates memory pressure
Swap Activity (swap)
| Column | Description |
|--------|-------------|
| si | Memory swapped in from disk (KB/s) |
| so | Memory swapped out to disk (KB/s) |
Performance Impact:
- Consistent non-zero values indicate memory shortage
- High swap activity severely impacts performance
I/O Statistics (io)
| Column | Description |
|--------|-------------|
| bi | Blocks received from block devices (blocks/s) |
| bo | Blocks sent to block devices (blocks/s) |
Monitoring Notes:
- High values indicate intensive disk activity
- Useful for identifying I/O-bound processes
System Activity (system)
| Column | Description |
|--------|-------------|
| in | Interrupts per second |
| cs | Context switches per second |
Performance Indicators:
- High `cs` values may indicate excessive multitasking
- Unusual `in` spikes might suggest hardware issues
CPU Utilization (cpu)
| Column | Description |
|--------|-------------|
| us | User time percentage |
| sy | System time percentage |
| id | Idle time percentage |
| wa | Wait for I/O percentage |
| st | Stolen time (virtualized environments) |
Optimization Targets:
- High `us`: CPU-intensive applications
- High `sy`: System overhead or kernel issues
- High `wa`: I/O bottlenecks
- Low `id`: Overall system stress
Advanced vmstat Options
Display Options
```bash
Show active and inactive memory
vmstat -a
Display statistics in megabytes
vmstat -S M
Show disk statistics
vmstat -d
Display partition statistics
vmstat -p /dev/sda1
```
Detailed Memory Information
```bash
Verbose memory statistics
vmstat -s
```
Sample Output:
```
8388608 K total memory
2097152 K used memory
1048576 K active memory
524288 K inactive memory
6291456 K free memory
65536 K buffer memory
1048576 K swap cache
0 K total swap
0 K used swap
0 K free swap
```
Disk Statistics
```bash
Comprehensive disk statistics
vmstat -d
```
Output Explanation:
```
disk- ------------reads------------ ------------writes----------- -----IO------
total merged sectors ms total merged sectors ms cur sec
sda 1234 56 12345 890 2345 123 23456 1234 0 12
```
Practical Examples and Use Cases
Example 1: Identifying CPU Bottlenecks
```bash
Monitor CPU usage every 2 seconds
vmstat 2
```
Interpreting Results:
```
procs -----------memory---------- ---swap-- -----io---- -system-- ------cpu-----
r b swpd free buff cache si so bi bo in cs us sy id wa st
15 0 0 1048576 65536 524288 0 0 45 12 2500 5000 85 10 3 2 0
12 0 0 1045000 65536 528000 0 0 50 15 2600 5200 88 8 2 2 0
```
Analysis:
- High `r` (runnable processes): 12-15 processes waiting for CPU
- High `us` (user time): 85-88% CPU usage by applications
- Low `id` (idle): Only 2-3% CPU idle time
- Conclusion: CPU bottleneck identified
Example 2: Detecting Memory Pressure
```bash
Monitor memory with 5-second intervals
vmstat -a 5
```
Sample Output:
```
procs -----------memory---------- ---swap-- -----io---- -system-- ------cpu-----
r b swpd free inact active si so bi bo in cs us sy id wa st
2 3 524288 65536 2097152 4194304 250 180 45 120 150 300 25 15 45 15 0
```
Analysis:
- High `swpd`: 524MB in swap
- Active swap I/O: `si=250`, `so=180`
- High `b` (blocked processes): I/O wait
- Conclusion: Memory pressure causing swap activity
Example 3: I/O Performance Analysis
```bash
Monitor I/O activity
vmstat 1 60
```
Identifying I/O Issues:
```
procs -----------memory---------- ---swap-- -----io---- -system-- ------cpu-----
r b swpd free buff cache si so bi bo in cs us sy id wa st
1 8 0 2097152 131072 1048576 0 0 1500 800 200 400 15 5 20 60 0
```
Analysis:
- High `b`: 8 processes blocked on I/O
- High `bi/bo`: Intensive disk activity
- High `wa`: 60% CPU waiting for I/O
- Conclusion: I/O bottleneck detected
Monitoring Strategies
Baseline Establishment
Create performance baselines during normal operations:
```bash
Collect baseline data
vmstat 5 720 > baseline_vmstat.log # 1 hour of data
```
Automated Monitoring Script
```bash
#!/bin/bash
performance_monitor.sh
LOG_FILE="/var/log/vmstat_monitor.log"
THRESHOLD_CPU=80
THRESHOLD_MEMORY=90
while true; do
TIMESTAMP=$(date '+%Y-%m-%d %H:%M:%S')
VMSTAT_OUTPUT=$(vmstat 1 2 | tail -1)
# Extract CPU idle percentage
CPU_IDLE=$(echo $VMSTAT_OUTPUT | awk '{print $15}')
CPU_USAGE=$((100 - CPU_IDLE))
# Log high CPU usage
if [ $CPU_USAGE -gt $THRESHOLD_CPU ]; then
echo "$TIMESTAMP - HIGH CPU: ${CPU_USAGE}%" >> $LOG_FILE
fi
sleep 60
done
```
Performance Trending
```bash
Daily performance summary
vmstat 300 288 > daily_performance_$(date +%Y%m%d).log # 24 hours, 5-minute intervals
```
Common Issues and Troubleshooting
Issue 1: High Load Average with Low CPU Usage
Symptoms:
```
procs -----------memory---------- ---swap-- -----io---- -system-- ------cpu-----
r b swpd free buff cache si so bi bo in cs us sy id wa st
1 15 0 4194304 262144 2097152 0 0 2000 1500 300 600 10 5 15 70 0
```
Analysis:
- High `b` (blocked processes): 15
- High `wa` (I/O wait): 70%
- High disk I/O: `bi=2000`, `bo=1500`
Solution:
1. Identify I/O-intensive processes: `iotop`
2. Check disk health: `smartctl -a /dev/sda`
3. Optimize disk usage or upgrade storage
Issue 2: Memory Leaks Detection
Symptoms:
```bash
Progressive memory consumption
vmstat 60 60 # Monitor for 1 hour
```
Monitoring Pattern:
```
Time 1
free: 4194304, cache: 1048576
Time 30
free: 2097152, cache: 1048576
Time 60
free: 1048576, cache: 1048576
```
Solution:
1. Identify memory-consuming processes: `ps aux --sort=-%mem`
2. Check for memory leaks: `valgrind` for applications
3. Monitor specific processes: `pmap -x PID`
Issue 3: Context Switch Storm
Symptoms:
```
procs -----------memory---------- ---swap-- -----io---- -system-- ------cpu-----
r b swpd free buff cache si so bi bo in cs us sy id wa st
25 0 0 2097152 131072 1048576 0 0 45 12 500 50000 30 60 8 2 0
```
Analysis:
- Extremely high `cs` (context switches): 50,000/second
- High system time `sy`: 60%
- Many runnable processes `r`: 25
Solution:
1. Identify problematic processes: `pidstat -w 1`
2. Check for threading issues
3. Optimize application design or reduce concurrency
Best Practices
1. Regular Monitoring Schedule
```bash
Cron job for regular monitoring
/etc/crontab
/5 * root vmstat 1 5 >> /var/log/vmstat.log 2>&1
```
2. Alert Thresholds
Establish meaningful thresholds:
- CPU Usage: Alert when idle < 10% for 5+ minutes
- Memory: Alert when free memory < 5% of total
- Swap: Alert on any consistent swap activity
- I/O Wait: Alert when wa > 50% for extended periods
3. Data Retention
```bash
Log rotation configuration
/etc/logrotate.d/vmstat
/var/log/vmstat.log {
daily
rotate 30
compress
delaycompress
missingok
notifempty
}
```
4. Performance Correlation
Combine vmstat with other tools:
```bash
Comprehensive monitoring script
#!/bin/bash
echo "=== System Performance Report ===" > report.txt
echo "Date: $(date)" >> report.txt
echo "" >> report.txt
echo "=== vmstat ===" >> report.txt
vmstat 1 5 >> report.txt
echo "=== Top Processes ===" >> report.txt
ps aux --sort=-%cpu | head -10 >> report.txt
```
5. Historical Analysis
```bash
Weekly performance analysis
awk '
/procs/ { next }
{
cpu_usage = 100 - $15
if (cpu_usage > max_cpu) max_cpu = cpu_usage
if (cpu_usage < min_cpu || min_cpu == 0) min_cpu = cpu_usage
total_cpu += cpu_usage
count++
}
END {
print "Average CPU Usage:", total_cpu/count "%"
print "Maximum CPU Usage:", max_cpu "%"
print "Minimum CPU Usage:", min_cpu "%"
}' weekly_vmstat.log
```
Integration with Other Tools
Combining with iostat
```bash
Comprehensive I/O monitoring
vmstat 5 & iostat -x 5 &
```
Integration with sar
```bash
System Activity Reporter correlation
sar -u 5 10 & vmstat 5 10
```
Custom Dashboard Creation
```bash
#!/bin/bash
system_dashboard.sh
clear
while true; do
echo "=== System Performance Dashboard ==="
echo "Time: $(date)"
echo ""
echo "=== CPU & Memory (vmstat) ==="
vmstat 1 2 | tail -1 | awk '{
printf "CPU Usage: %d%% | Memory Free: %d MB | I/O Wait: %d%%\n",
100-$15, $4/1024, $16
}'
echo ""
echo "=== Top 5 CPU Processes ==="
ps aux --sort=-%cpu | head -6 | tail -5
sleep 5
clear
done
```
Conclusion
vmstat is an indispensable tool for Linux system administrators and performance engineers. This comprehensive guide has covered everything from basic usage to advanced monitoring strategies, providing you with the knowledge to:
- Monitor system performance effectively using vmstat
- Interpret complex output and identify performance bottlenecks
- Implement monitoring strategies for proactive system management
- Troubleshoot common issues using vmstat data
- Integrate vmstat with other monitoring tools for comprehensive analysis
Key Takeaways
1. Regular Monitoring: Establish baseline performance metrics and monitor trends
2. Holistic Analysis: Combine vmstat data with other system monitoring tools
3. Proactive Alerting: Set up automated monitoring with meaningful thresholds
4. Historical Tracking: Maintain performance logs for trend analysis
5. Continuous Learning: Stay updated with system performance optimization techniques
Next Steps
To further enhance your system monitoring capabilities:
1. Explore Advanced Tools: Learn tools like `htop`, `iotop`, and `atop`
2. Implement Monitoring Solutions: Consider tools like Nagios, Zabbix, or Prometheus
3. Develop Custom Scripts: Create automated monitoring and alerting systems
4. Study Performance Tuning: Learn system optimization techniques
5. Practice Regularly: Use vmstat in various scenarios to build expertise
With the knowledge gained from this guide, you're now equipped to effectively monitor and optimize Linux system performance using vmstat. Regular practice and continuous learning will help you become proficient in identifying and resolving performance issues before they impact your systems and users.