On Linux, the zero-copy technology

This article discusses the Linux main several zero-copy technology as well as zero-copy technology scene applicable . In order to quickly establish the concept of zero-copy, we take a common scene in the introduction:

## citations

When writing a server application (Web Server or File Server), file download is a basic function. This time the task server: the server host files on disk without modification sent out from the socket has been connected , we usually done with the following code:

while((n = read(diskfd, buf, BUF_SIZE)) > 0)
    write(sockfd, buf , n);

The basic operation is the cycle of reading the contents of a file from disk into a buffer, then sent to the contents of the buffer socket. However, due to the Linux I/Ooperating default buffer I/O. This is primarily used inside readand writetwo system calls, we do not know the operating system in which what has been done. In fact in the above I/Ooperation, the occurrence of the multiple copies of data.

When the application to access a data block, the operating system first checks, this is not recently accessed files, content is cached in the kernel buffer, and if so, according to the operating system directly readprovided by the system call bufaddress, the kernel buffer copy the contents of bufthe specified user to the buffer space. If not, the first operating system to copy data on the disk kernel buffer, this step is mainly rely on DMAto transmit, then copy the contents of the kernel buffer to the user buffer.
Next, the writesystem calls the user and then copy the contents of the buffer to the network stack associated kernel buffer, and finally socketthen send the contents of kernel buffer to the card.
Having said that, as Figure clearly:

Data copy


As can be seen from the figure above, it produced a total of four copies of the data, even if the DMAprocessed communications with hardware, the CPU still needs to deal with two copies of data, while in user mode and kernel mode also occurred multiple context switches, no doubt increased the burden on CPU.

 

In this process, we did not make any changes to the contents of the file, then copy the data back and forth in the kernel space and user space is undoubtedly a waste, and zero copy is mainly to address this inefficiency.

What is a zero-copy technology (zero-copy)? ##

Zero-copy main task is to avoid CPU to copy data from one memory to another piece of storage, mainly using a variety of zero-copy technology, avoid CPU to do a lot of data copy tasks, reduce unnecessary copy, or get other components to do this type of simple data transfer tasks, CPU-free to focus on other tasks. This allows more efficient use of system resources.

We continue to return to the example of citation, how do we reduce the number of data copies of it? An obvious focus is to reduce the data obtained in kernel space and user space copy back and forth, which introduces a type of zero-copy:

So that data transmission does not require the user space

Use mmap #####

One way to reduce the number of copies we are calling mmap () call instead read:

buf = mmap(diskfd, len);
write(sockfd, buf, len);

The application calls mmap(), data on the disk will pass DMAthe copy of the kernel buffer, then will this operating system kernel buffer and application sharing, so you do not need the contents of the kernel buffer to the user space of the copy. The application then calls write()the operating system kernel buffer content directly copied to the socketbuffer, it all happened in kernel mode, finally, socketthe buffer data is then sent to the card go.
Similarly, the Figure is simple:

 

mmap


使用mmap替代read很明显减少了一次拷贝,当拷贝数据量很大时,无疑提升了效率。但是使用mmap是有代价的。当你使用mmap时,你可能会遇到一些隐藏的陷阱。例如,当你的程序map了一个文件,但是当这个文件被另一个进程截断(truncate)时, write系统调用会因为访问非法地址而被SIGBUS信号终止。SIGBUS信号默认会杀死你的进程并产生一个coredump,如果你的服务器这样被中止了,那会产生一笔损失。

 

通常我们使用以下解决方案避免这种问题:

  1. 为SIGBUS信号建立信号处理程序
    当遇到SIGBUS信号时,信号处理程序简单地返回,write系统调用在被中断之前会返回已经写入的字节数,并且errno会被设置成success,但是这是一种糟糕的处理办法,因为你并没有解决问题的实质核心。
  2. 使用文件租借锁
    通常我们使用这种方法,在文件描述符上使用租借锁,我们为文件向内核申请一个租借锁,当其它进程想要截断这个文件时,内核会向我们发送一个实时的RT_SIGNAL_LEASE信号,告诉我们内核正在破坏你加持在文件上的读写锁。这样在程序访问非法内存并且被SIGBUS杀死之前,你的write系统调用会被中断。write会返回已经写入的字节数,并且置errno为success。

我们应该在mmap文件之前加锁,并且在操作完文件后解锁:

if(fcntl(diskfd, F_SETSIG, RT_SIGNAL_LEASE) == -1) {
    perror("kernel lease set signal");
    return -1;
}
/* l_type can be F_RDLCK F_WRLCK  加锁*/
/* l_type can be  F_UNLCK 解锁*/
if(fcntl(diskfd, F_SETLEASE, l_type)){
    perror("kernel lease set type");
    return -1;
}

使用sendfile#####

从2.1版内核开始,Linux引入了sendfile来简化操作:

#include<sys/sendfile.h>
ssize_t sendfile(int out_fd, int in_fd, off_t *offset, size_t count);

系统调用sendfile()在代表输入文件的描述符in_fd和代表输出文件的描述符out_fd之间传送文件内容(字节)。描述符out_fd必须指向一个套接字,而in_fd指向的文件必须是可以mmap的。这些局限限制了sendfile的使用,使sendfile只能将数据从文件传递到套接字上,反之则不行。
使用sendfile不仅减少了数据拷贝的次数,还减少了上下文切换,数据传送始终只发生在kernel space

sendfile系统调用过程

在我们调用sendfile时,如果有其它进程截断了文件会发生什么呢?假设我们没有设置任何信号处理程序,sendfile调用仅仅返回它在被中断之前已经传输的字节数,errno会被置为success。如果我们在调用sendfile之前给文件加了锁,sendfile的行为仍然和之前相同,我们还会收到RT_SIGNAL_LEASE的信号。

目前为止,我们已经减少了数据拷贝的次数了,但是仍然存在一次拷贝,就是页缓存到socket缓存的拷贝。那么能不能把这个拷贝也省略呢?

借助于硬件上的帮助,我们是可以办到的。之前我们是把页缓存的数据拷贝到socket缓存中,实际上,我们仅仅需要把缓冲区描述符传到socket缓冲区,再把数据长度传过去,这样DMA控制器直接将页缓存中的数据打包发送到网络中就可以了。

总结一下,sendfile系统调用利用DMA引擎将文件内容拷贝到内核缓冲区去,然后将带有文件位置和长度信息的缓冲区描述符添加socket缓冲区去,这一步不会将内核中的数据拷贝到socket缓冲区中,DMA引擎会将内核缓冲区的数据拷贝到协议引擎中去,避免了最后一次拷贝。

带DMA的sendfile

 

不过这一种收集拷贝功能是需要硬件以及驱动程序支持的。

使用splice#####

sendfile只适用于将数据从文件拷贝到套接字上,限定了它的使用范围。Linux在2.6.17版本引入splice系统调用,用于在两个文件描述符中移动数据:

#define _GNU_SOURCE         /* See feature_test_macros(7) */
#include <fcntl.h>
ssize_t splice(int fd_in, loff_t *off_in, int fd_out, loff_t *off_out, size_t len, unsigned int flags);

splice调用在两个文件描述符之间移动数据,而不需要数据在内核空间和用户空间来回拷贝。他从fd_in拷贝len长度的数据到fd_out,但是有一方必须是管道设备,这也是目前splice的一些局限性。flags参数有以下几种取值:

  • SPLICE_F_MOVE :尝试去移动数据而不是拷贝数据。这仅仅是对内核的一个小提示:如果内核不能从pipe移动数据或者pipe的缓存不是一个整页面,仍然需要拷贝数据。Linux最初的实现有些问题,所以从2.6.21开始这个选项不起作用,后面的Linux版本应该会实现。
  • ** SPLICE_F_NONBLOCK** :splice 操作不会被阻塞。然而,如果文件描述符没有被设置为不可被阻塞方式的 I/O ,那么调用 splice 有可能仍然被阻塞。
  • ** SPLICE_F_MORE**: 后面的splice调用会有更多的数据。

splice调用利用了Linux提出的管道缓冲区机制, 所以至少一个描述符要为管道。

以上几种零拷贝技术都是减少数据在用户空间和内核空间拷贝技术实现的,但是有些时候,数据必须在用户空间和内核空间之间拷贝。这时候,我们只能针对数据在用户空间和内核空间拷贝的时机上下功夫了。Linux通常利用写时复制(copy on write)来减少系统开销,这个技术又时常称作COW

由于篇幅原因,本文不详细介绍写时复制。大概描述下就是:如果多个程序同时访问同一块数据,那么每个程序都拥有指向这块数据的指针,在每个程序看来,自己都是独立拥有这块数据的,只有当程序需要对数据内容进行修改时,才会把数据内容拷贝到程序自己的应用空间里去,这时候,数据才成为该程序的私有数据。如果程序不需要对数据进行修改,那么永远都不需要拷贝数据到自己的应用空间里。这样就减少了数据的拷贝。写时复制的内容可以再写一篇文章了。。。

除此之外,还有一些零拷贝技术,比如传统的Linux I/O中加上O_DIRECT标记可以直接I/O,避免了自动缓存,还有尚未成熟的fbufs技术,本文尚未覆盖所有零拷贝技术,只是介绍常见的一些,如有兴趣,可以自行研究,一般成熟的服务端项目也会自己改造内核中有关I/O的部分,提高自己的数据传输速率。

Guess you like

Origin blog.csdn.net/u012501054/article/details/90240611