0N/A/*
2362N/A * Copyright (c) 2003, 2006, Oracle and/or its affiliates. All rights reserved.
0N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
0N/A *
0N/A * This code is free software; you can redistribute it and/or modify it
0N/A * under the terms of the GNU General Public License version 2 only, as
2362N/A * published by the Free Software Foundation. Oracle designates this
0N/A * particular file as subject to the "Classpath" exception as provided
2362N/A * by Oracle in the LICENSE file that accompanied this code.
0N/A *
0N/A * This code is distributed in the hope that it will be useful, but WITHOUT
0N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
0N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
0N/A * version 2 for more details (a copy is included in the LICENSE file that
0N/A * accompanied this code).
0N/A *
0N/A * You should have received a copy of the GNU General Public License version
0N/A * 2 along with this work; if not, write to the Free Software Foundation,
0N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
0N/A *
2362N/A * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
2362N/A * or visit www.oracle.com if you need additional information or have any
2362N/A * questions.
0N/A */
0N/A
0N/Apackage com.sun.management;
0N/A
0N/Aimport java.lang.management.MemoryUsage;
0N/Aimport javax.management.openmbean.CompositeData;
0N/Aimport javax.management.openmbean.CompositeDataView;
0N/Aimport javax.management.openmbean.CompositeType;
0N/Aimport java.util.Collection;
0N/Aimport java.util.Collections;
0N/Aimport java.util.HashMap;
0N/Aimport java.util.Map;
0N/Aimport java.util.List;
0N/Aimport sun.management.GcInfoCompositeData;
0N/Aimport sun.management.GcInfoBuilder;
0N/A
0N/A/**
0N/A * Garbage collection information. It contains the following
0N/A * information for one garbage collection as well as GC-specific
0N/A * attributes:
0N/A * <blockquote>
0N/A * <ul>
0N/A * <li>Start time</li>
0N/A * <li>End time</li>
0N/A * <li>Duration</li>
0N/A * <li>Memory usage before the collection starts</li>
0N/A * <li>Memory usage after the collection ends</li>
0N/A * </ul>
0N/A * </blockquote>
0N/A *
0N/A * <p>
0N/A * <tt>GcInfo</tt> is a {@link CompositeData CompositeData}
0N/A * The GC-specific attributes can be obtained via the CompositeData
0N/A * interface. This is a historical relic, and other classes should
0N/A * not copy this pattern. Use {@link CompositeDataView} instead.
0N/A *
0N/A * <h4>MXBean Mapping</h4>
0N/A * <tt>GcInfo</tt> is mapped to a {@link CompositeData CompositeData}
0N/A * with attributes as specified in the {@link #from from} method.
0N/A *
0N/A * @author Mandy Chung
0N/A * @since 1.5
0N/A */
0N/Apublic class GcInfo implements CompositeData, CompositeDataView {
0N/A private final long index;
0N/A private final long startTime;
0N/A private final long endTime;
0N/A private final Map<String, MemoryUsage> usageBeforeGc;
0N/A private final Map<String, MemoryUsage> usageAfterGc;
0N/A private final Object[] extAttributes;
0N/A private final CompositeData cdata;
0N/A private final GcInfoBuilder builder;
0N/A
0N/A private GcInfo(GcInfoBuilder builder,
0N/A long index, long startTime, long endTime,
0N/A MemoryUsage[] muBeforeGc,
0N/A MemoryUsage[] muAfterGc,
0N/A Object[] extAttributes) {
0N/A this.builder = builder;
0N/A this.index = index;
0N/A this.startTime = startTime;
0N/A this.endTime = endTime;
0N/A String[] poolNames = builder.getPoolNames();
0N/A this.usageBeforeGc = new HashMap<String, MemoryUsage>(poolNames.length);
0N/A this.usageAfterGc = new HashMap<String, MemoryUsage>(poolNames.length);
0N/A for (int i = 0; i < poolNames.length; i++) {
0N/A this.usageBeforeGc.put(poolNames[i], muBeforeGc[i]);
0N/A this.usageAfterGc.put(poolNames[i], muAfterGc[i]);
0N/A }
0N/A this.extAttributes = extAttributes;
0N/A this.cdata = new GcInfoCompositeData(this, builder, extAttributes);
0N/A }
0N/A
0N/A private GcInfo(CompositeData cd) {
0N/A GcInfoCompositeData.validateCompositeData(cd);
0N/A
0N/A this.index = GcInfoCompositeData.getId(cd);
0N/A this.startTime = GcInfoCompositeData.getStartTime(cd);
0N/A this.endTime = GcInfoCompositeData.getEndTime(cd);
0N/A this.usageBeforeGc = GcInfoCompositeData.getMemoryUsageBeforeGc(cd);
0N/A this.usageAfterGc = GcInfoCompositeData.getMemoryUsageAfterGc(cd);
0N/A this.extAttributes = null;
0N/A this.builder = null;
0N/A this.cdata = cd;
0N/A }
0N/A
0N/A /**
0N/A * Returns the identifier of this garbage collection which is
0N/A * the number of collections that this collector has done.
0N/A *
0N/A * @return the identifier of this garbage collection which is
0N/A * the number of collections that this collector has done.
0N/A */
0N/A public long getId() {
0N/A return index;
0N/A }
0N/A
0N/A /**
0N/A * Returns the start time of this GC in milliseconds
0N/A * since the Java virtual machine was started.
0N/A *
0N/A * @return the start time of this GC.
0N/A */
0N/A public long getStartTime() {
0N/A return startTime;
0N/A }
0N/A
0N/A /**
0N/A * Returns the end time of this GC in milliseconds
0N/A * since the Java virtual machine was started.
0N/A *
0N/A * @return the end time of this GC.
0N/A */
0N/A public long getEndTime() {
0N/A return endTime;
0N/A }
0N/A
0N/A /**
0N/A * Returns the elapsed time of this GC in milliseconds.
0N/A *
0N/A * @return the elapsed time of this GC in milliseconds.
0N/A */
0N/A public long getDuration() {
0N/A return endTime - startTime;
0N/A }
0N/A
0N/A /**
0N/A * Returns the memory usage of all memory pools
0N/A * at the beginning of this GC.
0N/A * This method returns
0N/A * a <tt>Map</tt> of the name of a memory pool
0N/A * to the memory usage of the corresponding
0N/A * memory pool before GC starts.
0N/A *
0N/A * @return a <tt>Map</tt> of memory pool names to the memory
0N/A * usage of a memory pool before GC starts.
0N/A */
0N/A public Map<String, MemoryUsage> getMemoryUsageBeforeGc() {
0N/A return Collections.unmodifiableMap(usageBeforeGc);
0N/A }
0N/A
0N/A /**
0N/A * Returns the memory usage of all memory pools
0N/A * at the end of this GC.
0N/A * This method returns
0N/A * a <tt>Map</tt> of the name of a memory pool
0N/A * to the memory usage of the corresponding
0N/A * memory pool when GC finishes.
0N/A *
0N/A * @return a <tt>Map</tt> of memory pool names to the memory
0N/A * usage of a memory pool when GC finishes.
0N/A */
0N/A public Map<String, MemoryUsage> getMemoryUsageAfterGc() {
0N/A return Collections.unmodifiableMap(usageAfterGc);
0N/A }
0N/A
0N/A /**
0N/A * Returns a <tt>GcInfo</tt> object represented by the
0N/A * given <tt>CompositeData</tt>. The given
0N/A * <tt>CompositeData</tt> must contain
0N/A * all the following attributes:
0N/A *
0N/A * <p>
0N/A * <blockquote>
0N/A * <table border>
0N/A * <tr>
0N/A * <th align=left>Attribute Name</th>
0N/A * <th align=left>Type</th>
0N/A * </tr>
0N/A * <tr>
0N/A * <td>index</td>
0N/A * <td><tt>java.lang.Long</tt></td>
0N/A * </tr>
0N/A * <tr>
0N/A * <td>startTime</td>
0N/A * <td><tt>java.lang.Long</tt></td>
0N/A * </tr>
0N/A * <tr>
0N/A * <td>endTime</td>
0N/A * <td><tt>java.lang.Long</tt></td>
0N/A * </tr>
0N/A * <tr>
0N/A * <td>memoryUsageBeforeGc</td>
0N/A * <td><tt>javax.management.openmbean.TabularData</tt></td>
0N/A * </tr>
0N/A * <tr>
0N/A * <td>memoryUsageAfterGc</td>
0N/A * <td><tt>javax.management.openmbean.TabularData</tt></td>
0N/A * </tr>
0N/A * </table>
0N/A * </blockquote>
0N/A *
0N/A * @throws IllegalArgumentException if <tt>cd</tt> does not
0N/A * represent a <tt>GcInfo</tt> object with the attributes
0N/A * described above.
0N/A *
0N/A * @return a <tt>GcInfo</tt> object represented by <tt>cd</tt>
0N/A * if <tt>cd</tt> is not <tt>null</tt>; <tt>null</tt> otherwise.
0N/A */
0N/A public static GcInfo from(CompositeData cd) {
0N/A if (cd == null) {
0N/A return null;
0N/A }
0N/A
0N/A if (cd instanceof GcInfoCompositeData) {
0N/A return ((GcInfoCompositeData) cd).getGcInfo();
0N/A } else {
0N/A return new GcInfo(cd);
0N/A }
0N/A
0N/A }
0N/A
0N/A // Implementation of the CompositeData interface
0N/A public boolean containsKey(String key) {
0N/A return cdata.containsKey(key);
0N/A }
0N/A
0N/A public boolean containsValue(Object value) {
0N/A return cdata.containsValue(value);
0N/A }
0N/A
0N/A public boolean equals(Object obj) {
0N/A return cdata.equals(obj);
0N/A }
0N/A
0N/A public Object get(String key) {
0N/A return cdata.get(key);
0N/A }
0N/A
0N/A public Object[] getAll(String[] keys) {
0N/A return cdata.getAll(keys);
0N/A }
0N/A
0N/A public CompositeType getCompositeType() {
0N/A return cdata.getCompositeType();
0N/A }
0N/A
0N/A public int hashCode() {
0N/A return cdata.hashCode();
0N/A }
0N/A
0N/A public String toString() {
0N/A return cdata.toString();
0N/A }
0N/A
0N/A public Collection values() {
0N/A return cdata.values();
0N/A }
0N/A
0N/A /**
0N/A * <p>Return the {@code CompositeData} representation of this
0N/A * {@code GcInfo}, including any GC-specific attributes. The
0N/A * returned value will have at least all the attributes described
0N/A * in the {@link #from(CompositeData) from} method, plus optionally
0N/A * other attributes.
0N/A *
0N/A * @param ct the {@code CompositeType} that the caller expects.
0N/A * This parameter is ignored and can be null.
0N/A *
0N/A * @return the {@code CompositeData} representation.
0N/A */
0N/A public CompositeData toCompositeData(CompositeType ct) {
0N/A return cdata;
0N/A }
0N/A}