iostat Command: Tutorial & Examples

Display Detailed Information About CPU And Disk I/O Performance

The iostat command is a powerful tool used to monitor and report statistics related to the system’s input/output (I/O) performance and CPU utilization. It provides detailed information about the usage of CPU resources and activity on block devices such as hard drives, solid-state drives, and RAID arrays. This information is invaluable for diagnosing performance bottlenecks related to disk I/O or CPU load on Linux servers and virtual machines.

This article explains how the iostat command works, what information it provides, how to use its common options, and practical examples for monitoring system I/O performance. Additionally, it covers potential pitfalls, troubleshooting tips, and related commands to extend your system monitoring capabilities.

How iostat Works

The iostat command collects data from the Linux kernel’s proc filesystem, primarily from /proc/diskstats for device I/O statistics and /proc/stat for CPU usage. It reads cumulative counters that the kernel maintains about I/O operations and CPU time spent in different states since system boot.

When you run iostat, it calculates the difference between two snapshots of these counters taken at specific intervals to provide per-second rates and percentages. This allows you to monitor real-time performance and identify trends or spikes in disk and CPU activity.

Because iostat relies on kernel-maintained counters, it does not generate additional I/O load itself, making it efficient for continuous monitoring.

What iostat Does

The output of iostat is generally divided into two main sections:

  1. CPU Usage Statistics
    Displays the percentage of CPU time spent in various modes, such as user processes, system processes, idle time, and waiting for I/O operations.

  2. Device I/O Statistics
    Shows detailed information about each block device’s I/O activity, including the number of transactions per second, throughput, and data transferred.

Explanation of CPU Fields

  • %user: Time spent running non-kernel code (user processes).
  • %nice: Time spent running user processes with a positive nice value (lower priority).
  • %system: Time spent running kernel processes.
  • %iowait: Time spent waiting for I/O operations to complete.
  • %steal: Time stolen from a virtual CPU by the hypervisor for other tasks.
  • %idle: Time the CPU was idle and not waiting for I/O.

Explanation of Device Fields (Default Output)

  • Device: Name of the block device.
  • tps: Transactions per second (number of I/O requests).
  • kB_read/s: Kilobytes read per second.
  • kB_wrtn/s: Kilobytes written per second.
  • kB_dscd/s: Kilobytes discarded per second (trim/discard operations).
  • kB_read: Total kilobytes read.
  • kB_wrtn: Total kilobytes written.
  • kB_dscd: Total kilobytes discarded.

Understanding these metrics helps identify devices under heavy load or systems that are bottlenecked by slow disk operations.

Common Command Line Parameters

Here are some of the most useful options for iostat:

  • -x
    Display extended statistics with additional metrics such as average queue size and service time.

  • -d
    Show only device I/O statistics without CPU usage.

  • -k, -m
    Display statistics in kilobytes (default) or megabytes per second.

  • -t
    Include a timestamp for each report.

  • interval [count]
    Repeat the report every interval seconds count times. This is useful for continuous monitoring.

  • -p [device]
    Report statistics for devices and their partitions. If no device is specified, reports all.

Example usage:

iostat -x 5 3

This runs iostat with extended statistics every 5 seconds, three times.

Practical Examples Using iostat

Basic Usage Showing CPU and Device Stats

iostat

Sample output:

Linux 5.10.0-16-amd64 (server.example.com)    06/01/2024    x86_64    (8 CPU)

avg-cpu:  %user   %nice %system %iowait  %steal   %idle
          5.23    0.00    3.12    0.50     0.00    91.15

Device            tps    kB_read/s    kB_wrtn/s    kB_read    kB_wrtn
sda              47.08       512.33      1024.66  123456789  246913578

Display Only Device Statistics

iostat -d

Sample output:

Device            tps    kB_read/s    kB_wrtn/s    kB_read    kB_wrtn
sda              47.08       512.33      1024.66  123456789  246913578

Show Extended Statistics With Timestamp Every 10 Seconds

iostat -x -t 10

Sample output:

2024-06-01 12:00:00
avg-cpu:  %user   %nice %system %iowait  %steal   %idle
          5.23    0.00    3.12    0.50     0.00    91.15

Device            r/s     w/s    rkB/s    wkB/s  rrqm/s  wrqm/s  %rrqm  %wrqm  r_await  w_await  aqu-sz  %util
sda              20.5    30.3    512.0   1024.0     0.0     0.0    0.0    0.0      1.2      2.3    0.01   2.5

Monitor I/O Every 5 Seconds Three Times

iostat 5 3

This command prints the statistics every 5 seconds for 3 iterations, which helps observe trends and spikes.

Using iostat in a Script

You can parse iostat output in scripts to trigger alerts when I/O wait or device utilization is too high.

Example Bash snippet:

    #!/bin/bash
    # Alert if %iowait exceeds 20%

    iowait=$(iostat -c 1 2 | tail -n 1 | awk '{print $4}')
    if (( $(echo "$iowait > 20.0" | bc -l) )); then
        echo "Warning: High I/O wait detected: $iowait%"
    fi

Potential Problems and Pitfalls

  • High %iowait values often indicate that the CPU is waiting for disk operations, which may cause performance bottlenecks. However, low %iowait does not always mean disk performance is good; other factors may be involved.

  • Device names in the output (e.g., sda, dm-0, nvme0n1) can be confusing. Devices with dm- prefixes often represent device mapper layers like LVM or RAID. Understanding your system’s storage setup is important.

  • The tps field counts I/O requests, not individual bytes transferred, so it can be misleading if requests vary in size.

  • iostat output is cumulative since boot on the first report and only reflects the interval on subsequent reports.

  • Permissions: Running iostat typically requires read access to /proc/diskstats. Most normal users can run it, but some systems may restrict access.

Troubleshooting and Common Errors

  • Command Not Found
    Install the sysstat package, which provides iostat. On Debian/Ubuntu:

    sudo apt-get install sysstat  
    

    On CentOS/Fedora:

    sudo yum install sysstat  
    
  • No Data or Zero Values
    This may happen if the kernel modules for block devices are missing or if /proc is not mounted properly. Check that /proc is accessible.

  • Unusual Device Names or Missing Devices
    In virtualized environments or containers, device naming may differ. Use lsblk to verify device names.

Performance And Security Considerations

  • iostat is lightweight but running it with very short intervals or large output counts may impact system performance slightly.

  • The command reads kernel counters and does not require elevated privileges, but be aware of your system’s security policy.

Possible Alternatives or Related Commands

  • vmstat
    Provides CPU, memory, and I/O statistics but less detailed per-device information.

  • pidstat
    Shows statistics per process, including I/O.

  • sar
    Part of the sysstat package; collects and reports historical statistics over time, useful for long-term monitoring.

iostat Cheatsheet

  • iostat
    Display average CPU and device I/O stats since boot.

  • iostat 5
    Display stats every 5 seconds indefinitely.

  • iostat 5 3
    Display stats every 5 seconds, 3 times.

  • iostat -x
    Extended device statistics.

  • iostat -d
    Device statistics only.

  • iostat -t
    Add timestamps.

  • iostat -p
    Show device partitions.

See Also

Further Reading

As an Amazon Associate, I earn from qualifying purchases.

The text above is licensed under CC BY-SA 4.0 CC BY SA