Path: blob/master/jcl/src/jdk.management/share/classes/com/sun/management/GarbageCollectionNotificationInfo.java
12733 views
/*[INCLUDE-IF Sidecar18-SE]*/1/*******************************************************************************2* Copyright (c) 2016, 2021 IBM Corp. and others3*4* This program and the accompanying materials are made available under5* the terms of the Eclipse Public License 2.0 which accompanies this6* distribution and is available at https://www.eclipse.org/legal/epl-2.0/7* or the Apache License, Version 2.0 which accompanies this distribution and8* is available at https://www.apache.org/licenses/LICENSE-2.0.9*10* This Source Code may also be made available under the following11* Secondary Licenses when the conditions for such availability set12* forth in the Eclipse Public License, v. 2.0 are satisfied: GNU13* General Public License, version 2 with the GNU Classpath14* Exception [1] and GNU General Public License, version 2 with the15* OpenJDK Assembly Exception [2].16*17* [1] https://www.gnu.org/software/classpath/license.html18* [2] http://openjdk.java.net/legal/assembly-exception.html19*20* SPDX-License-Identifier: EPL-2.0 OR Apache-2.0 OR GPL-2.0 WITH Classpath-exception-2.0 OR LicenseRef-GPL-2.0 WITH Assembly-exception21*******************************************************************************/22package com.sun.management;2324import javax.management.openmbean.CompositeData;25import javax.management.openmbean.CompositeDataView;26import javax.management.openmbean.CompositeType;2728import com.ibm.java.lang.management.internal.ManagementUtils;29import com.sun.management.internal.GarbageCollectionNotificationInfoUtil;3031/**32* Information about a garbage collection.33*34* <p>35* A garbage collection notification is emitted by {@link GarbageCollectorMXBean}36* when the Java virtual machine completes a garbage collection action.37* The notification emitted will contain the garbage collection notification38* information about the status of the memory:39* <ul>40* <li>The name of the garbage collector used perform the collection.</li>41* <li>The action performed by the garbage collector.</li>42* <li>The cause of the garbage collection action.</li>43* <li>A {@link GcInfo} object containing some statistics about the GC cycle44* (start time, end time) and the memory usage before and after45* the GC cycle.</li>46* </ul>47*48* <p>49* A {@link CompositeData CompositeData} representing50* the {@code GarbageCollectionNotificationInfo} object51* is stored in the52* {@linkplain javax.management.Notification#setUserData userdata}53* of a {@linkplain javax.management.Notification notification}.54* The {@link #from from} method is provided to convert from55* a {@code CompositeData} to a {@code GarbageCollectionNotificationInfo}56* object. For example:57*58* <blockquote><pre>59* Notification notif;60*61* // receive the notification emitted by a GarbageCollectorMXBean and save in notif62* ...63*64* String notifType = notif.getType();65* if (notifType.equals(GarbageCollectionNotificationInfo.GARBAGE_COLLECTION_NOTIFICATION)) {66* // retrieve the garbage collection notification information67* CompositeData cd = (CompositeData) notif.getUserData();68* GarbageCollectionNotificationInfo info = GarbageCollectionNotificationInfo.from(cd);69* ...70* }71* </pre></blockquote>72*73* <p>74* The type of the notification emitted by a {@code GarbageCollectorMXBean} is:75* <ul>76* <li>A {@linkplain #GARBAGE_COLLECTION_NOTIFICATION garbage collection notification}.77* <br>Used by every notification emitted by the garbage collector, the details about78* the notification are provided in the {@linkplain #getGcAction action} String.79* </li>80* </ul>81**/82public class GarbageCollectionNotificationInfo implements CompositeDataView {8384/**85* Notification type denoting that the Java virtual machine has completed a garbage collection cycle.86* This notification is emitted by a GarbageCollectorMXBean. The value of this notification type is87* "com.ibm.lang.management.gc.notification" which matches RI naming for compatibility.88*/89public static final String GARBAGE_COLLECTION_NOTIFICATION = "com.sun.management.gc.notification"; //$NON-NLS-1$9091/**92* Comment for <code>gcName</code>93*/94private final String gcName;9596/**97* Comment for <code>gcAction</code>98*/99private final String gcAction;100101/**102* Comment for <code>gcCause</code>103*/104private final String gcCause;105106/**107* Comment for <code>gcInfo</code>108*/109private final GcInfo gcInfo;110111private CompositeData cdata;112113private CompositeData getCompositeData() {114if (null == cdata) {115cdata = GarbageCollectionNotificationInfoUtil.toCompositeData(this);116}117return cdata;118}119120private void setCompositeData(CompositeData cd) {121cdata = cd;122}123124/**125* Creates a new <code>GarbageCollectionNotificationInfo</code> instance.126*127* @param gcName128* the name of the garbage collector used to perform the collection129* @param gcAction130* the action of the performed by the garbage collector131* @param gcCause132* the cause the garbage collection133* @param gcInfo134* a GcInfo object providing statistics about the GC cycle135*136*/137public GarbageCollectionNotificationInfo(String gcName, String gcAction, String gcCause, GcInfo gcInfo) {138super();139this.gcName = gcName;140this.gcAction = gcAction;141this.gcCause = gcCause;142this.gcInfo = gcInfo;143}144145/**146* @return the name of the garbage collector used to perform the collection.147*/148public String getGcName() {149return this.gcName;150}151152/**153* @return the action of the performed by the garbage collector154*/155public String getGcAction() {156return this.gcAction;157}158159/**160* @return the cause of the garbage collection161*/162public String getGcCause() {163return this.gcCause;164}165166/**167* @return the GC information related to the last garbage collection168*/169public GcInfo getGcInfo() {170return gcInfo;171}172173/**174* Returns a {@code GarbageCollectionNotificationInfo} object represented by the175* given {@code CompositeData}.176* The given {@code CompositeData} must contain the following attributes:177* <blockquote>178* <table border=1>179* <caption>CompositeData attributes</caption>180* <tr>181* <th style="text-align:left">Attribute Name</th>182* <th style="text-align:left">Type</th>183* </tr>184* <tr>185* <td>gcName</td>186* <td>{@code java.lang.String}</td>187* </tr>188* <tr>189* <td>gcAction</td>190* <td>{@code java.lang.String}</td>191* </tr>192* <tr>193* <td>gcCause</td>194* <td>{@code java.lang.String}</td>195* </tr>196* <tr>197* <td>gcInfo</td>198* <td>{@code com.ibm.lang.management.GcInfo}</td>199* </tr>200* </table>201* </blockquote>202*203* @param cd {@code CompositeData} representing a204* {@code GarbageCollectionNotificationInfo}205*206* @throws IllegalArgumentException if {@code cd} does not207* represent a {@code GarbageCollectionNotificationInfo} object208*209* @return a {@code GarbageCollectionNotificationInfo} object represented210* by {@code cd} if {@code cd} is not {@code null};211* {@code null} otherwise212*/213public static GarbageCollectionNotificationInfo from(CompositeData cd) {214GarbageCollectionNotificationInfo result = null;215216if (cd != null) {217/* Does cd meet the necessary criteria to create a new218* GarbageCollectionNotificationInfo?219* If not then exit on an IllegalArgumentException.220*/221ManagementUtils.verifyFieldNumber(cd, 4);222String[] attributeNames = { "gcName", "gcAction", "gcCause", "gcInfo" }; //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$ //$NON-NLS-4$223ManagementUtils.verifyFieldNames(cd, attributeNames);224String[] attributeTypes = { "java.lang.String", //$NON-NLS-1$225"java.lang.String", //$NON-NLS-1$226"java.lang.String", //$NON-NLS-1$227CompositeData.class.getName() };228ManagementUtils.verifyFieldTypes(cd, attributeNames, attributeTypes);229230/* Extract the values of the attributes and use them to construct231* a new GarbageCollectionNotificationInfo.232*/233Object[] attributeVals = cd.getAll(attributeNames);234String gcNameVal = (String) attributeVals[0];235String gcActionVal = (String) attributeVals[1];236String gcCauseVal = (String) attributeVals[2];237GcInfo gcInfoVal = GcInfo.from((CompositeData) attributeVals[3]);238239result = new GarbageCollectionNotificationInfo(gcNameVal, gcActionVal, gcCauseVal, gcInfoVal);240result.setCompositeData(cd);241}242243return result;244}245246/* Implementation of the CompositeDataView interface */247248/**249* <p>Return the {@code CompositeData} representation of this250* {@code GarbageCollectionNotificationInfo}.251*252* @param ct the {@code CompositeType} that the caller expects.253* This parameter is ignored and can be null.254*255* @return the {@code CompositeData} representation.256*/257@Override258public CompositeData toCompositeData(CompositeType ct) {259return getCompositeData();260}261}262263264