changeset 3079:b79600ecf0e4

4837564: (bf) Please make DirectByteBuffer performance enhancements Reviewed-by: chegar
author alanb
date Mon, 18 Oct 2010 10:29:59 +0100
parents f24699d8c892
children c64772f0492f
files src/share/classes/java/nio/Direct-X-Buffer.java.template src/share/classes/sun/misc/VM.java test/java/nio/Buffer/LimitDirectMemory.sh
diffstat 3 files changed, 27 insertions(+), 17 deletions(-) [+]
line wrap: on
line diff
--- a/src/share/classes/java/nio/Direct-X-Buffer.java.template	Fri Oct 15 15:09:37 2010 +0100
+++ b/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/src/share/classes/sun/misc/VM.java	Fri Oct 15 15:09:37 2010 +0100
+++ b/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/test/java/nio/Buffer/LimitDirectMemory.sh	Fri Oct 15 15:09:37 2010 +0100
+++ b/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}