当前位置:网站首页>零拷贝技术
零拷贝技术
2022-04-23 13:33:00 【TABE_】
这里写目录标题
传统的文件传输
如果服务端要提供文件传输的功能,我们能想到的最简单的方式是:将磁盘上的文件读取出来,然后通过网络协议发送给客户端。
传统 I/O 的工作方式是,数据读取和写入是从用户空间到内核空间来回复制,而内核空间的数据是通过操作系统层面的 I/O 接口从磁盘读取或写入。

首先,期间共发生了 4 次用户态与内核态的上下文切换,因为发生了两次系统调用,一次是 read() ,一次是 write(),每次系统调用都得先从用户态切换到内核态,等内核完成任务后,再从内核态切换回用户态。上下文切换到成本并不小,一次切换需要耗时几十纳秒到几微秒,虽然时间看上去很短,但是在高并发的场景下,这类时间容易被累积和放大,从而影响系统的性能。
其次,还发生了 4 次数据拷贝,其中两次是 DMA 的拷贝,另外两次则是通过 CPU 拷贝的,下面说一下这个过程:第一次拷贝,把磁盘上的数据拷贝到操作系统内核的缓冲区里,这个拷贝的过程是通过 DMA 搬运的。第二次拷贝,把内核缓冲区的数据拷贝到用户的缓冲区里,于是我们应用程序就可以使用这部分数据了,这个拷贝到过程是由 CPU 完成的。第三次拷贝,把刚才拷贝到用户的缓冲区里的数据,再拷贝到内核的 socket 的缓冲区里,这个过程依然还是由 CPU 搬运的。第四次拷贝,把内核的 socket 缓冲区里的数据,拷贝到网卡的缓冲区里,这个过程又是由 DMA 搬运的。
我们只是搬运一份数据,结果却搬运了 4 次,多了很多不必要的开销,,大大降低了系统性能。所以,要想提高文件传输的性能,就需要减少用户态与内核态的上下文切换和内存拷贝的次数。
零拷贝技术
mmap + write
在前面我们知道,read() 系统调用的过程中会把内核缓冲区的数据拷贝到用户的缓冲区里,于是为了减少这一步开销,我们可以用 mmap() 替换 read() 系统调用函数。mmap() 系统调用函数会直接把内核缓冲区里的数据「映射」到用户空间,这样,操作系统内核与用户空间就不需要再进行任何的数据拷贝操作。

具体过程如下:
- 应用进程调用了 mmap() 后,DMA 会把磁盘的数据拷贝到内核的缓冲区里。
- 接着,应用进程跟操作系统内核「共享」这个缓冲区;
- 应用进程再调用 write(),操作系统直接将内核缓冲区的数据拷贝到 socket 缓冲区中,这一切都发生在内核态,由CPU来搬运数据;
- 最后,把内核的 socket 缓冲区里的数据,拷贝到网卡的缓冲区里,这个过程是由 DMA 搬运的。 我们可以得知,通过使用
mmap() 来代替 read(), 可以减少一次数据拷贝的过程。但这还不是最理想的零拷贝,因为仍然需要通过 CPU 把内核缓冲区的数据拷贝到 socket 缓冲区里,而且仍然需要 4 次上下文切换,因为系统调用还是 2 次。
sendfile
在 Linux 内核版本 2.1 中,提供了一个专门发送文件的系统调用函数 sendfile(),函数形式为ssize_t sendfile(int out_fd, int in_fd, off_t *offset, size_t count);它的前两个参数分别是目的端和源端的文件描述符,后面两个参数是源端的偏移量和复制数据的长度,返回值是实际复制数据的长度。
首先,它可以替代前面的 read() 和 write() 这两个系统调用,这样就可以减少一次系统调用,也就减少了 2 次上下文切换的开销。
其次,该系统调用,可以直接把内核缓冲区里的数据拷贝到 socket 缓冲区里,不再拷贝到用户态,这样就只有 2 次上下文切换,和 3 次数据拷贝。如下图:

版权声明
本文为[TABE_]所创,转载请带上原文链接,感谢
https://blog.csdn.net/TABE_/article/details/124354621
边栏推荐
- Hbuilderx + uniapp packaging IPA submission app store stepping on the pit
- Machine learning -- PCA and LDA
- According to the salary statistics of programmers in June 2021, the average salary is 15052 yuan. Are you holding back?
- 解决虚拟机中Oracle每次要设置ip的问题
- Cross carbon market and Web3 to achieve renewable transformation
- 缘结西安 | CSDN与西安思源学院签约,全面开启IT人才培养新篇章
- Vscode tips
- Conflict between Mui picker and drop-down refresh
- [notes de marche]
- Filter and listener of three web components
猜你喜欢
![[barycentric coordinate interpolation, perspective correction interpolation] principle and usage opinions](/img/db/2e7d7f519f97f0c4179a29af8942a6.png)
[barycentric coordinate interpolation, perspective correction interpolation] principle and usage opinions

RTOS mainstream assessment
![[point cloud series] pointfilter: point cloud filtering via encoder decoder modeling](/img/da/02d1e18400414e045ce469425db644.png)
[point cloud series] pointfilter: point cloud filtering via encoder decoder modeling

Request和Response及其ServletContext总结

The interviewer dug a hole for me: what's the use of "/ /" in URI?
![[point cloud series] Introduction to scene recognition](/img/1f/c64712b03ae5b235b5dd3347c3e86a.png)
[point cloud series] Introduction to scene recognition

Learning notes of AMBA protocol

2020最新Android大厂高频面试题解析大全(BAT TMD JD 小米)
![[point cloud series] learning representations and generative models for 3D point clouds](/img/c5/712bd448fa6c0bffc09ce57f6e56b5.png)
[point cloud series] learning representations and generative models for 3D point clouds

C语言之字符串与字符数组的区别
随机推荐
SHA512 / 384 principle and C language implementation (with source code)
TCP 复位gongji原理和实战复现
Aicoco AI frontier promotion (4.23)
面试官给我挖坑:URI中的 “//” 有什么用?
[point cloud series] pointfilter: point cloud filtering via encoder decoder modeling
[point cloud series] so net: self organizing network for point cloud analysis
Innobackupex incremental backup
[tensorflow] sharing mechanism
Isparta is a tool that generates webp, GIF and apng from PNG and supports the transformation of webp, GIF and apng
普通大学生如何拿到大厂offer?敖丙教你一招致胜!
数据仓库—什么是OLAP
"Play with Lighthouse" lightweight application server self built DNS resolution server
CSDN College Club "famous teacher college trip" -- Hunan Normal University Station
Xi'an CSDN signed a contract with Xi'an Siyuan University, opening a new chapter in IT talent training
The difference between string and character array in C language
Conflict between Mui picker and drop-down refresh
Tangent space
Data warehouse - what is OLAP
Part 3: docker installing MySQL container (custom port)
web三大组件之Filter、Listener