Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/src/share/classes/sun/management/GcInfoCompositeData.java
38827 views
/*1* Copyright (c) 2004, 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*/2425package sun.management;2627import java.lang.management.MemoryUsage;28import java.lang.reflect.Method;29import java.lang.reflect.Field;30import java.util.Iterator;31import java.util.Map;32import java.util.HashMap;33import java.util.List;34import java.util.Collections;35import java.io.InvalidObjectException;36import javax.management.openmbean.CompositeType;37import javax.management.openmbean.CompositeData;38import javax.management.openmbean.CompositeDataSupport;39import javax.management.openmbean.TabularData;40import javax.management.openmbean.SimpleType;41import javax.management.openmbean.OpenType;42import javax.management.openmbean.OpenDataException;43import com.sun.management.GcInfo;44import com.sun.management.GarbageCollectionNotificationInfo;45import java.security.AccessController;46import java.security.PrivilegedAction;4748/**49* A CompositeData for GcInfo for the local management support.50* This class avoids the performance penalty paid to the51* construction of a CompositeData use in the local case.52*/53public class GcInfoCompositeData extends LazyCompositeData {54private final GcInfo info;55private final GcInfoBuilder builder;56private final Object[] gcExtItemValues;5758public GcInfoCompositeData(GcInfo info,59GcInfoBuilder builder,60Object[] gcExtItemValues) {61this.info = info;62this.builder = builder;63this.gcExtItemValues = gcExtItemValues;64}6566public GcInfo getGcInfo() {67return info;68}6970public static CompositeData toCompositeData(final GcInfo info) {71final GcInfoBuilder builder = AccessController.doPrivileged (new PrivilegedAction<GcInfoBuilder>() {72public GcInfoBuilder run() {73try {74Class cl = Class.forName("com.sun.management.GcInfo");75Field f = cl.getDeclaredField("builder");76f.setAccessible(true);77return (GcInfoBuilder)f.get(info);78} catch(ClassNotFoundException | NoSuchFieldException | IllegalAccessException e) {79return null;80}81}82});83final Object[] extAttr = AccessController.doPrivileged (new PrivilegedAction<Object[]>() {84public Object[] run() {85try {86Class cl = Class.forName("com.sun.management.GcInfo");87Field f = cl.getDeclaredField("extAttributes");88f.setAccessible(true);89return (Object[])f.get(info);90} catch(ClassNotFoundException | NoSuchFieldException | IllegalAccessException e) {91return null;92}93}94});95GcInfoCompositeData gcicd =96new GcInfoCompositeData(info,builder,extAttr);97return gcicd.getCompositeData();98}99100protected CompositeData getCompositeData() {101// CONTENTS OF THIS ARRAY MUST BE SYNCHRONIZED WITH102// baseGcInfoItemNames!103final Object[] baseGcInfoItemValues;104105try {106baseGcInfoItemValues = new Object[] {107new Long(info.getId()),108new Long(info.getStartTime()),109new Long(info.getEndTime()),110new Long(info.getDuration()),111memoryUsageMapType.toOpenTypeData(info.getMemoryUsageBeforeGc()),112memoryUsageMapType.toOpenTypeData(info.getMemoryUsageAfterGc()),113};114} catch (OpenDataException e) {115// Should never reach here116throw new AssertionError(e);117}118119// Get the item values for the extension attributes120final int gcExtItemCount = builder.getGcExtItemCount();121if (gcExtItemCount == 0 &&122gcExtItemValues != null && gcExtItemValues.length != 0) {123throw new AssertionError("Unexpected Gc Extension Item Values");124}125126if (gcExtItemCount > 0 && (gcExtItemValues == null ||127gcExtItemCount != gcExtItemValues.length)) {128throw new AssertionError("Unmatched Gc Extension Item Values");129}130131Object[] values = new Object[baseGcInfoItemValues.length +132gcExtItemCount];133System.arraycopy(baseGcInfoItemValues, 0, values, 0,134baseGcInfoItemValues.length);135136if (gcExtItemCount > 0) {137System.arraycopy(gcExtItemValues, 0, values,138baseGcInfoItemValues.length, gcExtItemCount);139}140141try {142return new CompositeDataSupport(builder.getGcInfoCompositeType(),143builder.getItemNames(),144values);145} catch (OpenDataException e) {146// Should never reach here147throw new AssertionError(e);148}149}150151private static final String ID = "id";152private static final String START_TIME = "startTime";153private static final String END_TIME = "endTime";154private static final String DURATION = "duration";155private static final String MEMORY_USAGE_BEFORE_GC = "memoryUsageBeforeGc";156private static final String MEMORY_USAGE_AFTER_GC = "memoryUsageAfterGc";157158private static final String[] baseGcInfoItemNames = {159ID,160START_TIME,161END_TIME,162DURATION,163MEMORY_USAGE_BEFORE_GC,164MEMORY_USAGE_AFTER_GC,165};166167168private static MappedMXBeanType memoryUsageMapType;169static {170try {171Method m = GcInfo.class.getMethod("getMemoryUsageBeforeGc");172memoryUsageMapType =173MappedMXBeanType.getMappedType(m.getGenericReturnType());174} catch (NoSuchMethodException | OpenDataException e) {175// Should never reach here176throw new AssertionError(e);177}178}179180static String[] getBaseGcInfoItemNames() {181return baseGcInfoItemNames;182}183184private static OpenType[] baseGcInfoItemTypes = null;185static synchronized OpenType[] getBaseGcInfoItemTypes() {186if (baseGcInfoItemTypes == null) {187OpenType<?> memoryUsageOpenType = memoryUsageMapType.getOpenType();188baseGcInfoItemTypes = new OpenType<?>[] {189SimpleType.LONG,190SimpleType.LONG,191SimpleType.LONG,192SimpleType.LONG,193194memoryUsageOpenType,195memoryUsageOpenType,196};197}198return baseGcInfoItemTypes;199}200201public static long getId(CompositeData cd) {202return getLong(cd, ID);203}204public static long getStartTime(CompositeData cd) {205return getLong(cd, START_TIME);206}207public static long getEndTime(CompositeData cd) {208return getLong(cd, END_TIME);209}210211public static Map<String, MemoryUsage>212getMemoryUsageBeforeGc(CompositeData cd) {213try {214TabularData td = (TabularData) cd.get(MEMORY_USAGE_BEFORE_GC);215return cast(memoryUsageMapType.toJavaTypeData(td));216} catch (InvalidObjectException | OpenDataException e) {217// Should never reach here218throw new AssertionError(e);219}220}221222@SuppressWarnings("unchecked")223public static Map<String, MemoryUsage> cast(Object x) {224return (Map<String, MemoryUsage>) x;225}226public static Map<String, MemoryUsage>227getMemoryUsageAfterGc(CompositeData cd) {228try {229TabularData td = (TabularData) cd.get(MEMORY_USAGE_AFTER_GC);230//return (Map<String,MemoryUsage>)231return cast(memoryUsageMapType.toJavaTypeData(td));232} catch (InvalidObjectException | OpenDataException e) {233// Should never reach here234throw new AssertionError(e);235}236}237238/**239* Returns true if the input CompositeData has the expected240* CompositeType (i.e. contain all attributes with expected241* names and types). Otherwise, return false.242*/243public static void validateCompositeData(CompositeData cd) {244if (cd == null) {245throw new NullPointerException("Null CompositeData");246}247248if (!isTypeMatched(getBaseGcInfoCompositeType(),249cd.getCompositeType())) {250throw new IllegalArgumentException(251"Unexpected composite type for GcInfo");252}253}254255// This is only used for validation.256private static CompositeType baseGcInfoCompositeType = null;257static synchronized CompositeType getBaseGcInfoCompositeType() {258if (baseGcInfoCompositeType == null) {259try {260baseGcInfoCompositeType =261new CompositeType("sun.management.BaseGcInfoCompositeType",262"CompositeType for Base GcInfo",263getBaseGcInfoItemNames(),264getBaseGcInfoItemNames(),265getBaseGcInfoItemTypes());266} catch (OpenDataException e) {267// shouldn't reach here268throw Util.newException(e);269}270}271return baseGcInfoCompositeType;272}273274private static final long serialVersionUID = -5716428894085882742L;275}276277278