標籤:tostring bin blocks view throw cloud pac tab sts
1、摘要
java 的zero copy多在網路應用程式中使用。Java的libaries在linux和unix中支援zero copy,關鍵的api是java.nio.channel.FileChannel的transferTo(),transferFrom()方法。我們可以用這兩個方法來把bytes直接從調用它的channel傳輸到另一個writable byte channel,中間不會使data經過應用程式,以便提高資料轉移的效率。
2、介紹
java 的zero copy多在網路應用程式中使用。Java的libaries在linux和unix中支援zero copy,關鍵的api是java.nio.channel.FileChannel的transferTo(),transferFrom()方法。我們可以用這兩個方法來把bytes直接從調用它的channel傳輸到另一個writable byte channel,中間不會使data經過應用程式,以便提高資料轉移的效率。
許多web應用都會向使用者提供大量的靜態內容,這意味著有很多data從硬碟讀出之後,會原封不動的通過socket傳輸給使用者。這種操作看起來可能不會怎麼消耗CPU,但是實際上它是低效的:kernal把資料從disk讀出來,然後把它傳輸給user級的application,然後application再次把同樣的內容再傳回給處於kernal級的socket。這種情境下,application實際上只是作為一種低效的中間介質,用來把disk file的data傳給socket。
data每次穿過user-kernel boundary,都會被copy,這會消耗cpu,並且佔用RAM的頻寬。幸運的是,你可以用一種叫做Zero-Copy的技術來去掉這些無謂的 copy。應用程式用zero copy來請求kernel直接把disk的data傳輸給socket,而不是通過應用程式傳輸。Zero copy大大提高了應用程式的效能,並且減少了kernel和user模式的環境切換
使用kernel buffer做中介(而不是直接把data傳到user buffer中)看起來比較低效(多了一次copy)。然而實際上kernel buffer是用來提高效能的。在進行讀操作的時候,kernel buffer起到了預讀cache的作用。當寫請求的data size比kernel buffer的size小的時候,這能夠顯著的提升效能。在進行寫操作時,kernel buffer的存在可以使得寫請求完全非同步。
悲劇的是,當請求的data size遠大於kernel buffer size的時候,這個方法本身變成了效能的瓶頸。因為data需要在disk,kernel buffer,user buffer之間拷貝很多次(每次寫滿整個buffer)。
而Zero copy正是通過消除這些多餘的data copy來提升效能。
3、傳統方式及涉及到的環境切換
通過網路把一個檔案傳輸給另一個程式,在OS的內部,這個copy操作要經曆四次user mode和kernel mode之間的環境切換,甚至連資料都被拷貝了四次,如:
具體步驟如下:
- read() 調用導致一次從user mode到kernel mode的環境切換。在內部調用了sys_read() 來從檔案中讀取data。第一次copy由DMA (direct memory access)完成,將檔案內容從disk讀出,儲存在kernel的buffer中。
- 然後請求的資料被copy到user buffer中,此時read()成功返回。調用的返回觸發了第二次context switch: 從kernel到user。至此,資料存放區在user的buffer中。
- send() Socket call 帶來了第三次context switch,這次是從user mode到kernel mode。同時,也發生了第三次copy:把data放到了kernel adress space中。當然,這次的kernel buffer和第一步的buffer是不同的buffer。
- 最終 send() system call 返回了,同時也造成了第四次context switch。同時第四次copy發生,DMA egine將data從kernel buffer拷貝到protocol engine中。第四次copy是獨立而且非同步。
4、zero copy方式及涉及的上下文轉換
在linux 2.4及以上版本的核心中(如linux 6或centos 6以上的版本),開發人員修改了socket buffer descriptor,使網卡支援 gather operation,通過kernel進一步減少資料的拷貝操作。這個方法不僅減少了context switch,還消除了和CPU有關的資料拷貝。user層面的使用方法沒有變,但是內部原理卻發生了變化:
transferTo()方法使得檔案內容被copy到了kernel buffer,這一動作由DMA engine完成。 沒有data被copy到socket buffer。取而代之的是socket buffer被追加了一些descriptor的資訊,包括data的位置和長度。然後DMA engine直接把data從kernel buffer傳輸到protocol engine,這樣就消除了唯一的一次需要佔用CPU的拷貝操作。
5、零拷貝樣本
通過網路把一個檔案從client傳到server:
1 /** 2 * disk-nic零拷貝 3 */ 4 class ZerocopyServer { 5 ServerSocketChannel listener = null; 6 7 protected void mySetup() { 8 InetSocketAddress listenAddr = new InetSocketAddress(9026); 9 10 try {11 listener = ServerSocketChannel.open();12 ServerSocket ss = listener.socket();13 ss.setReuseAddress(true);14 ss.bind(listenAddr);15 System.out.println("監聽的連接埠:" + listenAddr.toString());16 } catch (IOException e) {17 System.out.println("連接埠綁定失敗 : " + listenAddr.toString() + " 連接埠可能已經被使用,出錯原因: " + e.getMessage());18 e.printStackTrace();19 }20 21 }22 23 public static void main(String[] args) {24 ZerocopyServer dns = new ZerocopyServer();25 dns.mySetup();26 dns.readData();27 }28 29 private void readData() {30 ByteBuffer dst = ByteBuffer.allocate(4096);31 try {32 while (true) {33 SocketChannel conn = listener.accept();34 System.out.println("建立的串連: " + conn);35 conn.configureBlocking(true);36 int nread = 0;37 while (nread != -1) {38 try {39 nread = conn.read(dst);40 } catch (IOException e) {41 e.printStackTrace();42 nread = -1;43 }44 dst.rewind();45 }46 }47 } catch (IOException e) {48 e.printStackTrace();49 }50 }51 }52 53 class ZerocopyClient {54 public static void main(String[] args) throws IOException {55 ZerocopyClient sfc = new ZerocopyClient();56 sfc.testSendfile();57 }58 59 public void testSendfile() throws IOException {60 String host = "localhost";61 int port = 9026;62 SocketAddress sad = new InetSocketAddress(host, port);63 SocketChannel sc = SocketChannel.open();64 sc.connect(sad);65 sc.configureBlocking(true);66 67 String fname = "src/main/java/zerocopy/test.data";68 FileChannel fc = new FileInputStream(fname).getChannel();69 long start = System.nanoTime();70 long nsent = 0, curnset = 0;71 curnset = fc.transferTo(0, fc.size(), sc);72 System.out.println("發送的總位元組數:" + curnset + " 耗時(ns):" + (System.nanoTime() - start));73 try {74 sc.close();75 fc.close();76 } catch (IOException e) {77 System.out.println(e);78 }79 }80 }View Code
檔案到檔案的零拷貝:
1 /** 2 * disk-disk零拷貝 3 */ 4 class ZerocopyFile { 5 @SuppressWarnings("resource") 6 public static void transferToDemo(String from, String to) throws IOException { 7 FileChannel fromChannel = new RandomAccessFile(from, "rw").getChannel(); 8 FileChannel toChannel = new RandomAccessFile(to, "rw").getChannel(); 9 10 long position = 0;11 long count = fromChannel.size();12 13 fromChannel.transferTo(position, count, toChannel);14 15 fromChannel.close();16 toChannel.close();17 }18 19 @SuppressWarnings("resource")20 public static void transferFromDemo(String from, String to) throws IOException {21 FileChannel fromChannel = new FileInputStream(from).getChannel();22 FileChannel toChannel = new FileOutputStream(to).getChannel();23 24 long position = 0;25 long count = fromChannel.size();26 27 toChannel.transferFrom(fromChannel, position, count);28 29 fromChannel.close();30 toChannel.close();31 }32 33 public static void main(String[] args) throws IOException {34 String from = "src/main/java/zerocopy/1.data";35 String to = "src/main/java/zerocopy/2.data";36 // transferToDemo(from,to);37 transferFromDemo(from, to);38 }39 }View Code6、參考資料
https://my.oschina.net/cloudcoder/blog/299944
Java零拷貝