How to use the command iostat (with examples)
- Linux
- December 25, 2023
The iostat
command is a powerful tool for monitoring system input/output (I/O) statistics. It provides detailed information about CPU utilization and disk activity, which can be helpful for system analysis, troubleshooting, and optimization.
Use case 1: Display a report of CPU and disk statistics since system startup
Code:
iostat
Motivation:
By running iostat
with no arguments, you can obtain an overview of the system’s current CPU and disk statistics. This can be useful for understanding the overall workload on the system and identifying any potential bottlenecks or excessive resource utilization.
Explanation:
The command iostat
without any arguments will display a report of various statistics since system startup. This includes average CPU usage, device utilization, throughput, and more. The output is displayed in units specific to each statistic (e.g., percentage for CPU utilization).
Example output:
Linux 5.4.0-84-generic (ubuntu) 09/22/2021 _x86_64_ (4 CPU)
avg-cpu: %user %nice %system %iowait %steal %idle
3.23 0.00 2.18 0.12 0.00 94.47
Device tps kB_read/s kB_wrtn/s kB_read kB_wrtn
sda 0.15 0.13 1.35 3455069 35544134
Use case 2: Display a report of CPU and disk statistics with units converted to megabytes
Code:
iostat -m
Motivation: When dealing with large amounts of data, it can be helpful to have disk statistics expressed in megabytes rather than the default units. This makes it easier to interpret and compare values, especially when working with modern storage systems and large file sizes.
Explanation:
By adding the -m
option to the iostat
command, the disk statistics are displayed in megabytes instead of the default units. This applies to both read and write rates as well as the cumulative values.
Example output:
Linux 5.4.0-84-generic (ubuntu) 09/22/2021 _x86_64_ (4 CPU)
avg-cpu: %user %nice %system %iowait %steal %idle
3.23 0.00 2.18 0.12 0.00 94.47
Device tps MB_read/s MB_wrtn/s MB_read MB_wrtn
sda 0.15 0.00 0.00 10 100
Use case 3: Display CPU statistics
Code:
iostat -c
Motivation: Sometimes, you may only be interested in monitoring the CPU statistics of a system. This can be useful for identifying CPU bottlenecks, understanding workload distribution, or tracking the impact of specific processes or applications on CPU utilization.
Explanation:
By using the -c
option with the iostat
command, you can display only the CPU statistics. This includes the percentage of time spent in user mode, system mode, and idle mode, as well as the average CPU utilization.
Example output:
avg-cpu: %user %nice %system %iowait %steal %idle
3.23 0.00 2.18 0.12 0.00 94.47
Use case 4: Display disk statistics with disk names (including LVM)
Code:
iostat -N
Motivation: When dealing with multiple disks or logical volumes, it can be helpful to have the disk names included in the output. This allows for easier identification of individual devices and better correlation with other system monitoring tools.
Explanation:
The -N
option adds the names of disks and logical volumes to the output of iostat
. This is particularly useful in scenarios where you have multiple disks, partitions, or logical storage volumes, including those managed by LVM (Logical Volume Manager).
Example output:
Linux 5.4.0-84-generic (ubuntu) 09/22/2021 _x86_64_ (4 CPU)
Device tps kB_read/s kB_wrtn/s kB_read kB_wrtn
sda 0.15 0.13 1.35 3455069 35544134
Use case 5: Display extended disk statistics with disk names for device “sda”
Code:
iostat -xN sda
Motivation: In certain situations, you might want to analyze the detailed disk statistics for a specific device, such as “sda.” This can be helpful for identifying performance issues, diagnosing disk-related problems, or monitoring the behavior of a particular disk in real-time.
Explanation:
The -xN
option combined with the device name (e.g., “sda”) allows you to display extended disk statistics specifically for that device. This includes additional information like average service time, queue size, utilization percentage, and more.
Example output:
Linux 5.4.0-84-generic (ubuntu) 09/22/2021 _x86_64_ (4 CPU)
Device r/s w/s rkB/s wkB/s avgrq-sz avgqu-sz await svctm %util
sda 0.01 0.14 0.05 6.89 100.4 0.00 25.92 6.79 0.10
Use case 6: Display incremental reports of CPU and disk statistics every 2 seconds
Code:
iostat 2
Motivation: When troubleshooting or monitoring system performance in real-time, it can be valuable to have incremental reports that update regularly. This allows you to observe changes over time and identify any patterns or fluctuations in CPU and disk usage.
Explanation:
By providing a numeric argument after the iostat
command (e.g., 2
), you can specify the interval in seconds at which the reports should be generated. In this example, the output will be displayed every 2 seconds.
Example output:
Linux 5.4.0-84-generic (ubuntu) 09/22/2021 _x86_64_ (4 CPU)
avg-cpu: %user %nice %system %iowait %steal %idle
3.23 0.00 2.18 0.12 0.00 94.47
Device tps kB_read/s kB_wrtn/s kB_read kB_wrtn
sda 0.15 0.13 1.35 3455069 35544134
Linux 5.4.0-84-generic (ubuntu) 09/22/2021 _x86_64_ (4 CPU)
avg-cpu: %user %nice %system %iowait %steal %idle
1.01 0.00 0.67 0.09 0.00 98.23
Device tps kB_read/s kB_wrtn/s kB_read kB_wrtn
sda 0.00 0.00 0.00 0 0
Conclusion:
The iostat
command is a versatile tool for monitoring CPU and disk statistics on a Linux system. By leveraging its various options and arguments, you can gain valuable insights into system performance, detect potential issues, and optimize resource utilization. Whether you need an overall view of the system, granular data for specific devices, or real-time monitoring, iostat
provides the necessary capabilities.