How to Use iostat Command in Linux?

The iostat command monitors your system’s input/output (I/O) statistics and can be used to check the system’s disk performance, diagnose problems, or detect bottlenecks. Using the iostat command, users can monitor how occupied their disk(s) are, including the number of reads and writes, kilobytes or blocks transferred, and the average wait time and service time. 

This guide will provide a brief insight into the iostat command with the following outcomes: 

How to Install iostat in Linux?

The iostat command depends on the sysstat package, and if it is not installed, it won’t work. To install it, you can run either of the commands depending on the Linux distribution you are using:

$ sudo yum install sysstat                    #For RedHat/Fedora/CentOS
$ sudo apt install sysstat                    #For Debian/Ubuntu-based

What is the Syntax of iostat Command?

The syntax of iostat is simple, just like every other Linux command, and here’s how you can use it.

$ iostat [options]

Multiple options or options can be used with the iostat command, details of which are as follows.

Here are some of Linux’s most used options/options of the iostat command.

  • -c: Display CPU utilization statistics.
  • -k: To view the statistics in KB/s.
  • -m: To view statistics in MB/s.
  • -p: To view statistics for only the specified disks.
  • -t: Display timestamps.
  • -x: Display extended statistics.

How to Use the iostat Command in Linux? 

The primary purpose of the iostat command is to get the CPU statistics in various ways. It is typically used to identify performance bottlenecks and to determine the overall system’s health. The reports generated by iostat are based on data retrieved from several files in the Linux file system.

  • The /proc/stat file is used by iostat to calculate system-wide CPU utilization and other performance metrics.
  • The /proc/uptime file is used by iostat to calculate the system’s uptime and to determine how long the system has been running.
  • The /proc/diskstats file is used by iostat to calculate disk I/O statistics, such as disk utilization and throughput.
  • The /sys directory is used by iostat to provide detailed information about the performance of specific storage devices and partitions.
  • The /proc/self/mountstats file is used by iostat to provide information about the performance of these filesystems and to identify any potential issues with network connectivity.
  • The /dev/disk directory is used by iostat to map device names to specific storage devices and partitions so that the utility can provide detailed statistics and performance metrics for each device.

Let’s explore the examples describing the usage of the iostat command:

Example 1: View the CPU Utilization Stats 

To view the CPU utilization stats, use the “-c” option of iostat as in this command:

$ iostat -c
  • The “%user” shows the CPU’s time executing user-level processes.
  • The “%nice” represents the percentage of time the CPU spent executing processes that have been given a lower priority and are therefore entitled to less CPU time. 
  • The “%system” shows the percentage of time spent by the CPU to execute system-level processes.
  • The “%iowait” shows the inactive CPU time while waiting for the disk.
  • The “%steal” field shows the percentage of time that the CPU was idle while waiting for a virtual machine to be scheduled on a real CPU.
  • The “%idle” shows the inactive time of the CPU in percentage.

Example 2: View the Stats in Kilobytes 

To view the number of reads, writes, the number of kilobytes transferred, and the average wait & service time for each disk(s), the iostat command with the “-k” option is used like this:

$ iostat -k
  • Here, the “tps” column shows the number of requests issued to the device in a second.
  • kB_read/s” indicates the number of read requests in kilobytes per second.
  • kB_wrtn/s” indicates the number of write requests in kilobytes per second.
  • kB_dscd/s” is the number of kilobytes discarded due to some errors on the disk in seconds, and if it is anything other than ZERO, there are errors on your disk.
  • kB_read” column is for the total kilobytes that were read.
  • kB_wrtn” column is for the total kilobytes that were written.
  • kB_dscd” shows the total kilobytes discarded due to an error or other problem.

If you replace the “-k” option with “-m”, then it’ll show you the output in Megabytes:

$ iostat -m

Example 3: View I/O Stats for a Specific Disk 

If you want to view the I/O stats for a specific disk (we used sda3), the “-p” option of iostat is used in this way:

$ iostat -p sda3

The above command displays the I/O stats of the disk called sda3.

Example 4: View the Timestamp

If there’s a need to view the timestamp along with the I/O stats of disk(s), the “-t” option of the iostat command is used in this format.

$ iostat -t

The output is the same as the “-k” option, but the only difference is the timestamp which could come in handy to check for system bottlenecks.

Example 5: View Extended I/O Stats 

The “-x” option of the iostat command is used to view the extended I/O stats of all disks in the default unit (Kilobytes). It also displays the number of milliseconds spent in various I/O wait states when used in this format.

$ iostat -x

You can also combine the “-p” option to view the I/O stats of the specified disk (sda in our case) as in this format.

$ iostat -x -p sda3

It also displays the time in seconds the CPU had to wait to perform read or write operations on the disk(s).

Conclusion

Monitoring the I/O stats of your system’s disk(s) is easy using the iostat command of Linux. Using its options/options, users can spot errors or bottlenecks on their system and a few other things like CPU utilization, get info about a specific disk, and more.