当前位置:网站首页>Zero copy of network file transfer
Zero copy of network file transfer
2022-07-26 01:18:00 【Outsiders inside】
One 、 Traditional file transfer
The operation process of traditional file transfer is as follows :
- First call Java Medium file.read Method , because Java Of IO Flow does not have IO Reading and writing ability , So at this time, you will first switch from user mode to kernel mode (Kernel), Use the ability of the kernel to read the files in the disk and store the data in the kernel buffer , During this period, the user thread will block .( The kernel using DMA(Direct Memory Access Direct memory access ) To read the file , Will not be used during CPU);
- Switch from kernel mode to user mode , Read data from kernel buffer to user buffer (buffer), period CPU Participate in copying ;
- call file.write Method , Write data from user buffer to socket buffer ,CPU Participate in copying ;
- At this point you need to start from socket The buffer writes data to the network card , User mode does not have this ability , So you need to switch to kernel mode again , Call the write ability of the kernel , Use DMA Take data from socket Write buffer to network card , Unwanted CPU, File copy complete .
You can see that the read-write surface of the above file is used Java Of API, but Java Of API It's not actually a physical device level read / write , Just cache level replication , The reading and writing of the bottom layer is actually completed by the kernel , in the meantime , The file data is copied 4 Time , There are three switches between user mode and kernel mode ( Heavyweight operations ), It consumes performance .
Two 、 Optimize V1.0
NIO The optimized process is as follows :

NIO adopt directByteBuffer To optimize file transfer ,directByteBuffer from ByteBuffer.allocateDirect() Allocated , Is an operating system memory and jvm Memory mapped memory space , amount to jvm Can directly operate the operating system memory , This eliminates the process of copying data from the kernel buffer to the user buffer when reading data , Because now these two pieces of memory are actually the same memory mapped to each other , And this memory is not affected JVM The impact of recycling , Memory address is fixed , advantageous to IO Reading and writing .
Java Medium directByteBuffer Object only maintains the virtual reference of this memory object , Memory reclamation is divided into two steps :
- directByteBuffer Object is recycled , Virtual references are added to the reference queue ;
- Access the reference queue through a dedicated thread , Release the off heap memory corresponding to the virtual reference .
3、 ... and 、 Optimize V2.0
stay 2.0 edition ,NIO application Linux2.1 Provided later sendFile The method is further optimized ,Java It is embodied in two channel call transferTo/transferFrom On the way :

Compared with the original transmission ,Java First call transferTo Method , At this time, the user mode is switched to the kernel mode , Use DMA Read the file data into the kernel buffer , Don't use CPU; And then use it CPU Copy the data from the kernel buffer to socket buffer ; End use DMA Send the data from socket Copy buffer to network card . There is only one switch from user mode to kernel mode in the whole transmission process , And the file has been copied only three times .
Four 、 Optimize V3.0
stay linux2.4 And then to transferTo/transferFrom The bottom layer of the method is further optimized , Pictured :

Compared with the original transmission :Java First call transferTo Method , At this time, the user mode is switched to the kernel mode , Use DMA Read the file data into the kernel buffer , Don't use CPU; And then use it CPU Will some offset and length Copy information to socket Buffer zone , Almost no consumption ; End use DMA Copy the data from the kernel buffer to the network card . There is only one switch from user mode to kernel mode in the whole transmission process , The data was copied only twice , It's called Zero copy ( Note that zero copy is not really no copy , Instead, only small data offset and length Copy the information , Duplicate data will not be copied in jvm In the memory ).
Zero copy advantage : The number of switching between kernel mode and user mode is less ; Do not use CPU Calculate and copy files , Reduce CPU Cache pseudo share ; Suitable for small file transfer .
边栏推荐
- [CTF] crypto preliminary basic outline
- [go] III. The simplest restful API server
- Rotate the minimum number of the array
- matlab 按位 与 或 非
- Sqli-labs Less7
- Dot screen precautions
- 记一次自定义 Redis 分布式锁导致的故障
- [RTOS training camp] learn C language from a higher perspective
- NLP introduction + practice: Chapter 3: gradient descent and back propagation
- 【秒杀概念】原反补
猜你喜欢

FreeBSD bnxt以太网驱动源码阅读记录二:

链表相关面试题

Google Gson用法详解

Win11打不开自带杀毒软件怎么办?win11自带杀毒功能打不开
![[Jizhong] July 16, 2022 1432. Oil pipeline](/img/60/55a7e35cd067948598332d08eccfb1.jpg)
[Jizhong] July 16, 2022 1432. Oil pipeline

Redis数据结构详解,结合书本

换ip软件的用途很广及原理 动态IP更换的四种方法来保护网络隐私

Detailed explanation of rest assured interface testing framework

What is the dynamic IP address? Why do you recommend dynamic IP proxy?

NLP introduction + practice: Chapter 4: using pytorch to manually realize linear regression
随机推荐
[Code] refers to the repeated number in the offer 03 array
格式化JS代码,调试JS代码
Inverse matrix block matrix
【秒杀概念】大小端
[Jizhong] July 16, 2022 1432. Oil pipeline
Browser development and use skills
"Introduction to natural language processing practice" deep learning foundation - attention mechanism, transformer deep analysis and learning material summary
Should we test the Dao layer?
How does the proxy IP server ensure its information security in the network
《暗黑破坏神:不朽》手游如何多开搬砖及新手入门搬砖攻略
Fundamentals of MATLAB shift operation
Timeout settings for feign and hystrix
Embedded development: tips and tricks -- seven tips for designing powerful boot loader
数据库系统原理与应用教程(056)—— MySQL 查询(十八):其他类型函数的用法
How to obtain the cash flow data of advertising services to help analyze the advertising effect?
MySQL pymysql operation
点屏注意事项
ZK-Rollups工作原理
Diablo: immortality mobile game how to open more brick moving and novice introduction brick moving strategy
Sqli-labs Less7