博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
面试刷题12:zero copy是怎么回事?
阅读量:4210 次
发布时间:2019-05-26

本文共 6334 字,大约阅读时间需要 21 分钟。

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

原创不易,转载请注明出处,让我们互通有无,共同进步,欢迎多沟通交流

你可能感兴趣的文章
记腾讯互娱网站布局(1)
查看>>
记腾讯互娱网站布局(2)
查看>>
记腾讯互娱网站布局(3)
查看>>
大小不固定的图片和多行文字的垂直水平居中
查看>>
display:table-cell的集中应用
查看>>
display:table-cell自适应布局下连续单词字符换行
查看>>
0115 springboot template方式操作mongodb
查看>>
0116 spring的webFlux
查看>>
解决 Asp.net 中,url传参乱码 方法之一:(UrlDecode)
查看>>
pdf的转换网址:
查看>>
c++设计模式之三~抽象工厂模式
查看>>
c++设计模式之单例模式
查看>>
c++设计模式之建造者模式
查看>>
c++设计模式之原型模式
查看>>
c++设计模式之适配器模式
查看>>
c++设计模式之桥接模式
查看>>
c++设计模式之装饰模式
查看>>
Mysql学习笔记(八)- 两个简单实用的优化方法
查看>>
mysql学习笔记(九)- 增删改查的优化
查看>>
Jenkins学习笔记(一)
查看>>