当前位置:网站首页>Ali Er Mian: what is MMAP? (not MMP)
Ali Er Mian: what is MMAP? (not MMP)
2022-07-22 02:36:00 【Programmer Xiaohui】
Sorting out the notes of previous dry goods
LeetCode Algorithm brush question C/C++ Version answers pdf download
LeetCode Algorithm brush question Java Version answers pdf download
Java Back end development learning path + Summary of knowledge points
Front end development learning path + Summary of knowledge points
Learning route for big data development + Summary of knowledge points
C/C++( backstage ) Learning route + Summary of knowledge points
Embedded development learning route + Summary of knowledge points
Hello everyone , Today we will share a high-frequency interview knowledge point with the content of an article .
Usually in the interview process , Frequently encountered high-frequency problems Namely :RocketMQ Why fast ?Kafka Why fast ? What is? mmap? wait .
One of the problems of this kind is that Zero copy , Although there are other reasons , But today our topic mainly focuses on zero copy .
Tradition IO
Before we start talking about zero copy , First of all, we should pay attention to the traditional culture IO There is a concept of way .
Traditional based IO The way , The bottom layer actually calls read()
and write()
To achieve .
adopt read()
Read data from the hard disk to the kernel buffer , And then copy it to the user buffer ; And then through write()
Write to socket buffer
, Finally, write to the NIC device .

The whole process happened 4 Context switching between the secondary user state and the kernel state and 4 Second copy , The specific process is as follows :
The user process passes through
read()
Method to the operating system , At this point, the context changes from user mode to kernel modeDMA The controller copies the data from the hard disk to the read buffer
CPU Copy read buffer data to application buffer , Context changes from kernel state to user state ,
read()
returnThe user process passes through
write()
Method invocation , Context changes from user mode to kernel modeCPU Copy the data in the application buffer to socket buffer
DMA The controller takes data from socket Buffer copy to network card , Context switches from kernel state to user state ,
write()
return

that , I mean User mode 、 Kernel mode What does it mean ? What is context switching ?
Simply speaking , User space refers to the running space of user process , Kernel space is the running space of kernel .
If a process is running in kernel space, it is in kernel state , Running in user space is user mode .
For safety's sake , They are isolated from each other , The context switching between user mode and kernel mode is also time-consuming .
We can see from above , One simple IO The process produces 4 Subcontext switch , This will undoubtedly have a great impact on performance in high concurrency scenarios .
So what is DMA What about the copy ?
Because for a IO In terms of operation , It's all through CPU Issue the corresponding instruction to complete , But compared to the CPU Come on ,IO It's too slow ,CPU There's a lot of time to wait IO The state of .
So that's what happened DMA(Direct Memory Access) Direct memory access technology , In essence, it is an independent chip on the motherboard , It's used for memory and IO Data transmission of equipment , Thereby reducing CPU The waiting time of .
But no matter who copies it , Frequent copy times also have an impact on performance .
Zero copy
Zero copy technology refers to when a computer performs an operation ,CPU There is no need to copy data from one memory to another , This technology is usually used to save time when transferring files over the network CPU Cycle and memory bandwidth .
So for zero copy , It's not really a process without data copy at all , It's just to reduce the switching times between user mode and kernel mode and CPU The number of copies .
here , Just talk about several common zero copy technologies .
mmap+write
mmap+write To put it simply, use mmap
To replace the read+write Medium read operation , One less CPU A copy of the .
mmap
The main way is to map the address of the read buffer to the address of the user buffer , Kernel buffer and application buffer are shared , This reduces the time from the read buffer to the user buffer CPU Copy .

The whole process happened 4 Context switching between the secondary user state and the kernel state and 3 Second copy , The specific process is as follows :
The user process passes through
mmap()
Method to the operating system , Context changes from user mode to kernel modeDMA The controller copies the data from the hard disk to the read buffer
Context changes from kernel state to user state ,mmap Call return
The user process passes through
write()
Method invocation , Context changes from user mode to kernel modeCPU Copy the data in the read buffer to socket buffer
DMA The controller takes data from socket Buffer copy to network card , Context switches from kernel state to user state ,
write()
return
mmap
It's a way to save CPU Copy , At the same time, because the memory in the user process is virtual , Just a read buffer mapped to the kernel , So you can save half the memory space , More suitable for large file transmission .
sendfile
comparison mmap
Come on ,sendfile
Also reduced by one CPU Copy , And it also reduces 2 Subcontext switch .

sendfile
yes Linux2.1 A system call function introduced after the kernel version , By using sendfile
Data can be transferred directly in kernel space , So the copy of user space and kernel space is avoided , At the same time due to the use of sendfile
Replaced the read+write
This saves a system call , That is to say 2 Subcontext switch .

The whole process happened 2 Context switching between the secondary user state and the kernel state and 3 Second copy , The specific process is as follows :
The user process passes through
sendfile()
Method to the operating system , Context changes from user mode to kernel modeDMA The controller copies the data from the hard disk to the read buffer
CPU Copy the data in the read buffer to socket buffer
DMA The controller takes data from socket Buffer copy to network card , Context switches from kernel state to user state ,
sendfile
Call return
sendfile
Method IO Data is completely invisible to user space , So it can only be applied to the situation where user space processing is not needed at all , Like static file servers .
sendfile+DMA Scatter/Gather
Linux2.4 After the kernel version sendfile
Further optimization has been made , By introducing new hardware support , It's called DMA Scatter/Gather Dispersed / Collection function .
It will read the data description in the buffer -- The memory address and offset are recorded to socket buffer , from DMA Copy the data from the read buffer to the network card , It's one less than the previous version CPU The process of copying

The whole process happened 2 Context switching between the secondary user state and the kernel state and 2 Second copy , More importantly, there is no CPU Copy , The specific process is as follows :
The user process passes through
sendfile()
Method to the operating system , Context changes from user mode to kernel modeDMA The controller uses scatter Copy data from hard disk to read buffer for discrete storage
CPU Send the file descriptor and data length in the read buffer to socket buffer
DMA Controller based on file descriptor and data length , Use scatter/gather Copy the data from the kernel buffer to the network card
sendfile()
Call return , Context switches from kernel state to user state
DMA gather
and sendfile
The same data is not visible to the user space , And it needs hardware support , At the same time, the input file descriptor can only be a file , But there's nothing in the process CPU Copy process , Greatly improved performance .
Application scenarios
For the two scenarios mentioned at the beginning of the article :RocketMQ and Kafka They all use zero copy technology .
about MQ for , It's just that producers send data to MQ And then persistent to disk , After that, consumers start from MQ Reading data .
about RocketMQ For example, these two steps use mmap+write
, and Kafka Is the use mmap+write
Persistent data , Send data using sendfile
.
summary
because CPU and IO The difference in speed , Produced DMA technology , adopt DMA Transport to reduce CPU The waiting time of .
Conventional IOread+write
The way it works 2 Time DMA Copy +2 Time CPU Copy , At the same time there is 4 Subcontext switch .
And by mmap+write
The way is to produce 2 Time DMA Copy +1 Time CPU Copy ,4 Subcontext switch , One reduction through memory mapping CPU Copy , Can reduce memory usage , Suitable for large file transfer .
sendfile
The way is to add a new system call function , produce 2 Time DMA Copy +1 Time CPU Copy , But only 2 Subcontext switch . Because there's only one call , Reduced context switching , But user space is important for IO The data is not visible , For static file servers .
sendfile+DMA gather
The way to produce 2 Time DMA Copy , No, CPU Copy , And only 2 Subcontext switch . Although it greatly improves performance , But we need to rely on new hardware support .
That's what we're sharing today , See you next time .
Reference reading :
https://juejin.cn/post/6844903949359644680#heading-19
https://www.cnblogs.com/xiaolincoding/p/13719610.html
https://time.geekbang.org/column/article/118657
https://www.toutiao.com/i6898240850917114380/
边栏推荐
- 【H3C设备组网配置】
- 电脑是怎样上网的 (三) 报文头封装和接入网与网络运营商
- [论文精读]Attention Is All You Need
- 面试北京XX科技总结
- Creation of gateway routing service
- VMware Workstation Pro virtual machine network three types of network cards and their usage
- STM32+ENC28J60+UIP协议栈实现WEB服务器示例
- Use dichotomy to find peak value
- DHCP protocol
- Skywalking custom link tracking and performance analysis
猜你喜欢
Gateway route assertion factory, filter factory, cross domain processing
How does the computer access the Internet (II) from network cable to network equipment
How the computer accesses the Internet (I) message generation DNS
With a wave of operations, I have improved the efficiency of SQL execution by 10000000 times
[H3C device networking configuration]
How computers access the Internet (III) packet header packaging and access network and network operators
CTF problem solving ideas
Transport layer protocol
小米12S Ultra产品力这么强老外却买不到 雷军:先专心做好中国市场
Yunyuanyuan (IX) | Devops chapter Jenkins installation and actual combat
随机推荐
Ancient Chinese civilization
Seata details of distributed transactions
(leisure) leetcode_ 7. Integer inversion
基于JSP实现OA办公系统
mysql进阶(十四) 批量更新与批量更新多条记录的不同值实现方法
(leisure) leetcode13 Roman to Integer
conda创建、查看、删除虚拟环境
Resolved (selenium operation Firefox browser error) typeerror:__ init__ () got an unexpected keyword argument ‘firefox_ options‘
mysql.h: No such file or directory
Compass Sinan
云原生(九) | Devops篇之Jenkins安装与实战
Excel if interprets that if the cell is empty, it will not participate in the calculation
身份证号码中间位数隐藏
R language ggplot2 visualization: ggplot2 visual grouping box diagram, place the legend of the visual image at the bottom of the image in the middle, where the legend information is horizontally tiled
已解决(selenium操作火狐浏览器报错)TypeError: __init__() got an unexpected keyword argument ‘firefox_options‘
Introduction to web security TCP stress testing and defense
小米12S Ultra产品力这么强老外却买不到 雷军:先专心做好中国市场
Feign details, log configuration + contract settings + timeout + custom interceptors
网络安全(4)
Distributed transaction two-phase commit, at mode, TCC mode