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