From 5bc456a8888746013419203001f73305320948c7 Mon Sep 17 00:00:00 2001 From: alanb Date: Mon, 18 Oct 2010 10:29:59 +0100 Subject: [PATCH] 4837564: (bf) Please make DirectByteBuffer performance enhancements Reviewed-by: chegar --- .../java/nio/Direct-X-Buffer.java.template | 6 +++-- src/share/classes/sun/misc/VM.java | 16 ++++++++++++++ test/java/nio/Buffer/LimitDirectMemory.sh | 22 ++++++------------- 3 files changed, 27 insertions(+), 17 deletions(-) diff --git a/src/share/classes/java/nio/Direct-X-Buffer.java.template b/src/share/classes/java/nio/Direct-X-Buffer.java.template index 520b9c465..6c96df87e 100644 --- a/src/share/classes/java/nio/Direct-X-Buffer.java.template +++ b/src/share/classes/java/nio/Direct-X-Buffer.java.template @@ -29,6 +29,7 @@ package java.nio; import sun.misc.Cleaner; import sun.misc.Unsafe; +import sun.misc.VM; import sun.nio.ch.DirectBuffer; @@ -114,8 +115,9 @@ class Direct$Type$Buffer$RW$$BO$ 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 @@ class Direct$Type$Buffer$RW$$BO$ 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 { diff --git a/src/share/classes/sun/misc/VM.java b/src/share/classes/sun/misc/VM.java index 8b810b5a3..9ea6990ae 100644 --- a/src/share/classes/sun/misc/VM.java +++ b/src/share/classes/sun/misc/VM.java @@ -178,6 +178,17 @@ public class VM { 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 @@ public class VM { } } + // 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". diff --git a/test/java/nio/Buffer/LimitDirectMemory.sh b/test/java/nio/Buffer/LimitDirectMemory.sh index 3ac1056a7..38e8c0c05 100644 --- a/test/java/nio/Buffer/LimitDirectMemory.sh +++ b/test/java/nio/Buffer/LimitDirectMemory.sh @@ -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 @@ runTest -XX:MaxDirectMemorySize=65M -cp ${TESTCLASSES} \ # 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} -- GitLab