From 520e458d4f7d742ddde36b8b370a623b07270adb Mon Sep 17 00:00:00 2001 From: Chris Nauroth Date: Thu, 10 Jul 2014 23:26:16 +0000 Subject: [PATCH] HADOOP-10815. Implement Windows equivalent of mlock. Contributed by Chris Nauroth. git-svn-id: https://svn.apache.org/repos/asf/hadoop/common/trunk@1609588 13f79535-47bb-0310-9956-ffa450edef68 --- .../hadoop-common/CHANGES.txt | 2 + .../apache/hadoop/io/nativeio/NativeIO.java | 13 ++++++ .../org/apache/hadoop/io/nativeio/NativeIO.c | 42 +++++++++++++++++-- .../hadoop/io/nativeio/TestNativeIO.java | 2 - 4 files changed, 54 insertions(+), 5 deletions(-) diff --git a/hadoop-common-project/hadoop-common/CHANGES.txt b/hadoop-common-project/hadoop-common/CHANGES.txt index 2272f8865e1..996262f1630 100644 --- a/hadoop-common-project/hadoop-common/CHANGES.txt +++ b/hadoop-common-project/hadoop-common/CHANGES.txt @@ -393,6 +393,8 @@ Release 2.6.0 - UNRELEASED HADOOP-10808. Remove unused native code for munlock. (cnauroth) + HADOOP-10815. Implement Windows equivalent of mlock. (cnauroth) + OPTIMIZATIONS BUG FIXES diff --git a/hadoop-common-project/hadoop-common/src/main/java/org/apache/hadoop/io/nativeio/NativeIO.java b/hadoop-common-project/hadoop-common/src/main/java/org/apache/hadoop/io/nativeio/NativeIO.java index 423383e4d1a..976a93f91b1 100644 --- a/hadoop-common-project/hadoop-common/src/main/java/org/apache/hadoop/io/nativeio/NativeIO.java +++ b/hadoop-common-project/hadoop-common/src/main/java/org/apache/hadoop/io/nativeio/NativeIO.java @@ -551,6 +551,19 @@ public class NativeIO { return access0(path, desiredAccess.accessRight()); } + /** + * Extends both the minimum and maximum working set size of the current + * process. This method gets the current minimum and maximum working set + * size, adds the requested amount to each and then sets the minimum and + * maximum working set size to the new values. Controlling the working set + * size of the process also controls the amount of memory it can lock. + * + * @param delta amount to increment minimum and maximum working set size + * @throws IOException for any error + * @see POSIX#mlock(ByteBuffer, long) + */ + public static native void extendWorkingSetSize(long delta) throws IOException; + static { if (NativeCodeLoader.isNativeCodeLoaded()) { try { diff --git a/hadoop-common-project/hadoop-common/src/main/native/src/org/apache/hadoop/io/nativeio/NativeIO.c b/hadoop-common-project/hadoop-common/src/main/native/src/org/apache/hadoop/io/nativeio/NativeIO.c index b19d88c334c..e3ad3272423 100644 --- a/hadoop-common-project/hadoop-common/src/main/native/src/org/apache/hadoop/io/nativeio/NativeIO.c +++ b/hadoop-common-project/hadoop-common/src/main/native/src/org/apache/hadoop/io/nativeio/NativeIO.c @@ -388,10 +388,10 @@ Java_org_apache_hadoop_io_nativeio_NativeIO_00024POSIX_mlock_1native( JNIEnv *env, jclass clazz, jobject buffer, jlong len) { -#ifdef UNIX void* buf = (void*)(*env)->GetDirectBufferAddress(env, buffer); PASS_EXCEPTIONS(env); +#ifdef UNIX if (mlock(buf, len)) { CHECK_DIRECT_BUFFER_ADDRESS(buf); throw_ioe(env, errno); @@ -399,8 +399,10 @@ Java_org_apache_hadoop_io_nativeio_NativeIO_00024POSIX_mlock_1native( #endif #ifdef WINDOWS - THROW(env, "java/io/IOException", - "The function POSIX.mlock_native() is not supported on Windows"); + if (!VirtualLock(buf, len)) { + CHECK_DIRECT_BUFFER_ADDRESS(buf); + throw_ioe(env, GetLastError()); + } #endif } @@ -980,6 +982,40 @@ cleanup: #endif } +/* + * Class: org_apache_hadoop_io_nativeio_NativeIO_Windows + * Method: extendWorkingSetSize + * Signature: (J)V + * + * The "00024" in the function name is an artifact of how JNI encodes + * special characters. U+0024 is '$'. + */ +JNIEXPORT void JNICALL +Java_org_apache_hadoop_io_nativeio_NativeIO_00024Windows_extendWorkingSetSize( + JNIEnv *env, jclass clazz, jlong delta) +{ +#ifdef UNIX + THROW(env, "java/io/IOException", + "The function extendWorkingSetSize(delta) is not supported on Unix"); +#endif + +#ifdef WINDOWS + SIZE_T min, max; + HANDLE hProcess = GetCurrentProcess(); + if (!GetProcessWorkingSetSize(hProcess, &min, &max)) { + throw_ioe(env, GetLastError()); + return; + } + if (!SetProcessWorkingSetSizeEx(hProcess, min + delta, max + delta, + QUOTA_LIMITS_HARDWS_MIN_DISABLE | QUOTA_LIMITS_HARDWS_MAX_DISABLE)) { + throw_ioe(env, GetLastError()); + return; + } + // There is no need to call CloseHandle on the pseudo-handle returned from + // GetCurrentProcess. +#endif +} + JNIEXPORT void JNICALL Java_org_apache_hadoop_io_nativeio_NativeIO_renameTo0(JNIEnv *env, jclass clazz, jstring jsrc, jstring jdst) diff --git a/hadoop-common-project/hadoop-common/src/test/java/org/apache/hadoop/io/nativeio/TestNativeIO.java b/hadoop-common-project/hadoop-common/src/test/java/org/apache/hadoop/io/nativeio/TestNativeIO.java index dc02409ed44..6c3f0038863 100644 --- a/hadoop-common-project/hadoop-common/src/test/java/org/apache/hadoop/io/nativeio/TestNativeIO.java +++ b/hadoop-common-project/hadoop-common/src/test/java/org/apache/hadoop/io/nativeio/TestNativeIO.java @@ -49,7 +49,6 @@ import org.apache.hadoop.fs.Path; import org.apache.hadoop.fs.permission.FsPermission; import org.apache.hadoop.security.UserGroupInformation; import org.apache.hadoop.util.NativeCodeLoader; -import org.apache.hadoop.util.Shell; import org.apache.hadoop.util.Time; public class TestNativeIO { @@ -572,7 +571,6 @@ public class TestNativeIO { @Test(timeout=10000) public void testMlock() throws Exception { assumeTrue(NativeIO.isAvailable()); - assumeTrue(Shell.LINUX); final File TEST_FILE = new File(new File( System.getProperty("test.build.data","build/test/data")), "testMlockFile");