当前位置:网站首页>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 .
边栏推荐
- Google Gson用法详解
- 【ICKIM 2022】第四届知识与信息管理国际会议
- 游戏思考17:寻路引擎recast和detour学习二:recast导航网格生成流程及局限性
- [secsha concept] original reverse supplement
- 光纤通信中信号劣化的原因
- 网络性能评估工具 ping/mtr
- 服务器可用资源查询脚本
- Small sample learning - getting started
- 手游多开用模拟器多开游戏如何切换IP搬砖
- [software development specification iv] application system security coding specification
猜你喜欢
![[go] III. The simplest restful API server](/img/1f/f6fc8cc9a3891d01a25e709170188d.png)
[go] III. The simplest restful API server

RHCE之at和crontab命令详解及chrony部署

Inverse matrix block matrix

How does the proxy IP server ensure its information security in the network

健身房一年关店8000家,逆势盈利的工作室是怎么开的?

android sqlite先分组后排序左连查询

How accurate is the IP address? What are dynamic IP and static IP? The most common method of switching IP

web中间件日志分析脚本3.0(shell脚本)

Docker advanced -mysql master-slave replication

How can I become an irreplaceable programmer?
随机推荐
力扣 25. K 个一组翻转链表
网络文件传输之零拷贝
全国一半人跑长沙,长沙一半人跑哪?
记一次自定义 Redis 分布式锁导致的故障
Implementation process of adding loading effect to easycvr page
Codeforces Round #810 (Div. 2)A~C
Timeout settings for feign and hystrix
《nlp入门+实战:第四章:使用pytorch手动实现线性回归 》
Four common simple and effective methods for changing IP addresses
Tutorial on principles and applications of database system (054) -- MySQL query (16): usage of date time function
编程学习过程中有哪些快速提高编程技巧的方法?
【数据挖掘】生成模型和判别模型的区别及优缺点
JDBC connection database (idea version)
Redis数据结构详解,结合书本
Paged query design of scenarios
《nlp入门+实战:第三章:梯度下降和反向传播 》
Subarray with 19 and K
[software development specification II] prohibited item development specification
Web middleware log analysis script 3.0 (shell script)
Docker advanced -mysql master-slave replication