当前位置:网站首页>Talk about the secret of high performance of message queue -- zero copy technology
Talk about the secret of high performance of message queue -- zero copy technology
2022-07-02 09:07:00 【Programmer Qiqi】
Preface
RocketMQ Why so soon? 、Kafka Why so soon? ? Using zero copy technology ? What is zero copy technology , Is there any difference in zero copy technology between them ?
Why zero copy is needed
In the computer industry ,I/O Compared with the speed of CPU, It's always too slow .SSD The hard disk IOPS You can achieve 2W、4W, But we CPU What are the main frequencies 2GHz above , It means that every second there will be 20 Hundred million operations . If for I/O operation , It's all by CPU Give the corresponding instruction , And then wait I/O Return to... After the device has completed its operation , that CPU A lot of time is actually waiting for I/O Equipment complete operation . however , This CPU The waiting for , In many cases , In fact, there is not much practical significance . We have to I/O Large scale operation of equipment , In fact, it's just the data in memory , Transferred to the I/O Equipment only . under these circumstances , Actually CPU Just waiting . Especially when the amount of data transmitted is large , For example, copying large files , If all the data goes through CPU, It's a bit of a waste of time . So computer engineers invented DMA technology , That is, direct memory access (Direct Memory Access) technology , To reduce CPU Waiting time .
DMA technology
This article does not do too much related introduction , Here I'd like to briefly summarize my understanding of it .
As mentioned above ,CPU Resources are precious , If you use it to deal with I/O It would be a great loss , For example, when we use gigabit network card or hard disk to transmit a large amount of data , If you use them all CPU To carry it , I must be too busy , So you can choose DMAC(DMA The controller is DMA Controller, abbreviation DMAC),CPU tell DMAC What data does it need to transmit , From where , Where to send this information , And then to DMAC To do ,DMAC You can wait until all the data are available , Send the signal again , hand CPU To deal with , Rather than let CPU Where to wait .(DMAC: We don't process data , Just the data movers )
The specific transmission process ( From disk to network ) Pictured :

Zero copy
As we found above , Though through DMA Technology can make CPU Don't wait I/O operation , Relieved some of the pressure , But it's also clear from the picture that , two CPU Of Copy It's totally funny , Can we get rid of these two steps ? That's what zero copy needs to do , And as we know it RocketMQ、Kafka They all use zero copy technology to optimize I/O, And their zero copy processing is somewhat different .
Kafka Zero copy ——SendFile
Kafka The code called Java NIO library , The concrete is FileChannel Inside transferTo Method ( The bottom is . Our data is not read into the middle application memory , But directly through Channel, Write to the corresponding network device . And for the Socket The operation of , It's not written to Socket Of Buffer Inside , It's directly based on the descriptor (Descriptor) Write to the buffer of the network card . therefore , In the process , Only two data transfers were made .( Because it's not in the user mode memory layer Copy data , Twice CPU Of Copy, So we call it Zero copy (Zero-Copy)
SendFile How it works
system call sendfile() adopt DMA Copy disk data to kernel buffer(read buffer), And then the data is kernel Copy directly to another and socket dependent kernel buffer(socket buffer). In this way, there is no switch between user mode and kernel mode , It's done directly from the kernel, from a buffer To another buffer A copy of the , Because the data is right there kernel in .
Pictured :
for the first time , It's through DMA, Read directly from the hard disk to the read buffer of the operating system kernel . The second time , It is based on Socket Descriptor information for , Directly from the read buffer , Write to the buffer of the network card .

This is a Kafka At present, the standard solution of real-time data transmission pipeline , It's also Kafka One of the secrets of high throughput , Zero copy .
RocketMQ Zero copy ——Mmap
Mmap Full name Memory Mapped Files. Simply describe its function : Map disk files to memory , Users can modify disk files by modifying memory .
It works by using the operating system directly Page To map files directly to physical memory , After mapping, your operations on physical memory will be synchronized to disk ( The operating system at the right time ).
adopt mmap There is also a very obvious flaw —— unreliable , writes mmap The data in is not actually written to disk , The operating system will call in the program flush The data is actually written to the disk at the same time .
RocketMQ Mainly through MappedByteBuffer Read and write files . among , Take advantage of NIO Medium FileChannel The model maps the physical files on the disk directly to the memory address in user mode ( such Mmap In a way that reduces tradition IO The performance overhead of copying disk file data back and forth between the buffer of the operating system kernel address space and the buffer of the user application address space ), The operation of the file is converted to the operation of the memory address directly , This greatly improves the efficiency of reading and writing files ( Because of the need to use memory mapping mechanism , so RocketMQ All files are stored in fixed length structure , It is convenient to map the whole file to memory at one time ).
Pictured :

summary
- CPU Than I/O Much better performance , We should try our best to give CPU compromise , Let it do more , So there was DMA.DMA Yes CPU say ," You tell me what data to move , Where to move , I'll tell you , You go ahead and do something else "
- We found that , In the process of data transmission , Twice CPU Of Copy It can be omitted. , So there's memory mapping technology (Mmap)、SendFile technology ( Kernel data Copy), Give Way CPU There's no need to work in vain
- Excellent middleware well known in the market, such as RocketMQ The zero copy technology used is Mmap、Kafka Using SendFile
边栏推荐
猜你喜欢

C4D quick start tutorial - Chamfer

Driving test Baodian and its spokesperson Huang Bo appeared together to call for safe and civilized travel

聊聊消息队列高性能的秘密——零拷贝技术

oracle修改数据库字符集

Nacos download, start and configure MySQL database

Solution of Xiaomi TV's inability to access computer shared files
![[blackmail virus data recovery] suffix Crylock blackmail virus](/img/b2/8e3a65dd250b9194cfc175138c740c.jpg)
[blackmail virus data recovery] suffix Crylock blackmail virus

Linux安装Oracle Database 19c RAC

Redis安装部署(Windows/Linux)

Cloudreve自建云盘实践,我说了没人能限制得了我的容量和速度
随机推荐
MYSQL安装出现问题(The service already exists)
Minecraft air Island service
PCL calculates the intersection of three mutually nonparallel planes
Minecraft group service opening
Tensorflow2 keras 分类模型
C Gaode map obtains the address according to longitude and latitude
ORA-12514问题解决方法
Avoid breaking changes caused by modifying constructor input parameters
Qunhui NAS configuring iSCSI storage
QT qtimer class
Service de groupe minecraft
Function ‘ngram‘ is not defined
kubeadm部署kubernetes v1.23.5集群
gocv图片读取并展示
Finishing the interview essentials of secsha system!!!
Win10 uses docker to pull the redis image and reports an error read only file system: unknown
2022/2/14 summary
「Redis源码系列」关于源码阅读的学习与思考
我服了,MySQL表500W行,居然有人不做分区?
During MySQL installation, mysqld Exe reports that the application cannot start normally (0xc000007b)`