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