GcInfoBuilder.java revision 2362
0N/A/*
0N/A * Copyright (c) 2003, 2008, 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
0N/A * published by the Free Software Foundation. Oracle designates this
0N/A * particular file as subject to the "Classpath" exception as provided
0N/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 *
0N/A * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
0N/A * or visit www.oracle.com if you need additional information or have any
0N/A * 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) {
0N/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':
0N/A allItemTypes[i] = SimpleType.BOOLEAN;
0N/A break;
0N/A case 'B':
0N/A allItemTypes[i] = SimpleType.BYTE;
0N/A break;
0N/A case 'C':
0N/A allItemTypes[i] = SimpleType.CHARACTER;
0N/A break;
0N/A case 'S':
0N/A allItemTypes[i] = SimpleType.SHORT;
0N/A break;
0N/A case 'I':
0N/A allItemTypes[i] = SimpleType.INTEGER;
0N/A break;
0N/A case 'J':
0N/A allItemTypes[i] = SimpleType.LONG;
0N/A break;
0N/A case 'F':
0N/A allItemTypes[i] = SimpleType.FLOAT;
0N/A break;
0N/A case 'D':
0N/A allItemTypes[i] = SimpleType.DOUBLE;
0N/A break;
0N/A default:
0N/A throw new AssertionError(
0N/A "Unsupported type [" + gcExtItemTypes[i] + "]");
0N/A }
0N/A }
0N/A }
0N/A
0N/A CompositeType gict = null;
0N/A try {
0N/A final String typeName =
0N/A "sun.management." + gc.getName() + ".GcInfoCompositeType";
0N/A
0N/A gict = new CompositeType(typeName,
0N/A "CompositeType for GC info for " +
0N/A gc.getName(),
0N/A allItemNames,
0N/A allItemDescs,
0N/A allItemTypes);
0N/A } catch (OpenDataException e) {
0N/A // shouldn't reach here
0N/A throw Util.newException(e);
0N/A }
0N/A gcInfoCompositeType = gict;
0N/A
0N/A return gcInfoCompositeType;
0N/A }
0N/A
0N/A synchronized String[] getItemNames() {
0N/A if (allItemNames == null) {
0N/A // initialize when forming the composite type
0N/A getGcInfoCompositeType();
0N/A }
0N/A return allItemNames;
0N/A }
0N/A
0N/A // Retrieve information about extension attributes
0N/A private native int getNumGcExtAttributes(GarbageCollectorMXBean gc);
0N/A private native void fillGcAttributeInfo(GarbageCollectorMXBean gc,
0N/A int numAttributes,
0N/A String[] attributeNames,
0N/A char[] types,
0N/A String[] descriptions);
0N/A
0N/A /**
0N/A * Returns the last GcInfo
0N/A *
0N/A * @param gc GarbageCollectorMXBean that the gc info is associated with.
0N/A * @param numExtAtts number of extension attributes
0N/A * @param extAttValues Values of extension attributes to be filled.
0N/A * @param before Memory usage before GC to be filled.
0N/A * @param after Memory usage after GC to be filled.
0N/A */
0N/A private native GcInfo getLastGcInfo0(GarbageCollectorMXBean gc,
0N/A int numExtAtts,
0N/A Object[] extAttValues,
0N/A char[] extAttTypes,
0N/A MemoryUsage[] before,
0N/A MemoryUsage[] after);
0N/A}
0N/A