I'm writing C code with some real-time constraints. I tested out the speed I can write to a disk with dd:
dd if=/dev/zero of=/dev/sdb bs=32K count=32768 oflag=direct
This writes 1GB of zeros to /dev/sdb in 32K block sizes
I reach about 103 MB/s with this
Now I programmatically do something similar:
open("/dev/sdb",O_WRONLY|O_CREAT|O_DIRECT|O_TRUNC, 0666);
I get a timestamp value write from a 32K buffer to /dev/sdb 10,000 times (in a for loop) get another timestamp value do a bit of number crunching to get the rate in MB/s and it is about 49 MB/s
Why can't I reach the same speed as dd? An strace reveals the same open command that I use.
Check what system calls dd
makes, not just the open but also the subsequent read
s and writes
. Using the right buffer sizes can make a significant difference in this kind of large copy. Note that /dev/zero
is not a good test for benchmarking if your final goal is a disk-to-disk copy.
If you can't match dd
's speed by matching it system call for system call... well, read the source.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With