当前位置:网站首页>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
边栏推荐
- NFT监管要点和海外政策
- MySQL8.0学习记录17 -Create Table
- 大型工厂设备管理痛点和解决方案
- Nacos - 配置管理
- What are the differences between the architecture a, R and m of arm V7, and in which fields are they applied?
- Memory size end
- Pain points and solutions of fixed assets management of group companies
- 1.jetson与摄像头的对接
- Programming with C language: calculate with formula: e ≈ 1+1/1+ 1/2! …+ 1/n!, Accuracy is 10-6
- 如何高效拉齐团队认知
猜你喜欢

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

Nacos - gestion de la configuration

jeecg 重启报40001

Nacos - 配置管理

Brief introduction to AES

How can enterprises and developers take the lead in the outbreak of cloud native landing?

factory type_ Id:: create process resolution
![[MFC development (17)] advanced list control list control](/img/e8/24c52cb51defc6c96b43c2ef3232ff.png)
[MFC development (17)] advanced list control list control

Bimianhongfu queren()

你了解数据是如何存储的吗?(C整型和浮点型两类)
随机推荐
如何高效拉齐团队认知
又到年中,固定资产管理该何去何从?
Personal decoration notes
个人装修笔记
Performance improvement 2-3 times! The second generation Kunlun core server of Baidu AI Cloud was launched
如何做好固定资产管理?易点易动提供智能化方案
Shell script - special variables: shell $, $*, [email protected], $$$
R语言观察日志(part24)--初始化设置
TV size and viewing distance
Shell script -read command: read data entered from the keyboard
1.jetson与摄像头的对接
Shell script -if else statement
FreeRTOS学习简易笔记
Brief introduction to AES
AVL树的理解和实现
如何一站式高效管理固定资产?
Programming with C language: calculate with formula: e ≈ 1+1/1+ 1/2! …+ 1/n!, Accuracy is 10-6
固定资产管理系统让企业动态掌握资产情况
Shell脚本-select in循环
Advanced level of C language pointer (Part 1)