Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow prefetching of a range of mapped file #66

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions include/inc_linux/jni_md.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,5 +20,6 @@ typedef long long jlong;
#endif

typedef signed char jbyte;
typedef unsigned char jboolean;

#endif /* !_JAVASOFT_JNI_MD_H_ */
1 change: 1 addition & 0 deletions include/inc_mac/jni_md.h
Original file line number Diff line number Diff line change
Expand Up @@ -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_ */
1 change: 1 addition & 0 deletions include/inc_win/jni_md.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,5 +15,6 @@
typedef long jint;
typedef __int64 jlong;
typedef signed char jbyte;
typedef unsigned char jboolean;

#endif /* !_JAVASOFT_JNI_MD_H_ */
16 changes: 15 additions & 1 deletion larray-mmap/src/main/java/xerial/larray/impl/LArrayNative.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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

}
8 changes: 8 additions & 0 deletions larray-mmap/src/main/java/xerial/larray/impl/LArrayNative.h

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
18 changes: 18 additions & 0 deletions larray-mmap/src/main/java/xerial/larray/mmap/MMapBuffer.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down