/*
* @(#)GcInfo.java 1.9 06/03/15
*
* Copyright 2006 Sun Microsystems, Inc. All rights reserved.
* SUN PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
*/
package com.sun.management;
import java.lang.management.MemoryUsage;
import javax.management.openmbean.CompositeData;
import javax.management.openmbean.CompositeDataView;
import javax.management.openmbean.CompositeType;
import java.util.Collection;
import java.util.Collections;
import java.util.HashMap;
import java.util.Map;
import java.util.List;
import sun.management.GcInfoCompositeData;
import sun.management.GcInfoBuilder;
/**
* Garbage collection information. It contains the following
* information for one garbage collection as well as GC-specific
* attributes:
*
*
* - Start time
* - End time
* - Duration
* - Memory usage before the collection starts
* - Memory usage after the collection ends
*
*
*
*
* GcInfo is a {@link CompositeData CompositeData}
* The GC-specific attributes can be obtained via the CompositeData
* interface. This is a historical relic, and other classes should
* not copy this pattern. Use {@link CompositeDataView} instead.
*
*
MXBean Mapping
* GcInfo is mapped to a {@link CompositeData CompositeData}
* with attributes as specified in the {@link #from from} method.
*
* @author Mandy Chung
* @version 1.9, 03/15/06
* @since 1.5
*/
public class GcInfo implements CompositeData, CompositeDataView {
private final long index;
private final long startTime;
private final long endTime;
private final Map usageBeforeGc;
private final Map usageAfterGc;
private final Object[] extAttributes;
private final CompositeData cdata;
private final GcInfoBuilder builder;
private GcInfo(GcInfoBuilder builder,
long index, long startTime, long endTime,
MemoryUsage[] muBeforeGc,
MemoryUsage[] muAfterGc,
Object[] extAttributes) {
this.builder = builder;
this.index = index;
this.startTime = startTime;
this.endTime = endTime;
String[] poolNames = builder.getPoolNames();
this.usageBeforeGc = new HashMap(poolNames.length);
this.usageAfterGc = new HashMap(poolNames.length);
for (int i = 0; i < poolNames.length; i++) {
this.usageBeforeGc.put(poolNames[i], muBeforeGc[i]);
this.usageAfterGc.put(poolNames[i], muAfterGc[i]);
}
this.extAttributes = extAttributes;
this.cdata = new GcInfoCompositeData(this, builder, extAttributes);
}
private GcInfo(CompositeData cd) {
//OJVM GcInfoCompositeData.validateCompositeData(cd);
this.index = GcInfoCompositeData.getId(cd);
System.out.println("-- index: " + index);
this.startTime = GcInfoCompositeData.getStartTime(cd);
System.out.println("-- startTime: " + startTime);
this.endTime = GcInfoCompositeData.getEndTime(cd);
System.out.println("-- endTime: " + endTime);
this.usageBeforeGc = GcInfoCompositeData.getMemoryUsageBeforeGc(cd);
System.out.println("-- usageBeforeGc: " + usageBeforeGc);
this.usageAfterGc = GcInfoCompositeData.getMemoryUsageAfterGc(cd);
System.out.println("-- usageAfterGc: " + usageAfterGc);
this.extAttributes = null;
this.builder = null;
this.cdata = cd;
}
/**
* Returns the identifier of this garbage collection which is
* the number of collections that this collector has done.
*
* @return the identifier of this garbage collection which is
* the number of collections that this collector has done.
*/
public long getId() {
return index;
}
/**
* Returns the start time of this GC in milliseconds
* since the Java virtual machine was started.
*
* @return the start time of this GC.
*/
public long getStartTime() {
return startTime;
}
/**
* Returns the end time of this GC in milliseconds
* since the Java virtual machine was started.
*
* @return the end time of this GC.
*/
public long getEndTime() {
return endTime;
}
/**
* Returns the elapsed time of this GC in milliseconds.
*
* @return the elapsed time of this GC in milliseconds.
*/
public long getDuration() {
return endTime - startTime;
}
/**
* Returns the memory usage of all memory pools
* at the beginning of this GC.
* This method returns
* a Map of the name of a memory pool
* to the memory usage of the corresponding
* memory pool before GC starts.
*
* @return a Map of memory pool names to the memory
* usage of a memory pool before GC starts.
*/
public Map getMemoryUsageBeforeGc() {
return Collections.unmodifiableMap(usageBeforeGc);
}
/**
* Returns the memory usage of all memory pools
* at the end of this GC.
* This method returns
* a Map of the name of a memory pool
* to the memory usage of the corresponding
* memory pool when GC finishes.
*
* @return a Map of memory pool names to the memory
* usage of a memory pool when GC finishes.
*/
public Map getMemoryUsageAfterGc() {
return Collections.unmodifiableMap(usageAfterGc);
}
/**
* Returns a GcInfo object represented by the
* given CompositeData. The given
* CompositeData must contain
* all the following attributes:
*
*
*
*
*
* Attribute Name |
* Type |
*
*
* index |
* java.lang.Long |
*
*
* startTime |
* java.lang.Long |
*
*
* endTime |
* java.lang.Long |
*
*
* memoryUsageBeforeGc |
* javax.management.openmbean.TabularData |
*
*
* memoryUsageAfterGc |
* javax.management.openmbean.TabularData |
*
*
*
*
* @throws IllegalArgumentException if cd does not
* represent a GcInfo object with the attributes
* described above.
*
* @return a GcInfo object represented by cd
* if cd is not null; null otherwise.
*/
public static GcInfo from(CompositeData cd) {
if (cd == null) {
return null;
}
if (cd instanceof GcInfoCompositeData) {
return ((GcInfoCompositeData) cd).getGcInfo();
} else {
return new GcInfo(cd);
}
}
// Implementation of the CompositeData interface
public boolean containsKey(String key) {
return cdata.containsKey(key);
}
public boolean containsValue(Object value) {
return cdata.containsValue(value);
}
public boolean equals(Object obj) {
return cdata.equals(obj);
}
public Object get(String key) {
return cdata.get(key);
}
public Object[] getAll(String[] keys) {
return cdata.getAll(keys);
}
public CompositeType getCompositeType() {
return cdata.getCompositeType();
}
public int hashCode() {
return cdata.hashCode();
}
public String toString() {
return cdata.toString();
}
public Collection values() {
return cdata.values();
}
/**
* Return the {@code CompositeData} representation of this
* {@code GcInfo}, including any GC-specific attributes. The
* returned value will have at least all the attributes described
* in the {@link #from(CompositeData) from} method, plus optionally
* other attributes.
*
* @param ct the {@code CompositeType} that the caller expects.
* This parameter is ignored and can be null.
*
* @return the {@code CompositeData} representation.
*/
public CompositeData toCompositeData(CompositeType ct) {
return cdata;
}
}