当前位置:网站首页>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
边栏推荐
- Minecraft空岛服开服
- 【Go实战基础】gin 高效神器,如何将参数绑定到结构体
- WSL安装、美化、网络代理和远程开发
- Find the node with the smallest value range in the linked list and move it to the front of the linked list
- Openshift container platform community okd 4.10.0 deployment
- Loadbalancer dynamically refreshes Nacos server
- [blackmail virus data recovery] suffix Hydra blackmail virus
- Openshift deployment application
- Using recursive functions to solve the inverse problem of strings
- Introduction to the basic concept of queue and typical application examples
猜你喜欢

Minecraft plug-in service opening

查看was发布的应用程序的端口

【Go实战基础】gin 如何自定义和使用一个中间件

commands out of sync. did you run multiple statements at once

「Redis源码系列」关于源码阅读的学习与思考

队列管理器running状态下无法查看通道

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

Programmer training, crazy job hunting, overtime ridiculed by colleagues deserve it

Qunhui NAS configuring iSCSI storage

远程连接IBM MQ报错AMQ4036解决方法
随机推荐
What is the future value of fluorite mine of karaqin Xinbao Mining Co., Ltd. under zhongang mining?
Minecraft group service opening
Find the node with the smallest value range in the linked list and move it to the front of the linked list
WSL installation, beautification, network agent and remote development
History of Web Technology
Minecraft air Island service
Leetcode sword finger offer brush questions - day 22
Installing Oracle database 19C RAC on Linux
OpenShift 容器平台社区版 OKD 4.10.0部署
WSL安装、美化、网络代理和远程开发
聊聊消息队列高性能的秘密——零拷贝技术
Redis安装部署(Windows/Linux)
Flink-使用流批一体API统计单词数量
OpenShift构建镜像
以字节跳动内部 Data Catalog 架构升级为例聊业务系统的性能优化
【Go实战基础】gin 如何验证请求参数
Minecraft module service opening
【Go实战基础】gin 如何自定义和使用一个中间件
commands out of sync. did you run multiple statements at once
Oracle 相关统计