Skip to content

Commit

Permalink
(fix) sun.nio.ch.DirectBuffer compile error in latest jdk, sofastack#633
Browse files Browse the repository at this point in the history
 (sofastack#636)

* (fix) sun.nio.ch.DirectBuffer compile error in latest jdk, sofastack#633
  • Loading branch information
killme2008 authored Jul 9, 2021
1 parent 8e5d1f3 commit f6a86f8
Showing 1 changed file with 47 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,9 @@

import java.io.File;
import java.io.IOException;
import java.lang.invoke.MethodHandle;
import java.lang.invoke.MethodHandles;
import java.lang.reflect.Method;
import java.nio.ByteBuffer;
import java.nio.MappedByteBuffer;
import java.nio.channels.FileChannel;
Expand Down Expand Up @@ -45,8 +48,6 @@
import com.sun.jna.NativeLong;
import com.sun.jna.Pointer;

import sun.nio.ch.DirectBuffer;

/**
* A fixed size file. The content format is:
* <pre>
Expand Down Expand Up @@ -316,24 +317,56 @@ private void swapIn() {
}
}

// Cached method for sun.nio.ch.DirectBuffer#address
private static MethodHandle ADDRESS_METHOD = null;

static {
try {
Class<?> clazz = Class.forName("sun.nio.ch.DirectBuffer");
if (clazz != null) {
Method method = clazz.getMethod("address");
if (method != null) {
ADDRESS_METHOD = MethodHandles.lookup().unreflect(method);
}
}
} catch (Throwable t) {
// NOPMD
}
}

private Pointer getPointer() {
if (ADDRESS_METHOD != null) {
try {
final long address = (long) ADDRESS_METHOD.invoke(this.buffer);
Pointer pointer = new Pointer(address);
return pointer;
} catch (Throwable t) {
// NOPMD
}
}
return null;
}

public void hintLoad() {
final long address = ((DirectBuffer) (this.buffer)).address();
Pointer pointer = new Pointer(address);
Pointer pointer = getPointer();

long beginTime = Utils.monotonicMs();
int ret = LibC.INSTANCE.madvise(pointer, new NativeLong(this.size), LibC.MADV_WILLNEED);
LOG.info("madvise(MADV_WILLNEED) {} {} {} ret = {} time consuming = {}", address, this.path, this.size, ret,
Utils.monotonicMs() - beginTime);
if (pointer != null) {
long beginTime = Utils.monotonicMs();
int ret = LibC.INSTANCE.madvise(pointer, new NativeLong(this.size), LibC.MADV_WILLNEED);
LOG.info("madvise(MADV_WILLNEED) {} {} {} ret = {} time consuming = {}", pointer, this.path, this.size,
ret, Utils.monotonicMs() - beginTime);
}
}

public void hintUnload() {
final long address = ((DirectBuffer) (this.buffer)).address();
Pointer pointer = new Pointer(address);
Pointer pointer = getPointer();

long beginTime = Utils.monotonicMs();
int ret = LibC.INSTANCE.madvise(pointer, new NativeLong(this.size), LibC.MADV_DONTNEED);
LOG.info("madvise(MADV_DONTNEED) {} {} {} ret = {} time consuming = {}", address, this.path, this.size, ret,
Utils.monotonicMs() - beginTime);
if (pointer != null) {
long beginTime = Utils.monotonicMs();
int ret = LibC.INSTANCE.madvise(pointer, new NativeLong(this.size), LibC.MADV_DONTNEED);
LOG.info("madvise(MADV_DONTNEED) {} {} {} ret = {} time consuming = {}", pointer, this.path, this.size,
ret, Utils.monotonicMs() - beginTime);
}
}

public void swapOut() {
Expand Down

0 comments on commit f6a86f8

Please sign in to comment.