2

I am zeroing a CF card using dd

dd if=/dev/zero of=/dev/sdg

Watching the system using gkrellm or iostat I see lots of reads from the CF card device, followed by bursts of writes.

With this example:

# dd if=/dev/zero of=/dev/sdg count=200000
200000+0 records in
200000+0 records out
102400000 bytes (102 MB) copied, 46.1357 s, 2.2 MB/s

Iostat with a 1 second interval produces this:

Linux 2.6.32-573.3.1.el6.x86_64
Device:            tps   Blk_read/s   Blk_wrtn/s   Blk_read   Blk_wrtn
sdg               0.00         0.00         0.00          0          0
sdg               0.00         0.00         0.00          0          0
sdg               0.00         0.00         0.00          0          0
sdg             406.00      3248.00         0.00       3248          0
sdg             719.00      5752.00         0.00       5752          0
sdg             738.00      5904.00         0.00       5904          0
sdg             721.00      5768.00         0.00       5768          0
sdg             735.00      5880.00         0.00       5880          0
sdg             715.00      5720.00         0.00       5720          0
sdg             732.00      5856.00         0.00       5856          0
sdg             717.00      5736.00         0.00       5736          0
sdg             732.00      5856.00         0.00       5856          0
sdg             717.00      5736.00         0.00       5736          0
sdg             732.00      5856.00         0.00       5856          0
sdg             715.00      5720.00         0.00       5720          0
sdg             732.00      5856.00         0.00       5856          0
sdg             714.00      5712.00         0.00       5712          0
sdg             733.00      5864.00         0.00       5864          0
sdg             716.00      5728.00         0.00       5728          0
sdg             732.00      5856.00         0.00       5856          0
sdg             715.00      5720.00         0.00       5720          0
sdg             732.00      5856.00         0.00       5856          0
sdg             712.00      5696.00         0.00       5696          0
sdg             732.00      5856.00         0.00       5856          0
sdg             715.00      5720.00         0.00       5720          0
sdg             734.00      5872.00         0.00       5872          0
sdg             715.00      5720.00         0.00       5720          0
sdg             734.00      5872.00         0.00       5872          0
sdg             715.00      5720.00         0.00       5720          0
sdg             732.00      5856.00         0.00       5856          0
sdg             715.00      5720.00         0.00       5720          0
sdg             732.00      5856.00         0.00       5856          0
sdg             715.00      5720.00         0.00       5720          0
sdg             733.00      5864.00         0.00       5864          0
sdg             716.00      5728.00         0.00       5728          0
sdg             733.00      5864.00         0.00       5864          0
sdg             444.00      3320.00      6960.00       3320       6960
sdg              71.00        56.00     15360.00         56      15360
sdg              81.00        72.00     17280.00         72      17280
sdg              83.00        80.00     17520.00         80      17520
sdg              81.00        80.00     17040.00         80      17040
sdg              82.00        72.00     17520.00         72      17520
sdg              81.00        72.00     17280.00         72      17280
sdg              83.00        80.00     17520.00         80      17520
sdg              81.00        80.00     17040.00         80      17040
sdg              82.00        72.00     17520.00         72      17520
sdg              82.00        80.00     17280.00         80      17280
sdg             204.00      1152.00     14352.00       1152      14352
sdg             718.00      5744.00         0.00       5744          0
sdg             160.00      1024.00      7328.00       1024       7328
sdg               0.00         0.00         0.00          0          0
sdg               0.00         0.00         0.00          0          0
sdg               0.00         0.00         0.00          0          0
sdg               0.00         0.00         0.00          0          0

Running strace on dd reveals nothing strange:

open("/dev/zero", O_RDONLY)             = 3
dup2(3, 0)                              = 0
close(3)                                = 0
lseek(0, 0, SEEK_CUR)                   = 0
open("/dev/sdg", O_WRONLY|O_CREAT|O_TRUNC, 0666) = 3
dup2(3, 1)                              = 1
close(3)                                = 0
read(0, "\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0"..., 512) = 512
write(1, "\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0"..., 512) = 512
read(0, "\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0"..., 512) = 512
write(1, "\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0"..., 512) = 512
read(0, "\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0"..., 512) = 512
write(1, "\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0"..., 512) = 512

Yet the same behaviour is not observed using

cat /dev/zero >/dev/sdg

iostat:

Device:            tps   Blk_read/s   Blk_wrtn/s   Blk_read   Blk_wrtn
sdg               0.00         0.00         0.00          0          0
sdg               0.00         0.00         0.00          0          0
sdg               0.00         0.00         0.00          0          0
sdg              37.00         0.00      8880.00          0       8880
sdg              73.00         0.00     17520.00          0      17520
sdg              74.00         0.00     17528.00          0      17528
sdg              75.00         0.00     17304.00          0      17304
sdg              50.00         0.00     11768.00          0      11768
sdg              56.00         0.00     12976.00          0      12976
sdg              44.00         0.00     10328.00          0      10328
sdg              76.00         0.00     17544.00          0      17544
sdg              75.00         0.00     17536.00          0      17536
sdg              75.00         0.00     17536.00          0      17536

What is going on?

1
  • Try using a sensible block size (4K-64K)
    – qasdfdsaq
    Aug 18, 2015 at 13:36

2 Answers 2

1

Although the CF card is reported as having 512 byte blocks:

kernel: sd 11:0:0:0: [sdk] 15662304 512-byte logical blocks

Using a 4k block size on df does remove the reading and therefore increases performance:

# dd if=/dev/zero of=/dev/sdk count=80000
80000+0 records in
80000+0 records out
40960000 bytes (41 MB) copied, 5.49363 s, 7.5 MB/s
# dd if=/dev/zero of=/dev/sdk count=10000 bs=4k
10000+0 records in
10000+0 records out
40960000 bytes (41 MB) copied, 2.82353 s, 14.5 MB/s

I guess that the kernel might be reading the card to assemble a full block to write back, but it doesn't seem like correct behaviour.

4
  • Curious, why do you think this is not the correct behaviour? How else could it work, if you are writing out only part of a block? Aug 19, 2015 at 15:06
  • Buffer the writes until I close the file, or the buffer fills up..
    – Neik
    Aug 19, 2015 at 18:33
  • I like the answer. Just curious, what would the results look like if you set block size bigger than 4k, say 8k, or 16k? I'd assume they'd remove the reading and get around 14.5 MB/s as well.
    – xpt
    Aug 20, 2015 at 2:20
  • Yes. Changing the block size to the values you suggest gives the same result. Block sizes of 1k and 2k work as badly as 512 bytes.
    – Neik
    Aug 23, 2015 at 18:16
0

Your device has a small buffer on it to help speed things up. When you redirect using cat, buffering is done by the OS instead. You can tell dd to write directly to the device without caching. For example:

dd if=/dev/zero of=/dev/sdg oflag=direct

See man 5 open and read about O_DIRECT. Of course though, this will be very slow. If you want to speed it up, but still enjoy the capabilities of dd, you can do something like this:

dd if=/dev/zero | cat >/dev/sdg
1
  • I don't see why this requires reading from the CF card though. I am only writing to it. Also I am taking a massive performance hit caused by all these unnecessary reads.
    – Neik
    Aug 18, 2015 at 13:12

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .