How do you max out a disk’s random accesses under Linux?
My original goal was to monitor disk_io_time
in order to make sure
our disks were not overloaded. However, how would I know what the
limits of disk_io_time
were, so I could correctly set the thresholds
for the monitoring service?
I could max out random accesses and then observe the resulting
disk_io_time
s. So I looked for a simple script to do that … and
did not find any.
So here we go
#!/bin/bash
help() {
echo 'usage: read_random_disk_sectors BLOCK_DEVICE_NAME'
echo ' read_random_disk_sectors --help'
echo
echo ' Will start reading random disk sectors one'
echo ' after another. Useful for maxing out random'
echo ' disk accesses'
echo
echo " BLOCK_DEVICE_NAME should be something like 'sda'"
echo
echo " You can run this command for 5 seconds by issuing"
echo " something like this:"
echo
echo " sudo timeout -s INT 5s ./read_random_disk_sectors BLOCK_DEVICE_NAME"
exit 1
}
[ "$1" == "--help" ] && help
if [ "$1" == "" ]; then
( echo 'ERROR: path to block device needs to be given'; echo ) >&2
help
fi
set -e # stop on error
set -u # stop on undefined variable
set -o pipefail # stop part of pipeline failing
show_counter() {
echo "I have done $counter reads"
}
trap show_counter INT
DISK="$1"
SECTORS=$( cat /sys/block/${DISK}/size )
LAST_SECTOR=$(( $SECTORS -1 ))
counter=0
while true; do
SECTOR_TO_READ=$( shuf -i 0-${LAST_SECTOR} -n 1 )
dd if=/dev/${DISK} of=/dev/null bs=512 skip=${SECTOR_TO_READ=} count=1 2>/dev/null
counter=$(( $counter + 1 ))
done