提交 2b03f78a authored 作者: Evgenij Ryazanov's avatar Evgenij Ryazanov

Add Java 9+ support to NIO_CLEANER_HACK

上级 0494963b
......@@ -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();
}
}
......
/*
* 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() {
}
}
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论