当前位置:网站首页>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

原网站

版权声明
本文为[Ma Nong Xiao Wang]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/182/202207010823116948.html