理解Netty中的零拷贝(Zero-Copy)机制
理解Netty中的零拷貝(Zero-Copy)機(jī)制
發(fā)表于2年前(2014-01-13 15:11)?? 閱讀(10209)?|?評(píng)論(12) 164人收藏此文章,我要收藏 贊2912月12日北京OSC源創(chuàng)會(huì) —— 開源技術(shù)的年終盛典 ???
Java Netty 零拷貝 源碼分析目錄[-]
- 理解零拷貝
- Netty3中零拷貝的實(shí)現(xiàn)機(jī)制
- ChannelBuffer接口
- CompositeChannelBuffer類
- 創(chuàng)建
- 數(shù)據(jù)訪問
- 參考資料
理解零拷貝
零拷貝是Netty的重要特性之一,而究竟什么是零拷貝呢? 
 WIKI中對(duì)其有如下定義:
“Zero-copy” describes computer operations in which the CPU does not perform the task of copying data from one memory area to another.
從WIKI的定義中,我們看到“零拷貝”是指計(jì)算機(jī)操作的過程中,CPU不需要為數(shù)據(jù)在內(nèi)存之間的拷貝消耗資源。而它通常是指計(jì)算機(jī)在網(wǎng)絡(luò)上發(fā)送文件時(shí),不需要將文件內(nèi)容拷貝到用戶空間(User Space)而直接在內(nèi)核空間(Kernel Space)中傳輸?shù)骄W(wǎng)絡(luò)的方式。
Non-Zero Copy方式: 
 
Zero Copy方式: 
 
從上圖中可以清楚的看到,Zero Copy的模式中,避免了數(shù)據(jù)在用戶空間和內(nèi)存空間之間的拷貝,從而提高了系統(tǒng)的整體性能。Linux中的sendfile()以及Java NIO中的FileChannel.transferTo()方法都實(shí)現(xiàn)了零拷貝的功能,而在Netty中也通過在FileRegion中包裝了NIO的FileChannel.transferTo()方法實(shí)現(xiàn)了零拷貝。
而在Netty中還有另一種形式的零拷貝,即Netty允許我們將多段數(shù)據(jù)合并為一整段虛擬數(shù)據(jù)供用戶使用,而過程中不需要對(duì)數(shù)據(jù)進(jìn)行拷貝操作,這也是我們今天要講的重點(diǎn)。我們都知道在stream-based transport(如TCP/IP)的傳輸過程中,數(shù)據(jù)包有可能會(huì)被重新封裝在不同的數(shù)據(jù)包中,例如當(dāng)你發(fā)送如下數(shù)據(jù)時(shí):
有可能實(shí)際收到的數(shù)據(jù)如下:
因此在實(shí)際應(yīng)用中,很有可能一條完整的消息被分割為多個(gè)數(shù)據(jù)包進(jìn)行網(wǎng)絡(luò)傳輸,而單個(gè)的數(shù)據(jù)包對(duì)你而言是沒有意義的,只有當(dāng)這些數(shù)據(jù)包組成一條完整的消息時(shí)你才能做出正確的處理,而Netty可以通過零拷貝的方式將這些數(shù)據(jù)包組合成一條完整的消息供你來使用。而此時(shí),零拷貝的作用范圍僅在用戶空間中。
Netty3中零拷貝的實(shí)現(xiàn)機(jī)制
以下以Netty 3.8.0.Final的源代碼來進(jìn)行說明
ChannelBuffer接口
Netty為需要傳輸?shù)臄?shù)據(jù)制定了統(tǒng)一的ChannelBuffer接口。該接口的主要設(shè)計(jì)思路如下:
- 使用getByte(int index)方法來實(shí)現(xiàn)隨機(jī)訪問
- 使用雙指針的方式實(shí)現(xiàn)順序訪問 - 每個(gè)Buffer都有一個(gè)讀指針(readIndex)和寫指針(writeIndex)
- 在讀取數(shù)據(jù)時(shí)讀指針后移,在寫入數(shù)據(jù)時(shí)寫指針后移 
 
 
定義了統(tǒng)一的接口之后,就是來做各種實(shí)現(xiàn)了。Netty主要實(shí)現(xiàn)了HeapChannelBuffer,ByteBufferBackedChannelBuffer等等,下面我們就來講講與Zero Copy直接相關(guān)的CompositeChannelBuffer類。
CompositeChannelBuffer類
CompositeChannelBuffer類的作用是將多個(gè)ChannelBuffer組成一個(gè)虛擬的ChannelBuffer來進(jìn)行操作。為什么說是虛擬的呢,因?yàn)镃ompositeChannelBuffer并沒有將多個(gè)ChannelBuffer真正的組合起來,而只是保存了他們的引用,這樣就避免了數(shù)據(jù)的拷貝,實(shí)現(xiàn)了Zero Copy。 
 下面我們來看看具體的代碼實(shí)現(xiàn),首先是成員變量
以上這里列出了幾個(gè)比較重要的成員變量。其中readerIndex既讀指針和writerIndex既寫指針是從AbstractChannelBuffer繼承而來的;然后components是一個(gè)ChannelBuffer的數(shù)組,他保存了組成這個(gè)虛擬Buffer的所有子Buffer,indices是一個(gè)int類型的數(shù)組,它保存的是各個(gè)Buffer的索引值;最后的lastAccessedComponentId是一個(gè)int值,它記錄了最后一次訪問時(shí)的子Buffer ID。從這個(gè)數(shù)據(jù)結(jié)構(gòu),我們不難發(fā)現(xiàn)所謂的CompositeChannelBuffer實(shí)際上就是將一系列的Buffer通過數(shù)組保存起來,然后實(shí)現(xiàn)了ChannelBuffer 的接口,使得在上層看來,操作這些Buffer就像是操作一個(gè)單獨(dú)的Buffer一樣。
創(chuàng)建
接下來,我們再看一下CompositeChannelBuffer.setComponents方法,它會(huì)在初始化CompositeChannelBuffer時(shí)被調(diào)用。
/*** Setup this ChannelBuffer from the list*/ private void setComponents(List<ChannelBuffer> newComponents) {assert !newComponents.isEmpty();// Clear the cache.lastAccessedComponentId = 0;// Build the component array.components = new ChannelBuffer[newComponents.size()];for (int i = 0; i < components.length; i ++) {ChannelBuffer c = newComponents.get(i);if (c.order() != order()) {throw new IllegalArgumentException("All buffers must have the same endianness.");}assert c.readerIndex() == 0;assert c.writerIndex() == c.capacity();components[i] = c;}// Build the component lookup table.indices = new int[components.length + 1];indices[0] = 0;for (int i = 1; i <= components.length; i ++) {indices[i] = indices[i - 1] + components[i - 1].capacity();}// Reset the indexes.setIndex(0, capacity()); }通過代碼可以看到該方法的功能就是將一個(gè)ChannelBuffer的List給組合起來。它首先將List中得元素放入到components數(shù)組中,然后創(chuàng)建indices用于數(shù)據(jù)的查找,最后使用setIndex來重置指針。這里需要注意的是setIndex(0, capacity())會(huì)將讀指針設(shè)置為0,寫指針設(shè)置為當(dāng)前Buffer的長度,這也就是前面需要做assert c.readerIndex() == 0和assert c.writerIndex() == c.capacity()這兩個(gè)判斷的原因,否則很容易會(huì)造成數(shù)據(jù)重復(fù)讀寫的問題,所以Netty推薦我們使用ChannelBuffers.wrappedBuffer方法來進(jìn)行Buffer的合并,因?yàn)樵谠摲椒ㄖ蠳etty會(huì)通過slice()方法來確保構(gòu)建CompositeChannelBuffer是傳入的所有子Buffer都是符合要求的。
數(shù)據(jù)訪問
CompositeChannelBuffer.getByte(int index)的實(shí)現(xiàn)如下:
public byte getByte(int index) {int componentId = componentId(index);return components[componentId].getByte(index - indices[componentId]); }從代碼我們可以看到,在隨機(jī)查找時(shí)會(huì)首先通過index獲取這個(gè)字節(jié)所在的componentId既字節(jié)所在的子Buffer序列,然后通過index - indices[componentId]計(jì)算出它在這個(gè)子Buffer中的第幾個(gè)字節(jié),然后返回結(jié)果。
下面再來看一下componentId(int index)的實(shí)現(xiàn):
private int componentId(int index) {int lastComponentId = lastAccessedComponentId;if (index >= indices[lastComponentId]) {if (index < indices[lastComponentId + 1]) {return lastComponentId;}// Search rightfor (int i = lastComponentId + 1; i < components.length; i ++) {if (index < indices[i + 1]) {lastAccessedComponentId = i;return i;}}} else {// Search leftfor (int i = lastComponentId - 1; i >= 0; i --) {if (index >= indices[i]) {lastAccessedComponentId = i;return i;}}}throw new IndexOutOfBoundsException("Invalid index: " + index + ", maximum: " + indices.length); }從代碼中我們發(fā)現(xiàn),Netty以lastComponentId既上次訪問的子Buffer序號(hào)為中心,向左右兩邊進(jìn)行搜索,這樣做的目的是,當(dāng)我們兩次隨機(jī)查找的字符序列相近時(shí)(大部分情況下都是這樣),可以最快的搜索到目標(biāo)索引的componentId。
參考資料
總結(jié)
以上是生活随笔為你收集整理的理解Netty中的零拷贝(Zero-Copy)机制的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
 
                            
                        - 上一篇: 用于角点检测的FAST算法
- 下一篇: Adobe illustrator 排版
