当前位置:网站首页>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
边栏推荐
- Installing Oracle database 19C RAC on Linux
- Minecraft plug-in service opening
- Npoi export word font size correspondence
- C # save web pages as pictures (using WebBrowser)
- HackTheBox-Gunship
- C# 百度地图,高德地图,Google地图(GPS) 经纬度转换
- Analysis and solution of a classical Joseph problem
- Minecraft空岛服开服
- Solution of Xiaomi TV's inability to access computer shared files
- 【Go实战基础】gin 高效神器,如何将参数绑定到结构体
猜你喜欢
Nacos 下载启动、配置 MySQL 数据库
Web技术发展史
Minecraft module service opening
Minecraft群组服开服
将一串数字顺序后移
OpenShift 容器平台社区版 OKD 4.10.0部署
Illegal use of crawlers, an Internet company was terminated, the police came to the door, and 23 people were taken away
Minecraft plug-in service opening
oracle修改数据库字符集
HackTheBox-Gunship
随机推荐
使用递归函数求解字符串的逆置问题
Service de groupe minecraft
Multi version concurrency control mvcc of MySQL
将一串数字顺序后移
C4D quick start tutorial - Chamfer
gocv拆分颜色通道
WSL installation, beautification, network agent and remote development
NPOI 导出Word 字号对应
Nacos download, start and configure MySQL database
Openshift deployment application
History of Web Technology
Use of libusb
Don't spend money, spend an hour to build your own blog website
Qt QTimer类
C# 百度地图,高德地图,Google地图(GPS) 经纬度转换
一个经典约瑟夫问题的分析与解答
OpenShift构建镜像
Oracle修改表空间名称以及数据文件
Openshift container platform community okd 4.10.0 deployment
Linux安装Oracle Database 19c RAC