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

image.png








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


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


zero-copy是怎麼回事?


操做系統的空間劃分爲內核態空間, 用戶態空間;


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


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


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


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

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

    }

}




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

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的方式加載不一樣文件系統的本地處理代碼。


分類以下:






ide

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,這個是直接操做堆外內存,比較高效。可是用比如較困難,除非是流媒體的行業,不會問的這麼細,直接翻源碼好好準備,問通常也是技術專家來問你了。


spa

小結


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


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


不囉嗦,能夠快速經過下圖條理化本篇內容,但願對你有所幫助。


操作系統

image.png

原創不易,轉載請註明出處,讓咱們互通有無,共同進步,歡迎溝通交流。
相關文章
相關標籤/搜索