001: /*
002: * Copyright 2003-2004 Sun Microsystems, Inc. All Rights Reserved.
003: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
004: *
005: * This code is free software; you can redistribute it and/or modify it
006: * under the terms of the GNU General Public License version 2 only, as
007: * published by the Free Software Foundation. Sun designates this
008: * particular file as subject to the "Classpath" exception as provided
009: * by Sun in the LICENSE file that accompanied this code.
010: *
011: * This code is distributed in the hope that it will be useful, but WITHOUT
012: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
013: * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
014: * version 2 for more details (a copy is included in the LICENSE file that
015: * accompanied this code).
016: *
017: * You should have received a copy of the GNU General Public License version
018: * 2 along with this work; if not, write to the Free Software Foundation,
019: * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
020: *
021: * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
022: * CA 95054 USA or visit www.sun.com if you need additional information or
023: * have any questions.
024: */
025: package sun.management;
026:
027: import java.lang.management.GarbageCollectorMXBean;
028: import java.lang.management.MemoryUsage;
029: import javax.management.openmbean.OpenType;
030: import javax.management.openmbean.SimpleType;
031: import javax.management.openmbean.TabularType;
032: import javax.management.openmbean.TabularData;
033: import javax.management.openmbean.TabularDataSupport;
034: import javax.management.openmbean.CompositeType;
035: import javax.management.openmbean.CompositeData;
036: import javax.management.openmbean.CompositeDataSupport;
037: import javax.management.openmbean.OpenDataException;
038: import com.sun.management.GcInfo;
039:
040: /**
041: * Helper class to build composite data.
042: */
043: public class GcInfoBuilder {
044: private final GarbageCollectorMXBean gc;
045: private final String[] poolNames;
046: private String[] allItemNames;
047:
048: // GC-specific composite type:
049: // Each GarbageCollectorMXBean may have different GC-specific attributes
050: // the CompositeType for the GcInfo could be different.
051: private CompositeType gcInfoCompositeType;
052:
053: // GC-specific items
054: private final int gcExtItemCount;
055: private final String[] gcExtItemNames;
056: private final String[] gcExtItemDescs;
057: private final char[] gcExtItemTypes;
058:
059: GcInfoBuilder(GarbageCollectorMXBean gc, String[] poolNames) {
060: this .gc = gc;
061: this .poolNames = poolNames;
062: this .gcExtItemCount = getNumGcExtAttributes(gc);
063: this .gcExtItemNames = new String[gcExtItemCount];
064: this .gcExtItemDescs = new String[gcExtItemCount];
065: this .gcExtItemTypes = new char[gcExtItemCount];
066:
067: // Fill the information about extension attributes
068: fillGcAttributeInfo(gc, gcExtItemCount, gcExtItemNames,
069: gcExtItemTypes, gcExtItemDescs);
070:
071: // lazily build the CompositeType for the GcInfo
072: // including the GC-specific extension attributes
073: this .gcInfoCompositeType = null;
074: }
075:
076: GcInfo getLastGcInfo() {
077: MemoryUsage[] usageBeforeGC = new MemoryUsage[poolNames.length];
078: MemoryUsage[] usageAfterGC = new MemoryUsage[poolNames.length];
079: Object[] values = new Object[gcExtItemCount];
080:
081: return getLastGcInfo0(gc, gcExtItemCount, values,
082: gcExtItemTypes, usageBeforeGC, usageAfterGC);
083: }
084:
085: public String[] getPoolNames() {
086: return poolNames;
087: }
088:
089: int getGcExtItemCount() {
090: return gcExtItemCount;
091: }
092:
093: // Returns the CompositeType for the GcInfo including
094: // the extension attributes
095: synchronized CompositeType getGcInfoCompositeType() {
096: if (gcInfoCompositeType != null)
097: return gcInfoCompositeType;
098:
099: // First, fill with the attributes in the GcInfo
100: String[] gcInfoItemNames = GcInfoCompositeData
101: .getBaseGcInfoItemNames();
102: OpenType[] gcInfoItemTypes = GcInfoCompositeData
103: .getBaseGcInfoItemTypes();
104: int numGcInfoItems = gcInfoItemNames.length;
105:
106: int itemCount = numGcInfoItems + gcExtItemCount;
107: allItemNames = new String[itemCount];
108: String[] allItemDescs = new String[itemCount];
109: OpenType[] allItemTypes = new OpenType[itemCount];
110:
111: System.arraycopy(gcInfoItemNames, 0, allItemNames, 0,
112: numGcInfoItems);
113: System.arraycopy(gcInfoItemNames, 0, allItemDescs, 0,
114: numGcInfoItems);
115: System.arraycopy(gcInfoItemTypes, 0, allItemTypes, 0,
116: numGcInfoItems);
117:
118: // Then fill with the extension GC-specific attributes, if any.
119: if (gcExtItemCount > 0) {
120: fillGcAttributeInfo(gc, gcExtItemCount, gcExtItemNames,
121: gcExtItemTypes, gcExtItemDescs);
122: System.arraycopy(gcExtItemNames, 0, allItemNames,
123: numGcInfoItems, gcExtItemCount);
124: System.arraycopy(gcExtItemDescs, 0, allItemDescs,
125: numGcInfoItems, gcExtItemCount);
126: for (int i = numGcInfoItems, j = 0; j < gcExtItemCount; i++, j++) {
127: switch (gcExtItemTypes[j]) {
128: case 'Z':
129: allItemTypes[i] = SimpleType.BOOLEAN;
130: break;
131: case 'B':
132: allItemTypes[i] = SimpleType.BYTE;
133: break;
134: case 'C':
135: allItemTypes[i] = SimpleType.CHARACTER;
136: break;
137: case 'S':
138: allItemTypes[i] = SimpleType.SHORT;
139: break;
140: case 'I':
141: allItemTypes[i] = SimpleType.INTEGER;
142: break;
143: case 'J':
144: allItemTypes[i] = SimpleType.LONG;
145: break;
146: case 'F':
147: allItemTypes[i] = SimpleType.FLOAT;
148: break;
149: case 'D':
150: allItemTypes[i] = SimpleType.DOUBLE;
151: break;
152: default:
153: throw new InternalError("Unsupported type ["
154: + gcExtItemTypes[i] + "]");
155: }
156: }
157: }
158:
159: CompositeType gict = null;
160: try {
161: final String typeName = "sun.management." + gc.getName()
162: + ".GcInfoCompositeType";
163:
164: gict = new CompositeType(typeName,
165: "CompositeType for GC info for " + gc.getName(),
166: allItemNames, allItemDescs, allItemTypes);
167: } catch (OpenDataException e) {
168: // shouldn't reach here
169: throw Util.newException(e);
170: }
171: gcInfoCompositeType = gict;
172:
173: return gcInfoCompositeType;
174: }
175:
176: synchronized String[] getItemNames() {
177: if (allItemNames == null) {
178: // initialize when forming the composite type
179: getGcInfoCompositeType();
180: }
181: return allItemNames;
182: }
183:
184: // Retrieve information about extension attributes
185: private native int getNumGcExtAttributes(GarbageCollectorMXBean gc);
186:
187: private native void fillGcAttributeInfo(GarbageCollectorMXBean gc,
188: int numAttributes, String[] attributeNames, char[] types,
189: String[] descriptions);
190:
191: /**
192: * Returns the last GcInfo
193: *
194: * @param gc GarbageCollectorMXBean that the gc info is associated with.
195: * @param numExtAtts number of extension attributes
196: * @param extAttValues Values of extension attributes to be filled.
197: * @param before Memory usage before GC to be filled.
198: * @param after Memory usage after GC to be filled.
199: */
200: private native GcInfo getLastGcInfo0(GarbageCollectorMXBean gc,
201: int numExtAtts, Object[] extAttValues, char[] extAttTypes,
202: MemoryUsage[] before, MemoryUsage[] after);
203: }
|