Linux iostat Command with Practical Examples

LinuxLinuxBeginner
Practice Now

Introduction

In this lab, we will explore the Linux iostat command, which is a powerful tool for monitoring system input/output (I/O) performance, including CPU utilization and disk I/O statistics. We will start by understanding the purpose and usage of the iostat command, including how to install the required sysstat package and run the basic iostat command to view CPU and I/O statistics. Then, we will dive deeper into analyzing the CPU and I/O statistics provided by iostat, as well as monitoring disk I/O performance using the command.

Linux Commands Cheat Sheet


Skills Graph

%%%%{init: {'theme':'neutral'}}%%%% flowchart RL linux(("`Linux`")) -.-> linux/SystemInformationandMonitoringGroup(["`System Information and Monitoring`"]) linux/SystemInformationandMonitoringGroup -.-> linux/ps("`Process Displaying`") linux/SystemInformationandMonitoringGroup -.-> linux/top("`Task Displaying`") subgraph Lab Skills linux/ps -.-> lab-422739{{"`Linux iostat Command with Practical Examples`"}} linux/top -.-> lab-422739{{"`Linux iostat Command with Practical Examples`"}} end

Understand the Purpose and Usage of iostat Command

In this step, we will learn about the purpose and usage of the iostat command in Linux. The iostat command is a powerful tool for monitoring system input/output (I/O) performance, including CPU utilization and disk I/O statistics.

First, let's install the sysstat package, which includes the iostat command:

sudo apt-get update
sudo apt-get install -y sysstat

Now, let's run the iostat command to see the basic CPU and I/O statistics:

iostat

Example output:

Linux 5.15.0-52-generic (labex-ubuntu)   07/11/2023      _x86_64_        (2 CPU)

avg-cpu:  %user   %nice %system %iowait  %steal   %idle
           0.50    0.00     0.25     0.00     0.00    99.25

Device             tps    kB_read/s    kB_wrtn/s    kB_read    kB_wrtn
sda              0.17         1.67         1.67       1024       1024

The output shows the average CPU utilization and various disk I/O metrics, such as transactions per second (tps), read and write throughput (kB_read/s and kB_wrtn/s), and total read and write data (kB_read and kB_wrtn).

You can also use the iostat command to monitor specific devices or display more detailed statistics. For example, to monitor the sda device every 2 seconds for 10 times:

iostat -d sda 2 10

This will provide a more detailed view of the I/O performance for the sda device over time.

Analyze CPU and I/O Statistics with iostat

In this step, we will learn how to analyze the CPU and I/O statistics provided by the iostat command.

First, let's run the iostat command to see the overall system statistics:

iostat

Example output:

Linux 5.15.0-52-generic (labex-ubuntu)   07/11/2023      _x86_64_        (2 CPU)

avg-cpu:  %user   %nice %system %iowait  %steal   %idle
           0.50    0.00     0.25     0.00     0.00    99.25

Device             tps    kB_read/s    kB_wrtn/s    kB_read    kB_wrtn
sda              0.17         1.67         1.67       1024       1024

The output shows the average CPU utilization and various disk I/O metrics. Let's break down the information:

  • avg-cpu: This section shows the average CPU utilization, including the percentage of time spent in user mode (%user), nice mode (%nice), system mode (%system), I/O wait (%iowait), and idle time (%idle).
  • Device: This section shows the I/O statistics for each block device, such as the transactions per second (tps), read and write throughput (kB_read/s and kB_wrtn/s), and total read and write data (kB_read and kB_wrtn).

Now, let's take a closer look at the CPU utilization. To see the CPU statistics for each individual CPU, we can use the -c option:

iostat -c

Example output:

Linux 5.15.0-52-generic (labex-ubuntu)   07/11/2023      _x86_64_        (2 CPU)

avg-cpu:  %user   %nice %system %iowait  %steal   %idle
           0.50    0.00     0.25     0.00     0.00    99.25
           0.50    0.00     0.25     0.00     0.00    99.25

This shows the CPU utilization for each individual CPU core.

To monitor the I/O statistics for a specific device, we can use the -d option followed by the device name:

iostat -d sda

Example output:

Linux 5.15.0-52-generic (labex-ubuntu)   07/11/2023      _x86_64_        (2 CPU)

Device             tps    kB_read/s    kB_wrtn/s    kB_read    kB_wrtn
sda              0.17         1.67         1.67       1024       1024

This provides more detailed I/O statistics for the sda device.

Monitor Disk I/O Performance Using iostat

In this step, we will learn how to use the iostat command to monitor the disk I/O performance of your system.

First, let's run the iostat command with the -x option to get more detailed disk I/O statistics:

iostat -x

Example output:

Linux 5.15.0-52-generic (labex-ubuntu)   07/11/2023      _x86_64_        (2 CPU)

avg-cpu:  %user   %nice %system %iowait  %steal   %idle
           0.50    0.00     0.25     0.00     0.00    99.25

Device            r/s     w/s     rkB/s     wkB/s   await  r_await w_await  svctm  %util
sda              0.17    0.17       1.67      1.67    5.00     5.00     5.00   2.00   0.03

The additional metrics provided by the -x option include:

  • r/s: Reads per second
  • w/s: Writes per second
  • rkB/s: Read throughput in kB/s
  • wkB/s: Write throughput in kB/s
  • await: Average time for I/O requests (in milliseconds)
  • r_await: Average time for read requests (in milliseconds)
  • w_await: Average time for write requests (in milliseconds)
  • svctm: Average service time (in milliseconds)
  • %util: Percentage of CPU time during which I/O requests were issued (CPU utilization for I/O)

These metrics can help you identify performance bottlenecks or issues with your disk I/O.

To monitor the disk I/O performance over time, you can use the iostat command with the -x option and specify a delay and count:

iostat -x 2 5

This will display the disk I/O statistics every 2 seconds for 5 iterations, allowing you to observe the changes in performance over time.

Summary

In this lab, we learned about the purpose and usage of the iostat command in Linux. The iostat command is a powerful tool for monitoring system input/output (I/O) performance, including CPU utilization and disk I/O statistics. We installed the sysstat package and ran the iostat command to view the basic CPU and I/O statistics, as well as more detailed statistics for specific devices. We also learned how to analyze the CPU and I/O statistics provided by the iostat command, including transactions per second, read and write throughput, and total read and write data.

Linux Commands Cheat Sheet

Other Linux Tutorials you may like