Path: blob/master/test/jdk/java/nio/channels/FileChannel/directio/DirectIOTest.java
41161 views
/*1* Copyright (c) 2017, 2021, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/2223/*24* @test25* @bug 816490026* @summary Test for ExtendedOpenOption.DIRECT flag27* @requires (os.family == "linux" | os.family == "aix")28* @library /test/lib29* @build jdk.test.lib.Platform30* @run main/native DirectIOTest31*/3233import java.io.*;34import java.nio.ByteBuffer;35import java.nio.CharBuffer;36import java.nio.channels.*;37import java.nio.channels.FileChannel;38import java.nio.file.Paths;39import java.nio.file.Path;40import java.nio.file.Files;41import jdk.test.lib.Platform;42import java.nio.file.FileStore;43import java.nio.file.StandardOpenOption;44import com.sun.nio.file.ExtendedOpenOption;4546public class DirectIOTest {4748private static final int BASE_SIZE = 4096;4950private static int testWrite(Path p, long blockSize) throws Exception {51try (FileChannel fc = FileChannel.open(p, StandardOpenOption.WRITE,52ExtendedOpenOption.DIRECT)) {53int bs = (int)blockSize;54int size = Math.max(BASE_SIZE, bs);55int alignment = bs;56ByteBuffer src = ByteBuffer.allocateDirect(size + alignment - 1)57.alignedSlice(alignment);58assert src.capacity() != 0;59for (int j = 0; j < size; j++) {60src.put((byte)0);61}62src.flip();63fc.write(src);64return size;65}66}6768private static int testRead(Path p, long blockSize) throws Exception {69try (FileChannel fc = FileChannel.open(p, ExtendedOpenOption.DIRECT)) {70int bs = (int)blockSize;71int size = Math.max(BASE_SIZE, bs);72int alignment = bs;73ByteBuffer dest = ByteBuffer.allocateDirect(size + alignment - 1)74.alignedSlice(alignment);75assert dest.capacity() != 0;76fc.read(dest);77return size;78}79}8081public static Path createTempFile() throws IOException {82return Files.createTempFile(83Paths.get(System.getProperty("test.dir", ".")), "test", null);84}8586private static boolean isFileInCache(int size, Path p) {87String path = p.toString();88return isFileInCache0(size, path);89}9091private static native boolean isFileInCache0(int size, String path);9293public static void main(String[] args) throws Exception {94Path p = createTempFile();95long blockSize = Files.getFileStore(p).getBlockSize();9697System.loadLibrary("DirectIO");9899try {100int size = testWrite(p, blockSize);101if (isFileInCache(size, p)) {102throw new RuntimeException("DirectIO is not working properly with "103+ "write. File still exists in cache!");104}105size = testRead(p, blockSize);106if (isFileInCache(size, p)) {107throw new RuntimeException("DirectIO is not working properly with "108+ "read. File still exists in cache!");109}110} finally {111Files.delete(p);112}113}114}115116117