๐ 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:
P
to sort by CPU usageM
to sort by memory usagek
to 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
top
to find heavy processes quickly. - Use
vmstat
for quick memory/CPU load snapshots. - Use
iostat
anddstat
for disk I/O investigation. - Use
sar
for 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 :