0N/A/*
4235N/A * Copyright (c) 2004, 2011, 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
2362N/A * published by the Free Software Foundation. Oracle designates this
0N/A * particular file as subject to the "Classpath" exception as provided
2362N/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 *
2362N/A * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
2362N/A * or visit www.oracle.com if you need additional information or have any
2362N/A * questions.
0N/A */
0N/A
0N/Apackage sun.management;
0N/A
0N/Aimport java.lang.management.MemoryUsage;
0N/Aimport java.lang.reflect.Method;
4235N/Aimport java.lang.reflect.Field;
0N/Aimport java.util.Iterator;
0N/Aimport java.util.Map;
0N/Aimport java.util.HashMap;
0N/Aimport java.util.List;
0N/Aimport java.util.Collections;
0N/Aimport java.io.InvalidObjectException;
0N/Aimport javax.management.openmbean.CompositeType;
0N/Aimport javax.management.openmbean.CompositeData;
0N/Aimport javax.management.openmbean.CompositeDataSupport;
0N/Aimport javax.management.openmbean.TabularData;
0N/Aimport javax.management.openmbean.SimpleType;
0N/Aimport javax.management.openmbean.OpenType;
0N/Aimport javax.management.openmbean.OpenDataException;
0N/Aimport com.sun.management.GcInfo;
4235N/Aimport com.sun.management.GarbageCollectionNotificationInfo;
4235N/Aimport java.security.AccessController;
4235N/Aimport java.security.PrivilegedAction;
0N/A
0N/A/**
0N/A * A CompositeData for GcInfo for the local management support.
0N/A * This class avoids the performance penalty paid to the
0N/A * construction of a CompositeData use in the local case.
0N/A */
0N/Apublic class GcInfoCompositeData extends LazyCompositeData {
0N/A private final GcInfo info;
0N/A private final GcInfoBuilder builder;
0N/A private final Object[] gcExtItemValues;
0N/A
0N/A public GcInfoCompositeData(GcInfo info,
0N/A GcInfoBuilder builder,
0N/A Object[] gcExtItemValues) {
0N/A this.info = info;
0N/A this.builder = builder;
0N/A this.gcExtItemValues = gcExtItemValues;
0N/A }
0N/A
0N/A public GcInfo getGcInfo() {
0N/A return info;
0N/A }
0N/A
4235N/A public static CompositeData toCompositeData(final GcInfo info) {
4235N/A final GcInfoBuilder builder = AccessController.doPrivileged (new PrivilegedAction<GcInfoBuilder>() {
4235N/A public GcInfoBuilder run() {
4235N/A try {
4235N/A Class cl = Class.forName("com.sun.management.GcInfo");
4235N/A Field f = cl.getDeclaredField("builder");
4235N/A f.setAccessible(true);
4235N/A return (GcInfoBuilder)f.get(info);
4235N/A } catch(ClassNotFoundException e) {
4235N/A return null;
4235N/A } catch(NoSuchFieldException e) {
4235N/A return null;
4235N/A } catch(IllegalAccessException e) {
4235N/A return null;
4235N/A }
4235N/A }
4235N/A });
4235N/A final Object[] extAttr = AccessController.doPrivileged (new PrivilegedAction<Object[]>() {
4235N/A public Object[] run() {
4235N/A try {
4235N/A Class cl = Class.forName("com.sun.management.GcInfo");
4235N/A Field f = cl.getDeclaredField("extAttributes");
4235N/A f.setAccessible(true);
4235N/A return (Object[])f.get(info);
4235N/A } catch(ClassNotFoundException e) {
4235N/A return null;
4235N/A } catch(NoSuchFieldException e) {
4235N/A return null;
4235N/A } catch(IllegalAccessException e) {
4235N/A return null;
4235N/A }
4235N/A }
4235N/A });
4235N/A GcInfoCompositeData gcicd =
4235N/A new GcInfoCompositeData(info,builder,extAttr);
4235N/A return gcicd.getCompositeData();
4235N/A }
4235N/A
0N/A protected CompositeData getCompositeData() {
0N/A // CONTENTS OF THIS ARRAY MUST BE SYNCHRONIZED WITH
0N/A // baseGcInfoItemNames!
0N/A final Object[] baseGcInfoItemValues;
0N/A
0N/A try {
0N/A baseGcInfoItemValues = new Object[] {
0N/A new Long(info.getId()),
0N/A new Long(info.getStartTime()),
0N/A new Long(info.getEndTime()),
0N/A new Long(info.getDuration()),
0N/A memoryUsageMapType.toOpenTypeData(info.getMemoryUsageBeforeGc()),
0N/A memoryUsageMapType.toOpenTypeData(info.getMemoryUsageAfterGc()),
0N/A };
0N/A } catch (OpenDataException e) {
0N/A // Should never reach here
178N/A throw new AssertionError(e);
0N/A }
0N/A
0N/A // Get the item values for the extension attributes
0N/A final int gcExtItemCount = builder.getGcExtItemCount();
0N/A if (gcExtItemCount == 0 &&
0N/A gcExtItemValues != null && gcExtItemValues.length != 0) {
178N/A throw new AssertionError("Unexpected Gc Extension Item Values");
0N/A }
0N/A
0N/A if (gcExtItemCount > 0 && (gcExtItemValues == null ||
0N/A gcExtItemCount != gcExtItemValues.length)) {
178N/A throw new AssertionError("Unmatched Gc Extension Item Values");
0N/A }
0N/A
0N/A Object[] values = new Object[baseGcInfoItemValues.length +
0N/A gcExtItemCount];
0N/A System.arraycopy(baseGcInfoItemValues, 0, values, 0,
0N/A baseGcInfoItemValues.length);
0N/A
0N/A if (gcExtItemCount > 0) {
0N/A System.arraycopy(gcExtItemValues, 0, values,
0N/A baseGcInfoItemValues.length, gcExtItemCount);
0N/A }
0N/A
0N/A try {
0N/A return new CompositeDataSupport(builder.getGcInfoCompositeType(),
0N/A builder.getItemNames(),
0N/A values);
0N/A } catch (OpenDataException e) {
0N/A // Should never reach here
178N/A throw new AssertionError(e);
0N/A }
0N/A }
0N/A
0N/A private static final String ID = "id";
0N/A private static final String START_TIME = "startTime";
0N/A private static final String END_TIME = "endTime";
0N/A private static final String DURATION = "duration";
0N/A private static final String MEMORY_USAGE_BEFORE_GC = "memoryUsageBeforeGc";
0N/A private static final String MEMORY_USAGE_AFTER_GC = "memoryUsageAfterGc";
0N/A
0N/A private static final String[] baseGcInfoItemNames = {
0N/A ID,
0N/A START_TIME,
0N/A END_TIME,
0N/A DURATION,
0N/A MEMORY_USAGE_BEFORE_GC,
0N/A MEMORY_USAGE_AFTER_GC,
0N/A };
0N/A
0N/A
0N/A private static MappedMXBeanType memoryUsageMapType;
0N/A static {
0N/A try {
0N/A Method m = GcInfo.class.getMethod("getMemoryUsageBeforeGc");
0N/A memoryUsageMapType =
0N/A MappedMXBeanType.getMappedType(m.getGenericReturnType());
0N/A } catch (NoSuchMethodException e) {
0N/A // Should never reach here
178N/A throw new AssertionError(e);
0N/A } catch (OpenDataException e) {
0N/A // Should never reach here
178N/A throw new AssertionError(e);
0N/A }
0N/A }
0N/A
0N/A static String[] getBaseGcInfoItemNames() {
0N/A return baseGcInfoItemNames;
0N/A }
0N/A
0N/A private static OpenType[] baseGcInfoItemTypes = null;
0N/A static synchronized OpenType[] getBaseGcInfoItemTypes() {
0N/A if (baseGcInfoItemTypes == null) {
0N/A OpenType<?> memoryUsageOpenType = memoryUsageMapType.getOpenType();
0N/A baseGcInfoItemTypes = new OpenType[] {
0N/A SimpleType.LONG,
0N/A SimpleType.LONG,
0N/A SimpleType.LONG,
0N/A SimpleType.LONG,
0N/A
0N/A memoryUsageOpenType,
0N/A memoryUsageOpenType,
0N/A };
0N/A }
0N/A return baseGcInfoItemTypes;
0N/A }
0N/A
0N/A public static long getId(CompositeData cd) {
0N/A return getLong(cd, ID);
0N/A }
0N/A public static long getStartTime(CompositeData cd) {
0N/A return getLong(cd, START_TIME);
0N/A }
0N/A public static long getEndTime(CompositeData cd) {
0N/A return getLong(cd, END_TIME);
0N/A }
0N/A
0N/A public static Map<String, MemoryUsage>
0N/A getMemoryUsageBeforeGc(CompositeData cd) {
0N/A try {
0N/A TabularData td = (TabularData) cd.get(MEMORY_USAGE_BEFORE_GC);
0N/A return cast(memoryUsageMapType.toJavaTypeData(td));
0N/A } catch (InvalidObjectException e) {
0N/A // Should never reach here
178N/A throw new AssertionError(e);
0N/A } catch (OpenDataException e) {
0N/A // Should never reach here
178N/A throw new AssertionError(e);
0N/A }
0N/A }
0N/A
0N/A @SuppressWarnings("unchecked")
0N/A public static Map<String, MemoryUsage> cast(Object x) {
0N/A return (Map<String, MemoryUsage>) x;
0N/A }
0N/A public static Map<String, MemoryUsage>
0N/A getMemoryUsageAfterGc(CompositeData cd) {
0N/A try {
0N/A TabularData td = (TabularData) cd.get(MEMORY_USAGE_AFTER_GC);
0N/A //return (Map<String,MemoryUsage>)
0N/A return cast(memoryUsageMapType.toJavaTypeData(td));
0N/A } catch (InvalidObjectException e) {
0N/A // Should never reach here
178N/A throw new AssertionError(e);
0N/A } catch (OpenDataException e) {
0N/A // Should never reach here
178N/A throw new AssertionError(e);
0N/A }
0N/A }
0N/A
0N/A /**
0N/A * Returns true if the input CompositeData has the expected
0N/A * CompositeType (i.e. contain all attributes with expected
0N/A * names and types). Otherwise, return false.
0N/A */
0N/A public static void validateCompositeData(CompositeData cd) {
0N/A if (cd == null) {
0N/A throw new NullPointerException("Null CompositeData");
0N/A }
0N/A
0N/A if (!isTypeMatched(getBaseGcInfoCompositeType(),
0N/A cd.getCompositeType())) {
0N/A throw new IllegalArgumentException(
0N/A "Unexpected composite type for GcInfo");
0N/A }
0N/A }
0N/A
0N/A // This is only used for validation.
0N/A private static CompositeType baseGcInfoCompositeType = null;
4235N/A static synchronized CompositeType getBaseGcInfoCompositeType() {
0N/A if (baseGcInfoCompositeType == null) {
0N/A try {
0N/A baseGcInfoCompositeType =
0N/A new CompositeType("sun.management.BaseGcInfoCompositeType",
0N/A "CompositeType for Base GcInfo",
0N/A getBaseGcInfoItemNames(),
0N/A getBaseGcInfoItemNames(),
0N/A getBaseGcInfoItemTypes());
0N/A } catch (OpenDataException e) {
0N/A // shouldn't reach here
0N/A throw Util.newException(e);
0N/A }
0N/A }
0N/A return baseGcInfoCompositeType;
0N/A }
0N/A
1807N/A private static final long serialVersionUID = -5716428894085882742L;
0N/A}