4837564: (bf) Please make DirectByteBuffer performance enhancements
authoralanb
Mon, 18 Oct 2010 10:29:59 +0100
changeset 6902 3352f8839320
parent 6897 561a431cf238
child 6903 9a677f58dc85
4837564: (bf) Please make DirectByteBuffer performance enhancements Reviewed-by: chegar
jdk/src/share/classes/java/nio/Direct-X-Buffer.java.template
jdk/src/share/classes/sun/misc/VM.java
jdk/test/java/nio/Buffer/LimitDirectMemory.sh
--- a/jdk/src/share/classes/java/nio/Direct-X-Buffer.java.template	Fri Oct 15 15:09:37 2010 +0100
+++ b/jdk/src/share/classes/java/nio/Direct-X-Buffer.java.template	Mon Oct 18 10:29:59 2010 +0100
@@ -29,6 +29,7 @@
 
 import sun.misc.Cleaner;
 import sun.misc.Unsafe;
+import sun.misc.VM;
 import sun.nio.ch.DirectBuffer;
 
 
@@ -114,8 +115,9 @@
     Direct$Type$Buffer$RW$(int cap) {                   // package-private
 #if[rw]
         super(-1, 0, cap, cap, false);
+        boolean pa = VM.isDirectMemoryPageAligned();
         int ps = Bits.pageSize();
-        int size = cap + ps;
+        long size = Math.max(1L, (long)cap + (pa ? ps : 0));
         Bits.reserveMemory(size, cap);
 
         long base = 0;
@@ -126,7 +128,7 @@
             throw x;
         }
         unsafe.setMemory(base, size, (byte) 0);
-        if (base % ps != 0) {
+        if (pa && (base % ps != 0)) {
             // Round up to page boundary
             address = base + ps - (base & (ps - 1));
         } else {
--- a/jdk/src/share/classes/sun/misc/VM.java	Fri Oct 15 15:09:37 2010 +0100
+++ b/jdk/src/share/classes/sun/misc/VM.java	Mon Oct 18 10:29:59 2010 +0100
@@ -178,6 +178,17 @@
         return directMemory;
     }
 
+    // User-controllable flag that determines if direct buffers should be page
+    // aligned. The "-XX:+PageAlignDirectMemory" option can be used to force
+    // buffers, allocated by ByteBuffer.allocateDirect, to be page aligned.
+    private static boolean pageAlignDirectMemory;
+
+    // Returns {@code true} if the direct buffers should be page aligned. This
+    // variable is initialized by saveAndRemoveProperties.
+    public static boolean isDirectMemoryPageAligned() {
+        return pageAlignDirectMemory;
+    }
+
     // A user-settable boolean to determine whether ClassLoader.loadClass should
     // accept array syntax.  This value may be changed during VM initialization
     // via the system property "sun.lang.ClassLoader.allowArraySyntax".
@@ -252,6 +263,11 @@
             }
         }
 
+        // Check if direct buffers should be page aligned
+        s = (String)props.remove("sun.nio.PageAlignDirectMemory");
+        if ("true".equals(s))
+            pageAlignDirectMemory = true;
+
         // Set a boolean to determine whether ClassLoader.loadClass accepts
         // array syntax.  This value is controlled by the system property
         // "sun.lang.ClassLoader.allowArraySyntax".
--- a/jdk/test/java/nio/Buffer/LimitDirectMemory.sh	Fri Oct 15 15:09:37 2010 +0100
+++ b/jdk/test/java/nio/Buffer/LimitDirectMemory.sh	Mon Oct 18 10:29:59 2010 +0100
@@ -30,18 +30,7 @@
 # @build LimitDirectMemory
 # @run shell LimitDirectMemory.sh
 
-# set platform-dependent variable
-OS=`uname -s`
-case "$OS" in
-  SunOS | Linux ) TMP=/tmp ;;
-  Windows* ) TMP="c:/temp" ;;
-  * )
-    echo "Unrecognized system!"
-    exit 1;
-    ;;
-esac
-
-TMP1=${TMP}/tmp1_$$
+TMP1=tmp_$$
 
 runTest() {
   echo "Testing: $*"
@@ -82,18 +71,21 @@
 
 # Exactly the default amount of memory is available.
 runTest -cp ${TESTCLASSES} LimitDirectMemory false 10 1
-runTest -cp ${TESTCLASSES} LimitDirectMemory false 0 DEFAULT
-runTest -cp ${TESTCLASSES} LimitDirectMemory true 0 DEFAULT+1
+runTest -Xmx64m -cp ${TESTCLASSES} LimitDirectMemory false 0 DEFAULT
+runTest -Xmx64m -cp ${TESTCLASSES} LimitDirectMemory true 0 DEFAULT+1
 
 # We should be able to eliminate direct memory allocation entirely.
 runTest -XX:MaxDirectMemorySize=0 -cp ${TESTCLASSES} LimitDirectMemory true 0 1
 
 # Setting the system property should not work so we should be able to allocate
 # the default amount.
-runTest -Dsun.nio.MaxDirectMemorySize=1K -cp ${TESTCLASSES} \
+runTest -Dsun.nio.MaxDirectMemorySize=1K -Xmx64m -cp ${TESTCLASSES} \
   LimitDirectMemory false DEFAULT-1 DEFAULT/2
 
 # Various bad values fail to launch the VM.
 launchFail foo
 launchFail 10kmt
 launchFail -1
+
+# Clean-up
+rm ${TMP1}