Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/openj9
Path: blob/master/jcl/src/jdk.management/share/classes/com/sun/management/GarbageCollectionNotificationInfo.java
12733 views
1
/*[INCLUDE-IF Sidecar18-SE]*/
2
/*******************************************************************************
3
* Copyright (c) 2016, 2021 IBM Corp. and others
4
*
5
* This program and the accompanying materials are made available under
6
* the terms of the Eclipse Public License 2.0 which accompanies this
7
* distribution and is available at https://www.eclipse.org/legal/epl-2.0/
8
* or the Apache License, Version 2.0 which accompanies this distribution and
9
* is available at https://www.apache.org/licenses/LICENSE-2.0.
10
*
11
* This Source Code may also be made available under the following
12
* Secondary Licenses when the conditions for such availability set
13
* forth in the Eclipse Public License, v. 2.0 are satisfied: GNU
14
* General Public License, version 2 with the GNU Classpath
15
* Exception [1] and GNU General Public License, version 2 with the
16
* OpenJDK Assembly Exception [2].
17
*
18
* [1] https://www.gnu.org/software/classpath/license.html
19
* [2] http://openjdk.java.net/legal/assembly-exception.html
20
*
21
* 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-exception
22
*******************************************************************************/
23
package com.sun.management;
24
25
import javax.management.openmbean.CompositeData;
26
import javax.management.openmbean.CompositeDataView;
27
import javax.management.openmbean.CompositeType;
28
29
import com.ibm.java.lang.management.internal.ManagementUtils;
30
import com.sun.management.internal.GarbageCollectionNotificationInfoUtil;
31
32
/**
33
* Information about a garbage collection.
34
*
35
* <p>
36
* A garbage collection notification is emitted by {@link GarbageCollectorMXBean}
37
* when the Java virtual machine completes a garbage collection action.
38
* The notification emitted will contain the garbage collection notification
39
* information about the status of the memory:
40
* <ul>
41
* <li>The name of the garbage collector used perform the collection.</li>
42
* <li>The action performed by the garbage collector.</li>
43
* <li>The cause of the garbage collection action.</li>
44
* <li>A {@link GcInfo} object containing some statistics about the GC cycle
45
* (start time, end time) and the memory usage before and after
46
* the GC cycle.</li>
47
* </ul>
48
*
49
* <p>
50
* A {@link CompositeData CompositeData} representing
51
* the {@code GarbageCollectionNotificationInfo} object
52
* is stored in the
53
* {@linkplain javax.management.Notification#setUserData userdata}
54
* of a {@linkplain javax.management.Notification notification}.
55
* The {@link #from from} method is provided to convert from
56
* a {@code CompositeData} to a {@code GarbageCollectionNotificationInfo}
57
* object. For example:
58
*
59
* <blockquote><pre>
60
* Notification notif;
61
*
62
* // receive the notification emitted by a GarbageCollectorMXBean and save in notif
63
* ...
64
*
65
* String notifType = notif.getType();
66
* if (notifType.equals(GarbageCollectionNotificationInfo.GARBAGE_COLLECTION_NOTIFICATION)) {
67
* // retrieve the garbage collection notification information
68
* CompositeData cd = (CompositeData) notif.getUserData();
69
* GarbageCollectionNotificationInfo info = GarbageCollectionNotificationInfo.from(cd);
70
* ...
71
* }
72
* </pre></blockquote>
73
*
74
* <p>
75
* The type of the notification emitted by a {@code GarbageCollectorMXBean} is:
76
* <ul>
77
* <li>A {@linkplain #GARBAGE_COLLECTION_NOTIFICATION garbage collection notification}.
78
* <br>Used by every notification emitted by the garbage collector, the details about
79
* the notification are provided in the {@linkplain #getGcAction action} String.
80
* </li>
81
* </ul>
82
**/
83
public class GarbageCollectionNotificationInfo implements CompositeDataView {
84
85
/**
86
* Notification type denoting that the Java virtual machine has completed a garbage collection cycle.
87
* This notification is emitted by a GarbageCollectorMXBean. The value of this notification type is
88
* "com.ibm.lang.management.gc.notification" which matches RI naming for compatibility.
89
*/
90
public static final String GARBAGE_COLLECTION_NOTIFICATION = "com.sun.management.gc.notification"; //$NON-NLS-1$
91
92
/**
93
* Comment for <code>gcName</code>
94
*/
95
private final String gcName;
96
97
/**
98
* Comment for <code>gcAction</code>
99
*/
100
private final String gcAction;
101
102
/**
103
* Comment for <code>gcCause</code>
104
*/
105
private final String gcCause;
106
107
/**
108
* Comment for <code>gcInfo</code>
109
*/
110
private final GcInfo gcInfo;
111
112
private CompositeData cdata;
113
114
private CompositeData getCompositeData() {
115
if (null == cdata) {
116
cdata = GarbageCollectionNotificationInfoUtil.toCompositeData(this);
117
}
118
return cdata;
119
}
120
121
private void setCompositeData(CompositeData cd) {
122
cdata = cd;
123
}
124
125
/**
126
* Creates a new <code>GarbageCollectionNotificationInfo</code> instance.
127
*
128
* @param gcName
129
* the name of the garbage collector used to perform the collection
130
* @param gcAction
131
* the action of the performed by the garbage collector
132
* @param gcCause
133
* the cause the garbage collection
134
* @param gcInfo
135
* a GcInfo object providing statistics about the GC cycle
136
*
137
*/
138
public GarbageCollectionNotificationInfo(String gcName, String gcAction, String gcCause, GcInfo gcInfo) {
139
super();
140
this.gcName = gcName;
141
this.gcAction = gcAction;
142
this.gcCause = gcCause;
143
this.gcInfo = gcInfo;
144
}
145
146
/**
147
* @return the name of the garbage collector used to perform the collection.
148
*/
149
public String getGcName() {
150
return this.gcName;
151
}
152
153
/**
154
* @return the action of the performed by the garbage collector
155
*/
156
public String getGcAction() {
157
return this.gcAction;
158
}
159
160
/**
161
* @return the cause of the garbage collection
162
*/
163
public String getGcCause() {
164
return this.gcCause;
165
}
166
167
/**
168
* @return the GC information related to the last garbage collection
169
*/
170
public GcInfo getGcInfo() {
171
return gcInfo;
172
}
173
174
/**
175
* Returns a {@code GarbageCollectionNotificationInfo} object represented by the
176
* given {@code CompositeData}.
177
* The given {@code CompositeData} must contain the following attributes:
178
* <blockquote>
179
* <table border=1>
180
* <caption>CompositeData attributes</caption>
181
* <tr>
182
* <th style="text-align:left">Attribute Name</th>
183
* <th style="text-align:left">Type</th>
184
* </tr>
185
* <tr>
186
* <td>gcName</td>
187
* <td>{@code java.lang.String}</td>
188
* </tr>
189
* <tr>
190
* <td>gcAction</td>
191
* <td>{@code java.lang.String}</td>
192
* </tr>
193
* <tr>
194
* <td>gcCause</td>
195
* <td>{@code java.lang.String}</td>
196
* </tr>
197
* <tr>
198
* <td>gcInfo</td>
199
* <td>{@code com.ibm.lang.management.GcInfo}</td>
200
* </tr>
201
* </table>
202
* </blockquote>
203
*
204
* @param cd {@code CompositeData} representing a
205
* {@code GarbageCollectionNotificationInfo}
206
*
207
* @throws IllegalArgumentException if {@code cd} does not
208
* represent a {@code GarbageCollectionNotificationInfo} object
209
*
210
* @return a {@code GarbageCollectionNotificationInfo} object represented
211
* by {@code cd} if {@code cd} is not {@code null};
212
* {@code null} otherwise
213
*/
214
public static GarbageCollectionNotificationInfo from(CompositeData cd) {
215
GarbageCollectionNotificationInfo result = null;
216
217
if (cd != null) {
218
/* Does cd meet the necessary criteria to create a new
219
* GarbageCollectionNotificationInfo?
220
* If not then exit on an IllegalArgumentException.
221
*/
222
ManagementUtils.verifyFieldNumber(cd, 4);
223
String[] attributeNames = { "gcName", "gcAction", "gcCause", "gcInfo" }; //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$ //$NON-NLS-4$
224
ManagementUtils.verifyFieldNames(cd, attributeNames);
225
String[] attributeTypes = { "java.lang.String", //$NON-NLS-1$
226
"java.lang.String", //$NON-NLS-1$
227
"java.lang.String", //$NON-NLS-1$
228
CompositeData.class.getName() };
229
ManagementUtils.verifyFieldTypes(cd, attributeNames, attributeTypes);
230
231
/* Extract the values of the attributes and use them to construct
232
* a new GarbageCollectionNotificationInfo.
233
*/
234
Object[] attributeVals = cd.getAll(attributeNames);
235
String gcNameVal = (String) attributeVals[0];
236
String gcActionVal = (String) attributeVals[1];
237
String gcCauseVal = (String) attributeVals[2];
238
GcInfo gcInfoVal = GcInfo.from((CompositeData) attributeVals[3]);
239
240
result = new GarbageCollectionNotificationInfo(gcNameVal, gcActionVal, gcCauseVal, gcInfoVal);
241
result.setCompositeData(cd);
242
}
243
244
return result;
245
}
246
247
/* Implementation of the CompositeDataView interface */
248
249
/**
250
* <p>Return the {@code CompositeData} representation of this
251
* {@code GarbageCollectionNotificationInfo}.
252
*
253
* @param ct the {@code CompositeType} that the caller expects.
254
* This parameter is ignored and can be null.
255
*
256
* @return the {@code CompositeData} representation.
257
*/
258
@Override
259
public CompositeData toCompositeData(CompositeType ct) {
260
return getCompositeData();
261
}
262
}
263
264