当前位置:网站首页>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
边栏推荐
- How do ordinary college students get offers from big factories? Ao Bing teaches you one move to win!
- Common analog keys of ADB shell: keycode
- LeetCode_ DFS_ Medium_ 695. Maximum area of the island
- [tensorflow] sharing mechanism
- Android clear app cache
- C语言之字符串与字符数组的区别
- 2020最新Android大厂高频面试题解析大全(BAT TMD JD 小米)
- Logstash数据处理服务的输入插件Input常见类型以及基本使用
- [point cloud series] unsupervised multi task feature learning on point clouds
- Common types and basic usage of input plug-in of logstash data processing service
猜你喜欢
Request和Response及其ServletContext总结
Ding ~ your scholarship has arrived! C certified enterprise scholarship list released
9419 page analysis of the latest first-line Internet Android interview questions
[point cloud series] pointfilter: point cloud filtering via encoder decoder modeling
Summary of request and response and their ServletContext
Cross carbon market and Web3 to achieve renewable transformation
web三大组件之Servlet
[point cloud series] unsupervised multi task feature learning on point clouds
Example interview | sun Guanghao: College Club grows and starts a business with me
Logstash数据处理服务的输入插件Input常见类型以及基本使用
随机推荐
CSDN College Club "famous teacher college trip" -- Hunan Normal University Station
这个SQL语名是什么意思
[barycentric coordinate interpolation, perspective correction interpolation] principle and usage opinions
X509 parsing
Riscv MMU overview
Explanation of input components in Chapter 16
Ai21 labs | standing on the shoulders of giant frozen language models
What do the raddr and rport in webrtc ice candidate mean?
web三大组件之Filter、Listener
SHA512 / 384 principle and C language implementation (with source code)
Common types and basic usage of input plug-in of logstash data processing service
【快排】215. 数组中的第K个最大元素
Scons build embedded ARM compiler
"Play with Lighthouse" lightweight application server self built DNS resolution server
Hbuilderx + uniapp packaging IPA submission app store stepping on the pit
The interviewer dug a hole for me: how many concurrent TCP connections can a single server have?
Isparta is a tool that generates webp, GIF and apng from PNG and supports the transformation of webp, GIF and apng
你和42W奖金池,就差一次“长沙银行杯”腾讯云启创新大赛!
Playwright controls local Google browsing to open and download files
[point cloud series] summary of papers related to implicit expression of point cloud