GcInfoBuilder.java revision 178
0N/A/*
3909N/A * Copyright 2003-2004 Sun Microsystems, Inc. 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. Sun designates this
0N/A * particular file as subject to the "Classpath" exception as provided
2362N/A * by Sun 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 Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
2362N/A * CA 95054 USA or visit www.sun.com if you need additional information or
2362N/A * have any questions.
0N/A */
0N/Apackage sun.management;
0N/A
0N/Aimport java.lang.management.GarbageCollectorMXBean;
0N/Aimport java.lang.management.MemoryUsage;
0N/Aimport javax.management.openmbean.OpenType;
0N/Aimport javax.management.openmbean.SimpleType;
0N/Aimport javax.management.openmbean.TabularType;
0N/Aimport javax.management.openmbean.TabularData;
0N/Aimport javax.management.openmbean.TabularDataSupport;
0N/Aimport javax.management.openmbean.CompositeType;
0N/Aimport javax.management.openmbean.CompositeData;
0N/Aimport javax.management.openmbean.CompositeDataSupport;
0N/Aimport javax.management.openmbean.OpenDataException;
0N/Aimport com.sun.management.GcInfo;
0N/A
0N/A/**
0N/A * Helper class to build composite data.
0N/A */
0N/Apublic class GcInfoBuilder {
0N/A private final GarbageCollectorMXBean gc;
0N/A private final String[] poolNames;
0N/A private String[] allItemNames;
0N/A
0N/A // GC-specific composite type:
0N/A // Each GarbageCollectorMXBean may have different GC-specific attributes
0N/A // the CompositeType for the GcInfo could be different.
0N/A private CompositeType gcInfoCompositeType;
0N/A
0N/A // GC-specific items
0N/A private final int gcExtItemCount;
0N/A private final String[] gcExtItemNames;
0N/A private final String[] gcExtItemDescs;
0N/A private final char[] gcExtItemTypes;
0N/A
0N/A GcInfoBuilder(GarbageCollectorMXBean gc, String[] poolNames) {
0N/A this.gc = gc;
0N/A this.poolNames = poolNames;
0N/A this.gcExtItemCount = getNumGcExtAttributes(gc);
0N/A this.gcExtItemNames = new String[gcExtItemCount];
0N/A this.gcExtItemDescs = new String[gcExtItemCount];
0N/A this.gcExtItemTypes = new char[gcExtItemCount];
0N/A
0N/A // Fill the information about extension attributes
0N/A fillGcAttributeInfo(gc, gcExtItemCount, gcExtItemNames,
0N/A gcExtItemTypes, gcExtItemDescs);
0N/A
0N/A // lazily build the CompositeType for the GcInfo
0N/A // including the GC-specific extension attributes
0N/A this.gcInfoCompositeType = null;
0N/A }
0N/A
0N/A GcInfo getLastGcInfo() {
0N/A MemoryUsage[] usageBeforeGC = new MemoryUsage[poolNames.length];
0N/A MemoryUsage[] usageAfterGC = new MemoryUsage[poolNames.length];
0N/A Object[] values = new Object[gcExtItemCount];
0N/A
0N/A return getLastGcInfo0(gc, gcExtItemCount, values, gcExtItemTypes,
0N/A usageBeforeGC, usageAfterGC);
0N/A }
0N/A
0N/A public String[] getPoolNames() {
0N/A return poolNames;
0N/A }
0N/A
0N/A int getGcExtItemCount() {
0N/A return gcExtItemCount;
0N/A }
0N/A
0N/A // Returns the CompositeType for the GcInfo including
0N/A // the extension attributes
0N/A synchronized CompositeType getGcInfoCompositeType() {
0N/A if (gcInfoCompositeType != null)
0N/A return gcInfoCompositeType;
0N/A
0N/A // First, fill with the attributes in the GcInfo
0N/A String[] gcInfoItemNames = GcInfoCompositeData.getBaseGcInfoItemNames();
0N/A OpenType[] gcInfoItemTypes = GcInfoCompositeData.getBaseGcInfoItemTypes();
0N/A int numGcInfoItems = gcInfoItemNames.length;
0N/A
0N/A int itemCount = numGcInfoItems + gcExtItemCount;
0N/A allItemNames = new String[itemCount];
0N/A String[] allItemDescs = new String[itemCount];
0N/A OpenType[] allItemTypes = new OpenType[itemCount];
0N/A
0N/A System.arraycopy(gcInfoItemNames, 0, allItemNames, 0, numGcInfoItems);
0N/A System.arraycopy(gcInfoItemNames, 0, allItemDescs, 0, numGcInfoItems);
0N/A System.arraycopy(gcInfoItemTypes, 0, allItemTypes, 0, numGcInfoItems);
0N/A
0N/A // Then fill with the extension GC-specific attributes, if any.
0N/A if (gcExtItemCount > 0) {
459N/A fillGcAttributeInfo(gc, gcExtItemCount, gcExtItemNames,
0N/A gcExtItemTypes, gcExtItemDescs);
0N/A System.arraycopy(gcExtItemNames, 0, allItemNames,
0N/A numGcInfoItems, gcExtItemCount);
0N/A System.arraycopy(gcExtItemDescs, 0, allItemDescs,
0N/A numGcInfoItems, gcExtItemCount);
0N/A for (int i = numGcInfoItems, j = 0; j < gcExtItemCount; i++, j++) {
0N/A switch (gcExtItemTypes[j]) {
0N/A case 'Z':
allItemTypes[i] = SimpleType.BOOLEAN;
break;
case 'B':
allItemTypes[i] = SimpleType.BYTE;
break;
case 'C':
allItemTypes[i] = SimpleType.CHARACTER;
break;
case 'S':
allItemTypes[i] = SimpleType.SHORT;
break;
case 'I':
allItemTypes[i] = SimpleType.INTEGER;
break;
case 'J':
allItemTypes[i] = SimpleType.LONG;
break;
case 'F':
allItemTypes[i] = SimpleType.FLOAT;
break;
case 'D':
allItemTypes[i] = SimpleType.DOUBLE;
break;
default:
throw new AssertionError(
"Unsupported type [" + gcExtItemTypes[i] + "]");
}
}
}
CompositeType gict = null;
try {
final String typeName =
"sun.management." + gc.getName() + ".GcInfoCompositeType";
gict = new CompositeType(typeName,
"CompositeType for GC info for " +
gc.getName(),
allItemNames,
allItemDescs,
allItemTypes);
} catch (OpenDataException e) {
// shouldn't reach here
throw Util.newException(e);
}
gcInfoCompositeType = gict;
return gcInfoCompositeType;
}
synchronized String[] getItemNames() {
if (allItemNames == null) {
// initialize when forming the composite type
getGcInfoCompositeType();
}
return allItemNames;
}
// Retrieve information about extension attributes
private native int getNumGcExtAttributes(GarbageCollectorMXBean gc);
private native void fillGcAttributeInfo(GarbageCollectorMXBean gc,
int numAttributes,
String[] attributeNames,
char[] types,
String[] descriptions);
/**
* Returns the last GcInfo
*
* @param gc GarbageCollectorMXBean that the gc info is associated with.
* @param numExtAtts number of extension attributes
* @param extAttValues Values of extension attributes to be filled.
* @param before Memory usage before GC to be filled.
* @param after Memory usage after GC to be filled.
*/
private native GcInfo getLastGcInfo0(GarbageCollectorMXBean gc,
int numExtAtts,
Object[] extAttValues,
char[] extAttTypes,
MemoryUsage[] before,
MemoryUsage[] after);
}