Please indicate the original source (http://blog.sina.com.cn/wyw1976) and author's email (wyw1976@gmail.com)
DD is a commonly used command on Linux. For example: DD if=/1.txtof=/tmp/2.txt
(where the if represents the input file;of on behalf of output file, the result of the command is to copy the 1.txt contents of the root directory to 2.txt in the/tmp directory.) )
Of course, it's obviously a lot more to do with DD, because CP can do the same thing, and it's simpler.
Here's a brief introduction to the "Advanced" Application of DD:
(i) direct read and write Flash
In embedded systems, often need to test system file read and write performance, if you want to bypass the file system (file system generally have their own cache), directly test the performance of Flash drive, the simplest method is DD.
Generally, flash memory (NAND) will have several partitions, each partition corresponds to a/dev/mtd* node:
Write operations
DD IF=/DEV/URANDOMOF=/DEV/MTD1 bs=4096 count=1000
(The above command is to copy files from the/dev/urandom to MTD1 this partition, each read and write the amount of data is 4,096 bytes, copy 1000 times, so the total amount of data is 4M.)
Read operations
DD IF=/DEV/MTD1 of=/dev/nullbs=4096 count=1000
(The above command is to copy files from the partition MTD1 to the empty device/dev/null, each read and write data is 4,096 bytes, copy 1000 times, so the total amount of data is 4M.)
The throughput rate that the DD will output when it finishes execution.
(ii) Directio
In some systems, there is no MTD node or direct operation of the storage, and if you want to test file read and write performance, how to eliminate the impact of the file system?
The DD command has a set of parameters Oflag and Iflag that control the reading and writing of the source and target files as Directio, that is, read or write buffer over the operating system when reading or writing files. If Oflag=direct,nonblock is specified, the cache effect is ignored when writing a file, and if Iflag=direct,nonblock is specified, the cache effect is ignored when reading files
The following is a set of measured data from which to see the effect of cache on the final performance.
In addition to specifying direct and Nonblock in flag, you can specify additional values, as detailed in: http://www.gnu.org/software/coreutils/manual/html_node/dd-invocation.html
It should be noted that some platforms may support only a subset of flag.
(iii) Copy screen
Use DD to achieve screenshots, assuming the screen corresponding to the device node/dev/fb0, screen size is 600x800, screen pixel format is rgb565 (each pixel corresponds to two bytes), then the command is as follows:
DD if=/dev/fb0 Of=/tmp/1.dat bs=600 count=1600
Conversely, you can also use DD to write the screen directly, for example, the following command will randomly render the screen:
DD If=/dev/urandom of=/dev/fb0 bs=600count=1600
(iv) serial reading and writing
DD can also write the serial port, assuming the serial port node is/DEV/TTYS0, the following command writes the file to the serial port:
DD If=1.txt OF=/DEV/TTYS0
The following is the call process for the DD command:
The F_OP data structure in the above figure is assigned when a file or device is open, where the read,write points to the actual function that operates the file. Through this flowchart, we can see that the device or file driver to achieve the standard read and write function, you can use the DD to read and write.