Zero-Copy && mmap

来源:互联网 发布:美国交通事故数据集 编辑:程序博客网 时间:2024/06/13 21:29

转载:http://www.linuxjournal.com/article/6345?page=0,0

本地文件网络传输过程

这里写图片描述
传统的文件读写过程,涉及到系统调用read、write,产生了4次上下文切换、2次CPU拷贝和2次DMA拷贝,CPU拷贝会占消耗CPU资源,成本高,而DMA拷贝是驱动进行的,对CPU的消耗很小,具体过程如下:

Step one: the read system call causes a context switch from user mode to kernel mode. The first copy is performed by the DMA engine, which reads file contents from the disk and stores them into a kernel address space buffer.
Step two: data is copied from the kernel buffer into the user buffer, and the read system call returns. The return from the call caused a context switch from kernel back to user mode. Now the data is stored in the user address space buffer, and it can begin its way down again.
Step three: the write system call causes a context switch from user mode to kernel mode. A third copy is performed to put the data into a kernel address space buffer again. This time, though, the data is put into a different buffer, a buffer that is associated with sockets specifically.
Step four: the write system call returns, creating our fourth context switch. Independently and asynchronously, a fourth copy happens as the DMA engine passes the data from the kernel buffer to the protocol engine.

使用mmap

mmap
使用mmap进行文件读写,涉及到系统调用mmap,产生2次上下文切换、1次CPU拷贝(写数据时)和2次DMA拷贝,具体过程如下:

Step one: the mmap system call causes the file contents to be copied into a kernel buffer by the DMA engine. The buffer is shared then with the user process, without any copy being performed between the kernel and user memory spaces.
Step two: the write operation causes the kernel to copy the data from the original kernel buffers into another kernel buffer associated with the same hard drive.
Step three: the third copy happens as the DMA engine passes the data into the kernel buffer.

Zero-Copy过程

这里写图片描述
Zero-Copy涉及到了sendfile系统调用,产生了2次上下文切换和2次DMA拷贝,具体过程如下:

Step one: the sendfile system call causes the file contents to be copied into a kernel buffer by the DMA engine.
Step two: no data is copied into the socket buffer. Instead, only descriptors with information about the whereabouts and length of the data are appended to the socket buffer. The DMA engine passes data directly from the kernel buffer to the protocol engine, thus eliminating the remaining final copy.

Zero-Copy概念

Zero-Copy是针对操作系统而言的,因为没有CPU拷贝,原文如下:

Because data still is actually copied from the disk to the memory and from the memory to the wire, some might argue this is not a true zero copy. This is zero copy from the operating system standpoint, though, because the data is not duplicated between kernel buffers.

Zero-Copy优点

Zero-Copy减少了2次上下文切换,节省了内存空间和CPU资源,因此相比于传统的文件传输过程速度会非常快,原文如下:

When using zero copy, other performance benefits can be had besides copy avoidance, such as fewer context switches, less CPU data cache pollution and no CPU checksum calculations.