当前位置:网站首页>NiO zero copy
NiO zero copy
2022-07-01 08:56:00 【Ma Nong Xiao Wang】
In traditional IO in , The process of initiating a read request
There are two context switches , And two copies of the content . The contents are moved from disk to kernel buffer and from kernel buffer to user buffer through cpu To complete , This stage cpu Can't do anything else . User threads are in a blocking state during this process IO Operation is completed .
introduce DMA
introduce DMA After that, the task of copying the contents from the disk to the memory cache is entrusted to DMA,DMA During copying content CPU Can do other things , But copying from the kernel cache to the user cache is still CPU To complete . Join in DMA After that, though CPU The operation of data handling is reduced , But there are still two context switches and two copies of the content .
Let's take a look at the traditional IO Operation details if data is to be read and transmitted over the network
It can be seen that four context switches and four content copies have taken place in the process of reading files and network transmission . If you want to optimize IO We can think from these two aspects .1、 Reduce the number of context switches .2、 Reduce the number of copies of content .
Realize zero copy
There are usually two ways to realize zero copy :mmap + write、sendfile
1、mmap + write The way
This way is to create a shared mapping cache of kernel and user space . The user thread can directly manipulate the content in the mapped cache without copying the content to the user space , This reduces one copy of the content . Call when writing to the network card write You can copy content directly from the mapping cache to Socket Buffer zone . This reduces copying content from the kernel cache to the user cache and from the user cache to Socket Two copies of cache , Added a copy of content from the kernel cache to Socket Buffer zone .
2、sendfile
Although the first method reduces one copy of content , But it still needs four context switches .sendfile(int out_fd, int in_fd, off_t *offset, size_t count) Method combines the read operation from two independent operations into one operation that copies the content from the source to the destination at one time . In this way, you only need to call sendfile Method to switch from user mode to kernel mode , After the transmission is successful, it is transferred from the kernel state to the user state . Two context switches are reduced . But it still needs to be cpu Participate in copying content from the kernel cache to Socket Buffer zone .
True zero copy should not be required CPU Carry out handling work . therefore sendfile When the method is called, if ⽹ card ⽀ a scatter-gather Feature, you can copy the content directly from the kernel cache to the network card . So you don't have to cpu The real zero copy is realized by the transportation of .
NIO Zero copy of
Use nio call transferTo() Method can achieve zero copy . The bottom layer is to call sendfile The way to achieve zero copy .
How to use nio To realize zero copy of files is not enough here . It is worth noting that nio A direct memory will be created when transferring files , Directly because it does not belong to jvm Cannot be gc Recycling , Therefore, the cost of creating and recycling direct memory is relatively high , But the read-write performance is relatively high .
Here are some leaders who summarized the use of NIO and IO Compare the performance of file operation : Big file copy , try NIO Memory mapping for _androidstarjack The blog of -CSDN Blog
NIO How to recycle direct memory :JVM project ( Nine )- Direct memory _IT- Daniel's blog -CSDN Blog _jvm Direct memory
边栏推荐
- 集团公司固定资产管理的痛点和解决方案
- Graduation season, I want to tell you
- pcl_ Viewer command
- Personal decoration notes
- 猿人学第20题(题目会不定时更新)
- 5mo3 UHI HII HII 17mn4 19Mn6 executive standard
- Nacos - 配置管理
- 19Mn6 German standard pressure vessel steel plate 19Mn6 Wugang fixed binding 19Mn6 chemical composition
- Promise异步编程
- Embedded Engineer Interview Question 3 Hardware
猜你喜欢
VSYNC+三重缓存机制+Choreographer
Centos7 shell script one click installation of JDK, Mongo, Kafka, FTP, PostgreSQL, PostGIS, pgrouting
3. Detailed explanation of Modbus communication protocol
【MFC开发(16)】树形控件Tree Control
Insert mathematical formula in MD document and mathematical formula in typora
Guidelines and principles of did
Glitch Free时钟切换技术
Only in China! Alicloud container service enters the Forrester leader quadrant
How to manage fixed assets well? Easy to point and move to provide intelligent solutions
V79.01 Hongmeng kernel source code analysis (user mode locking) | how to use the fast lock futex (Part 1) | hundreds of blogs analyze the openharmony source code
随机推荐
猿人学第20题(题目会不定时更新)
It technology ebook collection
Shell脚本-case in语句
Embedded Engineer Interview Question 3 Hardware
R语言观察日志(part24)--初始化设置
Jetson nano installs tensorflow GPU and problem solving
Jetson Nano 安装TensorFlow GPU及问题解决
Centos7 shell script one click installation of JDK, Mongo, Kafka, FTP, PostgreSQL, PostGIS, pgrouting
Screenshot tips
大型工厂设备管理痛点和解决方案
How to effectively align team cognition
避免按钮重复点击的小工具bimianchongfu.queren()
Yidian Yidong helps enterprises to efficiently manage equipment and improve equipment utilization
Advanced C language pointer (Part 2)
Software Engineer Interview Question brushing website and experience method
中小企业固定资产管理办法哪种好?
VSYNC+三重缓存机制+Choreographer
Memory size end
Shell脚本-特殊变量:Shell $#、$*、[email protected]、$?、$$
Ape anthropology topic 20 (the topic will be updated from time to time)