Skip to content

Commit

Permalink
[GR-18476] Using reflection to invoke HotSpotJVMCIRuntime.getCurrentJ…
Browse files Browse the repository at this point in the history
…avaThread.
  • Loading branch information
tzezula committed Apr 1, 2020
1 parent ab641b9 commit ab692b2
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,9 @@ public HotSpotGraalManagement() {
*/
@Override
public void initialize(HotSpotGraalRuntime runtime, GraalHotSpotVMConfig config) {
initializeJNI(config);
if (!initializeJNI(config)) {
return;
}
HotSpotGraalRuntimeMBean mbean = getBean();
if (mbean == null) {
if (runtime.getManagement() != this) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
*/
package org.graalvm.compiler.hotspot.management.libgraal;

import java.lang.reflect.Method;
import static org.graalvm.libgraal.jni.JNIUtil.createString;
import static org.graalvm.libgraal.jni.JNIUtil.getBinaryName;

Expand All @@ -49,6 +50,17 @@

class MBeanProxy<T extends DynamicMBean> {

private static final Method getCurrentJavaThreadMethod;
static {
Method m;
try {
m = HotSpotJVMCIRuntime.class.getMethod("getCurrentJavaThread");
} catch (NoSuchMethodException e) {
m = null;
}
getCurrentJavaThreadMethod = m;
}

// Classes defined in HotSpot heap by JNI, the values are filled by LibGraalFeature.
private static final String HS_BEAN_CLASS_NAME = null;
private static final byte[] HS_BEAN_CLASS = null;
Expand Down Expand Up @@ -159,13 +171,16 @@ ObjectName poll() {
return objName;
}

static void initializeJNI(GraalHotSpotVMConfig config) {
static boolean initializeJNI(GraalHotSpotVMConfig config) {
if (getCurrentJavaThreadMethod == null) {
return false;
}
if (jniEnvOffset == 0) {
synchronized (MBeanProxy.class) {
if (jniEnvOffset == 0) {
if (config.jniEnvironmentOffset == Integer.MIN_VALUE) {
// Old unsupported JVMCI version.
return;
return false;
}
memPoolBean = new LibGraalMemoryPoolMBean();
jniEnvOffset = config.jniEnvironmentOffset;
Expand All @@ -179,6 +194,7 @@ static void initializeJNI(GraalHotSpotVMConfig config) {
}
}
}
return true;
}

static JNI.JClass getHotSpotEntryPoints() {
Expand All @@ -192,8 +208,15 @@ static JNI.JNIEnv getCurrentJNIEnv() {
if (jniEnvOffset == 0) {
throw new IllegalStateException("JniEnvOffset is not yet initialized.");
}
long currentJavaThreadAddr = HotSpotJVMCIRuntime.runtime().getCurrentJavaThread();
return WordFactory.pointer(currentJavaThreadAddr + jniEnvOffset);
if (getCurrentJavaThreadMethod == null) {
throw new IllegalStateException("CurrentJavaThread not supported by JVMCI.");
}
try {
long currentJavaThreadAddr = (Long) getCurrentJavaThreadMethod.invoke(HotSpotJVMCIRuntime.runtime());
return WordFactory.pointer(currentJavaThreadAddr + jniEnvOffset);
} catch (ReflectiveOperationException reflectiveException) {
throw new RuntimeException("Failed to invoke HotSpotJVMCIRuntime::getCurrentJavaThread", reflectiveException);
}
}

/**
Expand Down

0 comments on commit ab692b2

Please sign in to comment.