sg_read is an utility of sg3_utils, it reads data from a Linux SCSI generic device(sg), a block device or a normal file with each read command issued to the same offset or logical block address(lba). Mostly it is used to test a disk caching, SCSI transport throughput, and SCSI command overhead.

Here is one simple example:

# sg_read if=/dev/sg13 bs=512 count=1024
1024+0 records in, SCSI READ commands issued: 8
# sg_read if=/dev/sg13 bs=512 count=2MB
2000000+0 records in, SCSI READ commands issued: 15625

Add time option

time=TI, When TI is 0 (default) doesn't perform timing.  When 1, times transfer and does throughput  calculation, sarting at the first issued command until completion. When 2, times transfer and does throughput calculat the second issued command until completion. When 3 times from third command, etc. Anverage number of commands (SCSI READs or Unix read()s) executed per second is also output.

# sg_read if=/dev/sg13 bs=512 count=2MB time=2
Time from second SCSI READ command to end was 6.809284 secs, 150.37 MB/sec
Average number of SCSI READ commands per second was 2294.66
2000000+0 records in, SCSI READ commands issued: 15625

Enable direct io

dio=0 | 1         default  is  0  which  selects indirect IO. Value of 1 attempts direct IO which, if not available, falls back to indirect IO and notes this at completion. This option is only active if IFILE is an  sg  device. If a    direct  IO  is selected and /proc/scsi/sg/allow_dio has the value of 0 then a warning is issued (and indirect IO is performed)

# echo 1 >/proc/scsi/sg/allow_dio    # enable direct io first
# sg_read if=/dev/sg13 bs=512 count=2MB dio=1 time=2
Time from second SCSI READ command to end was 6.140665 secs, 166.75 MB/sec
Average number of SCSI READ commands per second was 2544.51
2000000+0 records in, SCSI READ commands issued: 15625

Enable mmap

mmap=0 | 1  default is 0 which selects indirect IO. Value of 1 causes memory mapped IO to  be  performed.  Selecting both dio and mmap is an error. This option is only active if IFILE is an sg device.

 

# sg_read if=/dev/sg13 bs=512 count=2MB mmap=1 time=2
Time from second SCSI READ command to end was 6.030546 secs, 169.79 MB/sec
Average number of SCSI READ commands per second was 2590.98
2000000+0 records in, SCSI READ commands issued: 15625