test/jdk/java/nio/channels/FileChannel/directio/libDirectIO.c
author bpb
Wed, 25 Oct 2017 16:13:53 -0700
changeset 47448 75c90020d8e0
parent 47428 d72d7d55c765
permissions -rw-r--r--
8189775: java/nio/channels/FileChannel/directio/ReadDirect.java failed with NumberFormatException Summary: Clamp the offset so the scattering read remains within the channel. Reviewed-by: rriggs
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 if a file exists in the file system cache
d72d7d55c765 8164900: Add support for O_DIRECT
bpb
parents:
diff changeset
    26
 */
d72d7d55c765 8164900: Add support for O_DIRECT
bpb
parents:
diff changeset
    27
#include <stdlib.h>
d72d7d55c765 8164900: Add support for O_DIRECT
bpb
parents:
diff changeset
    28
#include <fcntl.h>
d72d7d55c765 8164900: Add support for O_DIRECT
bpb
parents:
diff changeset
    29
#include <sys/types.h>
d72d7d55c765 8164900: Add support for O_DIRECT
bpb
parents:
diff changeset
    30
#include <sys/stat.h>
d72d7d55c765 8164900: Add support for O_DIRECT
bpb
parents:
diff changeset
    31
#include <unistd.h>
d72d7d55c765 8164900: Add support for O_DIRECT
bpb
parents:
diff changeset
    32
#include <sys/mman.h>
d72d7d55c765 8164900: Add support for O_DIRECT
bpb
parents:
diff changeset
    33
d72d7d55c765 8164900: Add support for O_DIRECT
bpb
parents:
diff changeset
    34
#include "jni.h"
d72d7d55c765 8164900: Add support for O_DIRECT
bpb
parents:
diff changeset
    35
d72d7d55c765 8164900: Add support for O_DIRECT
bpb
parents:
diff changeset
    36
/*
d72d7d55c765 8164900: Add support for O_DIRECT
bpb
parents:
diff changeset
    37
 * Throws an exception with the given class name and detail message
d72d7d55c765 8164900: Add support for O_DIRECT
bpb
parents:
diff changeset
    38
 */
d72d7d55c765 8164900: Add support for O_DIRECT
bpb
parents:
diff changeset
    39
static void ThrowException(JNIEnv *env, const char *name, const char *msg) {
d72d7d55c765 8164900: Add support for O_DIRECT
bpb
parents:
diff changeset
    40
    jclass cls = (*env)->FindClass(env, name);
d72d7d55c765 8164900: Add support for O_DIRECT
bpb
parents:
diff changeset
    41
    if (cls != NULL) {
d72d7d55c765 8164900: Add support for O_DIRECT
bpb
parents:
diff changeset
    42
        (*env)->ThrowNew(env, cls, msg);
d72d7d55c765 8164900: Add support for O_DIRECT
bpb
parents:
diff changeset
    43
    }
d72d7d55c765 8164900: Add support for O_DIRECT
bpb
parents:
diff changeset
    44
}
d72d7d55c765 8164900: Add support for O_DIRECT
bpb
parents:
diff changeset
    45
d72d7d55c765 8164900: Add support for O_DIRECT
bpb
parents:
diff changeset
    46
/*
d72d7d55c765 8164900: Add support for O_DIRECT
bpb
parents:
diff changeset
    47
 * Class:     DirectIO
d72d7d55c765 8164900: Add support for O_DIRECT
bpb
parents:
diff changeset
    48
 * Method:    isFileInCache0
d72d7d55c765 8164900: Add support for O_DIRECT
bpb
parents:
diff changeset
    49
 * Signature: (ILjava/lang/String;)Z
d72d7d55c765 8164900: Add support for O_DIRECT
bpb
parents:
diff changeset
    50
 */
d72d7d55c765 8164900: Add support for O_DIRECT
bpb
parents:
diff changeset
    51
JNIEXPORT jboolean Java_DirectIOTest_isFileInCache0(JNIEnv *env,
d72d7d55c765 8164900: Add support for O_DIRECT
bpb
parents:
diff changeset
    52
                                                jclass cls,
d72d7d55c765 8164900: Add support for O_DIRECT
bpb
parents:
diff changeset
    53
                                                jint file_size,
d72d7d55c765 8164900: Add support for O_DIRECT
bpb
parents:
diff changeset
    54
                                                jstring file_path) {
d72d7d55c765 8164900: Add support for O_DIRECT
bpb
parents:
diff changeset
    55
    void *f_mmap;
d72d7d55c765 8164900: Add support for O_DIRECT
bpb
parents:
diff changeset
    56
#ifdef __linux__
d72d7d55c765 8164900: Add support for O_DIRECT
bpb
parents:
diff changeset
    57
    unsigned char *f_seg;
d72d7d55c765 8164900: Add support for O_DIRECT
bpb
parents:
diff changeset
    58
#else
d72d7d55c765 8164900: Add support for O_DIRECT
bpb
parents:
diff changeset
    59
    char *f_seg;
d72d7d55c765 8164900: Add support for O_DIRECT
bpb
parents:
diff changeset
    60
#endif
d72d7d55c765 8164900: Add support for O_DIRECT
bpb
parents:
diff changeset
    61
d72d7d55c765 8164900: Add support for O_DIRECT
bpb
parents:
diff changeset
    62
#ifdef __APPLE__
d72d7d55c765 8164900: Add support for O_DIRECT
bpb
parents:
diff changeset
    63
    size_t mask = MINCORE_INCORE;
d72d7d55c765 8164900: Add support for O_DIRECT
bpb
parents:
diff changeset
    64
#else
d72d7d55c765 8164900: Add support for O_DIRECT
bpb
parents:
diff changeset
    65
    size_t mask = 0x1;
d72d7d55c765 8164900: Add support for O_DIRECT
bpb
parents:
diff changeset
    66
#endif
d72d7d55c765 8164900: Add support for O_DIRECT
bpb
parents:
diff changeset
    67
d72d7d55c765 8164900: Add support for O_DIRECT
bpb
parents:
diff changeset
    68
    size_t page_size = getpagesize();
d72d7d55c765 8164900: Add support for O_DIRECT
bpb
parents:
diff changeset
    69
    size_t index = (file_size + page_size - 1) /page_size;
d72d7d55c765 8164900: Add support for O_DIRECT
bpb
parents:
diff changeset
    70
    jboolean result = JNI_FALSE;
d72d7d55c765 8164900: Add support for O_DIRECT
bpb
parents:
diff changeset
    71
d72d7d55c765 8164900: Add support for O_DIRECT
bpb
parents:
diff changeset
    72
    const char* path = (*env)->GetStringUTFChars(env, file_path, JNI_FALSE);
d72d7d55c765 8164900: Add support for O_DIRECT
bpb
parents:
diff changeset
    73
d72d7d55c765 8164900: Add support for O_DIRECT
bpb
parents:
diff changeset
    74
    int fd = open(path, O_RDWR);
d72d7d55c765 8164900: Add support for O_DIRECT
bpb
parents:
diff changeset
    75
d72d7d55c765 8164900: Add support for O_DIRECT
bpb
parents:
diff changeset
    76
    (*env)->ReleaseStringUTFChars(env, file_path, path);
d72d7d55c765 8164900: Add support for O_DIRECT
bpb
parents:
diff changeset
    77
d72d7d55c765 8164900: Add support for O_DIRECT
bpb
parents:
diff changeset
    78
    f_mmap = mmap(0, file_size, PROT_NONE, MAP_SHARED, fd, 0);
d72d7d55c765 8164900: Add support for O_DIRECT
bpb
parents:
diff changeset
    79
    if (f_mmap == MAP_FAILED) {
d72d7d55c765 8164900: Add support for O_DIRECT
bpb
parents:
diff changeset
    80
        close(fd);
d72d7d55c765 8164900: Add support for O_DIRECT
bpb
parents:
diff changeset
    81
        ThrowException(env, "java/io/IOException",
d72d7d55c765 8164900: Add support for O_DIRECT
bpb
parents:
diff changeset
    82
            "test of whether file exists in cache failed");
d72d7d55c765 8164900: Add support for O_DIRECT
bpb
parents:
diff changeset
    83
    }
d72d7d55c765 8164900: Add support for O_DIRECT
bpb
parents:
diff changeset
    84
    f_seg = malloc(index);
d72d7d55c765 8164900: Add support for O_DIRECT
bpb
parents:
diff changeset
    85
    if (f_seg != NULL) {
d72d7d55c765 8164900: Add support for O_DIRECT
bpb
parents:
diff changeset
    86
        if(mincore(f_mmap, file_size, f_seg) == 0) {
d72d7d55c765 8164900: Add support for O_DIRECT
bpb
parents:
diff changeset
    87
            size_t i;
d72d7d55c765 8164900: Add support for O_DIRECT
bpb
parents:
diff changeset
    88
            for (i = 0; i < index; i++) {
d72d7d55c765 8164900: Add support for O_DIRECT
bpb
parents:
diff changeset
    89
                if (f_seg[i] & mask) {
d72d7d55c765 8164900: Add support for O_DIRECT
bpb
parents:
diff changeset
    90
                    result = JNI_TRUE;
d72d7d55c765 8164900: Add support for O_DIRECT
bpb
parents:
diff changeset
    91
                    break;
d72d7d55c765 8164900: Add support for O_DIRECT
bpb
parents:
diff changeset
    92
                }
d72d7d55c765 8164900: Add support for O_DIRECT
bpb
parents:
diff changeset
    93
            }
d72d7d55c765 8164900: Add support for O_DIRECT
bpb
parents:
diff changeset
    94
        }
d72d7d55c765 8164900: Add support for O_DIRECT
bpb
parents:
diff changeset
    95
        free(f_seg);
d72d7d55c765 8164900: Add support for O_DIRECT
bpb
parents:
diff changeset
    96
    } else {
d72d7d55c765 8164900: Add support for O_DIRECT
bpb
parents:
diff changeset
    97
        ThrowException(env, "java/io/IOException",
d72d7d55c765 8164900: Add support for O_DIRECT
bpb
parents:
diff changeset
    98
            "test of whether file exists in cache failed");
d72d7d55c765 8164900: Add support for O_DIRECT
bpb
parents:
diff changeset
    99
    }
d72d7d55c765 8164900: Add support for O_DIRECT
bpb
parents:
diff changeset
   100
    close(fd);
d72d7d55c765 8164900: Add support for O_DIRECT
bpb
parents:
diff changeset
   101
    munmap(f_mmap, file_size);
d72d7d55c765 8164900: Add support for O_DIRECT
bpb
parents:
diff changeset
   102
    return result;
d72d7d55c765 8164900: Add support for O_DIRECT
bpb
parents:
diff changeset
   103
}