Path: blob/master/jcl/src/jdk.management/share/classes/com/sun/management/GcInfo.java
12730 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 java.lang.management.MemoryUsage;25import java.util.Collection;26import java.util.HashMap;27import java.util.Map;2829import javax.management.openmbean.CompositeData;30import javax.management.openmbean.CompositeDataView;31import javax.management.openmbean.CompositeType;32import javax.management.openmbean.TabularData;3334import com.ibm.java.lang.management.internal.ManagementUtils;35import com.sun.management.internal.GcInfoUtil;3637/**38* Garbage collection information. It contains the following39* information for one garbage collection as well as GC-specific40* attributes:41* <blockquote>42* <ul>43* <li>Start time</li>44* <li>End time</li>45* <li>Duration</li>46* <li>Memory usage before the collection starts</li>47* <li>Memory usage after the collection ends</li>48* </ul>49* </blockquote>50*51* @since 952*/53public class GcInfo implements CompositeData, CompositeDataView {5455/**56* Comment for <code>index</code>57*/58private final long index;5960/**61* Comment for <code>startTime</code>62*/63private final long startTime;6465/**66* Comment for <code>endTime</code>67*/68private final long endTime;6970/**71* Comment for <code>usageBeforeGc</code>72*/73private final Map<String, MemoryUsage> usageBeforeGc;7475/**76* Comment for <code>usageAfterGc</code>77*/78private final Map<String, MemoryUsage> usageAfterGc;7980private CompositeData cdata;8182private CompositeData getCompositeData() {83if (null == cdata) {84cdata = GcInfoUtil.toCompositeData(this);85}86return cdata;87}8889private void setCompositeData(CompositeData cd) {90cdata = cd;91}92/**93* Creates a new <code>GcInfo</code> instance.94*95* @param index96* the identifier of this garbage collection which is the number of collections that this collector has done97* @param startTime98* the start time of the collection in milliseconds since the Java virtual machine was started.99* @param endTime100* the end time of the collection in milliseconds since the Java virtual machine was started.101* @param usageBeforeGc102* the memory usage of all memory pools at the beginning of this GC.103* @param usageAfterGc104* the memory usage of all memory pools at the end of this GC.105*/106private GcInfo(long index, long startTime, long endTime, Map<String, MemoryUsage> usageBeforeGc, Map<String, MemoryUsage> usageAfterGc) {107super();108this.index = index;109this.startTime = startTime;110this.endTime = endTime;111this.usageBeforeGc = usageBeforeGc;112this.usageAfterGc = usageAfterGc;113}114115/**116* @return the identifier of this garbage collection which is117* the number of collections that this collector has done.118*/119public long getId() {120return index;121}122123/**124* Returns the start time of this GC in milliseconds125* since the Java virtual machine was started.126*127* @return the start time of this GC.128*/129public long getStartTime() {130return this.startTime;131}132133/**134* Returns the end time of this GC in milliseconds135* since the Java virtual machine was started.136*137* @return the end time of this GC.138*/139public long getEndTime() {140return this.endTime;141}142143/**144* Returns the elapsed time of this GC in milliseconds.145*146* @return the elapsed time of this GC in milliseconds.147*/148public long getDuration() {149return this.endTime - this.startTime;150}151152/**153* Returns the memory usage of all memory pools154* at the beginning of this GC.155* This method returns156* a <code>Map</code> of the name of a memory pool157* to the memory usage of the corresponding158* memory pool before GC starts.159*160* @return a <code>Map</code> of memory pool names to the memory161* usage of a memory pool before GC starts.162*/163public Map<String, MemoryUsage> getMemoryUsageBeforeGc() {164return this.usageBeforeGc;165}166167/**168* Returns the memory usage of all memory pools169* at the end of this GC.170* This method returns171* a <code>Map</code> of the name of a memory pool172* to the memory usage of the corresponding173* memory pool when GC finishes.174*175* @return a <code>Map</code> of memory pool names to the memory176* usage of a memory pool when GC finishes.177*/178public Map<String, MemoryUsage> getMemoryUsageAfterGc() {179return this.usageAfterGc;180}181182/**183* Returns a <code>GcInfo</code> object represented by the184* given <code>CompositeData</code>. The given185* <code>CompositeData</code> must contain186* all the following attributes:187*188* <blockquote>189* <table border=1>190* <caption>CompositeData attributes</caption>191* <tr>192* <th style="text-align:left">Attribute Name</th>193* <th style="text-align:left">Type</th>194* </tr>195* <tr>196* <td>index</td>197* <td><code>java.lang.Long</code></td>198* </tr>199* <tr>200* <td>startTime</td>201* <td><code>java.lang.Long</code></td>202* </tr>203* <tr>204* <td>endTime</td>205* <td><code>java.lang.Long</code></td>206* </tr>207* <tr>208* <td>memoryUsageBeforeGc</td>209* <td><code>javax.management.openmbean.TabularData</code></td>210* </tr>211* <tr>212* <td>memoryUsageAfterGc</td>213* <td><code>javax.management.openmbean.TabularData</code></td>214* </tr>215* </table>216* </blockquote>217* @param cd <code>CompositeData</code> representing a <code>GcInfo</code>218*219* @throws IllegalArgumentException if <code>cd</code> does not220* represent a <code>GcInfo</code> object with the attributes221* described above.222*223* @return a <code>GcInfo</code> object represented by <code>cd</code>224* if <code>cd</code> is not <code>null</code>; <code>null</code> otherwise.225*/226public static GcInfo from(CompositeData cd) {227GcInfo result = null;228229if (cd != null) {230// Does cd meet the necessary criteria to create a new GcInfo?231// If not then exit on an IllegalArgumentException.232ManagementUtils.verifyFieldNumber(cd, 5);233String[] attributeNames = { "index", "startTime", "endTime", "usageBeforeGc", "usageAfterGc" }; //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$ //$NON-NLS-4$ //$NON-NLS-5$234ManagementUtils.verifyFieldNames(cd, attributeNames);235String[] attributeTypes = { "java.lang.Long", //$NON-NLS-1$236"java.lang.Long", //$NON-NLS-1$237"java.lang.Long", //$NON-NLS-1$238TabularData.class.getName(),239TabularData.class.getName()};240ManagementUtils.verifyFieldTypes(cd, attributeNames, attributeTypes);241242// Extract the values of the attributes and use them to construct a new GcInfo.243Object[] attributeVals = cd.getAll(attributeNames);244long indexVal = ((Long) attributeVals[0]).longValue();245long startTimeVal = ((Long) attributeVals[1]).longValue();246long endTimeVal = ((Long) attributeVals[2]).longValue();247Map<String, MemoryUsage> usageBeforeGcVal = convertTabularDataToMemoryUsageMap((TabularData) attributeVals[3]);248Map<String, MemoryUsage> usageAfterGcVal = convertTabularDataToMemoryUsageMap((TabularData) attributeVals[4]);249250result = new GcInfo(indexVal, startTimeVal, endTimeVal, usageBeforeGcVal, usageAfterGcVal);251result.setCompositeData(cd);252}253254return result;255}256257private static Map<String, MemoryUsage> convertTabularDataToMemoryUsageMap(TabularData td) {258Map<String, MemoryUsage> result = new HashMap<>();259260for (CompositeData row : (Collection<CompositeData>) td.values()) {261String keyVal = (String) row.get("key"); //$NON-NLS-1$262MemoryUsage usageVal = MemoryUsage.from((CompositeData) row.get("value")); //$NON-NLS-1$263result.put(keyVal, usageVal);264}265266return result;267}268269/* Implementation of the CompositeData interface */270271/**272* {@inheritDoc}273*/274@Override275public boolean containsKey(String key) {276return getCompositeData().containsKey(key);277}278279/**280* {@inheritDoc}281*/282@Override283public boolean containsValue(Object value) {284return getCompositeData().containsValue(value);285}286287/**288* {@inheritDoc}289*/290@Override291public boolean equals(Object obj) {292return getCompositeData().equals(obj);293}294295/**296* {@inheritDoc}297*/298@Override299public Object get(String key) {300return getCompositeData().get(key);301}302303/**304* {@inheritDoc}305*/306@Override307public Object[] getAll(String[] keys) {308return getCompositeData().getAll(keys);309}310311/**312* {@inheritDoc}313*/314@Override315public CompositeType getCompositeType() {316return getCompositeData().getCompositeType();317}318319/**320* {@inheritDoc}321*/322@Override323public int hashCode() {324return getCompositeData().hashCode();325}326327/**328* {@inheritDoc}329*/330@Override331public String toString() {332return getCompositeData().toString();333}334335/**336* {@inheritDoc}337*/338@Override339public Collection<?> values() {340return getCompositeData().values();341}342343/* Implementation of the CompositeDataView interface */344345/**346* <p>Return the {@code CompositeData} representation of this347* {@code GcInfo}, including any GC-specific attributes. The348* returned value will have at least all the attributes described349* in the {@link #from(CompositeData) from} method, plus optionally350* other attributes.351*352* @param ct the {@code CompositeType} that the caller expects.353* This parameter is ignored and can be null.354*355* @return the {@code CompositeData} representation.356*/357@Override358public CompositeData toCompositeData(CompositeType ct) {359return getCompositeData();360}361}362363364