当前位置:网站首页>NIO-零拷贝
NIO-零拷贝
2022-07-01 08:23:00 【码农小汪】
在传统的IO中,发起一次读请求的过程

其中发生了两次上下文的切换,以及两次内容的拷贝。其中将内容从磁盘搬运到内核缓冲区以及从内核缓存区搬运到用户缓存区都是通过cpu来完成的,这个阶段cpu不能进行其它的事情。用户线程在这个过程中都是处于阻塞状态一道IO操作完成。
引入DMA

引入DMA之后就将从磁盘拷贝内容到内存缓存区的任务交给了DMA,DMA在拷贝内容期间CPU可以干其它的事情,但是从内核缓存区拷贝到用户缓存区仍然是CPU去完成。加入DMA之后虽然CPU减少了一次数据搬运的操作,但是仍然得进行两次上下文的切换以及两次内容的拷贝。
再来看一下传统的IO操作如果要将数据读取并且通过网络进行传输的细节

由此可以看出在一次读取文件并进行网络传输的过程中发生了四次的上下文的切换以及四次内容的拷贝。如果想要优化IO的过程可以从这两个方面去进行思考。1、减少上下文切换的次数。2、减少内容的拷贝次数。
实现零拷贝
零拷贝的实现方式通常有两种:mmap + write、sendfile
1、mmap + write的方式
这种方式是创建一个内核和用户空间的共享映射缓存区。用户线程就可以直接操作这个映射缓存区中的内容而不用再将内容拷贝的用户空间,这样减少了一次内容的复制。写入网卡的时候调用write可以将内容直接从映射缓存区拷贝到Socket缓存区。这样减少了将内容从内核缓存区拷贝到用户缓存区以及从用户缓存区拷贝到Socket缓存区两次拷贝,增加了一次将内容从内核缓存区拷贝到Socket缓存区。
2、sendfile
第一种方式虽然减少了一次内容的拷贝,但是仍然需要四次的上下文切换。sendfile(int out_fd, int in_fd, off_t *offset, size_t count)方法将读取操作从两个独立的操作合并成一次将内容从源拷贝到目标位置的一个操作。这样只需要调用sendfile方法时从用户态切到内核态,传输成功之后再从内核态传入到用户态。减少了两次上下文的切换。但是仍需要cpu参与将内容从内核缓存区拷贝到Socket缓存区。
真正的零拷贝应该是不需要CPU进行搬运工作。所以sendfile方法在调用的时候如果⽹卡⽀持 scatter-gather 特性那么可以将内容直接从内核缓存区拷贝到网卡。这样就不需要cpu的搬运实现了真正的零拷贝。
NIO的零拷贝
使用nio调用transferTo()方法可以实现零拷贝。其底层就是调用了sendfile的方式实现的零拷贝。
如何通过nio实现文件的零拷贝这里就不多说了。值得注意的是nio在进行文件的传输的时候会创建一个直接内存,直接由于不属于jvm所不能被gc回收,所以直接内存的创建和回收的成本都是比较高的,但是读写性能比较高。
这里有大佬总结了使用NIO和IO进行文件操作的性能对比:大文件拷贝,试试 NIO 的内存映射_androidstarjack的博客-CSDN博客
NIO直接内存如何回收:JVM专题(九)-直接内存_IT-老牛的博客-CSDN博客_jvm直接内存
边栏推荐
- 【面试必刷101】链表
- shardingSphere
- The data analyst will be ruined without project experience. These 8 project resources will not be taken away
- Intelligent water and fertilizer integrated control system
- String coordinates of number to excel
- 使用 setoolkit 伪造站点窃取用户信息
- 《微机原理》——微处理器内部及外部结构
- View drawing process analysis
- Principle and application of single chip microcomputer - off chip development
- Leetcode t31: prochain arrangement
猜你喜欢

Redis publish subscription

MAVROS发送自定义话题消息给PX4

MATLAB【函数求导】

Vscode customize the color of each area

《微机原理》——微处理器内部及外部结构

【华为机试真题详解】判断字符串子序列【2022 Q1 Q2 | 200分】

使用beef劫持用户浏览器

Principle and application of single chip microcomputer - principle of parallel IO port

Use threejs simple Web3D effect

Suivi des cibles de manoeuvre - - mise en oeuvre du modèle statistique actuel (modèle CS) filtre Kalman étendu / filtre Kalman sans trace par MATLAB
随机推荐
[redis] it takes you through redis installation and connection at one go
[JS reverse] MD5 encryption parameter cracking
01 numpy introduction
vscode自定义各个区域的颜色
Learn reptiles for a month and earn 6000 a month? Tell you the truth about the reptile, netizen: I wish I had known it earlier
What is the material of 16MnDR, the minimum service temperature of 16MnDR, and the chemical composition of 16MnDR
Intelligent constant pressure irrigation system
Download jackson codehaus. org jar - downloading jackson. codehaus. org jar
MATLAB【函数和图像】
TypeError: __init__() got an unexpected keyword argument ‘autocompletion‘
Properties of 15MnNiNbDR low temperature vessel steel, Wugang 15MnNiDR and 15MnNiNbDR steel plates
MATLAB小技巧(23)矩阵分析--模拟退火
Data analysis notes 11
C basic knowledge review (Part 4 of 4)
Leetcode t29: divide two numbers
Stack implementation calculator
On several key issues of digital transformation
Advanced API
深度学习训练样本扩增同时修改标签名称
There are many problems in sewage treatment, and the automatic control system of pump station is solved in this way