How to Use the Command 'vmstat' (with examples)
- Linux
- December 17, 2024
The vmstat
command, short for virtual memory statistics, is a powerful utility that provides detailed information about system activity, including processes, memory, paging, block I/O, traps, disk, and CPU activity. It is particularly useful for performance monitoring and debugging, allowing system administrators and users to diagnose and analyze system performance issues in real-time or from historical data.
Use case 1: Display Virtual Memory Statistics
Code:
vmstat
Motivation:
Understanding the current state of a system’s resources is essential for maintaining optimal performance and diagnosing issues quickly. The basic use of vmstat
without any arguments displays a one-time snapshot of various system metrics. This is particularly useful for getting an instant glance at your system’s operational health when you suspect there might be a performance bottleneck or you are conducting a routine health check.
Explanation:
vmstat
: Running the command without any parameters provides a single report of virtual memory statistics. This output encompasses crucial system metrics such as process counts, memory usage, swap activity, and CPU workload. By offering a quick overview, users can immediately identify areas that may require a more in-depth analysis.
Example 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 1 52376 128408 32000 1075428 0 0 10 30 204 300 10 5 85 0 0
In this output:
procs
: Number of processes waiting for runtime (r) and those blocked (b).memory
: Memory usage with swpd (swap), free, buff, and cache.swap
: Swap in (si) and swap out (so) growth.io
: Blocks received from (bi) and sent to (bo) a block device.system
: Number of interrupts per second, including the clock (in), and context switches (cs).cpu
: Time spent on user (us), system (sy), idle (id), waiting for I/O (wa), and stolen time (st) from the virtual machine.
Use Case 2: Display Reports Every 2 Seconds for 5 Times
Code:
vmstat 2 5
Motivation:
Monitoring system performance over time, rather than a single instance, allows for better analysis and helps in identifying bottlenecks or trends that may affect performance. By periodically capturing system metrics, users can observe how system resources are being utilized and adjust workloads or troubleshoot as needed. This use case is ideal for scenarios where continuous monitoring is necessary to analyze trends or when the system is under a specific workload stress test.
Explanation:
vmstat 2 5
: This command instructsvmstat
to generate reports at 2-second intervals and repeat this process 5 times.2
: The number specifies the interval in seconds between updates; thus, every 2 seconds,vmstat
refreshes the statistics.5
: This number indicates how many updates are generated, amounting to a total observational period of 10 seconds.
This setup provides a time-based view of how system resources are fluctuating, capturing any transient spikes or dips that may be elusive in a single snapshot.
Example 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 52376 128408 32000 1075428 0 0 12 32 210 290 9 4 87 0 0
3 1 52376 128300 32100 1075300 0 0 16 34 220 310 8 5 86 1 0
1 0 52376 128000 32200 1075200 0 0 18 31 200 298 6 5 89 0 0
2 0 52376 128500 32150 1075325 0 0 11 29 206 305 7 6 87 0 0
0 0 52376 128300 32050 1075550 0 0 14 33 215 285 10 4 86 0 0
In this repeated report, you can track changes in system metrics over a defined interval. This provides a clear picture of how various aspects such as CPU usage or I/O activity fluctuate over short periods.
Conclusion:
The vmstat
command is a versatile tool that provides insights into a system’s performance and resource utilization. Whether you are interested in a quick snapshot to diagnose a sudden performance issue or wish to monitor the system over time to identify patterns, vmstat
can be an invaluable part of your performance monitoring toolkit. By understanding and utilizing the command effectively, users can maintain system efficiency and quickly address any emerging issues.