当前位置:网站首页>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
边栏推荐
猜你喜欢

5mo3 UHI HII HII 17mn4 19Mn6 executive standard

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

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

Only in China! Alicloud container service enters the Forrester leader quadrant

如何一站式高效管理固定资产?

Advanced level of C language pointer (Part 1)

How to solve the problem of fixed assets management and inventory?

Centos7 shell script one click installation of JDK, Mongo, Kafka, FTP, PostgreSQL, PostGIS, pgrouting

"Analysis of 43 cases of MATLAB neural network": Chapter 30 design of combined classifier based on random forest idea - breast cancer diagnosis

TypeError: __ init__ () got an unexpected keyword argument ‘autocompletion‘
随机推荐
公网集群对讲+GPS可视追踪|助力物流行业智能化管理调度
基础:2.图像的本质
Glitch free clock switching technology
factory type_ Id:: create process resolution
软件工程师面试刷题网站、经验方法
Set the type of the input tag to number, and remove the up and down arrows
Which method is good for the management of fixed assets of small and medium-sized enterprises?
Full mark standard for sports items in the high school entrance examination (Shenzhen, Anhui and Hubei)
个人装修笔记
Interrupt sharing variables with other functions and protection of critical resources
TV size and viewing distance
Bimianhongfu queren()
如何一站式高效管理固定资产?
Differences among tasks, threads and processes
Personal decoration notes
Shell script case in statement
Advanced C language pointer (Part 2)
Screenshot tips
JCL 和 SLF4J
Serialization, listening, custom annotation