当前位置:网站首页>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 .
边栏推荐
- 数据库系统原理与应用教程(054)—— MySQL 查询(十六):日期时间型函数的用法
- Kubernetes Pod启动流程
- How does the proxy IP server ensure its information security in the network
- 点屏注意事项
- IP地址能精确到哪步?动态IP及静态IP是什么?切换IP最常用的方法
- "Introduction to natural language processing practice" deep learning foundation - attention mechanism, transformer deep analysis and learning material summary
- 【秒杀概念】原反补
- It will be easier to implement MES system by doing well in these four stages
- Rotate the minimum number of the array
- How can I become an irreplaceable programmer?
猜你喜欢

The difference and application of in and exists in SQL statement

1.30 upgrade bin file, add suffix and file length

Web middleware log analysis script 3.0 (shell script)

Tencent employees' salary: the real 985 graduation salary, do you think I can be saved? Netizen: daily salary?

Machine learning: Bayesian Networks

Lua basic grammar

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

【纪中】2022.7.16 1432.输油管道

Implementation process of adding loading effect to easycvr page

How does the proxy IP server ensure its information security in the network
随机推荐
Timeout settings for feign and hystrix
REST-assured接口测试框架详解
NLP introduction + practice: Chapter 4: using pytorch to manually realize linear regression
MySQL pymysql operation
Docker高级篇-Mysql主从复制
FreeBSD bNXT Ethernet driver source code reading record 2:
SQL的CASE WHEN
【Code】剑指offer 03数组中重复的数字
Optimization of tableview
Rotate the minimum number of the array
【ICKIM 2022】第四届知识与信息管理国际会议
Mmocr usage guide
matlab 移位操作基础
手游多开用模拟器多开游戏如何切换IP搬砖
What are the ways to quickly improve programming skills in the process of programming learning?
[ickim 2022] the Fourth International Conference on knowledge and information management
[RTOS training camp] I2C and UART knowledge and preview arrangement + evening class questions
How does the proxy IP server ensure its information security in the network
Tutorial on principles and applications of database system (055) -- MySQL query (XVII): usage of mathematical functions
网络性能评估工具 ping/mtr