Skip to content
项目
群组
代码片段
帮助
正在加载...
帮助
为 GitLab 提交贡献
登录/注册
切换导航
H
h2database
项目
项目
详情
活动
周期分析
仓库
仓库
文件
提交
分支
标签
贡献者
分枝图
比较
统计图
议题
0
议题
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
CI / CD
CI / CD
流水线
作业
计划
统计图
Wiki
Wiki
代码片段
代码片段
成员
成员
折叠边栏
关闭边栏
活动
分枝图
统计图
创建新议题
作业
提交
议题看板
打开侧边栏
Administrator
h2database
Commits
7e3ef0c6
Unverified
提交
7e3ef0c6
authored
7月 19, 2018
作者:
Evgenij Ryazanov
提交者:
GitHub
7月 19, 2018
浏览文件
操作
浏览文件
下载
差异文件
Merge pull request #1312 from katzyn/munmap
Add Java 9+ support to NIO_CLEANER_HACK
上级
6be41dec
099004bb
隐藏空白字符变更
内嵌
并排
正在显示
4 个修改的文件
包含
157 行增加
和
26 行删除
+157
-26
FilePathNioMapped.java
h2/src/main/org/h2/store/fs/FilePathNioMapped.java
+12
-26
MemoryUnmapper.java
h2/src/main/org/h2/util/MemoryUnmapper.java
+92
-0
TestAll.java
h2/src/test/org/h2/test/TestAll.java
+2
-0
TestMemoryUnmapper.java
h2/src/test/org/h2/test/unit/TestMemoryUnmapper.java
+51
-0
没有找到文件。
h2/src/main/org/h2/store/fs/FilePathNioMapped.java
浏览文件 @
7e3ef0c6
...
...
@@ -9,7 +9,6 @@ import java.io.EOFException;
import
java.io.IOException
;
import
java.io.RandomAccessFile
;
import
java.lang.ref.WeakReference
;
import
java.lang.reflect.Method
;
import
java.nio.BufferUnderflowException
;
import
java.nio.ByteBuffer
;
import
java.nio.MappedByteBuffer
;
...
...
@@ -19,6 +18,7 @@ import java.nio.channels.NonWritableChannelException;
import
java.util.concurrent.TimeUnit
;
import
org.h2.engine.SysProperties
;
import
org.h2.util.MemoryUnmapper
;
/**
* This file system stores files on disk and uses java.nio to access the files.
...
...
@@ -78,36 +78,22 @@ class FileNioMapped extends FileBase {
// need to dispose old direct buffer, see bug
// http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=4724038
boolean
useSystemGc
=
true
;
if
(
SysProperties
.
NIO_CLEANER_HACK
)
{
try
{
Method
cleanerMethod
=
mapped
.
getClass
().
getMethod
(
"cleaner"
);
cleanerMethod
.
setAccessible
(
true
);
Object
cleaner
=
cleanerMethod
.
invoke
(
mapped
);
if
(
cleaner
!=
null
)
{
Method
clearMethod
=
cleaner
.
getClass
().
getMethod
(
"clean"
);
clearMethod
.
invoke
(
cleaner
);
}
useSystemGc
=
false
;
}
catch
(
Throwable
e
)
{
// useSystemGc is already true
}
finally
{
if
(
MemoryUnmapper
.
unmap
(
mapped
))
{
mapped
=
null
;
return
;
}
}
if
(
useSystemGc
)
{
WeakReference
<
MappedByteBuffer
>
bufferWeakRef
=
new
WeakReference
<>(
mapped
);
mapped
=
null
;
long
start
=
System
.
nanoTime
();
while
(
bufferWeakRef
.
get
()
!=
null
)
{
if
(
System
.
nanoTime
()
-
start
>
TimeUnit
.
MILLISECONDS
.
toNanos
(
GC_TIMEOUT_MS
))
{
throw
new
IOException
(
"Timeout ("
+
GC_TIMEOUT_MS
+
" ms) reached while trying to GC mapped buffer"
);
}
System
.
gc
();
Thread
.
yield
();
WeakReference
<
MappedByteBuffer
>
bufferWeakRef
=
new
WeakReference
<>(
mapped
);
mapped
=
null
;
long
start
=
System
.
nanoTime
();
while
(
bufferWeakRef
.
get
()
!=
null
)
{
if
(
System
.
nanoTime
()
-
start
>
TimeUnit
.
MILLISECONDS
.
toNanos
(
GC_TIMEOUT_MS
))
{
throw
new
IOException
(
"Timeout ("
+
GC_TIMEOUT_MS
+
" ms) reached while trying to GC mapped buffer"
);
}
System
.
gc
();
Thread
.
yield
();
}
}
...
...
h2/src/main/org/h2/util/MemoryUnmapper.java
0 → 100644
浏览文件 @
7e3ef0c6
/*
* Copyright 2004-2018 H2 Group. Multiple-Licensed under the MPL 2.0,
* and the EPL 1.0 (http://h2database.com/html/license.html).
* Initial Developer: H2 Group
*/
package
org
.
h2
.
util
;
import
java.lang.reflect.Field
;
import
java.lang.reflect.Method
;
import
java.nio.ByteBuffer
;
import
org.h2.engine.SysProperties
;
/**
* Unsafe memory unmapper.
*
* @see SysProperties#NIO_CLEANER_HACK
*/
public
final
class
MemoryUnmapper
{
private
static
final
boolean
ENABLED
;
private
static
final
Object
UNSAFE
;
private
static
final
Method
INVOKE_CLEANER
;
static
{
boolean
enabled
=
SysProperties
.
NIO_CLEANER_HACK
;
Object
unsafe
=
null
;
Method
invokeCleaner
=
null
;
if
(
enabled
)
{
try
{
Class
<?>
clazz
=
Class
.
forName
(
"sun.misc.Unsafe"
);
Field
field
=
clazz
.
getDeclaredField
(
"theUnsafe"
);
field
.
setAccessible
(
true
);
unsafe
=
field
.
get
(
null
);
// This method exists only on Java 9 and later versions
invokeCleaner
=
clazz
.
getMethod
(
"invokeCleaner"
,
ByteBuffer
.
class
);
}
catch
(
ReflectiveOperationException
e
)
{
// Java 7 or 8
unsafe
=
null
;
// invokeCleaner can be only null here
}
catch
(
Throwable
e
)
{
// Should be a SecurityException, but catch everything to be
// safe
enabled
=
false
;
unsafe
=
null
;
// invokeCleaner can be only null here
}
}
ENABLED
=
enabled
;
UNSAFE
=
unsafe
;
INVOKE_CLEANER
=
invokeCleaner
;
}
/**
* Tries to unmap memory for the specified byte buffer using Java internals
* in unsafe way if {@link SysProperties#NIO_CLEANER_HACK} is enabled and
* access is not denied by a security manager.
*
* @param buffer
* mapped byte buffer
* @return whether operation was successful
*/
public
static
boolean
unmap
(
ByteBuffer
buffer
)
{
if
(!
ENABLED
)
{
return
false
;
}
try
{
if
(
INVOKE_CLEANER
!=
null
)
{
// Java 9 or later
INVOKE_CLEANER
.
invoke
(
UNSAFE
,
buffer
);
return
true
;
}
// Java 7 or 8
Method
cleanerMethod
=
buffer
.
getClass
().
getMethod
(
"cleaner"
);
cleanerMethod
.
setAccessible
(
true
);
Object
cleaner
=
cleanerMethod
.
invoke
(
buffer
);
if
(
cleaner
!=
null
)
{
Method
clearMethod
=
cleaner
.
getClass
().
getMethod
(
"clean"
);
clearMethod
.
invoke
(
cleaner
);
}
return
true
;
}
catch
(
Throwable
e
)
{
return
false
;
}
}
private
MemoryUnmapper
()
{
}
}
h2/src/test/org/h2/test/TestAll.java
浏览文件 @
7e3ef0c6
...
...
@@ -193,6 +193,7 @@ import org.h2.test.unit.TestIntPerfectHash;
import
org.h2.test.unit.TestJmx
;
import
org.h2.test.unit.TestLocale
;
import
org.h2.test.unit.TestMathUtils
;
import
org.h2.test.unit.TestMemoryUnmapper
;
import
org.h2.test.unit.TestMode
;
import
org.h2.test.unit.TestModifyOnWrite
;
import
org.h2.test.unit.TestNetUtils
;
...
...
@@ -960,6 +961,7 @@ kill -9 `jps -l | grep "org.h2.test." | cut -d " " -f 1`
addTest
(
new
TestIntIntHashMap
());
addTest
(
new
TestIntPerfectHash
());
addTest
(
new
TestMathUtils
());
addTest
(
new
TestMemoryUnmapper
());
addTest
(
new
TestMode
());
addTest
(
new
TestObjectDeserialization
());
addTest
(
new
TestOverflow
());
...
...
h2/src/test/org/h2/test/unit/TestMemoryUnmapper.java
0 → 100644
浏览文件 @
7e3ef0c6
/*
* Copyright 2004-2018 H2 Group. Multiple-Licensed under the MPL 2.0,
* and the EPL 1.0 (http://h2database.com/html/license.html).
* Initial Developer: H2 Group
*/
package
org
.
h2
.
test
.
unit
;
import
java.lang.ProcessBuilder.Redirect
;
import
java.nio.ByteBuffer
;
import
org.h2.test.TestBase
;
import
org.h2.util.MemoryUnmapper
;
/**
* Tests memory unmapper.
*/
public
class
TestMemoryUnmapper
extends
TestBase
{
private
static
final
int
OK
=
0
,
/* EXCEPTION = 1, */
UNAVAILABLE
=
2
;
/**
* May be used to run only this test and may be launched by this test in a
* subprocess.
*
* @param a
* if empty run this test only
*/
public
static
void
main
(
String
...
a
)
throws
Exception
{
if
(
a
.
length
==
0
)
{
TestBase
.
createCaller
().
init
().
test
();
}
else
{
ByteBuffer
buffer
=
ByteBuffer
.
allocateDirect
(
10
);
System
.
exit
(
MemoryUnmapper
.
unmap
(
buffer
)
?
OK
:
UNAVAILABLE
);
}
}
@Override
public
void
test
()
throws
Exception
{
ProcessBuilder
pb
=
new
ProcessBuilder
().
redirectError
(
Redirect
.
INHERIT
);
// Test that unsafe unmapping is disabled by default
pb
.
command
(
getJVM
(),
"-cp"
,
getClassPath
(),
"-ea"
,
getClass
().
getName
(),
"dummy"
);
assertEquals
(
UNAVAILABLE
,
pb
.
start
().
waitFor
());
// Test that it can be enabled
pb
.
command
(
getJVM
(),
"-cp"
,
getClassPath
(),
"-ea"
,
"-Dh2.nioCleanerHack=true"
,
getClass
().
getName
(),
"dummy"
);
assertEquals
(
OK
,
pb
.
start
().
waitFor
());
// Test that it will not be enabled with a security manager
pb
.
command
(
getJVM
(),
"-cp"
,
getClassPath
(),
"-ea"
,
"-Djava.security.manager"
,
"-Dh2.nioCleanerHack=true"
,
getClass
().
getName
(),
"dummy"
);
assertEquals
(
UNAVAILABLE
,
pb
.
start
().
waitFor
());
}
}
编写
预览
Markdown
格式
0%
重试
或
添加新文件
添加附件
取消
您添加了
0
人
到此讨论。请谨慎行事。
请先完成此评论的编辑!
取消
请
注册
或者
登录
后发表评论