Linux零复制:使用vmsplice在两个进程之间传输内存页面 [英] Linux Zero-Copy: Transfer memory pages between two processes with vmsplice

查看:159
本文介绍了Linux零复制:使用vmsplice在两个进程之间传输内存页面的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当前,我正在尝试了解splice/vmsplice的值.关于IPC的用例,我偶然发现了关于stackoverflow的以下答案: https://stackoverflow.com/a/1350550/1305501

Currently, I am trying to understand the value of splice/vmsplice. Regarding the use case of IPC, I stumbled upon the following answer on stackoverflow: https://stackoverflow.com/a/1350550/1305501

问题:如何使用vmsplice将内存页从一个进程转移到另一个进程,而不复制数据(即零副本)?

Question: How to transfer memory pages from one process to another process using vmsplice without copying data (i.e. zero-copy)?

上面提到的答案声称这是可能的.但是,它不包含任何源代码.如果我正确理解了vmsplice的文档,则以下函数将在内存正确分配和对齐的情况下将内存页面转移到管道(内核缓冲区)中,而不进行复制.为了便于演示,省略了错误处理.

The answer mentioned above claims that it is possible. However, it doesn't contain any source code. If I understand the documentation of vmsplice correctly, the following function will transfer the memory pages into a pipe (kernel buffer) without copying, if the memory is properly allocated and aligned. Error handling omitted for the ease of presentation.

// data is aligned to page boundaries,
// and length is a multiple of the page size
void transfer_to_pipe(int pipe_out, char* data, size_t length)
{
    size_t offset = 0;
    while (offset < length) {
        struct iovec iov { data + offset, length - offset };
        offset += vmsplice(pipe_out, &iov, 1, SPLICE_F_GIFT);
    }
}

但是如何从用户空间访问内存页面而不进行复制呢?显然,以下方法不起作用:

But how can the memory pages be accessed from user space without copying? Apparently the following methods don't work:

  • vmsplice:此功能也可以用于反方向.但是根据
  • vmsplice: This function can also be used for the reverse direction. But according to the comments in the kernel sources, the data will be copied.
  • read: I can imagine, that this function does some magic if the memory is properly aligned, but I doubt it.
  • mmap: Not possible on pipe. But is there some kind of virtual file that can be used instead, i.e. splice the memory pages to the virtual file and mmap it?
  • ... ?

vmsplice完全不可能吗?

推荐答案

如R ..所述,您只需要将fd传递给接收进程

As R.. mentioned, you only need to pass the fd to the receiving process somehow and on the other side use it as a normal fd.

实际上,您必须在发送方使用vmsplice()将缓冲区映射到管道,而在接收方使用splice()在管道的另一端.在此处查看示例.

edit: Actually, you have to use vmsplice() on the sending side to map the buffer to the pipe and splice() on the receiving side on the other end of the pipe. See an example here.

另一种选择是使用共享的映射.

Another choice would be to use a shared mmap-ing.

这篇关于Linux零复制:使用vmsplice在两个进程之间传输内存页面的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆