当前位置:网站首页>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
边栏推荐
- 大型工厂设备管理痛点和解决方案
- How to effectively align team cognition
- Matlab tips (16) consistency verification of matrix eigenvector eigenvalue solution -- analytic hierarchy process
- Nacos - 服务发现
- Shell脚本-select in循环
- Common interview questions for embedded engineers 2-mcu_ STM32
- LogBack
- win7 pyinstaller打包exe 后报错 DLL load failed while importing _socket:参数错误
- AVL树的理解和实现
- 5mo3 UHI HII HII 17mn4 19Mn6 executive standard
猜你喜欢

猿人学第20题(题目会不定时更新)

Nacos - 配置管理

NIO-零拷贝

如何解决固定资产管理和盘点的难题?

中小企业固定资产管理办法哪种好?

一文纵览主流 NFT 市场平台版税、服务费设计

Matlab tips (16) consistency verification of matrix eigenvector eigenvalue solution -- analytic hierarchy process

Embedded Engineer Interview Question 3 Hardware

It is designed with high bandwidth, which is almost processed into an open circuit?

电脑小技巧
随机推荐
Installing Oracle EE
TypeError: __ init__ () got an unexpected keyword argument ‘autocompletion‘
1.jetson与摄像头的对接
避免按钮重复点击的小工具bimianchongfu.queren()
jeecg 重启报40001
目标检测的yolov3、4、5、6总结
Embedded Engineer Interview frequently asked questions
3. Detailed explanation of Modbus communication protocol
Public network cluster intercom +gps visual tracking | help the logistics industry with intelligent management and scheduling
Interrupt sharing variables with other functions and protection of critical resources
Graduation season, I want to tell you
R语言观察日志(part24)--初始化设置
ARM v7的体系结构A、R、M区别,分别应用在什么领域?
It is designed with high bandwidth, which is almost processed into an open circuit?
Redis -- lattice connects to redis cluster
Shell脚本-变量的定义、赋值和删除
日常办公耗材管理解决方案
Flink interview questions
LogBack
通过 代码实例 理解 浅复制 与 深复制