面試刷題12:zero copy是怎麼回事?

面試刷題12:zero copy是怎麼回事?


文件copy是java的io部分不可忽視的內容。


我是李福春,我在準備面試,今天的問題是:


zero-copy是怎麼回事?


操作系統的空間劃分為內核態空間, 用戶態空間;


內核態空間相對操作系統具備更高的權限和優先級;


用戶態空間即普通用戶所處空間。


zero-copy指的使用類似java.nio的transforTo方法進行文件copy,文件的copy直接從磁盤到內核態空間,不經過用戶態空間,再寫到磁盤,減少了io的消耗,避免了不必要的copy 和上下文切換,所以比較高效。


接下來對面試官可能擴展的問題進行一些拓展:

java的文件copy方式

java.io流式copy

<code>package org.example.mianshi.filecopy;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.nio.file.Files;

/**
* 說明:傳統的文件copy
* @author carter
* 創建時間: 2020年03月26日 9:32 上午
**/

public class JioFileCopyApp {

public static void main(String[] args) {

final File d = new File("/data/appenvs/denv.properties");
final File s = new File("/data/appenvs/env.properties");

System.out.println("source file content :" + s.exists());
System.out.println("target file content :" + d.exists());

System.out.println("source content:");
try {
Files.lines(s.toPath()).forEach(System.out::println);
} catch (IOException e) {
e.printStackTrace();
}

System.out.println("do file copy !");

copy(s, d);

System.out.println("target file content :" + d.exists());
System.out.println("target content:");
try {
Files.lines(d.toPath()).forEach(System.out::println);
} catch (IOException e) {
e.printStackTrace();
}

}

private static void copy(File s, File d) {

try (
final FileInputStream fileInputStream = new FileInputStream(s);


final FileOutputStream fileOutputStream = new FileOutputStream(d)
) {

byte[] buffer = new byte[1024];
int length;
while ((length = fileInputStream.read(buffer)) > 0) {
fileOutputStream.write(buffer, 0, length);
}

} catch (IOException e) {
e.printStackTrace();
}

}

}/<code>


代碼可以運行;

copy流程如下圖:

它不是zero-copy的,需要切換用戶態空間和內核態空間,路徑比較長,io消耗和上線文切換的消耗比較明顯,這是比較低效的copy.

面試刷題12:zero copy是怎麼回事?


java.nioChannel式copy

<code>package org.example.mianshi.filecopy;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.nio.channels.FileChannel;
import java.nio.file.Files;

/**
* 說明:傳統的文件copy
* @author carter
* 創建時間: 2020年03月26日 9:32 上午
**/

public class JnioFileCopyApp {

public static void main(String[] args) {

final File d = new File("/data/appenvs/ndenv.properties");
final File s = new File("/data/appenvs/env.properties");

System.out.println(s.getAbsolutePath() + "source file content :" + s.exists());
System.out.println(d.getAbsolutePath() +"target file content :" + d.exists());

System.out.println("source content:");
try {
Files.lines(s.toPath()).forEach(System.out::println);
} catch (IOException e) {
e.printStackTrace();
}

System.out.println("do file copy !");

copy(s, d);

System.out.println(d.getAbsolutePath() +"target file content :" + d.exists());
System.out.println("target content:");
try {
Files.lines(d.toPath()).forEach(System.out::println);
} catch (IOException e) {
e.printStackTrace();

}

}

private static void copy(File s, File d) {

try (
final FileChannel sourceFileChannel = new FileInputStream(s).getChannel();

final FileChannel targetFileChannel = new FileOutputStream(d).getChannel()
) {

for (long count= sourceFileChannel.size();count>0;){

final long transferTo = sourceFileChannel.transferTo(sourceFileChannel.position(), count, targetFileChannel);

count-=transferTo;

}

} catch (IOException e) {
e.printStackTrace();
}

}

}
/<code>


copy過程如下圖:

明顯,不用經過用戶態空間,是zero-copy,減少了io的消耗以及上下文切換,比較高效。

面試刷題12:zero copy是怎麼回事?


Files工具類copy

<code>package org.example.mianshi.filecopy;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.nio.file.CopyOption;
import java.nio.file.Files;
import java.nio.file.StandardCopyOption;

/**
* 說明:Files的文件copy
* @author carter
* 創建時間: 2020年03月26日 9:32 上午
**/

public class FilesFileCopyApp {

public static void main(String[] args) {

final File d = new File("/data/appenvs/fenv.properties");
final File s = new File("/data/appenvs/env.properties");

System.out.println("source file content :" + s.exists());
System.out.println("target file content :" + d.exists());

System.out.println("source content:");
try {
Files.lines(s.toPath()).forEach(System.out::println);
} catch (IOException e) {
e.printStackTrace();
}

System.out.println("do file copy !");

copy(s, d);

System.out.println("target file content :" + d.exists());
System.out.println("target content:");
try {
Files.lines(d.toPath()).forEach(System.out::println);
} catch (IOException e) {

e.printStackTrace();
}

}

private static void copy(File s, File d) {

try {
Files.copy(s.toPath(),d.toPath(), StandardCopyOption.COPY_ATTRIBUTES);
} catch (IOException e) {
e.printStackTrace();
}

}

}/<code>


面試官一般喜歡刨根問底,那麼來吧!貼一下源碼:

<code> public static Path copy(Path source, Path target, CopyOption... options)
throws IOException
{
FileSystemProvider provider = provider(source);
if (provider(target) == provider) {
// same provider
provider.copy(source, target, options);
} else {
// different providers
CopyMoveHelper.copyToForeignTarget(source, target, options);
}
return target;
}/<code>


底層通過SPI,即ServiceLoader的方式加載不同文件系統的本地處理代碼。


分類如下:

面試刷題12:zero copy是怎麼回事?


我們使用最多的UnixFsProvider,實際上是 直接從 用戶態空間copy到用戶態空間,使用了本地方法內聯加持優化,但是它不是zero-copy, 性能也不會太差。

如何提高io的效率


1, 使用緩存,減少io的操作次數;


2,使用zero-copy,即類似 java.nio的 transferTo方法進行copy;


3, 減少傳輸過程中不必要的轉換,比如編解碼,最好直接二進制傳輸;

buffer


buffer的類層級圖如下:

面試刷題12:zero copy是怎麼回事?


除了bool其他7個原生類型都有對應的Buffer;


面試官如果問細節,先說4個屬性, capacity, limit ,position, mark


再描述byteBuffer的讀寫流程。


然後是DirectBuffer,這個是直接操作堆外內存,比較高效。但是用好比較困難,除非是流媒體的行業,不會問的這麼細,直接翻源碼好好準備,問一般也是技術專家來問你了。

小結


本篇回答了什麼是zero-copy,然後介紹了java體系實現文件copy的3中方式,(擴展的第三方庫不算在內);


然後簡要介紹瞭如何提高io效率的三種方法,以及提高內存利用率的Buffer做了系統級的介紹。


不囉嗦,可以快速通過下圖條理化本篇內容,希望對你有所幫助。

面試刷題12:zero copy是怎麼回事?


分享到:


相關文章: