NOTE
Zero-Copy Mechanism
[toc] 1. Traditional I/O The normal data processing flow is: 1. Use DMA copy (CPU participation is not required) to copy data from disk to the disk buffer. 2. Use CPU copy to copy data from the disk buffer to user space. 3. Process it in user space.
This is a historical learning note and may contain outdated or incomplete understanding.
1. Traditional I/O
The normal data-processing flow is:
- Use DMA copy (CPU participation is not required) to copy data from disk to the disk buffer.
- Use CPU copy to copy data from the disk buffer to user space.
- Process it in user space.
- Use CPU copy to copy data from user space to the Socket buffer.
- Use DMA copy (CPU participation is not required) to send data from the Socket buffer through the socket.
- Return to user mode after the call completes.
It can be seen that traditional I/O performs four copies: 1, 2, 4, and 5, and four context switches: 1, 2, 4, and 6.
2. memory map Optimization
Through memory mapping, the file is mapped into the kernel buffer, while user space can share the data in kernel space.
The steps are:
- Use DMA copy (CPU participation is not required) to copy data from disk to the disk buffer.
- The disk buffer and user space share data, and user space processes it.
- Use CPU copy to copy data from user space to the Socket buffer.
- Use DMA copy (CPU participation is not required) to send data from the Socket buffer through the socket.
- Return to user mode after the call completes. It can be seen that memory map performs three copies: 1, 3, and 4, and three context switches: 1, 2, and 5.
3. Linux 2.1 sendfile
The steps are:
- Use DMA copy (CPU participation is not required) to copy data from disk to the disk buffer.
- Use CPU copy to copy data from the disk buffer to the Socket buffer.
- Use DMA copy (CPU participation is not required) to send data from the Socket buffer through the socket.
- Return to user mode after the call completes.
It can be seen that Linux 2.1 sendfile performs three copies: 1, 2, and 3, and two context switches: 1 and 4.
4. Linux 2.6 sendfile
The steps are:
- Use DMA copy (CPU participation is not required) to copy data from disk to the disk buffer.
- Use CPU copy (there is very little data, so it is fast and can be ignored) to copy a small amount of data from the disk buffer to the Socket buffer.
- Use DMA copy (CPU participation is not required) to send data from the Socket buffer through the socket.
- Return to user mode after the call completes.
It can be seen that Linux 2.6 sendfile performs two copies: 1 and 3, and two context switches: 1 and 4.
5. What Is Zero Copy?
The CPU does not need to spend resources copying data between memory areas. Put simply, the number of CPU copies is 0.

5.1. Zero Copy in Java
- Netty’s FileRegion is implemented by calling NIO’s FileChannel.transferTo method in the underlying source code.
- Off-heap memory from JDK NIO ByteBuffer.allocateDirect.
- JDK NIO FileChannel.map: uses Linux mmap underneath.
- JDK NIO FileChannel.transferTo: uses Linux sendfile underneath.
Discussion
Sign in with GitHub to comment. Discussions are stored as GitHub Issues.View on GitHub