Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/src/share/classes/com/sun/management/GcInfo.java
38831 views
/*1* Copyright (c) 2003, 2013, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation. Oracle designates this7* particular file as subject to the "Classpath" exception as provided8* by Oracle in the LICENSE file that accompanied this code.9*10* This code is distributed in the hope that it will be useful, but WITHOUT11* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or12* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License13* version 2 for more details (a copy is included in the LICENSE file that14* accompanied this code).15*16* You should have received a copy of the GNU General Public License version17* 2 along with this work; if not, write to the Free Software Foundation,18* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.19*20* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA21* or visit www.oracle.com if you need additional information or have any22* questions.23*/2425package com.sun.management;2627import java.lang.management.MemoryUsage;28import javax.management.openmbean.CompositeData;29import javax.management.openmbean.CompositeDataView;30import javax.management.openmbean.CompositeType;31import java.util.Collection;32import java.util.Collections;33import java.util.HashMap;34import java.util.Map;35import java.util.List;36import sun.management.GcInfoCompositeData;37import sun.management.GcInfoBuilder;3839/**40* Garbage collection information. It contains the following41* information for one garbage collection as well as GC-specific42* attributes:43* <blockquote>44* <ul>45* <li>Start time</li>46* <li>End time</li>47* <li>Duration</li>48* <li>Memory usage before the collection starts</li>49* <li>Memory usage after the collection ends</li>50* </ul>51* </blockquote>52*53* <p>54* <tt>GcInfo</tt> is a {@link CompositeData CompositeData}55* The GC-specific attributes can be obtained via the CompositeData56* interface. This is a historical relic, and other classes should57* not copy this pattern. Use {@link CompositeDataView} instead.58*59* <h4>MXBean Mapping</h4>60* <tt>GcInfo</tt> is mapped to a {@link CompositeData CompositeData}61* with attributes as specified in the {@link #from from} method.62*63* @author Mandy Chung64* @since 1.565*/66@jdk.Exported67public class GcInfo implements CompositeData, CompositeDataView {68private final long index;69private final long startTime;70private final long endTime;71private final Map<String, MemoryUsage> usageBeforeGc;72private final Map<String, MemoryUsage> usageAfterGc;73private final Object[] extAttributes;74private final CompositeData cdata;75private final GcInfoBuilder builder;7677private GcInfo(GcInfoBuilder builder,78long index, long startTime, long endTime,79MemoryUsage[] muBeforeGc,80MemoryUsage[] muAfterGc,81Object[] extAttributes) {82this.builder = builder;83this.index = index;84this.startTime = startTime;85this.endTime = endTime;86String[] poolNames = builder.getPoolNames();87this.usageBeforeGc = new HashMap<String, MemoryUsage>(poolNames.length);88this.usageAfterGc = new HashMap<String, MemoryUsage>(poolNames.length);89for (int i = 0; i < poolNames.length; i++) {90this.usageBeforeGc.put(poolNames[i], muBeforeGc[i]);91this.usageAfterGc.put(poolNames[i], muAfterGc[i]);92}93this.extAttributes = extAttributes;94this.cdata = new GcInfoCompositeData(this, builder, extAttributes);95}9697private GcInfo(CompositeData cd) {98GcInfoCompositeData.validateCompositeData(cd);99100this.index = GcInfoCompositeData.getId(cd);101this.startTime = GcInfoCompositeData.getStartTime(cd);102this.endTime = GcInfoCompositeData.getEndTime(cd);103this.usageBeforeGc = GcInfoCompositeData.getMemoryUsageBeforeGc(cd);104this.usageAfterGc = GcInfoCompositeData.getMemoryUsageAfterGc(cd);105this.extAttributes = null;106this.builder = null;107this.cdata = cd;108}109110/**111* Returns the identifier of this garbage collection which is112* the number of collections that this collector has done.113*114* @return the identifier of this garbage collection which is115* the number of collections that this collector has done.116*/117public long getId() {118return index;119}120121/**122* Returns the start time of this GC in milliseconds123* since the Java virtual machine was started.124*125* @return the start time of this GC.126*/127public long getStartTime() {128return startTime;129}130131/**132* Returns the end time of this GC in milliseconds133* since the Java virtual machine was started.134*135* @return the end time of this GC.136*/137public long getEndTime() {138return endTime;139}140141/**142* Returns the elapsed time of this GC in milliseconds.143*144* @return the elapsed time of this GC in milliseconds.145*/146public long getDuration() {147return endTime - startTime;148}149150/**151* Returns the memory usage of all memory pools152* at the beginning of this GC.153* This method returns154* a <tt>Map</tt> of the name of a memory pool155* to the memory usage of the corresponding156* memory pool before GC starts.157*158* @return a <tt>Map</tt> of memory pool names to the memory159* usage of a memory pool before GC starts.160*/161public Map<String, MemoryUsage> getMemoryUsageBeforeGc() {162return Collections.unmodifiableMap(usageBeforeGc);163}164165/**166* Returns the memory usage of all memory pools167* at the end of this GC.168* This method returns169* a <tt>Map</tt> of the name of a memory pool170* to the memory usage of the corresponding171* memory pool when GC finishes.172*173* @return a <tt>Map</tt> of memory pool names to the memory174* usage of a memory pool when GC finishes.175*/176public Map<String, MemoryUsage> getMemoryUsageAfterGc() {177return Collections.unmodifiableMap(usageAfterGc);178}179180/**181* Returns a <tt>GcInfo</tt> object represented by the182* given <tt>CompositeData</tt>. The given183* <tt>CompositeData</tt> must contain184* all the following attributes:185*186* <p>187* <blockquote>188* <table border>189* <tr>190* <th align=left>Attribute Name</th>191* <th align=left>Type</th>192* </tr>193* <tr>194* <td>index</td>195* <td><tt>java.lang.Long</tt></td>196* </tr>197* <tr>198* <td>startTime</td>199* <td><tt>java.lang.Long</tt></td>200* </tr>201* <tr>202* <td>endTime</td>203* <td><tt>java.lang.Long</tt></td>204* </tr>205* <tr>206* <td>memoryUsageBeforeGc</td>207* <td><tt>javax.management.openmbean.TabularData</tt></td>208* </tr>209* <tr>210* <td>memoryUsageAfterGc</td>211* <td><tt>javax.management.openmbean.TabularData</tt></td>212* </tr>213* </table>214* </blockquote>215*216* @throws IllegalArgumentException if <tt>cd</tt> does not217* represent a <tt>GcInfo</tt> object with the attributes218* described above.219*220* @return a <tt>GcInfo</tt> object represented by <tt>cd</tt>221* if <tt>cd</tt> is not <tt>null</tt>; <tt>null</tt> otherwise.222*/223public static GcInfo from(CompositeData cd) {224if (cd == null) {225return null;226}227228if (cd instanceof GcInfoCompositeData) {229return ((GcInfoCompositeData) cd).getGcInfo();230} else {231return new GcInfo(cd);232}233234}235236// Implementation of the CompositeData interface237public boolean containsKey(String key) {238return cdata.containsKey(key);239}240241public boolean containsValue(Object value) {242return cdata.containsValue(value);243}244245public boolean equals(Object obj) {246return cdata.equals(obj);247}248249public Object get(String key) {250return cdata.get(key);251}252253public Object[] getAll(String[] keys) {254return cdata.getAll(keys);255}256257public CompositeType getCompositeType() {258return cdata.getCompositeType();259}260261public int hashCode() {262return cdata.hashCode();263}264265public String toString() {266return cdata.toString();267}268269public Collection values() {270return cdata.values();271}272273/**274* <p>Return the {@code CompositeData} representation of this275* {@code GcInfo}, including any GC-specific attributes. The276* returned value will have at least all the attributes described277* in the {@link #from(CompositeData) from} method, plus optionally278* other attributes.279*280* @param ct the {@code CompositeType} that the caller expects.281* This parameter is ignored and can be null.282*283* @return the {@code CompositeData} representation.284*/285public CompositeData toCompositeData(CompositeType ct) {286return cdata;287}288}289290291