Read and Write files in directio mode (summary of other people's explanations on the Internet and my own opinions)

Source: Internet
Author: User

DMA: Direct Memory Access.

Objective: To directly store data in the buffer of user space through disk io (or DMA) without operating system buffering. This avoids the consumption of kernel buffer memory and CPU copy (data copying from kernel space to user space.

Technical explanation:
Directio Application Scenario: directio needs to read large files because DMA is initialized every time. If it is to read small files, it takes longer to initialize DMA than to read small files by the system, therefore, using directio for small files has no advantage. Large files are only read-only once, and no other applications will read the file again in the future. If other applications need to use it in the future, directio has no advantage at this time.

Direct actually has several advantages, instead of using the system cache. On the other hand, it uses DMA to directly control the buffer from memory input to user space without passing through the CPU for mov operations, no CPU consumption.

Refer to the webpage:

Http://topic.csdn.net/u/20080806/10/cdb1faa1-0146-4e96-8b12-26ba60acdbb5.html

Http://blog.csdn.net/nmzrl/article/details/1554772

Code example:
To read and write files in directio mode, you only need to select the o_direct option when opening the file, but you must add # DEFINE _ gnu_source before all the include operations. In addition, when you read and write files in directio mode, the opened buffer must be an integer multiple of the system's page size and must be aligned with the standard page size. For example, in linux2.6, the size of each page is 4096byt (function getpagesize ()), the requested buffer size must be an integer multiple of 4096.
Example:
(Test. c)
# DEFINE _ gnu_source
# Define buffer_size 8192
Int FD = open ("testfile", o_creat | o_rdwr | o_direct );
Int pagesize = getpagesize ();
Char * realbuff = malloc (buffer_size + pagesize );
Int pagesize = getpagesize ();
Char * alignedbuff = (char *) (inunsigned) realbuff + pagesize-1)/pagesize) * pagesize );
Write (FD, alignedbuff, buffer_size );
Free (realbuff );
The requested address realbuff is not set for it. After the following two operations, alignedbuff is set for its address. It is worth noting that the address must be completely released when space is being cast.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.