๐Ÿš€ 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:

ColumnMeaning
PIDProcess ID
USEROwner of the process
%CPUCPU usage
%MEMRAM usage
TIME+Total CPU time
COMMANDName of the command

๐Ÿงช Example:

top

โœ… Press:

  • P to sort by CPU usage
  • M to sort by memory usage
  • k 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 time
  • si/so = swap in/out
  • wa = 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 saturation
await > 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

ToolUse CaseReal-TimeHistoricalBest For
topProcess-level monitoringโœ…โŒCPU/memory usage per process
vmstatMemory, swap, CPU snapshotโœ…โŒQuick system overview
iostatDisk I/O and utilizationโœ…โŒDetecting disk bottlenecks
sarTime-series performance dataโœ…/โŒโœ…Logging system performance
dstatAll-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 and dstat 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 :

Leave a Reply

Your email address will not be published. Required fields are marked *

Share

๐Ÿ”ต Linux/Unix: System Performance (top, vmstat, iostat, sar, dstat)

Or Copy Link

CONTENTS
Scroll to Top