当前位置:网站首页>传统的IO存在什么问题?为什么引入零拷贝的?
传统的IO存在什么问题?为什么引入零拷贝的?
2022-06-25 06:41:00 【詹Sir(开源字节)】
传统的IO存在什么问题?为什么引入零拷贝的?
如果服务端要提供文件传输的功能,我们能想到的最简单的方式是:将磁盘上的文件读取出来,然后通过网络协议发送给客户端。
传统 I/O 的工作方式是,数据读取和写入是从用户空间到内核空间来回复制,而内核空间的数据是通过操作系统层面的 I/O 接口从磁盘读取或写入。
代码通常如下,一般会需要两个系统调用:
read(file, tmp_buf, len); write(socket, tmp_buf, len);
代码很简单,虽然就两行代码,但是这里面发生了不少的事情。
首先,期间共发生了 4 次用户态与内核态的上下文切换,因为发生了两次系统调用,一次是 read() ,一次是 write(),每次系统调用都得先从用户态切换到内核态,等内核完成任务后,再从内核态切换回用户态。
上下文切换到成本并不小,一次切换需要耗时几十纳秒到几微秒,虽然时间看上去很短,但是在高并发的场景下,这类时间容易被累积和放大,从而影响系统的性能。
其次,还发生了 4 次数据拷贝,其中两次是 DMA 的拷贝,另外两次则是通过 CPU 拷贝的,下面说一下这个过程:
- 第一次拷贝,把磁盘上的数据拷贝到操作系统内核的缓冲区里,这个拷贝的过程是通过 DMA 搬运的。
- 第二次拷贝,把内核缓冲区的数据拷贝到用户的缓冲区里,于是我们应用程序就可以使用这部分数据了,这个拷贝到过程是由 CPU 完成的。
- 第三次拷贝,把刚才拷贝到用户的缓冲区里的数据,再拷贝到内核的 socket 的缓冲区里,这个过程依然还是由 CPU 搬运的。
- 第四次拷贝,把内核的 socket 缓冲区里的数据,拷贝到网卡的缓冲区里,这个过程又是由 DMA 搬运的。
我们回过头看这个文件传输的过程,我们只是搬运一份数据,结果却搬运了 4 次,过多的数据拷贝无疑会消耗 CPU 资源,大大降低了系统性能。
这种简单又传统的文件传输方式,存在冗余的上文切换和数据拷贝,在高并发系统里是非常糟糕的,多了很多不必要的开销,会严重影响系统性能。
所以,要想提高文件传输的性能,就需要减少「用户态与内核态的上下文切换」和「内存拷贝」的次数。
如若转载,请注明出处:开源字节 https://sourcebyte.cn/article/169.html
边栏推荐
- realsense d455 semantic_slam实现语义八叉树建图
- 【蒸馏】PointDistiller: Structured Knowledge DistillationTowards Efficient and Compact 3D Detection
- Chuantu microelectronics breaks through the high-end isolator analog chip market with ca-is3062w
- C#入门教程
- Path planner based on time potential function in dynamic environment
- Can I open a stock account with a compass? Is it safe?
- LeetCode_哈希表_中等_454.四数相加 II
- Cglib dynamic proxy
- C get the version number of exe - file version and assembly version
- 【QT】qtcreator便捷快捷键以及QML介绍
猜你喜欢
CPDA|数据分析师成长之路如何起步?
Find out what informatization is, and let enterprises embark on the right path of transformation and upgrading
【QT】Qt 5 的程序:打印文档
Elk + filebeat log parsing, log warehousing optimization, logstash filter configuration attribute
Chuantu microelectronics high speed and high performance rs-485/422 transceiver series
OAuth 2.0一键登录那些事
Sichuan earth microelectronics 8-channel isolated digital input receiver
搞清信息化是什么,让企业转型升级走上正确的道路
微信小程序开通客服消息功能开发
点云智绘在智慧工地中的应用
随机推荐
What if there is no point in data visualization?
Without "rice", you can cook "rice". Strategy for retrieving missing ground points under airborne lidar forest using "point cloud intelligent mapping"
C get the version number of exe - file version and assembly version
Runtime - Methods member variable, cache member variable
STL tutorial 4- input / output stream and object serialization
一次弄清楚 Handler 可能导致的内存泄漏和解决办法
Different paths ii[dynamic planning improvement for DFS]
Four software 2021-10-14 suitable for beginners to draw PCB
NSIS silent installation vs2013 runtime
ELK + filebeat日志解析、日志入库优化 、logstash过滤器配置属性
OpenCV每日函数 结构分析和形状描述符(8) fitLine函数 拟合直线
STL教程4-输入输出流和对象序列化
搞清信息化是什么,让企业转型升级走上正确的道路
opencv最小值滤波(不局限于图像)
Sichuan Tuwei ca-if1051 can transceiver has passed aec-q100 grade 1 certification
Evolution of Alibaba e-commerce architecture
realsense d455 semantic_ Slam implements semantic octree mapping
How to use printf of 51 single chip microcomputer
国外LEAD域名邮箱获取途径
Research on 3D model retrieval method based on two channel attention residual network - Zhou Jie - paper notes