Linux/Unix: System Performance Monitoring β top, vmstat, iostat, sar, dstat Explained
Introduction β Why Monitor Linux System Performance?
Monitoring performance is critical for preventing slowdowns, detecting bottlenecks, and ensuring system health. Linux provides powerful CLI tools to track CPU, memory, disk, and process usage in real-time or via logs. Tools like top, vmstat, iostat, sar, and dstat give deep insights into system behavior with minimal overhead.
In this guide, youβll learn:
- How to monitor system metrics using essential performance tools
- When to use each command and how to interpret the output
- Real-world examples for diagnosing issues
1. top β Real-Time System Resource Monitor
What is top?
top shows real-time CPU, memory, and process usage. Itβs like a task manager for Linux.
Syntax:
top
Output Breakdown:
| Column | Meaning |
|---|---|
| PID | Process ID |
| USER | Owner of the process |
| %CPU | CPU usage |
| %MEM | RAM usage |
| TIME+ | Total CPU time |
| COMMAND | Name of the command |
Example:
top
Press:
Pto sort by CPU usageMto sort by memory usagekto kill a process by PID
Ideal for spotting CPU hogs or memory leaks.
2. vmstat β Memory & CPU Overview
What is vmstat?
vmstat reports memory, swap, I/O, system processes, and CPU statistics.
Syntax:
vmstat [interval] [count]
Example:
vmstat 2 5
Output:
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 123456 11234 543210 0 0 1 2 100 200 2 1 97 0 0
Key Fields:
us/sy= user/system CPU timesi/so= swap in/outwa= I/O wait time (important!)
π½ 3. iostat β Disk I/O Monitoring
What is iostat?
iostat shows CPU and disk input/output stats, helping identify disk bottlenecks.
Install:
sudo apt install sysstat # Debian/Ubuntu
sudo yum install sysstat # RHEL/CentOS
Syntax:
iostat -xz 2 3
Output:
Device: tps kB_read/s kB_wrtn/s %util
sda 1.2 102.5 30.1 5.67
%util > 70% = disk saturationawait > 20ms = disk lag
4. sar β Historical System Statistics
What is sar?
sar collects, reports, and saves system performance data over time. Great for trend analysis.
Install:
sudo apt install sysstat
Enable service:
sudo systemctl enable --now sysstat
Syntax:
sar [option] [interval] [count]
Examples:
CPU usage:
sar -u 1 5
Memory usage:
sar -r 1 3
View logs from yesterday:
sar -u -f /var/log/sysstat/sa$(date +%d --date="yesterday")
Ideal for offline troubleshooting.
5. dstat β All-in-One Real-Time System Monitor
What is dstat?
dstat replaces vmstat, iostat, netstat, and ifstat, offering real-time stats in one view.
Install:
sudo apt install dstat
Syntax:
dstat -cdnm
Output:
----cpu---- --dsk/total-- ---net/eth0-- ---memory---
usr sys idl| read writ| recv send| used buff cach
3 2 95 | 1kB 0kB | 0kB 0kB | 1.2G 56M 452M
Combine options:
-cβ CPU-dβ Disk-nβ Network-mβ Memory
Tool Comparison Table
| Tool | Use Case | Real-Time | Historical | Best For |
|---|---|---|---|---|
top | Process-level monitoring | CPU/memory usage per process | ||
vmstat | Memory, swap, CPU snapshot | Quick system overview | ||
iostat | Disk I/O and utilization | Detecting disk bottlenecks | ||
sar | Time-series performance data | / | Logging system performance | |
dstat | All-in-one live dashboard | Unified system stats in CLI |
Summary β Recap & Next Steps
Linux performance tools give you everything from live diagnostics to historical trend analysis. Whether you’re tracking disk performance, memory usage, or CPU spikes, these tools help you pinpoint the issue fast.
Key Takeaways:
- Use
topto find heavy processes quickly. - Use
vmstatfor quick memory/CPU load snapshots. - Use
iostatanddstatfor disk I/O investigation. - Use
sarfor long-term performance monitoring.
FAQs
Whatβs the best tool to detect memory leaks?
Use top or htop to find increasing %MEM over time.
How to monitor disk usage in real time?
Use:
iostat -xz 2 5
How do I log performance data for later analysis?
Use sar (with sysstat) which logs data to /var/log/sysstat.
Whatβs the difference between top and htop?
htop is more visual and user-friendly but needs to be installed manually:
sudo apt install htop
Can these tools be used in scripts?
Yes. Tools like vmstat, iostat, and sar work well with automation and cron jobs.
Share Now :
