提交 5bc456a8 编写于 作者: A alanb

4837564: (bf) Please make DirectByteBuffer performance enhancements

Reviewed-by: chegar
上级 dad4def2
...@@ -29,6 +29,7 @@ package java.nio; ...@@ -29,6 +29,7 @@ package java.nio;
import sun.misc.Cleaner; import sun.misc.Cleaner;
import sun.misc.Unsafe; import sun.misc.Unsafe;
import sun.misc.VM;
import sun.nio.ch.DirectBuffer; import sun.nio.ch.DirectBuffer;
...@@ -114,8 +115,9 @@ class Direct$Type$Buffer$RW$$BO$ ...@@ -114,8 +115,9 @@ class Direct$Type$Buffer$RW$$BO$
Direct$Type$Buffer$RW$(int cap) { // package-private Direct$Type$Buffer$RW$(int cap) { // package-private
#if[rw] #if[rw]
super(-1, 0, cap, cap, false); super(-1, 0, cap, cap, false);
boolean pa = VM.isDirectMemoryPageAligned();
int ps = Bits.pageSize(); int ps = Bits.pageSize();
int size = cap + ps; long size = Math.max(1L, (long)cap + (pa ? ps : 0));
Bits.reserveMemory(size, cap); Bits.reserveMemory(size, cap);
long base = 0; long base = 0;
...@@ -126,7 +128,7 @@ class Direct$Type$Buffer$RW$$BO$ ...@@ -126,7 +128,7 @@ class Direct$Type$Buffer$RW$$BO$
throw x; throw x;
} }
unsafe.setMemory(base, size, (byte) 0); unsafe.setMemory(base, size, (byte) 0);
if (base % ps != 0) { if (pa && (base % ps != 0)) {
// Round up to page boundary // Round up to page boundary
address = base + ps - (base & (ps - 1)); address = base + ps - (base & (ps - 1));
} else { } else {
......
...@@ -178,6 +178,17 @@ public class VM { ...@@ -178,6 +178,17 @@ public class VM {
return directMemory; 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 // A user-settable boolean to determine whether ClassLoader.loadClass should
// accept array syntax. This value may be changed during VM initialization // accept array syntax. This value may be changed during VM initialization
// via the system property "sun.lang.ClassLoader.allowArraySyntax". // via the system property "sun.lang.ClassLoader.allowArraySyntax".
...@@ -252,6 +263,11 @@ public class VM { ...@@ -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 // Set a boolean to determine whether ClassLoader.loadClass accepts
// array syntax. This value is controlled by the system property // array syntax. This value is controlled by the system property
// "sun.lang.ClassLoader.allowArraySyntax". // "sun.lang.ClassLoader.allowArraySyntax".
......
...@@ -30,18 +30,7 @@ ...@@ -30,18 +30,7 @@
# @build LimitDirectMemory # @build LimitDirectMemory
# @run shell LimitDirectMemory.sh # @run shell LimitDirectMemory.sh
# set platform-dependent variable TMP1=tmp_$$
OS=`uname -s`
case "$OS" in
SunOS | Linux ) TMP=/tmp ;;
Windows* ) TMP="c:/temp" ;;
* )
echo "Unrecognized system!"
exit 1;
;;
esac
TMP1=${TMP}/tmp1_$$
runTest() { runTest() {
echo "Testing: $*" echo "Testing: $*"
...@@ -82,18 +71,21 @@ runTest -XX:MaxDirectMemorySize=65M -cp ${TESTCLASSES} \ ...@@ -82,18 +71,21 @@ runTest -XX:MaxDirectMemorySize=65M -cp ${TESTCLASSES} \
# Exactly the default amount of memory is available. # Exactly the default amount of memory is available.
runTest -cp ${TESTCLASSES} LimitDirectMemory false 10 1 runTest -cp ${TESTCLASSES} LimitDirectMemory false 10 1
runTest -cp ${TESTCLASSES} LimitDirectMemory false 0 DEFAULT runTest -Xmx64m -cp ${TESTCLASSES} LimitDirectMemory false 0 DEFAULT
runTest -cp ${TESTCLASSES} LimitDirectMemory true 0 DEFAULT+1 runTest -Xmx64m -cp ${TESTCLASSES} LimitDirectMemory true 0 DEFAULT+1
# We should be able to eliminate direct memory allocation entirely. # We should be able to eliminate direct memory allocation entirely.
runTest -XX:MaxDirectMemorySize=0 -cp ${TESTCLASSES} LimitDirectMemory true 0 1 runTest -XX:MaxDirectMemorySize=0 -cp ${TESTCLASSES} LimitDirectMemory true 0 1
# Setting the system property should not work so we should be able to allocate # Setting the system property should not work so we should be able to allocate
# the default amount. # the default amount.
runTest -Dsun.nio.MaxDirectMemorySize=1K -cp ${TESTCLASSES} \ runTest -Dsun.nio.MaxDirectMemorySize=1K -Xmx64m -cp ${TESTCLASSES} \
LimitDirectMemory false DEFAULT-1 DEFAULT/2 LimitDirectMemory false DEFAULT-1 DEFAULT/2
# Various bad values fail to launch the VM. # Various bad values fail to launch the VM.
launchFail foo launchFail foo
launchFail 10kmt launchFail 10kmt
launchFail -1 launchFail -1
# Clean-up
rm ${TMP1}
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册