Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/src/share/classes/sun/management/GcInfoBuilder.java
38827 views
/*1* Copyright (c) 2003, 2012, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation. Oracle designates this7* particular file as subject to the "Classpath" exception as provided8* by Oracle in the LICENSE file that accompanied this code.9*10* This code is distributed in the hope that it will be useful, but WITHOUT11* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or12* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License13* version 2 for more details (a copy is included in the LICENSE file that14* accompanied this code).15*16* You should have received a copy of the GNU General Public License version17* 2 along with this work; if not, write to the Free Software Foundation,18* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.19*20* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA21* or visit www.oracle.com if you need additional information or have any22* questions.23*/24package sun.management;2526import java.lang.management.GarbageCollectorMXBean;27import java.lang.management.MemoryUsage;28import javax.management.openmbean.OpenType;29import javax.management.openmbean.SimpleType;30import javax.management.openmbean.TabularType;31import javax.management.openmbean.TabularData;32import javax.management.openmbean.TabularDataSupport;33import javax.management.openmbean.CompositeType;34import javax.management.openmbean.CompositeData;35import javax.management.openmbean.CompositeDataSupport;36import javax.management.openmbean.OpenDataException;37import com.sun.management.GcInfo;3839/**40* Helper class to build composite data.41*/42public class GcInfoBuilder {43private final GarbageCollectorMXBean gc;44private final String[] poolNames;45private String[] allItemNames;4647// GC-specific composite type:48// Each GarbageCollectorMXBean may have different GC-specific attributes49// the CompositeType for the GcInfo could be different.50private CompositeType gcInfoCompositeType;5152// GC-specific items53private final int gcExtItemCount;54private final String[] gcExtItemNames;55private final String[] gcExtItemDescs;56private final char[] gcExtItemTypes;5758GcInfoBuilder(GarbageCollectorMXBean gc, String[] poolNames) {59this.gc = gc;60this.poolNames = poolNames;61this.gcExtItemCount = getNumGcExtAttributes(gc);62this.gcExtItemNames = new String[gcExtItemCount];63this.gcExtItemDescs = new String[gcExtItemCount];64this.gcExtItemTypes = new char[gcExtItemCount];6566// Fill the information about extension attributes67fillGcAttributeInfo(gc, gcExtItemCount, gcExtItemNames,68gcExtItemTypes, gcExtItemDescs);6970// lazily build the CompositeType for the GcInfo71// including the GC-specific extension attributes72this.gcInfoCompositeType = null;73}7475GcInfo getLastGcInfo() {76MemoryUsage[] usageBeforeGC = new MemoryUsage[poolNames.length];77MemoryUsage[] usageAfterGC = new MemoryUsage[poolNames.length];78Object[] values = new Object[gcExtItemCount];7980return getLastGcInfo0(gc, gcExtItemCount, values, gcExtItemTypes,81usageBeforeGC, usageAfterGC);82}8384public String[] getPoolNames() {85return poolNames;86}8788int getGcExtItemCount() {89return gcExtItemCount;90}9192// Returns the CompositeType for the GcInfo including93// the extension attributes94synchronized CompositeType getGcInfoCompositeType() {95if (gcInfoCompositeType != null)96return gcInfoCompositeType;9798// First, fill with the attributes in the GcInfo99String[] gcInfoItemNames = GcInfoCompositeData.getBaseGcInfoItemNames();100OpenType[] gcInfoItemTypes = GcInfoCompositeData.getBaseGcInfoItemTypes();101int numGcInfoItems = gcInfoItemNames.length;102103int itemCount = numGcInfoItems + gcExtItemCount;104allItemNames = new String[itemCount];105String[] allItemDescs = new String[itemCount];106OpenType<?>[] allItemTypes = new OpenType<?>[itemCount];107108System.arraycopy(gcInfoItemNames, 0, allItemNames, 0, numGcInfoItems);109System.arraycopy(gcInfoItemNames, 0, allItemDescs, 0, numGcInfoItems);110System.arraycopy(gcInfoItemTypes, 0, allItemTypes, 0, numGcInfoItems);111112// Then fill with the extension GC-specific attributes, if any.113if (gcExtItemCount > 0) {114fillGcAttributeInfo(gc, gcExtItemCount, gcExtItemNames,115gcExtItemTypes, gcExtItemDescs);116System.arraycopy(gcExtItemNames, 0, allItemNames,117numGcInfoItems, gcExtItemCount);118System.arraycopy(gcExtItemDescs, 0, allItemDescs,119numGcInfoItems, gcExtItemCount);120for (int i = numGcInfoItems, j = 0; j < gcExtItemCount; i++, j++) {121switch (gcExtItemTypes[j]) {122case 'Z':123allItemTypes[i] = SimpleType.BOOLEAN;124break;125case 'B':126allItemTypes[i] = SimpleType.BYTE;127break;128case 'C':129allItemTypes[i] = SimpleType.CHARACTER;130break;131case 'S':132allItemTypes[i] = SimpleType.SHORT;133break;134case 'I':135allItemTypes[i] = SimpleType.INTEGER;136break;137case 'J':138allItemTypes[i] = SimpleType.LONG;139break;140case 'F':141allItemTypes[i] = SimpleType.FLOAT;142break;143case 'D':144allItemTypes[i] = SimpleType.DOUBLE;145break;146default:147throw new AssertionError(148"Unsupported type [" + gcExtItemTypes[i] + "]");149}150}151}152153CompositeType gict = null;154try {155final String typeName =156"sun.management." + gc.getName() + ".GcInfoCompositeType";157158gict = new CompositeType(typeName,159"CompositeType for GC info for " +160gc.getName(),161allItemNames,162allItemDescs,163allItemTypes);164} catch (OpenDataException e) {165// shouldn't reach here166throw Util.newException(e);167}168gcInfoCompositeType = gict;169170return gcInfoCompositeType;171}172173synchronized String[] getItemNames() {174if (allItemNames == null) {175// initialize when forming the composite type176getGcInfoCompositeType();177}178return allItemNames;179}180181// Retrieve information about extension attributes182private native int getNumGcExtAttributes(GarbageCollectorMXBean gc);183private native void fillGcAttributeInfo(GarbageCollectorMXBean gc,184int numAttributes,185String[] attributeNames,186char[] types,187String[] descriptions);188189/**190* Returns the last GcInfo191*192* @param gc GarbageCollectorMXBean that the gc info is associated with.193* @param numExtAtts number of extension attributes194* @param extAttValues Values of extension attributes to be filled.195* @param before Memory usage before GC to be filled.196* @param after Memory usage after GC to be filled.197*/198private native GcInfo getLastGcInfo0(GarbageCollectorMXBean gc,199int numExtAtts,200Object[] extAttValues,201char[] extAttTypes,202MemoryUsage[] before,203MemoryUsage[] after);204}205206207