Concept
Avoiding data copying operations between different memory regions reduces data copying or context switching, thereby reducing CPU load.
Traditional
Linux data transport mechanism:poll,I/O interrupt,DMA
I/O interrupt:
- Call read [context switch]
- kernel initiate I/O request to disk, data load into disk buffer
- Send I/O interrupt to kernel
- Transfer data from disk buffer to kernel buffer
- Tranfer to user buffer [context switch]
DMA:
- Call read [context switch]
- Kernel initiate I/O request to DMA [cpu schedule]
- DMA initiate I/O request to disk, data load into disk buffer
- Disk signals DMA controller,DMA copy data from disk buffer to kernel buffer
- Send I/O interrupt to kernel
- Transfer to user buffer [context switch]
Analysis:
4 context switch 2 DMA copy 2 CPU copy
Impls
Three ways to implement zero-copy
- Direct I/O
- Reduce data copies
- Copy-On-Write
Direct I/O
open with O_DIRECT, pypass kernel buffer cache
Reduce data copies
mmap + write
Replace read + write, reduce 1 CPU copy(kernel to user)
sendfile [non modify]
copy in the kernel through fd (read buffer -> socket buffer), reduce 2 context switch and 1 CPU copy
sendfile + DMA gather copy [non modify]
Zero CPU copy, only transfer fd and data_len to soocket buffer, DMA directy transfer from read buffer to NIC based on the fd and data_len
splice [non modify]
set pipeline to avoid CPU copy
COW
Fork create the child process which shares the parent’s memory until it tries to write data, triggering the kernel to copy only that specific page.
Link
https://zhuanlan.zhihu.com/p/83398714
https://xiaolincoding.com/os/8_network_system/zero_copy.html