1 /* 2 * Copyright (c) 2017, 2020, Oracle and/or its affiliates. All rights reserved. 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 4 * 5 * This code is free software; you can redistribute it and/or modify it 6 * under the terms of the GNU General Public License version 2 only, as 7 * published by the Free Software Foundation. 8 * 9 * This code is distributed in the hope that it will be useful, but WITHOUT 10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 12 * version 2 for more details (a copy is included in the LICENSE file that 13 * accompanied this code). 14 * 15 * You should have received a copy of the GNU General Public License version 16 * 2 along with this work; if not, write to the Free Software Foundation, 17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 18 * 19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 20 * or visit www.oracle.com if you need additional information or have any 21 * questions. 22 */ 23 24 /* 25 * @test 26 * @bug 8164900 27 * @summary Test for ExtendedOpenOption.DIRECT flag 28 * @requires (os.family == "linux" | os.family == "aix") 29 * @library /test/lib 30 * @build jdk.test.lib.Platform 31 * @run main/native DirectIOTest 32 */ 33 34 import java.io.*; 35 import java.nio.ByteBuffer; 36 import java.nio.CharBuffer; 37 import java.nio.channels.*; 38 import java.nio.channels.FileChannel; 39 import java.nio.file.Paths; 40 import java.nio.file.Path; 41 import java.nio.file.Files; 42 import jdk.test.lib.Platform; 43 import java.nio.file.FileStore; 44 import java.nio.file.StandardOpenOption; 45 import com.sun.nio.file.ExtendedOpenOption; 46 47 public class DirectIOTest { 48 49 private static final int SIZE = 4096; 50 51 private static void testWrite(Path p) throws Exception { 52 try (FileChannel fc = FileChannel.open(p, StandardOpenOption.WRITE, 53 ExtendedOpenOption.DIRECT)) { 54 FileStore fs = Files.getFileStore(p); 55 int alignment = (int)fs.getBlockSize(); 56 ByteBuffer src = ByteBuffer.allocateDirect(SIZE + alignment - 1) 57 .alignedSlice(alignment); 58 for (int j = 0; j < SIZE; j++) { 59 src.put((byte)0); 60 } 61 src.flip(); 62 fc.write(src); 63 } 64 } 65 66 private static void testRead(Path p) throws Exception { 67 try (FileChannel fc = FileChannel.open(p, ExtendedOpenOption.DIRECT)) { 68 FileStore fs = Files.getFileStore(p); 69 int alignment = (int)fs.getBlockSize(); 70 ByteBuffer dest = ByteBuffer.allocateDirect(SIZE + alignment - 1) 71 .alignedSlice(alignment); 72 fc.read(dest); 73 } 74 } 75 76 public static Path createTempFile() throws IOException { 77 return Files.createTempFile( 78 Paths.get(System.getProperty("test.dir", ".")), "test", null); 79 } 80 81 public static boolean isDirectIOSupportedByFS(Path p) throws Exception { 82 return true; 83 } 84 85 private static boolean isFileInCache(Path p) { 86 String path = p.toString(); 87 return isFileInCache0(SIZE, path); 88 } 89 90 private static native boolean isFileInCache0(int size, String path); 91 92 public static void main(String[] args) throws Exception { 93 Path p = createTempFile(); 94 95 if (!isDirectIOSupportedByFS(p)) { 96 Files.delete(p); 97 return; 98 } 99 100 System.loadLibrary("DirectIO"); 101 102 try { 103 testWrite(p); 104 if (isFileInCache(p)) { 105 throw new RuntimeException("DirectIO is not working properly with " 106 + "write. File still exists in cache!"); 107 } 108 testRead(p); 109 if (isFileInCache(p)) { 110 throw new RuntimeException("DirectIO is not working properly with " 111 + "read. File still exists in cache!"); 112 } 113 } finally { 114 Files.delete(p); 115 } 116 } 117 }