Skip to content

Commit

Permalink
chore(jmc-core): update RJMX sources (#228) (#235)
Browse files Browse the repository at this point in the history
* Updating missed RJMX classes

* Fixing newline at end of file

* Cleanup of CommercialFeaturesServiceFactory, adding missing DefaultConnectionHandle changes

* feat(jmx): check for SubstrateVM and whether JFR is available

Co-authored-by: Robert Toyonaga <rtoyonag@redhat.com>

---------

Co-authored-by: Andrew Azores <aazores@redhat.com>
Co-authored-by: Robert Toyonaga <rtoyonag@redhat.com>
(cherry picked from commit 4bc004e)

Co-authored-by: Joshua Matsuoka <Josh.matsuoka@gmail.com>
  • Loading branch information
mergify[bot] and Josh-Matsuoka authored Jun 13, 2023
1 parent 97059c6 commit d335f79
Show file tree
Hide file tree
Showing 7 changed files with 50 additions and 22 deletions.
3 changes: 3 additions & 0 deletions src/main/java/org/openjdk/jmc/rjmx/ConnectionToolkit.java
Original file line number Diff line number Diff line change
Expand Up @@ -365,6 +365,9 @@ public static boolean isHotSpot(IConnectionHandle connectionHandle) {
return vmName != null && JavaVMVersionToolkit.isHotspotJVMName(vmName);
}

public static boolean isSubstrateVm(IConnectionHandle connectionHandle) {
return getVMName(connectionHandle).equals("Substrate VM");
}
/**
* Returns {@code true} if the connection handle is associated with an Oracle built JVM,
* {@code false} otherwise. If the information is already present in the {@link JVMDescriptor},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ public class DefaultConnectionHandle implements IConnectionHandle {
public DefaultConnectionHandle(RJMXConnection connection, String description, IConnectionListener[] listeners) {
this.connection = connection;
this.description = description;
this.listeners = listeners;
this.listeners = listeners == null ? new IConnectionListener[0] : listeners;
registerDefaultServices();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,4 +52,9 @@ public interface ICommercialFeaturesService {
*/
void enableCommercialFeatures() throws Exception;

/**
* @return true if there are commercial features available, or false if this JVM doesn't have
* commercial features.
*/
boolean hasCommercialFeatures();
}
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ public ICommercialFeaturesService getServiceInstance(IConnectionHandle handle)
if (descriptor != null) {
JavaVersion version = new JavaVersion(descriptor.getJavaVersion());
if (version.getMajorVersion() >= 11) {
return new Jdk11CommercialFeaturesService();
return new NoCommercialFeaturesService();
}
}
return new HotSpot23CommercialFeaturesService(handle);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2018, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2018, 2019, Oracle and/or its affiliates. All rights reserved.
*
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
Expand Down Expand Up @@ -43,7 +43,7 @@
import javax.management.ObjectName;

public class HotSpot23CommercialFeaturesService implements ICommercialFeaturesService {
private final static String VM_FLAG = "UnlockCommercialFeatures"; //$NON-NLS-1$
private final static String UNLOCK_COMMERCIAL_FEATURES_FLAG = "UnlockCommercialFeatures"; //$NON-NLS-1$
private final static String UNLOCK_COMMAND = "VM.unlock_commercial_features"; //$NON-NLS-1$
private final MBeanServerConnection server;
private final IDiagnosticCommandService dcs;
Expand All @@ -54,7 +54,7 @@ public HotSpot23CommercialFeaturesService(IConnectionHandle handle)
server = handle.getServiceOrThrow(MBeanServerConnection.class);
dcs = handle.getServiceOrNull(IDiagnosticCommandService.class);
try {
HotspotManagementToolkit.getVMOption(server, VM_FLAG); // Will fail if option is not available
HotspotManagementToolkit.getVMOption(server, UNLOCK_COMMERCIAL_FEATURES_FLAG); // Will fail if option is not available
} catch (Exception e) {
// Commercial Feature option is not available but Flight Recorder is.
if (!isJfrMBeanAvailable()) {
Expand All @@ -66,7 +66,8 @@ public HotSpot23CommercialFeaturesService(IConnectionHandle handle)
@Override
public boolean isCommercialFeaturesEnabled() {
try {
return ((String) HotspotManagementToolkit.getVMOption(server, VM_FLAG)).contains("true"); //$NON-NLS-1$
return ((String) HotspotManagementToolkit.getVMOption(server, UNLOCK_COMMERCIAL_FEATURES_FLAG))
.contains("true"); //$NON-NLS-1$
} catch (Exception e) {
return false;
}
Expand All @@ -78,7 +79,7 @@ public void enableCommercialFeatures() throws Exception {
dcs.runCtrlBreakHandlerWithResult(UNLOCK_COMMAND);
}
if (!isCommercialFeaturesEnabled()) {
HotspotManagementToolkit.setVMOption(server, VM_FLAG, "true"); //$NON-NLS-1$
HotspotManagementToolkit.setVMOption(server, UNLOCK_COMMERCIAL_FEATURES_FLAG, "true"); //$NON-NLS-1$
}
}

Expand All @@ -96,4 +97,9 @@ private ObjectName getJfrMBeanObjectName() throws Exception {
server.getMBeanInfo(candidateObjectName);
return candidateObjectName;
}
}

@Override
public boolean hasCommercialFeatures() {
return true;
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2018, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2018, 2019, Oracle and/or its affiliates. All rights reserved.
*
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
Expand Down Expand Up @@ -34,8 +34,11 @@

import org.openjdk.jmc.rjmx.services.ICommercialFeaturesService;

public class Jdk11CommercialFeaturesService implements ICommercialFeaturesService {

/**
* Used by JVMs with no commercial features, e.g. OpenJDK 8 and JDK 11+ JVMs.
*/
public class NoCommercialFeaturesService implements ICommercialFeaturesService {

@Override
public boolean isCommercialFeaturesEnabled() {
return true;
Expand All @@ -45,4 +48,9 @@ public boolean isCommercialFeaturesEnabled() {
public void enableCommercialFeatures() throws Exception {
// Noop
}
}

@Override
public boolean hasCommercialFeatures() {
return false;
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2018, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2018, 2019, Oracle and/or its affiliates. All rights reserved.
*
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
Expand Down Expand Up @@ -103,17 +103,22 @@ public String getVersion() {
}

private boolean isDynamicFlightRecorderSupported(IConnectionHandle handle) {
return ConnectionToolkit.isHotSpot(handle)
&& ConnectionToolkit.isJavaVersionAboveOrEqual(handle, JavaVersionSupport.DYNAMIC_JFR_SUPPORTED);
}
// All OpenJDK versions of JFR support dynamic enablement of JFR, so if there are no commercial features in play
// all is A-OK.
if (ConnectionToolkit.isSubstrateVm(handle)){
// JFR may not have been built into the native image. Check that FlightRecorderMXBean is accessible from the MBean server.
return isAvailable(handle);
}

private boolean isFlightRecorderCommercial() {
return ConnectionToolkit.isHotSpot(connection)
&& !ConnectionToolkit.isJavaVersionAboveOrEqual(connection, JavaVersionSupport.JFR_NOT_COMMERCIAL);
return !cfs.hasCommercialFeatures() || (ConnectionToolkit.isHotSpot(handle)
&& ConnectionToolkit.isJavaVersionAboveOrEqual(handle, JavaVersionSupport.DYNAMIC_JFR_SUPPORTED));
}

private boolean isFlightRecorderDisabled(IConnectionHandle handle) {
if (cfs != null && isFlightRecorderCommercial()) {
if (ConnectionToolkit.isSubstrateVm(handle)){
// For SVM commercial features may be available but disabled and JFR is still enabled
return !isAvailable(handle);
} else if (cfs != null && cfs.hasCommercialFeatures()) {
return !cfs.isCommercialFeaturesEnabled() || JVMSupportToolkit.isFlightRecorderDisabled(handle, false);
} else {
return JVMSupportToolkit.isFlightRecorderDisabled(handle, false);
Expand All @@ -126,10 +131,11 @@ public static boolean isAvailable(IConnectionHandle handle) {

public FlightRecorderServiceV2(IConnectionHandle handle) throws ConnectionException, ServiceNotAvailableException {
cfs = handle.getServiceOrThrow(ICommercialFeaturesService.class);

if (!isDynamicFlightRecorderSupported(handle) && isFlightRecorderDisabled(handle)) {
throw new ServiceNotAvailableException(""); //$NON-NLS-1$
}
if (JVMSupportToolkit.isFlightRecorderDisabled(handle, true)) {
if (!ConnectionToolkit.isSubstrateVm(handle) && JVMSupportToolkit.isFlightRecorderDisabled(handle, true)) {
throw new ServiceNotAvailableException(""); //$NON-NLS-1$
}
connection = handle;
Expand Down Expand Up @@ -480,7 +486,7 @@ public InputStream openStream(IRecordingDescriptor descriptor, IQuantity lastPar
@Override
public boolean isEnabled() {
if (!wasEnabled) {
boolean isEnabled = isFlightRecorderCommercial() ? cfs.isCommercialFeaturesEnabled()
boolean isEnabled = cfs.hasCommercialFeatures() ? cfs.isCommercialFeaturesEnabled()
: isAvailable(connection);
if (isEnabled) {
wasEnabled = true;
Expand Down

0 comments on commit d335f79

Please sign in to comment.