当前位置:网站首页>[half understood] zero value copy
[half understood] zero value copy
2022-07-28 12:39:00 【hitechr】

Tradition IO
Application call read Method sends a request to the operating system to read data , At this time by User mode Switch to a Kernel mode When the system receives a request to read data , utilize DMA The controller reads the data from the disk into the system cache ( In the figure 2.1) And then CPU It will write the data in the system cache to the application cache ( chart 2.2), At this time by Kernel mode Switch to a User mode The application then calls write Method to inform the system to write data , At this time by User mode Switch to a Kernel mode . CPU Write the data in the application cache to the system cache ( chart 2.3) And then it's DMA The controller then writes the data from the system cache to the network card cache ( chart 2.4),write Method returns , At this time by Kernel mode Switch to a User mode .
In ordinary IO Copy with 4 Context switching process and 4 Secondary copy process , In high concurrency scenarios, performance will be greatly affected .
DMA
Data needs to be read and written CPU Issue the corresponding command to complete , because CPU Faster than IO It's much faster , In the process of copying data CPU It is impossible to be in the process of waiting , But if you don't wait ,CPU I don't know IO When is it done , In order to coordinate high-speed CPU And low-speed IO The contradiction of , So it introduces DMA,DMA(Direct Memory Access) Direct memory access technology , It's used for memory and IO Data transmission of equipment . As big as CPU to DMA Give an order , then DMA Get to work , then CPU Do other work ,DMA Tell when you're done CPU, This reduces CPU The waiting time of .
Zero value copy
The observation chart can 2.1 and 2.2 I feel that the data has done a useless job in this process , Move from kernel mode to user mode , Then move from user mode to kernel mode , Therefore, it is proposed that Zero value copy , Zero value copy is not no copy , Instead, there is no longer a data copy process between user state and kernel state , His realization has mmap and sendfile Two ways .
mmap
mmap (member map) Memory mapping , After the data is read into the system cache , The mapping of user mode data in the system cache can complete the data transmission process .

First send a to the operating system mmap command ( Upper figure 1.1), At this time by User mode Switch to a Kernel mode . System received mmap After the command , Will use DMA Read the data from the disk to the cache of memory ( Upper figure 1.2). Returns a map of the data buffer mmap( Upper figure 1.3), from Kernel mode Switch to User mode . The user is initiating a write The operation of ( Upper figure 1.4), from User mode Switch to Kernel mode . cpu Then copy the data in the memory cache to another cache ( Upper figure 1.5) DMA Copy the data to the network card and return ( Upper figure 1.6), And by the Kernel mode Switch to User mode
mmap The operation of 4 Context switching and 3 The next data copy process , Less than an ordinary copy cpu A copy of the .
sendfile
mmap The copy method of must first obtain the mapping of the data area , Then a user initiates a command to write data , Write on the network card , If there is no need to do any processing to the data , Just send the data intact ,mmap Command and write Commands can be merged into one command , Just tell the operating system which data to send out , The merged command is sendfile.

The user sends directly to the operating system sendfile The order of , At this time, the user mode is switched to the kernel mode After the system receives the command , from DMA Read the data from the disk to the system cache Then from cpu Copy the read data to another cache Again by DMA Write the data on the Internet Then return , Switch from kernel mode to user mode
sendfile The process of 2 Sub context switching and 3 The next data copy process , The data read in the whole process is not visible to the user space , Adapt to static file server .
sendfile upgrade
stay sendfile Of cpu Copy data from cache from one place to another , It will also waste extra space , Can I write data to the network card only for the first time DMA Copied data , Copy directly to the network card ? So right. sendfile Once again to upgrade , Introduced DMA Scatter/Gather Dispersed / Collection function .

User initiated sendfile command ( Upper figure 1.1), After the system receives , from User mode Switch to Kernel mode call DMA utilize scatter Read data from disk to cache discrete storage ( Upper figure 1.2) cpu Send the file descriptor and data length of the cache data to another cache ( Upper figure 1.3) DMA When reading data from another cache, according to the file descriptor and data length , Use scatter/gather Read from the cache to the network card ( Upper figure 1.4) return , And there are Kernel mode Switch to User mode .
This process is still invisible to user space , And it needs the support of hardware . Yes 2 Subcontext switching and 2 Secondary copy process , The performance is greatly improved .
scene
kafka and rocketmq Are used to mmap technology , Both middleware have a message persistence process , And message reading process .
mq It is used in the process of message persistence and reading mmap+write, and kafka The persistence process uses mmap+write, For reading messages sendfile.
summary
Why DMA?
DMA To solve the problem of high-speed CPU And low speed IO Contradiction of operation ,cpu After initiating a read instruction , from dma Take over the reading and copying of data ,cpu Deal with other events .
Tradition IO Compared with zero value copy
| name | The process | Number of context switches | Number of copies |
|---|---|---|---|
| Tradition IO | Read command 、 Read from disk 、 Write to cache 、 Write to user cache 、 Write to cache , Write to the network card | 4 | 4 |
| mmap+write | mmap command , Read from disk , Write to cache 、 return mmap、 launch write command , Read from cache , Write to another cache , Write to the network card | 4 | 3 |
| sendfile | sendfile command , Read from disk , Write buffer , Write to another cache , Write network card | 2 | 3 |
| sendfile Upgraded version | sendfile command , Read from disk , Write buffer , Read from cache according to file descriptor , Write network card | 2 | 2 |


边栏推荐
- 行业落地呈现新进展 | 2022 开放原子全球开源峰会 OpenAtom OpenHarmony 分论坛圆满召开
- PHP日期时间运用:添加或减去特定日期的天数
- 用arduino开发ESP8266 搭建开发环境
- What SaaS architecture design does a software architect need to know?
- 遭受痛苦和创伤后的四种本真姿态 齐泽克
- 用C语言开发NES游戏(CC65)07、控制器(和精灵碰撞)
- First in the country! The two standards of "data product registration" formulated by insight technology and Shandong data were officially released
- 让arduino支持nuvotom新唐
- Tencent two sides: @bean and @component are used in the same class, what will happen?
- 图书馆自动预约脚本
猜你喜欢

laravel表单数据验证

AsiaInfo technology released antdb7.0, a "Telecom grade" core transaction database, to help government and enterprises "trust" to create the future!

Localization, low latency, green and low carbon: Alibaba cloud officially launched Fuzhou data center

Come to tdengine Developer Conference and have an insight into the future trend of data technology development

易观分析:以用户为中心提升手机银行用户体验,助力用户价值增长

论治理与创新 | 2022 开放原子全球开源峰会 OpenAnolis 分论坛圆满召开

GMT installation and use

洪九果品通过聆讯:5个月经营利润9亿 阿里与中国农垦是股东

Top level "redis notes", cache avalanche + breakdown + penetration + cluster + distributed lock, Nb

Distributed session solution
随机推荐
Laravel之缓存
易观分析:以用户为中心提升手机银行用户体验,助力用户价值增长
Anhui Jingzhun: Beidou satellite synchronous clock | Beidou synchronous clock | NTP network clock server
金九银十 再不卷就来不及了
新零售电商O2O模式解析
用C语言开发NES游戏(CC65)07、控制器(和精灵碰撞)
SuperMap arsurvey license module division
【一知半解】零值拷贝
行业落地呈现新进展 | 2022 开放原子全球开源峰会 OpenAtom OpenHarmony 分论坛圆满召开
Open source office (ospo) unveils Secrets
Latex矩阵简单使用
[apue] 文件中的空洞
Developing NES games with C language (cc65) 08. Background collision
Zadig v1.13.0 believes in the power of openness, and workflow connects all values
PHP date calculation operation processing, the current date plus one day and the specified date minus one day
If you don't roll the golden nine and silver ten, it's too late
Kafaka丢消息吗
SQL injection less23 (filter comment)
Developing NES games with C language (cc65) 10. Game cycle
Fusion cloud native, enabling new mileage | 2022 open atom global open source summit cloud native sub forum successfully held