Principle of Zero Copy

1. Traditional data copy method ①After
Insert picture description here
a read system call, DMA performs a data copy, from disk to kernel space
②After the read ends, a second data copy occurs, and the CPU copies the data from the kernel space to the user space
③send system call , The cpu takes the third data copy, and the cpu copies the data from the user space to the kernel space (socket buffer).
④After the send system call, DMA executes the fourth data copy and copies the data from the kernel to the protocol engine.
⑤In addition, In these four processes, a context switch occurs in each process

2. Zero copy-sendfile corresponds to
FileChannel.transferTo(long position, long count, WritableByteChannel target) in java.//Transfers data from the file channel to the given writable byte channel
Insert picture description here

① DMA copies from the kernel buffer
② CPU copies the data from the kernel buffer to the kernel space (socket buffer)
③ DMA copies the data from the kernel to the protocol engine
④ A total of 2 context switches occur in these three processes, each of which initiates reading the file And send data

Three data copies occurred in the above process, one of which was completed by the cpu

3. After Linux kernel 2.4, the socket buffer has been adjusted, and DMA has a collection function, as shown in the figure below:

Insert picture description here
① DMA copies from the kernel to the kernel buffer
② Adds the descriptor of the data location and length information to the kernel space (socket buffer)
③ DMA copies the data from the kernel to the protocol engine

Zero copy-mmap corresponds to the
MappedByteBuffer//file memory mapping in java.
Data will not be copied to the user space, only in the kernel space, similar to sendfile, but the application can directly manipulate the memory. ——

————————————
Copyright Statement: This article is the original article of CSDN blogger "EvanKevin", and it follows the CC 4.0 BY-SA copyright agreement. Please attach the original source link and this statement for reprinting.
Original link: https://blog.csdn.net/u013018618/article/details/80146617

Guess you like

Origin blog.csdn.net/weixin_39195030/article/details/103696867