Learn through the super-clean Baeldung Pro experience:
>> Membership and Baeldung Pro.
No ads, dark-mode and 6 months free of IntelliJ Idea Ultimate to start with.
Last updated: August 26, 2024
Linux is a highly customizable and flexible operating system (OS). One of the most common tasks performed by a Linux user is to check the total read and write Input/Output Operations per Second (IOPS). This is significant as high IOPS values can have a negative effect on the system’s performance.
In this tutorial, we’ll go over some of the most commonly used Linux commands to check the total read and write IOPS values.
The sysstat package includes a number of programs for monitoring system resources, performance, and usage. In fact, some of them can collect performance and activity data. We’ll be using one such tool, called iostat, to check the system’s IOPS.
Let’s install the sysstat package via the apt command:
$ sudo apt-get install sysstat
Before proceeding, we’ll use dpkg to verify the package is installed correctly. By using the dpkg query tool, we can list all packages on our system:
$ dpkg --get-selections | grep -w "install" | head
acl install
gawk install
sysstat install
The above output shows the list of installed packages via the –get-selections option. Furthermore, we piped the output to a grep command that selects the lines matching the word install. Finally, the head command shows the first 10 lines of the original output.
We use the iostat command to monitor and analyze I/O performance statistics of the system’s disks and file systems. In particular, it provides a wealth of information related to disk utilization, I/O operations, transfer rates, and more.
iostat has several options that allow us to customize the output to our specific needs. In the following examples, we’ll use the awk command for its features as a powerful text processing tool that can be used to manipulate text files.
Let’s explore how to use iostat to obtain the total read and write IOPS:
$ iostat -d -k 1 sda | awk '/sda/ {printf "Read: "$3", Write: "$4"\n"}'
Read: 0.00, Write: 4.00
Read: 2.00, Write: 5.00
Read: 0.00, Write: 5.00
Firstly, the -d option of iostat displays the disk utilization statistics, while the -k option specifies the units as kilobytes. Secondly, the number 1 entered as the value of -k indicates the statistics refresh interval in seconds. Lastly, the sda argument defines the device we want to monitor.
Since there are multiple ways to format our command when using iostat, we opted for piping to awk. This way, we can make our output more organized and informative.
In the above case, the pipe | symbol sends the output of iostat to awk. Consequently, awk filters the output of iostat by searching for the device sda using the /sda/ pattern. Once it finds the matching line, it extracts the third and fourth columns, which represent the read and write IOPS respectively, and prints them in a human-readable format as shown in the output. Finally, we label the values with a printf statement at the end.
Often, acquiring real-time values of the total read-and-write IOPS is important. Nonetheless, there are situations when we need to obtain some insight into the IOPS by observing the average values over specific time intervals.
Therefore, we’ll use the iostat command to get more details. For example, we can display the average read-and-write IOPS for a specific time period:
$ iostat -d -m 30 10 sda | awk 'BEGIN {count = 0; r_sum = 0; w_sum = 0} /sda/ {count++; r_sum += $3; w_sum += $4} END {printf "Average Read IOPS: %.2f\nAverage Write IOPS: %.2f\n", r_sum/count, w_sum/count}'
Average Read IOPS: 0.46
Average Write IOPS: 0.55
The above output represents the average read and write IOPS and displays them independently across an adjustable previous period of time.
Initially, we again use the -d and -k options. However, in the above example, we changed the -k value from 1 to 300. This is to enable us to calculate the average values for read and write IOPS over a 5-minute interval.
Afterward, we piped the output of iostat into awk as illustrated before. In essence, awk initializes three variables.
So, for every line of output that matches the /sda/ pattern, awk increments the count variable and adds the read and write IOPS to the r_sum and w_sum variables respectively. Finally, after all the iterations are complete, awk uses the END block to calculate and print the average read and write IOPS in a human-readable format by using printf.
On the other hand, one may opt for displaying real-time I/O usage statistics for all disks and partitions every second:
$ iostat -x 1
Linux 5.11.0-27-generic (ubuntu) 08/19/2021 _x86_64_ (4 CPU)
In the above example, the -x option forces the extended statistics. Thus, it displays more detailed I/O statistics:
The first line above shows the system name, kernel version, and number of CPUs.
Moreover, the second line of the output shows the percentage of CPU usage for different categories. These categories are user, nice, system, iowait, steal, and idle:
avg-cpu: %user %nice %system %iowait %steal %idle
3.34 0.00 0.75 0.38 0.00 95.53
Finally, the last section of the output displays I/O usage statistics for each device or partition:
Device tps kB_read/s kB_wrtn/s kB_read kB_wrtn
sda 0.51 0.01 19.49 3407844 124151490
sda1 0.51 0.01 19.49 3407844 124151490
sdb 0.00 0.00 0.00 187070 12
sdc 0.00 0.00 0.00 50608 0
sdd 0.00 0.00 0.00 50608 0
md0 0.00 0.00 0.00 50608 0
There are a total of six columns:
Consulting these columns, we can not only get the total IOPS, but also the actual data over which they were achieved.
In this article, we looked at the iostat command. It’s a useful tool for monitoring I/O performance in Linux. With iostat and awk, we can calculate the total read and write IOPS for a specific time period. In addition, we can display the results in a human-readable format.
By using options such as -d (displays the disk utilization statistics), -k (specifies the unit as kilobytes), and -x (displays more detailed I/O statistics), we can customize the output to display more detailed I/O statistics.