1 /* 2 * Copyright (c) 2017, 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 == "solaris" 29 * | os.family == "aix") 30 * @library /test/lib 31 * @build jdk.test.lib.Platform 32 * @run main/native DirectIOTest 33 */ 34 35 import java.io.*; 36 import java.nio.ByteBuffer; 37 import java.nio.CharBuffer; 38 import java.nio.channels.*; 39 import java.nio.channels.FileChannel; 40 import java.nio.file.Paths; 41 import java.nio.file.Path; 42 import java.nio.file.Files; 43 import jdk.test.lib.Platform; 44 import java.nio.file.FileStore; 45 import java.nio.file.StandardOpenOption; 46 import com.sun.nio.file.ExtendedOpenOption; 47 48 public class DirectIOTest { 49 50 private static final int SIZE = 4096; 51 52 private static void testWrite(Path p) throws Exception { 53 try (FileChannel fc = FileChannel.open(p, StandardOpenOption.WRITE, 54 ExtendedOpenOption.DIRECT)) { 55 FileStore fs = Files.getFileStore(p); 56 int alignment = (int)fs.getBlockSize(); 57 ByteBuffer src = ByteBuffer.allocateDirect(SIZE + alignment - 1) 58 .alignedSlice(alignment); 59 for (int j = 0; j < SIZE; j++) { 60 src.put((byte)0); 61 } 62 src.flip(); 63 fc.write(src); 64 } 65 } 66 67 private static void testRead(Path p) throws Exception { 68 try (FileChannel fc = FileChannel.open(p, ExtendedOpenOption.DIRECT)) { 69 FileStore fs = Files.getFileStore(p); 70 int alignment = (int)fs.getBlockSize(); 71 ByteBuffer dest = ByteBuffer.allocateDirect(SIZE + alignment - 1) 72 .alignedSlice(alignment); 73 fc.read(dest); 74 } 75 } 76 77 public static Path createTempFile() throws IOException { 78 return Files.createTempFile( 79 Paths.get(System.getProperty("test.dir", ".")), "test", null); 80 } 81 82 public static boolean isDirectIOSupportedByFS(Path p) throws Exception { 83 boolean supported = true; 84 if (Platform.isSolaris()) { 85 String fsType = Files.getFileStore(p).type(); 86 if (!fsType.equals("nfs") && !fsType.equals("ufs")) { 87 // print a message and return without failing 88 System.out.format("Skipping test: file system type %s of " 89 + "FileStore of %s is neither nfs nor ufs.%n", fsType, p); 90 supported = false; 91 } 92 } 93 return supported; 94 } 95 96 private static boolean isFileInCache(Path p) { 97 String path = p.toString(); 98 return isFileInCache0(SIZE, path); 99 } 100 101 private static native boolean isFileInCache0(int size, String path); 102 103 public static void main(String[] args) throws Exception { 104 Path p = createTempFile(); 105 106 if (!isDirectIOSupportedByFS(p)) { 107 Files.delete(p); 108 return; 109 } 110 111 System.loadLibrary("DirectIO"); 112 113 try { 114 testWrite(p); 115 if (isFileInCache(p)) { 116 throw new RuntimeException("DirectIO is not working properly with " 117 + "write. File still exists in cache!"); 118 } 119 testRead(p); 120 if (isFileInCache(p)) { 121 throw new RuntimeException("DirectIO is not working properly with " 122 + "read. File still exists in cache!"); 123 } 124 } finally { 125 Files.delete(p); 126 } 127 } 128 }