< prev index next >

test/jdk/java/nio/channels/FileChannel/directio/DirectIOTest.java

Print this page


   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  */


  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 }
   1 /*
   2  * Copyright (c) 2017, 2021, 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  */


  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 BASE_SIZE = 4096;
  51     private static long blockSize;
  52 
  53     private static int testWrite(Path p) throws Exception {
  54         try (FileChannel fc = FileChannel.open(p, StandardOpenOption.WRITE,
  55              ExtendedOpenOption.DIRECT)) {
  56             int bs = (int)blockSize;
  57             int size = Math.max(BASE_SIZE, bs);
  58             int alignment = bs;
  59             ByteBuffer src = ByteBuffer.allocateDirect(size + alignment - 1)
  60                                        .alignedSlice(alignment);
  61             assert src.capacity() != 0;
  62             for (int j = 0; j < size; j++) {
  63                 src.put((byte)0);
  64             }
  65             src.flip();
  66             fc.write(src);
  67             return size;
  68         }
  69     }
  70 
  71     private static int testRead(Path p) throws Exception {
  72         try (FileChannel fc = FileChannel.open(p, ExtendedOpenOption.DIRECT)) {
  73             int bs = (int)blockSize;
  74             int size = Math.max(BASE_SIZE, bs);
  75             int alignment = bs;
  76             ByteBuffer dest = ByteBuffer.allocateDirect(size + alignment - 1)
  77                                         .alignedSlice(alignment);
  78             assert dest.capacity() != 0;
  79             fc.read(dest);
  80             return size;
  81         }
  82     }
  83 
  84     public static Path createTempFile() throws IOException {
  85         return Files.createTempFile(
  86             Paths.get(System.getProperty("test.dir", ".")), "test", null);
  87     }
  88 
  89     public static boolean isDirectIOSupportedByFS(Path p) throws Exception {
  90         boolean supported = true;
  91         if (Platform.isSolaris()) {
  92             String fsType = Files.getFileStore(p).type();
  93             if (!fsType.equals("nfs") && !fsType.equals("ufs")) {
  94                 // print a message and return without failing
  95                 System.out.format("Skipping test: file system type %s of "
  96                     + "FileStore of %s is neither nfs nor ufs.%n", fsType, p);
  97                 supported = false;
  98             }
  99         }
 100         return supported;
 101     }
 102 
 103     private static boolean isFileInCache(int size, Path p) {
 104         String path = p.toString();
 105         return isFileInCache0(size, path);
 106     }
 107 
 108     private static native boolean isFileInCache0(int size, String path);
 109 
 110     public static void main(String[] args) throws Exception {
 111         Path p = createTempFile();
 112         blockSize = Files.getFileStore(p).getBlockSize();
 113 
 114         if (!isDirectIOSupportedByFS(p)) {
 115             Files.delete(p);
 116             return;
 117         }
 118 
 119         System.loadLibrary("DirectIO");
 120 
 121         try {
 122             int size = testWrite(p);
 123             if (isFileInCache(size, p)) {
 124                 throw new RuntimeException("DirectIO is not working properly with "
 125                     + "write. File still exists in cache!");
 126             }
 127             size = testRead(p);
 128             if (isFileInCache(size, p)) {
 129                 throw new RuntimeException("DirectIO is not working properly with "
 130                     + "read. File still exists in cache!");
 131             }
 132         } finally {
 133             Files.delete(p);
 134         }
 135     }
 136 }
< prev index next >