Skip to content

Commit

Permalink
7903364: JOL: Fix support for modern SA
Browse files Browse the repository at this point in the history
  • Loading branch information
shipilev authored Oct 18, 2022
1 parent 247f12a commit 2d958ff
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 43 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ very dirty moves to access. Since the magic field offset code pokes (*writes*)
narrowKlassBase = saDetails.getNarrowKlassBase();

sizes = new Sizes(this);
lilliputVM = guessLilliput(objectHeaderSize);
lilliputVM = guessLilliput(addressSize);
}

HotspotUnsafe(Unsafe u, Instrumentation inst) {
Expand Down
2 changes: 2 additions & 0 deletions jol-core/src/main/java/org/openjdk/jol/vm/sa/Constants.java
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@ class Constants {

static final String HOTSPOT_AGENT_CLASSNAME = "sun.jvm.hotspot.HotSpotAgent";
static final String VM_CLASSNAME = "sun.jvm.hotspot.runtime.VM";
static final String COMP_OOPS_CLASSNAME = "sun.jvm.hotspot.oops.CompressedOops";
static final String COMP_KLASS_CLASSNAME = "sun.jvm.hotspot.oops.CompressedKlassPointers";
static final String UNIVERSE_CLASSNAME = "sun.jvm.hotspot.memory.Universe";

static final String SKIP_HOTSPOT_SA_ATTACH_FLAG = "jol.skipHotspotSAAttach";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -271,6 +271,7 @@ private List<String> getArguments(boolean sudoRequired, AgentStyle style) {
args.add("--add-exports"); args.add("jdk.hotspot.agent/sun.jvm.hotspot=ALL-UNNAMED");
args.add("--add-exports"); args.add("jdk.hotspot.agent/sun.jvm.hotspot.runtime=ALL-UNNAMED");
args.add("--add-exports"); args.add("jdk.hotspot.agent/sun.jvm.hotspot.memory=ALL-UNNAMED");
args.add("--add-exports"); args.add("jdk.hotspot.agent/sun.jvm.hotspot.oops=ALL-UNNAMED");
break;
default:
throw new IllegalStateException("Unhandled style: " + style);
Expand Down
80 changes: 38 additions & 42 deletions jol-core/src/main/java/org/openjdk/jol/vm/sa/UniverseTask.java
Original file line number Diff line number Diff line change
Expand Up @@ -43,57 +43,53 @@ public UniverseData process() {
try {
Class<?> universeClass = ClassUtils.loadClass(UNIVERSE_CLASSNAME);
Class<?> vmClass = ClassUtils.loadClass(VM_CLASSNAME);
Object vm = ClassUtils.loadClass(VM_CLASSNAME).getMethod("getVM").invoke(null);

Method getOopSizeMethod = vmClass.getMethod("getOopSize");
Method getObjectAlignmentInBytesMethod = vmClass.getMethod("getObjectAlignmentInBytes");
Method vmMethod = vmClass.getMethod("getVM");

Method getHeapOopSizeMethod = vmClass.getMethod("getHeapOopSize");
Method isCompressedOopsEnabledMethod = vmClass.getMethod("isCompressedOopsEnabled");
Method getNarrowOopBaseMethod = universeClass.getMethod("getNarrowOopBase");
Method getNarrowOopShiftMethod = universeClass.getMethod("getNarrowOopShift");
Method heapOopSizeMethod = vmClass.getMethod("getHeapOopSize");
Method oopSizeMethod = vmClass.getMethod("getOopSize");
Method objectAlignmentMethod = vmClass.getMethod("getObjectAlignmentInBytes");

Method isCompressedKlassPtrsEnabledMethod = null;
Method getNarrowKlassBaseMethod = null;
Method getNarrowKlassShiftMethod = null;
Method compOopsEnabledMethod = vmClass.getMethod("isCompressedOopsEnabled");
Method compKlassEnabledMethod = vmClass.getMethod("isCompressedKlassPointersEnabled");

Method narrowOopBaseMethod = null;
Method narrowOopShiftMethod = null;
try {
isCompressedKlassPtrsEnabledMethod = vmClass.getMethod("isCompressedKlassPointersEnabled");
getNarrowKlassBaseMethod = universeClass.getMethod("getNarrowKlassBase");
getNarrowKlassShiftMethod = universeClass.getMethod("getNarrowKlassShift");
} catch (NoSuchMethodException e) {
// There is nothing to do, seems target JVM is not Java 8
// Past JDK 13, JDK-8223136, we have a special class for this data.
Class<?> coopClass = ClassUtils.loadClass(COMP_OOPS_CLASSNAME);
narrowOopBaseMethod = coopClass.getMethod("getBase");
narrowOopShiftMethod = coopClass.getMethod("getShift");
} catch (Exception e) {
narrowOopBaseMethod = universeClass.getMethod("getNarrowOopBase");
narrowOopShiftMethod = universeClass.getMethod("getNarrowOopShift");
}

int addressSize = ((Long) getOopSizeMethod.invoke(vm)).intValue();
int objectAlignment = (Integer) getObjectAlignmentInBytesMethod.invoke(vm);

int oopSize = (Integer) getHeapOopSizeMethod.invoke(vm);
boolean compressedOopsEnabled = (Boolean) isCompressedOopsEnabledMethod.invoke(vm);
long narrowOopBase = (Long) getNarrowOopBaseMethod.invoke(null);
int narrowOopShift = (Integer) getNarrowOopShiftMethod.invoke(null);

/*
* If compressed klass references is not supported (before Java 8),
* use compressed oop references values instead of them.
*/
Method narrowKlassBaseMethod = null;
Method narrowKlassShiftMethod = null;
try {
// Past JDK 13, JDK-8223136, we have a special class for this data.
Class<?> coopClass = ClassUtils.loadClass(COMP_KLASS_CLASSNAME);
narrowKlassBaseMethod = coopClass.getMethod("getBase");
narrowKlassShiftMethod = coopClass.getMethod("getShift");
} catch (Exception e) {
narrowKlassBaseMethod = universeClass.getMethod("getNarrowKlassBase");
narrowKlassShiftMethod = universeClass.getMethod("getNarrowKlassShift");
}

boolean compressedKlassPtrsEnabled = isCompressedKlassPtrsEnabledMethod != null ?
(Boolean) isCompressedKlassPtrsEnabledMethod.invoke(vm) : compressedOopsEnabled;
long narrowKlassBase = getNarrowKlassBaseMethod != null ?
(Long) getNarrowKlassBaseMethod.invoke(null) : narrowOopBase;
int narrowKlassShift = getNarrowKlassShiftMethod != null ?
(Integer) getNarrowKlassShiftMethod.invoke(null) : narrowOopShift;
Object vm = vmMethod.invoke(null);

return new UniverseData(addressSize,
objectAlignment,
oopSize,
compressedOopsEnabled,
narrowOopBase,
narrowOopShift,
compressedKlassPtrsEnabled,
narrowKlassBase,
narrowKlassShift);
return new UniverseData(
((Long) oopSizeMethod.invoke(vm)).intValue(),
(Integer) objectAlignmentMethod.invoke(vm),
(Integer) heapOopSizeMethod.invoke(vm),
(Boolean) compOopsEnabledMethod.invoke(vm),
(Long) narrowOopBaseMethod.invoke(null),
(Integer) narrowOopShiftMethod.invoke(null),
(Boolean) compKlassEnabledMethod.invoke(vm),
(Long) narrowKlassBaseMethod.invoke(null),
(Integer) narrowKlassShiftMethod.invoke(null)
);
} catch (Throwable t) {
throw new RuntimeException(t.getMessage(), t);
}
Expand Down

0 comments on commit 2d958ff

Please sign in to comment.