面试刷题12:zero copy是怎么回事?

  • 2020 年 3 月 26 日
  • 筆記

image.png

文件copy是java的io部分不可忽视的内容。

我是李福春,我在准备面试,今天的问题是:

zero-copy是怎么回事?

操作系统的空间划分为内核态空间, 用户态空间;

内核态空间相对操作系统具备更高的权限和优先级;

用户态空间即普通用户所处空间。

zero-copy指的使用类似java.nio的transforTo方法进行文件copy,文件的copy直接从磁盘到内核态空间,不经过用户态空间,再写到磁盘,减少了io的消耗,避免了不必要的copy 和上下文切换,所以比较高效。

接下来对面试官可能扩展的问题进行一些拓展:

java的文件copy方式

java.io流式copy

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();          }        }    }  

代码可以运行;copy流程如下图:它不是zero-copy的,需要切换用户态空间和内核态空间,路径比较长,io消耗和上线文切换的消耗比较明显,这是比较低效的copy.

image.png

java.nioChannel式copy

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();          }        }    }    

copy过程如下图:明显,不用经过用户态空间,是zero-copy,减少了io的消耗以及上下文切换,比较高效。

image.png

Files工具类copy

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();          }        }    }  

面试官一般喜欢刨根问底,那么来吧!贴一下源码:

 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;      }  

底层通过SPI,即ServiceLoader的方式加载不同文件系统的本地处理代码。

分类如下:

image.png

我们使用最多的UnixFsProvider,实际上是 直接从 用户态空间copy到用户态空间,使用了本地方法内联加持优化,但是它不是zero-copy, 性能也不会太差。

如何提高io的效率

1, 使用缓存,减少io的操作次数;

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

3, 减少传输过程中不必要的转换,比如编解码,最好直接二进制传输;

buffer

buffer的类层级图如下:

image.png

除了bool其他7个原生类型都有对应的Buffer;

面试官如果问细节,先说4个属性, capacity, limit ,position, mark

再描述byteBuffer的读写流程。

然后是DirectBuffer,这个是直接操作堆外内存,比较高效。但是用好比较困难,除非是流媒体的行业,不会问的这么细,直接翻源码好好准备,问一般也是技术专家来问你了。

小结

本篇回答了什么是zero-copy,然后介绍了java体系实现文件copy的3中方式,(扩展的第三方库不算在内);

然后简要介绍了如何提高io效率的三种方法,以及提高内存利用率的Buffer做了系统级的介绍。

不啰嗦,可以快速通过下图条理化本篇内容,希望对你有所帮助。

image.png

原创不易,转载请注明出处。