Linux

查询i/o使用率

本文由 简悦 SimpRead 转码, 原文地址 www.cyberciti.biz

Explains iostat command that reports Linux disk utilization, CPU and I/O statistics for devices, part……

I‘ve already written about finding out Linux CPU utilization and usage using various CLI tools. You can use the iostat command to find disk utilization and monitor system input/output (I/O) device loading by observing the time the physical disks are active relative to their average transfer rates.

Tutorial details
Difficulty levelEasy
Root privilegesYes
RequirementsLinux terminal
CategorySystem Management
OS compatibilityAlma • AlpineArchDebianFedoraLinux • Mint • openSUSE • Pop!_OS • RHEL • Rocky • StreamSUSEUbuntu
Est. reading time7 minutes

Advertisement

Linux iostat syntax to get disk utilization information

The syntax is:
# iostat -d -x interval count
Where options are:

  • -d : Display the device utilization report (d == disk).
  • -x : Show extended statistics including disk utilization.
  • interval : It is time period in seconds between two samples. For example, iostat 2 will give data at each 2 seconds interval.
  • count : It is the number of times the data is needed. For instance, iostat 2 5 will give data at 2 seconds interval 5 times on the Linux terminal.

Let us see how to discover Linux Disk utilization using the CLI.

Using the iostat command on Linux (click to enlarge)

Installation

Prerequisite
By default, iostat command may not be installed on your system. Hence, use the apk command on Alpine Linux, dnf command/yum command on RHEL & co, apt command/apt-get command on Debian, Ubuntu & co, zypper command on SUSE/OpenSUSE, pacman command on Arch Linux to install the iostat.

On a Debian or Ubuntu Linux install the iostat as follows using the apt command or apt-get command:
$ sudo apt install sysstat

[sudo] password for vivek: 
Reading package lists... Done
Building dependency tree       
Reading state information... Done
Suggested packages:
  isag
The following NEW packages will be installed:
  sysstat
0 upgraded, 1 newly installed, 0 to remove and 0 not upgraded.
Need to get 448 kB of archives.
After this operation, 1,511 kB of additional disk space will be used.
Get:1 http://archive.ubuntu.com/ubuntu focal-updates/main amd64 sysstat amd64 12.2.0-2ubuntu0.1 [448 kB]
Fetched 448 kB in 2s (249 kB/s)   
Preconfiguring packages ...
Selecting previously unselected package sysstat.
(Reading database ... 294551 files and directories currently installed.)
Preparing to unpack .../sysstat_12.2.0-2ubuntu0.1_amd64.deb ...
Unpacking sysstat (12.2.0-2ubuntu0.1) ...
Setting up sysstat (12.2.0-2ubuntu0.1) ...
 
Creating config file /etc/default/sysstat with new version
update-alternatives: using /usr/bin/sar.sysstat to provide /usr/bin/sar (sar) in auto mode
Created symlink /etc/systemd/system/multi-user.target.wants/sysstat.service → /lib/systemd/system/sysstat.service.
Processing triggers for man-db (2.9.1-1) ...
Processing triggers for systemd (245.4-4ubuntu3.17) ...

Are you using the RHEL, Fedora, CentOS Stream, Rocky or Alma Linux? Try the dnf command or yum command as follows to install the sysstat package:
$ sudo dnf install sysstat

Examples

The syntax is
$ iostat -dyz $ iostat -dyz disk_name interval count

Monitoring Linux disk I/O usage

Open the terminal and type:
$ iostat -dyz 1 6

  • -d Displays the disk I/O utilization report.
  • -y Tell the iostat command to omit the first report with statistics since system boot.
  • -z Tell the iostat command to leav out any disk devices for which there is no activity.
  • 1 – Interval in seconds to update report.
  • 6 – Total count.

Sample outputs from above command:

Linux 5.15.0-1017-aws (www-1)     08/29/22    _x86_64_    (4 CPU)
 
 
Device             tps    kB_read/s    kB_wrtn/s    kB_dscd/s    kB_read    kB_wrtn    kB_dscd
 
 
Device             tps    kB_read/s    kB_wrtn/s    kB_dscd/s    kB_read    kB_wrtn    kB_dscd
xvda             33.00         0.00       180.00         0.00          0        180          0
 
 
Device             tps    kB_read/s    kB_wrtn/s    kB_dscd/s    kB_read    kB_wrtn    kB_dscd
 
 
Device             tps    kB_read/s    kB_wrtn/s    kB_dscd/s    kB_read    kB_wrtn    kB_dscd
xvda             15.00         0.00        68.00         0.00          0         68          0
 
 
Device             tps    kB_read/s    kB_wrtn/s    kB_dscd/s    kB_read    kB_wrtn    kB_dscd
xvdf            102.00         0.00       958.00         0.00          0        958          0
 
 
Device             tps    kB_read/s    kB_wrtn/s    kB_dscd/s    kB_read    kB_wrtn    kB_dscd

Where columns outputs are as follows:

  • tps – The transactions per second (IOPS – Input/output operations per second).
  • kB_read/s – Kilobytes read per second.
  • kB_wrtn/s – Kilobytes written per second.
  • kB_read – Total kilobytes read.
  • kB_wrtn – Total kilobytes written.

Display 3 reports of extended statistics at 5 second intervals for disk

Open the terminal application and then type the following command:
# iostat -d -x 5 3
Here is what I see from my RHEL server:

Linux 2.6.18-53.1.4.el5 (moon.nixcraft.in)   12/17/2007

Device:         rrqm/s   wrqm/s   r/s   w/s   rsec/s   wsec/s avgrq-sz avgqu-sz   await  svctm  %util
sda               1.10    39.82  3.41 13.59   309.50   427.48    43.36     0.17   10.03   1.03   1.75
sdb               0.20    18.32  1.15  6.08   117.36   195.25    43.22     0.51   71.14   1.26   0.91

Device:         rrqm/s   wrqm/s   r/s   w/s   rsec/s   wsec/s avgrq-sz avgqu-sz   await  svctm  %util
sda               0.00   108.40  1.40 64.40    49.60  1382.40    21.76     0.04    0.67   0.44   2.92
sdb               0.00    37.80  0.00 245.20     0.00  2254.40     9.19    28.91  108.49   1.08  26.36

Device:         rrqm/s   wrqm/s   r/s   w/s   rsec/s   wsec/s avgrq-sz avgqu-sz   await  svctm  %util
sda               0.00    97.01  1.00 57.29    39.92  1234.33    21.86     0.03    0.58   0.50   2.89
sdb               0.00    38.32  0.00 288.42     0.00  2623.55     9.10    32.97  122.30   1.15  33.27

Where,

  • rrqm/s : The number of read requests merged per second that were queued to the hard disk
  • wrqm/s : The number of write requests merged per second that were queued to the hard disk
  • r/s : The number of read requests per second
  • w/s : The number of write requests per second
  • rsec/s : The number of sectors read from the hard disk per second
  • wsec/s : The number of sectors written to the hard disk per second
  • avgrq-sz : The average size (in sectors) of the requests that were issued to the device.
  • avgqu-sz : The average queue length of the requests that were issued to the device
  • await : The average time (in milliseconds) for I/O requests issued to the device to be served. This includes the time spent by the requests in queue and the time spent servicing them.
  • svctm : The average service time (in milliseconds) for I/O requests that were issued to the device
  • %util : Percentage of CPU time during which I/O requests were issued to the device (bandwidth utilization for the device). Device saturation occurs when this value is close to 100%.

Finding out Linux disk utilization and usage on latest version of Linux

The output is updated as follows.
# iostat xvda 1 3
Outputs from Ubuntu Linux 22.04 LTS:

Linux 5.15.0-1017-aws (www-1)     08/29/22    _x86_64_    (4 CPU)
 
avg-cpu:  %user   %nice %system %iowait  %steal   %idle
           1.66    0.01    0.71    0.21    0.08   97.33
 
Device             tps    kB_read/s    kB_wrtn/s    kB_dscd/s    kB_read    kB_wrtn    kB_dscd
xvda              5.02         0.72       506.89         0.00    1104974  776504520          0
 
 
avg-cpu:  %user   %nice %system %iowait  %steal   %idle
           0.75    0.00    0.50    0.00    0.00   98.75
 
Device             tps    kB_read/s    kB_wrtn/s    kB_dscd/s    kB_read    kB_wrtn    kB_dscd
xvda              3.00         0.00        44.00         0.00          0         44          0
 
 
avg-cpu:  %user   %nice %system %iowait  %steal   %idle
           4.79    0.00    1.76    0.00    0.25   93.20
 
Device             tps    kB_read/s    kB_wrtn/s    kB_dscd/s    kB_read    kB_wrtn    kB_dscd
xvda              0.00         0.00         0.00         0.00          0          0          0

Some observations:

  • The avg-cpu line (first line of output) is nothing but a summary since the most recent Linux boot. In other words, you see the mean for the entire time the system has been up and running.
  • The rest of the line give info about device as per the interval chosen by the developer or sysadmin.

Say hello to the iotop utility

Use the iotop command to match up I/O statistics with Linux processes or threads currently running on the server. It is like the top command but for disks and block devices. For example:
$ sudo iotop

Click to enlarge

How do I interpret the output result for optimization?

First you need to note down following values from the iostat command outputs:

  1. The average service time (svctm)
  2. Percentage of CPU time during which I/O requests were issued (%util)
  3. See if a hard disk reports consistently high reads/writes (r/s and w/s)

If any one of these are high, you need to take one of the following action:

  • Get high speed disk and controller for file system (for example move from SATA I to SAS 15k disk or better SSD)
  • Tune software or application or kernel or file system for better disk utilization
  • Use RAID array to spread the file system load

For example, from about iostat report it appears that /dev/sdb under load. Hope this information will help you diagnose and optimize disk related issues. Please note that command and information discussed here almost applies to any other UNIX like variant.

Summing up

Now you know how to monitor disk utilization and I/O usage on Linux using the iostat command and iotop commands. However, these commands have many more options. Hence, see the following docs using the man command or help command or see the source code here:
$ man iostat $ man iotop

留言