76 import org.openjdk.jmc.rjmx.ConnectionToolkit;
77 import org.openjdk.jmc.rjmx.IConnectionHandle;
78 import org.openjdk.jmc.rjmx.JVMSupportToolkit;
79 import org.openjdk.jmc.rjmx.ServiceNotAvailableException;
80 import org.openjdk.jmc.rjmx.services.ICommercialFeaturesService;
81 import org.openjdk.jmc.rjmx.services.jfr.FlightRecorderException;
82 import org.openjdk.jmc.rjmx.services.jfr.IFlightRecorderService;
83 import org.openjdk.jmc.rjmx.services.jfr.IRecordingDescriptor;
84 import org.openjdk.jmc.rjmx.subscription.IMBeanHelperService;
85
86 public class FlightRecorderServiceV2 implements IFlightRecorderService {
87 final static Logger LOGGER = Logger.getLogger("org.openjdk.jmc.rjmx.services.jfr"); //$NON-NLS-1$
88 final private FlightRecorderCommunicationHelperV2 helper;
89 private long eventTypeMetaNextUpdate;
90 private List<EventTypeMetadataV2> eventTypeMetas;
91 private Map<EventTypeIDV2, EventTypeMetadataV2> eventTypeInfoById;
92 private Map<org.openjdk.jmc.flightrecorder.configuration.events.EventOptionID, OptionInfo<?>> optionInfoById;
93 private final ICommercialFeaturesService cfs;
94 private final IMBeanHelperService mbhs;
95 private final String serverId;
96
97 @Override
98 public String getVersion() {
99 return "2.0"; //$NON-NLS-1$
100 }
101
102 private boolean isDynamicFlightRecorderSupported(IConnectionHandle handle) {
103 return ConnectionToolkit.isHotSpot(handle)
104 && ConnectionToolkit.isJavaVersionAboveOrEqual(handle, JavaVersionSupport.DYNAMIC_JFR_SUPPORTED);
105 }
106
107 private boolean isFlightRecorderDisabled(IConnectionHandle handle) {
108 if (cfs != null) {
109 return !cfs.isCommercialFeaturesEnabled() || JVMSupportToolkit.isFlightRecorderDisabled(handle, false);
110 } else {
111 return true;
112 }
113 }
114
115 public static boolean isAvailable(IConnectionHandle handle) {
116 return FlightRecorderCommunicationHelperV2.isAvailable(handle);
117 }
118
119 public FlightRecorderServiceV2(IConnectionHandle handle) throws ConnectionException, ServiceNotAvailableException {
120 cfs = handle.getServiceOrThrow(ICommercialFeaturesService.class);
121 if (!isDynamicFlightRecorderSupported(handle) && isFlightRecorderDisabled(handle)) {
122 throw new ServiceNotAvailableException(""); //$NON-NLS-1$
123 }
124 if (JVMSupportToolkit.isFlightRecorderDisabled(handle, true)) {
125 throw new ServiceNotAvailableException(""); //$NON-NLS-1$
126 }
127 helper = new FlightRecorderCommunicationHelperV2(handle.getServiceOrThrow(MBeanServerConnection.class));
128 mbhs = handle.getServiceOrThrow(IMBeanHelperService.class);
129 serverId = handle.getServerDescriptor().getGUID();
130 }
131
132 @Override
133 public void stop(IRecordingDescriptor descriptor) throws FlightRecorderException {
134 stop(descriptor.getId());
135 }
136
137 private void stop(Long id) throws FlightRecorderException {
138 try {
139 helper.invokeOperation("stopRecording", id); //$NON-NLS-1$
140 } catch (Exception e) {
141 throw new FlightRecorderException("Could not stop the recording!", e); //$NON-NLS-1$
142 }
143 }
144
145 @Override
146 public void close(IRecordingDescriptor descriptor) throws FlightRecorderException {
453 * time by cloning the recording and getting the end time from there like in the commented
454 * out code below.
455 */
456 // IRecordingDescriptor streamDescriptor = descriptor;
457 // boolean clone = isStillRunning(descriptor);
458 // if (clone) {
459 // streamDescriptor = clone(descriptor);
460 // }
461 // IQuantity endTime = streamDescriptor.getDataEndTime();
462 // IQuantity startTime = endTime.subtract(lastPartDuration);
463 // return new JfrRecordingInputStreamV2(helper, streamDescriptor, toDate(startTime), toDate(endTime), clone | removeOnClose);
464
465 long serverTime = mbhs.getApproximateServerTime(System.currentTimeMillis());
466 IQuantity endDate = EPOCH_MS.quantity(serverTime);
467 IQuantity startDate = endDate.subtract(lastPartDuration);
468 return openStream(descriptor, startDate, endDate, removeOnClose);
469 }
470
471 @Override
472 public boolean isEnabled() {
473 return cfs.isCommercialFeaturesEnabled();
474 }
475
476 @Override
477 public void enable() throws FlightRecorderException {
478 try {
479 cfs.enableCommercialFeatures();
480 } catch (Exception e) {
481 throw new FlightRecorderException("Failed to enable commercial features", e); //$NON-NLS-1$
482 }
483 }
484 }
|
76 import org.openjdk.jmc.rjmx.ConnectionToolkit;
77 import org.openjdk.jmc.rjmx.IConnectionHandle;
78 import org.openjdk.jmc.rjmx.JVMSupportToolkit;
79 import org.openjdk.jmc.rjmx.ServiceNotAvailableException;
80 import org.openjdk.jmc.rjmx.services.ICommercialFeaturesService;
81 import org.openjdk.jmc.rjmx.services.jfr.FlightRecorderException;
82 import org.openjdk.jmc.rjmx.services.jfr.IFlightRecorderService;
83 import org.openjdk.jmc.rjmx.services.jfr.IRecordingDescriptor;
84 import org.openjdk.jmc.rjmx.subscription.IMBeanHelperService;
85
86 public class FlightRecorderServiceV2 implements IFlightRecorderService {
87 final static Logger LOGGER = Logger.getLogger("org.openjdk.jmc.rjmx.services.jfr"); //$NON-NLS-1$
88 final private FlightRecorderCommunicationHelperV2 helper;
89 private long eventTypeMetaNextUpdate;
90 private List<EventTypeMetadataV2> eventTypeMetas;
91 private Map<EventTypeIDV2, EventTypeMetadataV2> eventTypeInfoById;
92 private Map<org.openjdk.jmc.flightrecorder.configuration.events.EventOptionID, OptionInfo<?>> optionInfoById;
93 private final ICommercialFeaturesService cfs;
94 private final IMBeanHelperService mbhs;
95 private final String serverId;
96 private final IConnectionHandle connection;
97
98 @Override
99 public String getVersion() {
100 return "2.0"; //$NON-NLS-1$
101 }
102
103 private boolean isDynamicFlightRecorderSupported(IConnectionHandle handle) {
104 return ConnectionToolkit.isHotSpot(handle)
105 && ConnectionToolkit.isJavaVersionAboveOrEqual(handle, JavaVersionSupport.DYNAMIC_JFR_SUPPORTED);
106 }
107
108 private boolean isFlightRecorderCommercial() {
109 return ConnectionToolkit.isHotSpot(connection)
110 && !ConnectionToolkit.isJavaVersionAboveOrEqual(connection, JavaVersionSupport.JFR_NOT_COMMERCIAL);
111 }
112
113 private boolean isFlightRecorderDisabled(IConnectionHandle handle) {
114 if (cfs != null && isFlightRecorderCommercial()) {
115 return !cfs.isCommercialFeaturesEnabled() || JVMSupportToolkit.isFlightRecorderDisabled(handle, false);
116 } else {
117 return JVMSupportToolkit.isFlightRecorderDisabled(handle, false);
118 }
119 }
120
121 public static boolean isAvailable(IConnectionHandle handle) {
122 return FlightRecorderCommunicationHelperV2.isAvailable(handle);
123 }
124
125 public FlightRecorderServiceV2(IConnectionHandle handle) throws ConnectionException, ServiceNotAvailableException {
126 cfs = handle.getServiceOrThrow(ICommercialFeaturesService.class);
127 if (!isDynamicFlightRecorderSupported(handle) && isFlightRecorderDisabled(handle)) {
128 throw new ServiceNotAvailableException(""); //$NON-NLS-1$
129 }
130 if (JVMSupportToolkit.isFlightRecorderDisabled(handle, true)) {
131 throw new ServiceNotAvailableException(""); //$NON-NLS-1$
132 }
133 connection = handle;
134 helper = new FlightRecorderCommunicationHelperV2(handle.getServiceOrThrow(MBeanServerConnection.class));
135 mbhs = handle.getServiceOrThrow(IMBeanHelperService.class);
136 serverId = handle.getServerDescriptor().getGUID();
137 }
138
139 @Override
140 public void stop(IRecordingDescriptor descriptor) throws FlightRecorderException {
141 stop(descriptor.getId());
142 }
143
144 private void stop(Long id) throws FlightRecorderException {
145 try {
146 helper.invokeOperation("stopRecording", id); //$NON-NLS-1$
147 } catch (Exception e) {
148 throw new FlightRecorderException("Could not stop the recording!", e); //$NON-NLS-1$
149 }
150 }
151
152 @Override
153 public void close(IRecordingDescriptor descriptor) throws FlightRecorderException {
460 * time by cloning the recording and getting the end time from there like in the commented
461 * out code below.
462 */
463 // IRecordingDescriptor streamDescriptor = descriptor;
464 // boolean clone = isStillRunning(descriptor);
465 // if (clone) {
466 // streamDescriptor = clone(descriptor);
467 // }
468 // IQuantity endTime = streamDescriptor.getDataEndTime();
469 // IQuantity startTime = endTime.subtract(lastPartDuration);
470 // return new JfrRecordingInputStreamV2(helper, streamDescriptor, toDate(startTime), toDate(endTime), clone | removeOnClose);
471
472 long serverTime = mbhs.getApproximateServerTime(System.currentTimeMillis());
473 IQuantity endDate = EPOCH_MS.quantity(serverTime);
474 IQuantity startDate = endDate.subtract(lastPartDuration);
475 return openStream(descriptor, startDate, endDate, removeOnClose);
476 }
477
478 @Override
479 public boolean isEnabled() {
480 return isFlightRecorderCommercial()
481 ? cfs.isCommercialFeaturesEnabled()
482 : isAvailable(connection);
483 }
484
485 @Override
486 public void enable() throws FlightRecorderException {
487 try {
488 cfs.enableCommercialFeatures();
489 } catch (Exception e) {
490 throw new FlightRecorderException("Failed to enable commercial features", e); //$NON-NLS-1$
491 }
492 }
493 }
|