当前位置:网站首页>零拷贝技术
零拷贝技术
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
边栏推荐
- Launcher hides app icons that do not need to be displayed
- LeetCode_DFS_中等_695.岛屿的最大面积
- Summary of request and response and their ServletContext
- Comparison and summary of applicable scenarios of Clickhouse and MySQL database
- MySQL5.5安装教程
- Imx6ull QEMU bare metal tutorial 1: GPIO, iomux, I2C
- What does the SQL name mean
- 鸿蒙系统是抄袭?还是未来?3分钟听完就懂的专业讲解
- [Journal Conference Series] IEEE series template download guide
- You and the 42W bonus pool are one short of the "Changsha bank Cup" Tencent yunqi innovation competition!
猜你喜欢

Machine learning -- PCA and LDA
![[point cloud series] deepmapping: unsupervised map estimation from multiple point clouds](/img/a2/00c2745ec8469a719b180bdd85d1d2.png)
[point cloud series] deepmapping: unsupervised map estimation from multiple point clouds

9419 page analysis of the latest first-line Internet Android interview questions

The first lesson is canvas, showing a small case
![[point cloud series] so net: self organizing network for point cloud analysis](/img/3c/6136b7aa322c42089f40ce13a2d747.png)
[point cloud series] so net: self organizing network for point cloud analysis

LeetCode_DFS_中等_695.岛屿的最大面积
![[point cloud series] foldingnet: point cloud auto encoder via deep grid deformation](/img/c0/cc29ae6948dbe42954cd9da326ef77.png)
[point cloud series] foldingnet: point cloud auto encoder via deep grid deformation

Example interview | sun Guanghao: College Club grows and starts a business with me

Loading and using image classification dataset fashion MNIST in pytorch

On the bug of JS regular test method
随机推荐
Armv8m (cortex M33) MPU actual combat
The interviewer dug a hole for me: how many concurrent TCP connections can a single server have?
NPM err code 500 solution
Explanation of input components in Chapter 16
Loading and using image classification dataset fashion MNIST in pytorch
Android clear app cache
SSM整合之pom.xml
Example interview | sun Guanghao: College Club grows and starts a business with me
【重心坐标插值、透视矫正插值】原理以及用法见解
榜样专访 | 孙光浩:高校俱乐部伴我成长并创业
SHA512 / 384 principle and C language implementation (with source code)
集简云 x 飞书深诺,助力企业运营部实现自动化办公
Machine learning -- model optimization
Nodejs + Mysql realize simple registration function (small demo)
Nodejs + websocket cycle small case
C语言之字符串与字符数组的区别
Bottomsheetdialogfragment + viewpager + fragment + recyclerview sliding problem
[walking notes]
playwright控制本地穀歌瀏覽打開,並下載文件
Introduction to metalama 4 Use fabric to manipulate items or namespaces