From 742ba32e64d3e3db0283bd2fd2d7a4ae96eef234 Mon Sep 17 00:00:00 2001 From: Carlo Alberto Ferraris Date: Mon, 4 Mar 2019 13:56:51 +0900 Subject: [PATCH] wip --- include/inc_linux/jni_md.h | 1 + include/inc_mac/jni_md.h | 1 + include/inc_win/jni_md.h | 1 + .../java/xerial/larray/impl/LArrayNative.c | 16 +++++++++++++++- .../java/xerial/larray/impl/LArrayNative.h | 8 ++++++++ .../java/xerial/larray/impl/LArrayNative.java | 1 + .../java/xerial/larray/mmap/MMapBuffer.java | 18 ++++++++++++++++++ 7 files changed, 45 insertions(+), 1 deletion(-) diff --git a/include/inc_linux/jni_md.h b/include/inc_linux/jni_md.h index 9b5d1a8..a9f50a6 100644 --- a/include/inc_linux/jni_md.h +++ b/include/inc_linux/jni_md.h @@ -20,5 +20,6 @@ typedef long long jlong; #endif typedef signed char jbyte; +typedef unsigned char jboolean; #endif /* !_JAVASOFT_JNI_MD_H_ */ diff --git a/include/inc_mac/jni_md.h b/include/inc_mac/jni_md.h index 21cc90b..88c315c 100644 --- a/include/inc_mac/jni_md.h +++ b/include/inc_mac/jni_md.h @@ -19,5 +19,6 @@ typedef long jint; #endif typedef long long jlong; typedef signed char jbyte; +typedef unsigned char jboolean; #endif /* !_JAVASOFT_JNI_MD_H_ */ diff --git a/include/inc_win/jni_md.h b/include/inc_win/jni_md.h index f1a3354..6605ce1 100644 --- a/include/inc_win/jni_md.h +++ b/include/inc_win/jni_md.h @@ -15,5 +15,6 @@ typedef long jint; typedef __int64 jlong; typedef signed char jbyte; +typedef unsigned char jboolean; #endif /* !_JAVASOFT_JNI_MD_H_ */ diff --git a/larray-mmap/src/main/java/xerial/larray/impl/LArrayNative.c b/larray-mmap/src/main/java/xerial/larray/impl/LArrayNative.c index a0dc5a1..6ca3153 100755 --- a/larray-mmap/src/main/java/xerial/larray/impl/LArrayNative.c +++ b/larray-mmap/src/main/java/xerial/larray/impl/LArrayNative.c @@ -109,7 +109,7 @@ JNIEXPORT void JNICALL Java_xerial_larray_impl_LArrayNative_munmap JNIEXPORT void JNICALL Java_xerial_larray_impl_LArrayNative_msync -(JNIEnv *env, jclass cls, jlong fd, jlong addr, jlong size) { + (JNIEnv *env, jclass cls, jlong fd, jlong addr, jlong size) { #if defined(_WIN32) || defined(_WIN64) void *a = (void *) addr; @@ -161,3 +161,17 @@ JNIEXPORT jlong JNICALL Java_xerial_larray_impl_LArrayNative_duplicateHandle #endif } + +JNIEXPORT jboolean JNICALL Java_xerial_larray_impl_LArrayNative_prefetch + (JNIEnv *env, jclass cls, jlong addr, jlong size) { + +#if defined(_WIN32) || defined(_WIN64) + WIN32_MEMORY_RANGE_ENTRY range = {(PVOID) addr, (SIZE_T) size}; + // PrefetchVirtualMemory returns non-zero on success + return PrefetchVirtualMemory(GetCurrentProcess(), 1, &range, 0) != 0 ? JNI_TRUE : JNI_FALSE; +#else + // posix_madvise returns zero on success + return posix_madvise((void *) addr, (size_t) size, POSIX_MADV_WILLNEED) == 0 ? JNI_TRUE : JNI_FALSE; +#endif + +} \ No newline at end of file diff --git a/larray-mmap/src/main/java/xerial/larray/impl/LArrayNative.h b/larray-mmap/src/main/java/xerial/larray/impl/LArrayNative.h index fdd8e9f..267de06 100644 --- a/larray-mmap/src/main/java/xerial/larray/impl/LArrayNative.h +++ b/larray-mmap/src/main/java/xerial/larray/impl/LArrayNative.h @@ -55,6 +55,14 @@ JNIEXPORT void JNICALL Java_xerial_larray_impl_LArrayNative_msync JNIEXPORT jlong JNICALL Java_xerial_larray_impl_LArrayNative_duplicateHandle (JNIEnv *, jclass, jlong); +/* + * Class: xerial_larray_impl_LArrayNative + * Method: prefetch + * Signature: (JJ)Z + */ +JNIEXPORT jboolean JNICALL Java_xerial_larray_impl_LArrayNative_prefetch + (JNIEnv *, jclass, jlong, jlong); + #ifdef __cplusplus } #endif diff --git a/larray-mmap/src/main/java/xerial/larray/impl/LArrayNative.java b/larray-mmap/src/main/java/xerial/larray/impl/LArrayNative.java index 1c030e6..a831cc3 100644 --- a/larray-mmap/src/main/java/xerial/larray/impl/LArrayNative.java +++ b/larray-mmap/src/main/java/xerial/larray/impl/LArrayNative.java @@ -37,4 +37,5 @@ public class LArrayNative { public static native void munmap(long address, long size); public static native void msync(long handle, long address, long size); public static native long duplicateHandle(long handle); + public static native boolean prefetch(long address, long size); } diff --git a/larray-mmap/src/main/java/xerial/larray/mmap/MMapBuffer.java b/larray-mmap/src/main/java/xerial/larray/mmap/MMapBuffer.java index 4307625..7ba60fd 100644 --- a/larray-mmap/src/main/java/xerial/larray/mmap/MMapBuffer.java +++ b/larray-mmap/src/main/java/xerial/larray/mmap/MMapBuffer.java @@ -121,6 +121,24 @@ public void close() throws IOException { fc.close(); } + /** + * Attempts to prefetch the specified range in memory so that followup + * accesses do not incur I/O. This method is best-effort: there are no + * guarantees that the JVM and the OS will honor this request, and even + * if they do there are no guarantees for how long the data in the range + * will remain in memory before being paged out. When this method + * returns the prefetch operation may be still ongoing. + * You should only attempt to prefetch data you are going to access soon, + * and most likely length should be significantly smaller than the size of + * the page cache. + * This method returns false if the prefetch request could not be issued + * to the OS; true otherwise. Note that the best-effort behavior described + * above applies even if prefetch returns true. + */ + public bool prefetch(long offset, long length) { + return LArrayNative.prefetch(m.headerAddress()+offset, length); + } + protected long offset() { return pagePosition; }