Zero-Copy I/O: Concepts, Traditional IO, and Implementation with mmap and sendfile
Zero‑copy I/O minimizes data copying by using DMA and kernel mechanisms such as mmap + write or the sendfile() system call, with sendfile achieving true zero‑copy through fewer system calls, context switches, and memory copies compared to traditional read/write paths.
Zero-copy refers to reducing unnecessary data copies during network transmission.
Traditional I/O involves multiple copies between user space, kernel buffers, and CPU, leading to high CPU usage.
Typical steps: user calls read(), CPU issues disk command, disk fills kernel buffer, CPU copies to user buffer, then user writes to socket, etc.
DMA offloads data movement, allowing the DMA controller to transfer data between memory and I/O devices without CPU intervention.
Implementation methods:
1. mmap + write – mmap maps a file into the process address space, eliminating one copy but still incurs several context switches.
mmap(...)Data flow: disk → kernel buffer (DMA) → shared memory → write() copies to socket buffer → DMA to NIC.
2. sendfile() – a Linux system call that transfers data from a file descriptor to a socket directly, reducing system calls and context switches.
ssize_t sendfile(int out_fd, int in_fd, off_t *offset, size_t count);Data flow: disk → kernel buffer (DMA) → socket (via SG‑DMA) → NIC. Only one system call, two context switches, and two DMA copies are needed.
Comparison: mmap requires two system calls, four context switches, and three copies; sendfile achieves true zero-copy with fewer operations.
PageCache acts as a fast buffer for disk data, using read‑ahead to improve performance, but its limited size may require async I/O or direct I/O for large files.
Discussion highlights the trade‑offs and questions about using mmap vs. sendfile in specific systems like RocketMQ.
DeWu Technology
A platform for sharing and discussing tech knowledge, guiding you toward the cloud of technology.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.