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/GcInfo.java
12730 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 java.lang.management.MemoryUsage;
26
import java.util.Collection;
27
import java.util.HashMap;
28
import java.util.Map;
29
30
import javax.management.openmbean.CompositeData;
31
import javax.management.openmbean.CompositeDataView;
32
import javax.management.openmbean.CompositeType;
33
import javax.management.openmbean.TabularData;
34
35
import com.ibm.java.lang.management.internal.ManagementUtils;
36
import com.sun.management.internal.GcInfoUtil;
37
38
/**
39
* Garbage collection information. It contains the following
40
* information for one garbage collection as well as GC-specific
41
* attributes:
42
* <blockquote>
43
* <ul>
44
* <li>Start time</li>
45
* <li>End time</li>
46
* <li>Duration</li>
47
* <li>Memory usage before the collection starts</li>
48
* <li>Memory usage after the collection ends</li>
49
* </ul>
50
* </blockquote>
51
*
52
* @since 9
53
*/
54
public class GcInfo implements CompositeData, CompositeDataView {
55
56
/**
57
* Comment for <code>index</code>
58
*/
59
private final long index;
60
61
/**
62
* Comment for <code>startTime</code>
63
*/
64
private final long startTime;
65
66
/**
67
* Comment for <code>endTime</code>
68
*/
69
private final long endTime;
70
71
/**
72
* Comment for <code>usageBeforeGc</code>
73
*/
74
private final Map<String, MemoryUsage> usageBeforeGc;
75
76
/**
77
* Comment for <code>usageAfterGc</code>
78
*/
79
private final Map<String, MemoryUsage> usageAfterGc;
80
81
private CompositeData cdata;
82
83
private CompositeData getCompositeData() {
84
if (null == cdata) {
85
cdata = GcInfoUtil.toCompositeData(this);
86
}
87
return cdata;
88
}
89
90
private void setCompositeData(CompositeData cd) {
91
cdata = cd;
92
}
93
/**
94
* Creates a new <code>GcInfo</code> instance.
95
*
96
* @param index
97
* the identifier of this garbage collection which is the number of collections that this collector has done
98
* @param startTime
99
* the start time of the collection in milliseconds since the Java virtual machine was started.
100
* @param endTime
101
* the end time of the collection in milliseconds since the Java virtual machine was started.
102
* @param usageBeforeGc
103
* the memory usage of all memory pools at the beginning of this GC.
104
* @param usageAfterGc
105
* the memory usage of all memory pools at the end of this GC.
106
*/
107
private GcInfo(long index, long startTime, long endTime, Map<String, MemoryUsage> usageBeforeGc, Map<String, MemoryUsage> usageAfterGc) {
108
super();
109
this.index = index;
110
this.startTime = startTime;
111
this.endTime = endTime;
112
this.usageBeforeGc = usageBeforeGc;
113
this.usageAfterGc = usageAfterGc;
114
}
115
116
/**
117
* @return the identifier of this garbage collection which is
118
* the number of collections that this collector has done.
119
*/
120
public long getId() {
121
return index;
122
}
123
124
/**
125
* Returns the start time of this GC in milliseconds
126
* since the Java virtual machine was started.
127
*
128
* @return the start time of this GC.
129
*/
130
public long getStartTime() {
131
return this.startTime;
132
}
133
134
/**
135
* Returns the end time of this GC in milliseconds
136
* since the Java virtual machine was started.
137
*
138
* @return the end time of this GC.
139
*/
140
public long getEndTime() {
141
return this.endTime;
142
}
143
144
/**
145
* Returns the elapsed time of this GC in milliseconds.
146
*
147
* @return the elapsed time of this GC in milliseconds.
148
*/
149
public long getDuration() {
150
return this.endTime - this.startTime;
151
}
152
153
/**
154
* Returns the memory usage of all memory pools
155
* at the beginning of this GC.
156
* This method returns
157
* a <code>Map</code> of the name of a memory pool
158
* to the memory usage of the corresponding
159
* memory pool before GC starts.
160
*
161
* @return a <code>Map</code> of memory pool names to the memory
162
* usage of a memory pool before GC starts.
163
*/
164
public Map<String, MemoryUsage> getMemoryUsageBeforeGc() {
165
return this.usageBeforeGc;
166
}
167
168
/**
169
* Returns the memory usage of all memory pools
170
* at the end of this GC.
171
* This method returns
172
* a <code>Map</code> of the name of a memory pool
173
* to the memory usage of the corresponding
174
* memory pool when GC finishes.
175
*
176
* @return a <code>Map</code> of memory pool names to the memory
177
* usage of a memory pool when GC finishes.
178
*/
179
public Map<String, MemoryUsage> getMemoryUsageAfterGc() {
180
return this.usageAfterGc;
181
}
182
183
/**
184
* Returns a <code>GcInfo</code> object represented by the
185
* given <code>CompositeData</code>. The given
186
* <code>CompositeData</code> must contain
187
* all the following attributes:
188
*
189
* <blockquote>
190
* <table border=1>
191
* <caption>CompositeData attributes</caption>
192
* <tr>
193
* <th style="text-align:left">Attribute Name</th>
194
* <th style="text-align:left">Type</th>
195
* </tr>
196
* <tr>
197
* <td>index</td>
198
* <td><code>java.lang.Long</code></td>
199
* </tr>
200
* <tr>
201
* <td>startTime</td>
202
* <td><code>java.lang.Long</code></td>
203
* </tr>
204
* <tr>
205
* <td>endTime</td>
206
* <td><code>java.lang.Long</code></td>
207
* </tr>
208
* <tr>
209
* <td>memoryUsageBeforeGc</td>
210
* <td><code>javax.management.openmbean.TabularData</code></td>
211
* </tr>
212
* <tr>
213
* <td>memoryUsageAfterGc</td>
214
* <td><code>javax.management.openmbean.TabularData</code></td>
215
* </tr>
216
* </table>
217
* </blockquote>
218
* @param cd <code>CompositeData</code> representing a <code>GcInfo</code>
219
*
220
* @throws IllegalArgumentException if <code>cd</code> does not
221
* represent a <code>GcInfo</code> object with the attributes
222
* described above.
223
*
224
* @return a <code>GcInfo</code> object represented by <code>cd</code>
225
* if <code>cd</code> is not <code>null</code>; <code>null</code> otherwise.
226
*/
227
public static GcInfo from(CompositeData cd) {
228
GcInfo result = null;
229
230
if (cd != null) {
231
// Does cd meet the necessary criteria to create a new GcInfo?
232
// If not then exit on an IllegalArgumentException.
233
ManagementUtils.verifyFieldNumber(cd, 5);
234
String[] attributeNames = { "index", "startTime", "endTime", "usageBeforeGc", "usageAfterGc" }; //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$ //$NON-NLS-4$ //$NON-NLS-5$
235
ManagementUtils.verifyFieldNames(cd, attributeNames);
236
String[] attributeTypes = { "java.lang.Long", //$NON-NLS-1$
237
"java.lang.Long", //$NON-NLS-1$
238
"java.lang.Long", //$NON-NLS-1$
239
TabularData.class.getName(),
240
TabularData.class.getName()};
241
ManagementUtils.verifyFieldTypes(cd, attributeNames, attributeTypes);
242
243
// Extract the values of the attributes and use them to construct a new GcInfo.
244
Object[] attributeVals = cd.getAll(attributeNames);
245
long indexVal = ((Long) attributeVals[0]).longValue();
246
long startTimeVal = ((Long) attributeVals[1]).longValue();
247
long endTimeVal = ((Long) attributeVals[2]).longValue();
248
Map<String, MemoryUsage> usageBeforeGcVal = convertTabularDataToMemoryUsageMap((TabularData) attributeVals[3]);
249
Map<String, MemoryUsage> usageAfterGcVal = convertTabularDataToMemoryUsageMap((TabularData) attributeVals[4]);
250
251
result = new GcInfo(indexVal, startTimeVal, endTimeVal, usageBeforeGcVal, usageAfterGcVal);
252
result.setCompositeData(cd);
253
}
254
255
return result;
256
}
257
258
private static Map<String, MemoryUsage> convertTabularDataToMemoryUsageMap(TabularData td) {
259
Map<String, MemoryUsage> result = new HashMap<>();
260
261
for (CompositeData row : (Collection<CompositeData>) td.values()) {
262
String keyVal = (String) row.get("key"); //$NON-NLS-1$
263
MemoryUsage usageVal = MemoryUsage.from((CompositeData) row.get("value")); //$NON-NLS-1$
264
result.put(keyVal, usageVal);
265
}
266
267
return result;
268
}
269
270
/* Implementation of the CompositeData interface */
271
272
/**
273
* {@inheritDoc}
274
*/
275
@Override
276
public boolean containsKey(String key) {
277
return getCompositeData().containsKey(key);
278
}
279
280
/**
281
* {@inheritDoc}
282
*/
283
@Override
284
public boolean containsValue(Object value) {
285
return getCompositeData().containsValue(value);
286
}
287
288
/**
289
* {@inheritDoc}
290
*/
291
@Override
292
public boolean equals(Object obj) {
293
return getCompositeData().equals(obj);
294
}
295
296
/**
297
* {@inheritDoc}
298
*/
299
@Override
300
public Object get(String key) {
301
return getCompositeData().get(key);
302
}
303
304
/**
305
* {@inheritDoc}
306
*/
307
@Override
308
public Object[] getAll(String[] keys) {
309
return getCompositeData().getAll(keys);
310
}
311
312
/**
313
* {@inheritDoc}
314
*/
315
@Override
316
public CompositeType getCompositeType() {
317
return getCompositeData().getCompositeType();
318
}
319
320
/**
321
* {@inheritDoc}
322
*/
323
@Override
324
public int hashCode() {
325
return getCompositeData().hashCode();
326
}
327
328
/**
329
* {@inheritDoc}
330
*/
331
@Override
332
public String toString() {
333
return getCompositeData().toString();
334
}
335
336
/**
337
* {@inheritDoc}
338
*/
339
@Override
340
public Collection<?> values() {
341
return getCompositeData().values();
342
}
343
344
/* Implementation of the CompositeDataView interface */
345
346
/**
347
* <p>Return the {@code CompositeData} representation of this
348
* {@code GcInfo}, including any GC-specific attributes. The
349
* returned value will have at least all the attributes described
350
* in the {@link #from(CompositeData) from} method, plus optionally
351
* other attributes.
352
*
353
* @param ct the {@code CompositeType} that the caller expects.
354
* This parameter is ignored and can be null.
355
*
356
* @return the {@code CompositeData} representation.
357
*/
358
@Override
359
public CompositeData toCompositeData(CompositeType ct) {
360
return getCompositeData();
361
}
362
}
363
364