当前位置:网站首页>Zero copy technology
Zero copy technology
2022-04-23 13:37:00 【TABE_】
Here's the catalog title
Traditional file transfer
If the server wants to provide the function of file transfer , The simplest way we can think of is : Read out the files on the disk , And then send it to the client through the network protocol .
Tradition I/O The way we work is , Data read and write are copied back and forth from user space to kernel space , And the kernel space data is through the operating system level I/O Interface to read or write from disk .

First , A total of 4 Context switch between secondary user mode and kernel mode , Because there were two system calls , Once it was read() , Once it was write(), Every system call must first switch from user mode to kernel mode , After the kernel completes its task , Then switch from kernel mode to user mode . The cost of context switching is not small , A switch takes tens of nanoseconds to microseconds , Although the time seems short , But in a high concurrency scenario , This kind of time is easy to accumulate and amplify , This will affect the performance of the system .
secondly , It also happened 4 Secondary data copy , Two of them were DMA A copy of the , The other two were through CPU Copy , Let's talk about the process : The first copy , Copy the data on the disk into the buffer of the operating system kernel , The process of copying is through DMA Carrying . Second copy , Copy the data from the kernel buffer to the user's buffer , So our application can use this data , This copy to process is done by CPU Accomplished . The third copy , Just copy the data to the user's buffer , And then copy it to the kernel socket In the buffer of , The process is still by CPU Carrying . The fourth copy , Put the kernel of socket Data in the buffer , Copy to the network card buffer , This process is again caused by DMA Carrying .
We're just carrying a piece of data , It turned out to be carrying 4 Time , A lot of unnecessary expenses ,, Greatly reduces system performance . therefore , To improve the performance of file transfer , You need to reduce the number of context switches and memory copies between user state and kernel state .
Zero copy technology
mmap + write
In front of us, we know ,read() During the system call, the data in the kernel buffer will be copied to the user's buffer , So in order to reduce the cost of this step , We can use mmap() Replace read() System call function .mmap() The system call function will directly put the data in the kernel buffer 「 mapping 」 To user space , such , The operating system kernel and user space do not need any data copy operation .

The specific process is as follows :
- The application process called mmap() after ,DMA Will copy the disk data to the kernel buffer .
- next , Application process and operating system kernel 「 share 」 This buffer ;
- The application process calls again write(), The operating system directly copies the data from the kernel buffer to the socket Buffer zone , All this happens in kernel state , from CPU To carry data ;
- Last , Put the kernel of socket Data in the buffer , Copy to the network card buffer , This process is driven by DMA Carrying . We can see , By using
mmap() Instead of read(), It can reduce the process of one copy of data . But it's not the ideal zero copy , Because it still needs to pass CPU Copy the data from the kernel buffer to socket In the buffer , And still need to 4 Subcontext switch , Because the system call is still 2 Time .
sendfile
stay Linux Kernel version 2.1 in , Provides a special system call function to send files sendfile(), The function form is ssize_t sendfile(int out_fd, int in_fd, off_t *offset, size_t count); Its first two parameters are the file descriptors on the destination side and the source side respectively , The last two parameters are the offset of the source and the length of the copied data , The return value is the length of the actual copied data .
First , It can replace the front read() and write() These two system calls , This reduces the number of system calls , It's less 2 The overhead of sub context switching .
secondly , The system calls , You can directly copy the data in the kernel buffer to socket In the buffer , No longer copy to user mode , This is the only way 2 Subcontext switch , and 3 Secondary data copy . Here's the picture :

版权声明
本文为[TABE_]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/04/202204231333157741.html
边栏推荐
- web三大组件之Filter、Listener
- 【行走的笔记】
- XML
- [indicators] precision, recall
- Using open to open a file in JNI returns a - 1 problem
- 9419页最新一线互联网Android面试题解析大全
- Android clear app cache
- 普通大学生如何拿到大厂offer?敖丙教你一招致胜!
- PyTorch 21. NN in pytorch Embedding module
- 5 tricky activity life cycle interview questions. After learning, go and hang the interviewer!
猜你喜欢

数据仓库—什么是OLAP

Cross carbon market and Web3 to achieve renewable transformation

Lpddr4 notes

顶级元宇宙游戏Plato Farm,近期动作不断利好频频

切线空间(tangent space)

Android clear app cache

Ding ~ your scholarship has arrived! C certified enterprise scholarship list released

Why do you need to learn container technology to engage in cloud native development

【重心坐标插值、透视矫正插值】原理以及用法见解

Common types and basic usage of input plug-in of logstash data processing service
随机推荐
数据仓库—什么是OLAP
切线空间(tangent space)
@优秀的你!CSDN高校俱乐部主席招募!
鸿蒙系统是抄袭?还是未来?3分钟听完就懂的专业讲解
普通大学生如何拿到大厂offer?敖丙教你一招致胜!
叮~ 你的奖学金已到账!C认证企业奖学金名单出炉
ESP32 VHCI架构传统蓝牙设置scan mode,让设备能被搜索到
5 tricky activity life cycle interview questions. After learning, go and hang the interviewer!
Android clear app cache
解决虚拟机中Oracle每次要设置ip的问题
为什么从事云原生开发需要学习容器技术
TIA博途中基於高速計數器觸發中斷OB40實現定點加工動作的具體方法示例
2021年6月程序员工资统计,平均15052元,你拖后腿了吗?
The interviewer dug a hole for me: how many concurrent TCP connections can a single server have?
[point cloud series] relationship based point cloud completion
[Journal Conference Series] IEEE series template download guide
Hbuilderx + uniapp packaging IPA submission app store stepping on the pit
The difference between string and character array in C language
What do the raddr and rport in webrtc ice candidate mean?
SAP UI5 应用开发教程之七十二 - SAP UI5 页面路由的动画效果设置试读版