Zero-copy&sendfile Analysis

Source: Internet
Author: User
Tags sendfile

One, the problem of the typical IO call
A typical Web server transmits static files (such as CSS,JS, pictures, etc.) in the following process:

Read (file, tmp_buf, Len);
Write (socket, tmp_buf, Len);


The first call to read reads the file from disk to Tmp_buf and then calls write to write tmp_buf to the socket, where four data copy occurs, as shown in procedure 1

Figure 1

1. When calling read system call, the data is passed through DMA (direct Memory Access) copy to kernel mode
2. Then the cpu control the kernel-mode data copy to the  buffer in user mode
3. read call is complete, write call first to copy to kernel mode socket buffer
4. Finally through the DMA Copy the kernel mode socket buffer data copy to the network card device transmission.

From the above process can be seen, the data in vain from kernel mode to user mode walk a lap, wasted two times copy, and these two times copy is CPU copy, that is, CPU resources.

Second, Zero-copy&sendfile ()
The Linux 2.1 kernel introduces the sendfile function for transferring files through the socket.
Sendfile (socket, file, Len);
This function completes the transfer of the file through a system call, and reduces the mode switching of the original Read/write mode. In addition to reducing the copy of the data , thedetailed process of sendfile is shown in Figure 2:

Figure 2

Transferring files via sendfile requires only one system call when  sendfile is invoked:
Span lang= "en-us" >1. The data is first read from disk to kernel buffer by DMA copy
2. Then the data from kernel buffer copy to sokcet buffer via cpu copy
3. Finally through DMA copy will socket buffer data copy to the NIC Send sendfile with read/write mode, less   once mode switch cpu copy. However, from the above process can also be found from kernel buffer data copy to socket buffer is not necessary.

To this end, theLinux2.4 kernel has made improvements to sendfile, asshown in 3

Figure 3

The improved processing process is as follows:
1.DMA copy will disk dataCopy toKernel in buffer
2. ToAppend the current data to be sent in the socket bufferKernel position and offset in buffer
3.DMA gather copy is based onThe position and offset in the socket buffer directlyKernel data in BufferCopy to the NIC.
After this process, the data is only2 playsCopy is sent out of the disk.
(Maybe someone is going to tangle"Not to sayzero-copy? How there are two times copy ah zero copy is for the kernel, the data in kernel mode is zero-copy. In other words, the file itself on the porcelain plate to really completely zero-copy can be transmitted, that is the ghost of it).
Many of the current high-performance http servers introduce sendfile mechanisms, such as nginx,lighttpd and so on.

III, Java NIO transferto ()
Java nio
Filechannel.transferto (long position, long count, Writeablebytechannel target) The
method transfers data from the current channel to the target channel target, in support of linux system, the implementation of transferto () relies on sendfile () calls.

Iv. reference Documentation
"Zero Copy i:user-mode Perspective"http://www.linuxjournal.com/article/6345?page=0,0
"efficient data transfer through zero copy"http://www.ibm.com/developerworks/linux/library/j-zerocopy
"Thec10k problem"http://www.kegel.com/c10k.html

http://blog.csdn.net/u013074465/article/details/45822431

Zero-copy&sendfile Analysis

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.