NOTE

Zero-Copy Mechanism

Traditional I/O moves data through disks, kernel buffers, user space, and the network. These notes use that path to examine how zero-copy reduces overhead.

Operating Systems / LinuxCreated Updated 2 min readhistorical

This is a historical learning note and may contain outdated or incomplete understanding.

1. Traditional I/O

Linux I/O - Direct 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.
  4. Use CPU copy to copy data from user space to the Socket buffer.
  5. Use DMA copy (CPU participation is not required) to send data from the Socket buffer through the socket.
  6. Return to user mode after the call completes.

Traditional I/O performs four copies: 1, 2, 4, and 5, and four context switches: 1, 2, 4, and 6.

2. Memory Mapping Optimization

Linux I/O - memory map Through memory mapping, the file is mapped into the kernel buffer, while user space can share the data in kernel space. The steps are:

  1. Use DMA copy (CPU participation is not required) to copy data from disk to the disk buffer.
  2. The disk buffer and user space share data, and user space processes it.
  3. Use CPU copy to copy data from user space to the Socket buffer.
  4. Use DMA copy (CPU participation is not required) to send data from the Socket buffer through the socket.
  5. Return to user mode after the call completes. Memory mapping performs three copies: 1, 3, and 4, and three context switches: 1, 2, and 5.

3. Linux 2.1 sendfile

Linux I/O - Linux 2 The steps are:

  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 the Socket buffer.
  3. Use DMA copy (CPU participation is not required) to send data from the Socket buffer through the socket.
  4. Return to user mode after the call completes.

Linux 2.1 sendfile performs three copies: 1, 2, and 3, and two context switches: 1 and 4.

4. Linux 2.6 sendfile

Linux I/O - Linux 2 The steps are:

  1. Use DMA copy (CPU participation is not required) to copy data from disk to the disk buffer.
  2. 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.
  3. Use DMA copy (CPU participation is not required) to send data from the Socket buffer through the socket.
  4. Return to user mode after the call completes.

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 internally.
  • JDK NIO FileChannel.transferTo: uses Linux sendfile internally.

6. References

Discussion

Sign in with GitHub to comment. Discussions are stored as GitHub Issues.View on GitHub