Managing Files and Disk I/O Benchmarks with Linux dd Command
The dd utility serves as a powerful tool for low-level data copying and conversion on Linux systems. It is frequently employed for generating files of specific sizes and performing rudimentary disk I/O performance analysis.
Creating Allocated vs. Sparse Files
When generating a test file, one can choose to physically write data to the disk or merely create a file system entry that appears large. The command below creates a 512MB file named allocated_file.img by reading from /dev/zero and writing to the disk. This operation performs actual I/O, and its speed is constrained by the underlying storage device's write capabilities.
dd if=/dev/zero of=allocated_file.img bs=1M count=512
Alternatively, if the goal is to reserve address space without consuming physical blocks, a sparse file can be generated. By utilizing the seek parameter, dd advances the output file offset without writing actual data. The following command creates a 50GB file that occupies negligible actual disk space. The creation process is nearly instantaneous because it only manipulates file system metadata.
dd if=/dev/zero of=sparse_file.img bs=1G count=0 seek=50
This technique allows creating files larger than the available physical storage, provided they remain sparse.
Disk I/O Benchmarking
Combined with the time command, dd can estimate sequential read and write speeds. The block size (bs) and the count of blocks (count) define the total data transfer size.
Write Performance Test
To test the write speed of a target directory, data is read from /dev/zero (an infinite source of null bytes) and written to a new file. This isolates the disk's write throughput.
time dd if=/dev/zero of=write_bench.dat bs=1M count=1024 conv=fdatasync
Sample Output:
1024+0 records in
1024+0 records out
1073741824 bytes (1.1 GB, 1.0 GiB) copied, 2.54321 s, 422 MB/s
real 0m2.545s
user 0m0.012s
sys 0m0.890s
Read Performance Test
To test read speed, data is read from an existing file and written to /dev/null, which discards the output. This removes write overhead from the equation.
time dd if=write_bench.dat of=/dev/null bs=1M
Sample Output:
1024+0 records in
1024+0 records out
1073741824 bytes (1.1 GB, 1.0 GiB) copied, 0.23015 s, 4.7 GB/s
real 0m0.232s
user 0m0.004s
sys 0m0.228s
Combined Read/Write Test
Copying from one file to another exercises both the read and write mechanisms of the storage controller.
time dd if=write_bench.dat of=readwrite_copy.dat bs=1M
For statistically significant results, increase the data size and calculate the average of multiple runs. To automate logging, redirect the standard error stream (where dd reports statistics) to a log file:
dd if=/dev/zero of=/tmp/bench.img bs=1M count=4096 2>> benchmark_log.txt