码迷,mamicode.com
首页 > 其他好文 > 详细

Efficient data transfer through zero copy

时间:2014-09-24 02:06:45      阅读:514      评论:0      收藏:0      [点我收藏+]

标签:des   blog   http   io   java   ar   for   数据   2014   

一。传统数据传输

1.user mode & kernel mode

bubuko.com,布布扣 

 

2.context switch

bubuko.com,布布扣

The steps involved are:

1.The read() call causes a context switch (see Figure 2) from user mode to kernel mode. Internally a sys_read() (or equivalent) is

issued to read the data from the file. The first copy (see Figure 1) is performed by the direct memory access (DMA) engine, which

reads file contents from the disk and stores them into a kernel address space buffer.

2.The requested amount of data is copied from the read buffer into the user buffer, and the read() call returns. The return from the

call causes another context switch from kernel back to user mode. Now the data is stored in the user address space buffer.

3.The send() socket call causes a context switch from user mode to kernel mode. A third copy is performed to put the data into a kernel

address space buffer again. This time, though, the data is put into a different buffer, one that is associated with the destination socket.

4.The send() system call returns, creating the fourth context switch. Independently and asynchronously, a fourth copy happens as the

DMA engine passes the data from the kernel buffer to the protocol engine.

 

二。zero-copy

Notice that the second and third data copies are not actually required. The application does nothing other than cache the data and

transfer it back to the socket buffer. Instead, the data could be transferred directly from the read buffer to the socket buffer.

The java.util.current.FileChannel‘s transferTo() method lets you do exactly this. 

bubuko.com,布布扣

1.

bubuko.com,布布扣

The steps taken when you use transferTo()  are:
1.The transferTo() method causes the file contents to be copied into a read buffer by the DMA engine. Then the data is copied by

the kernel into the kernel buffer associated with the output socket.

2.The third copy happens as the DMA engine passes the data from the kernel socket buffers to the protocol engine.

 

三。再改进

This is an improvement: we‘ve reduced the number of context switches from four to two and reduced the number of data copies

from four to three (only one of which involves the CPU). But this does not yet get us to our goal of zero copy. We can further

reduce the data duplication done by the kernel if the underlying network interface card supports gather operations.

In Linux kernels 2.4 and later, the socket buffer descriptor was modified to accommodate this requirement. This approach not only

reduces multiple context switches but also eliminates the duplicated data copies that require CPU involvement. The user-side usage

still remains the same, but the intrinsics have changed:

1.The transferTo() method causes the file contents to be copied into a kernel buffer by the DMA engine.

2.No data is copied into the socket buffer. Instead, only descriptors with information about the location and length of the data are

appended to the socket buffer. The DMA engine passes data directly from the kernel buffer to the protocol engine, thus eliminating

the remaining final CPU copy.

bubuko.com,布布扣

 

 

四。出处

IBM developer

 

Efficient data transfer through zero copy

标签:des   blog   http   io   java   ar   for   数据   2014   

原文地址:http://www.cnblogs.com/yuyutianxia/p/3989578.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!