当前位置:网站首页>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
边栏推荐
- Hbuilderx + uniapp packaging IPA submission app store stepping on the pit
- 面试官给我挖坑:单台服务器并发TCP连接数到底可以有多少 ?
- X509 parsing
- 【行走的笔记】
- UEFI learning 01-arm aarch64 compilation, armplatformpripeicore (SEC)
- 【官宣】长沙软件人才实训基地成立!
- [point cloud series] so net: self organizing network for point cloud analysis
- SHA512 / 384 principle and C language implementation (with source code)
- The filter() traverses the array, which is extremely friendly
- 普通大学生如何拿到大厂offer?敖丙教你一招致胜!
猜你喜欢
[point cloud series] so net: self organizing network for point cloud analysis
数据仓库—什么是OLAP
Ai21 labs | standing on the shoulders of giant frozen language models
Analysis of the latest Android high frequency interview questions in 2020 (BAT TMD JD Xiaomi)
Servlet of three web components
Interface idempotency problem
Nodejs + Mysql realize simple registration function (small demo)
MySQL 8.0.11下载、安装和使用可视化工具连接教程
2020最新Android大厂高频面试题解析大全(BAT TMD JD 小米)
“湘见”技术沙龙 | 程序员&CSDN的进阶之路
随机推荐
[point cloud series] unsupervised multi task feature learning on point clouds
Nodejs + websocket cycle small case
Launcher hides app icons that do not need to be displayed
MySQL 8.0.11下载、安装和使用可视化工具连接教程
ESP32 VHCI架构传统蓝牙设置scan mode,让设备能被搜索到
Hbuilderx + uniapp packaging IPA submission app store stepping on the pit
TIA博途中基于高速计数器触发中断OB40实现定点加工动作的具体方法示例
web三大组件之Filter、Listener
[tensorflow] sharing mechanism
On the bug of JS regular test method
Machine learning -- naive Bayes
Mysql数据库的卸载
Common types and basic usage of input plug-in of logstash data processing service
SSM整合之pom.xml
Analysis of the latest Android high frequency interview questions in 2020 (BAT TMD JD Xiaomi)
SAP ui5 application development tutorial 72 - trial version of animation effect setting of SAP ui5 page routing
Nodejs + Mysql realize simple registration function (small demo)
Vscode tips
Playwright contrôle l'ouverture de la navigation Google locale et télécharge des fichiers
web三大组件之Servlet