In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/03 Report--
This article introduces the relevant knowledge of "what is zero-copy technology". In the operation of actual cases, many people will encounter such a dilemma. Then let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!
What is zero copy?
1. Start with a case
To explain this concept, let's start with a requirement that one day a leader sent you a task to complete a Mini Program that reads data from a file and transmits it to the network. The code is simple:
First of all, we find this file in our operating system, then read the data to the buffer, and finally send the buffer data to the network.
The code is very simple, and now let's consider the whole process of transferring data from the computer to the network:
Now we can see that the whole process of 1-> 2-> 3-> 4 has gone through four copies, but the real resource-consuming and time-wasting are the second and third times, because both times need to go through our CPU copy, and we also need to switch back and forth between kernel mode and user mode. Think about how valuable our CPU resources are and how many tasks we have to deal with. And copy a lot of data. Wouldn't it be nice to get rid of these two copies of CPU! It can not only save CPU resources, but also avoid the switching between kernel mode and user mode.
First of all, let's talk about the difference between user mode and kernel mode:
When executing in user mode, the memory space and objects that the process can access are limited, and the processor it occupies can be preempted.
When executing in kernel mode, all memory space and objects can be accessed, and the processor occupied is not allowed to be preempted.
2. Optimization scheme
To remove the second and third copies, Linux developers have long noticed this problem, so in the linux 2.1kernel, the action of "data is copy to socket buffer" is added, so our javaNIO can directly call the transferTo () method to achieve this phenomenon.
Now, I feel that the performance resources have been greatly improved, but it is not perfect yet. Because these three copies also use the copy technology of CPU, that is, the second time. But don't worry. Linux developers are more forward-thinking than we are.
3. Zero copy optimization scheme
The Linux2.4 kernel is optimized and instead descriptors containing only information about the location and length of the data are appended to the socket buffer buffer. The DMA engine transfers data directly from the kernel buffer to the protocol engine (protocol engine), eliminating the last CPU copy. After the above process, the data is transferred from the disk after only 2 times of copy. This is the real Zero-Copy.
Note: the zero copy here is actually divided according to the kernel state. There is no CPU copy here. The data has been copied zero times in the user state, so it is called zero copy, but it does not mean that there is no copy.
OK . Now that we know what zero-copy technology is, let's talk about those data structures that use zero-copy technology.
2. Where will zero copy technology be used?
1. NIO of java
First of all, java is to pave the way for the following netty. The Channel in Java NIO is equivalent to the buffer of the operating system's kernel space (kernel space), while the buffer (Buffer) corresponds to the user buffer (user buffer) of the operating system's user space (user space).
Out-of-heap memory (DirectBuffer) needs to be reclaimed manually by the application after use, while data from heap memory (HeapBuffer) may be automatically reclaimed during GC. Therefore, when using HeapBuffer to read and write data, in order to avoid buffer data loss due to GC, NIO will first copy the data inside HeapBuffer to the local memory (native memory) in a temporary DirectBuffer. This copy involves a call to sun.misc.Unsafe.copyMemory (), and the implementation principle is similar to memcpy (). Finally, the memory address of the temporarily generated data inside the DirectBuffer is passed to the Imax O calling function, which avoids accessing the Java object to handle Imax O read and write.
(1) MappedByteBuffer
MappedByteBuffer is an implementation of NIO based on the zero-copy approach of memory mapping (mmap), which means that the size-sized area of a file starting from the position location is mapped to a memory image file. This adds the address mapping instead of copying it.
(2) DirectByteBuffer
The object reference of DirectByteBuffer is located in the heap of the Java memory model. JVM can allocate and recycle the objects of DirectByteBuffer, which is the concrete implementation class of MappedByteBuffer. Therefore, it also has zero-copy technology.
(3) FileChannel
FileChannel defines two abstract methods, transferFrom () and transferTo (), which implement data transmission by establishing a connection between channels.
Let's look directly at the version of Linux2.4, the socket buffer has been adjusted, and DMA has a collection function.
(1) DMA is copied to the kernel buffer
(2) add the descriptor of the location and length of the data to the kernel space (socket buffer)
(3) DMA copies data from the kernel to the protocol engine
This replication process is a zero copy process.
2 、 Netty
The zero copy in Netty is quite different from the zero copy at the operating system level mentioned above. The zero copy of Netty we are talking about is entirely based on the user mode (Java level).
(1) Netty wraps the tranferTo () method of FileChannel through the DefaultFileRegion class, which is equivalent to making a zero copy indirectly through java.
(2) our data transmission is generally realized through TCP/IP protocol. In practical application, it is very possible that a complete message is divided into multiple data packets for network transmission, and a single data packet is meaningless to you. Only when these data packets form a complete message can you make correct processing. Netty can combine these packets into a complete message for you to use in a zero-copy way.
At this point, the scope of zero copy is only in user space. So how does Netty work? For this reason, we need to find the interface of Netty for data transmission, this interface must contain a function that can achieve zero copy, this interface is ChannelBuffer.
Since there is an interface, there must be an implementation class. One of the most important implementation classes is CompositeChannelBuffer. The main function of this class is to make multiple ChannelBuffer into a virtual ChannelBuffer to operate.
Why is it virtual? because CompositeChannelBuffer doesn't really combine multiple ChannelBuffer, but just saves their references, so it avoids copying data and implements Zero Copy.
(3) ByteBuf can wrap byte array, ByteBuf and ByteBuffer into a ByteBuf object through wrap operation, thus avoiding copy operation.
(4) ByteBuf supports slice operation, so ByteBuf can be decomposed into multiple ByteBuf that share the same storage area, avoiding the copy of memory.
3 、 kafka
The index file of Kafka uses mmap + write mode, and the data file uses sendfile mode. It is suitable for data persistence and transfer of high-throughput large files such as Syslog messages.
If there are 10 consumers, in the traditional way, the number of data replications is 4: 10, 40, while using "zero copy technology" only takes 1: 10, 11, once to copy from disk to page cache, 10 times means that 10 consumers each read the page cache.
This is the end of what is Zero copy Technology. Thank you for your reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.