Showing the changes to java/pkg/sun/management/GcInfoCompositeData.java (1 of 18352 files). Show all
Unified · Split
| @@ -0,0 +1,293 @@ | ||
| 1 | +/* | |
| 2 | + * Copyright (c) 2004, 2011, Oracle and/or its affiliates. All rights reserved. | |
| 3 | + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. | |
| 4 | + * | |
| 5 | + * This code is free software; you can redistribute it and/or modify it | |
| 6 | + * under the terms of the GNU General Public License version 2 only, as | |
| 7 | + * published by the Free Software Foundation. Oracle designates this | |
| 8 | + * particular file as subject to the "Classpath" exception as provided | |
| 9 | + * by Oracle in the LICENSE file that accompanied this code. | |
| 10 | + * | |
| 11 | + * This code is distributed in the hope that it will be useful, but WITHOUT | |
| 12 | + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or | |
| 13 | + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License | |
| 14 | + * version 2 for more details (a copy is included in the LICENSE file that | |
| 15 | + * accompanied this code). | |
| 16 | + * | |
| 17 | + * You should have received a copy of the GNU General Public License version | |
| 18 | + * 2 along with this work; if not, write to the Free Software Foundation, | |
| 19 | + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. | |
| 20 | + * | |
| 21 | + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA | |
| 22 | + * or visit www.oracle.com if you need additional information or have any | |
| 23 | + * questions. | |
| 24 | + */ | |
| 25 | + | |
| 26 | +package sun.management; | |
| 27 | + | |
| 28 | +import java.lang.management.MemoryUsage; | |
| 29 | +import java.lang.reflect.Method; | |
| 30 | +import java.lang.reflect.Field; | |
| 31 | +import java.util.Iterator; | |
| 32 | +import java.util.Map; | |
| 33 | +import java.util.HashMap; | |
| 34 | +import java.util.List; | |
| 35 | +import java.util.Collections; | |
| 36 | +import java.io.InvalidObjectException; | |
| 37 | +import javax.management.openmbean.CompositeType; | |
| 38 | +import javax.management.openmbean.CompositeData; | |
| 39 | +import javax.management.openmbean.CompositeDataSupport; | |
| 40 | +import javax.management.openmbean.TabularData; | |
| 41 | +import javax.management.openmbean.SimpleType; | |
| 42 | +import javax.management.openmbean.OpenType; | |
| 43 | +import javax.management.openmbean.OpenDataException; | |
| 44 | +import com.sun.management.GcInfo; | |
| 45 | +import com.sun.management.GarbageCollectionNotificationInfo; | |
| 46 | +import java.security.AccessController; | |
| 47 | +import java.security.PrivilegedAction; | |
| 48 | + | |
| 49 | +/** | |
| 50 | + * A CompositeData for GcInfo for the local management support. | |
| 51 | + * This class avoids the performance penalty paid to the | |
| 52 | + * construction of a CompositeData use in the local case. | |
| 53 | + */ | |
| 54 | +public class GcInfoCompositeData extends LazyCompositeData { | |
| 55 | + private final GcInfo info; | |
| 56 | + private final GcInfoBuilder builder; | |
| 57 | + private final Object[] gcExtItemValues; | |
| 58 | + | |
| 59 | + public GcInfoCompositeData(GcInfo info, | |
| 60 | + GcInfoBuilder builder, | |
| 61 | + Object[] gcExtItemValues) { | |
| 62 | + this.info = info; | |
| 63 | + this.builder = builder; | |
| 64 | + this.gcExtItemValues = gcExtItemValues; | |
| 65 | + } | |
| 66 | + | |
| 67 | + public GcInfo getGcInfo() { | |
| 68 | + return info; | |
| 69 | + } | |
| 70 | + | |
| 71 | + public static CompositeData toCompositeData(final GcInfo info) { | |
| 72 | + final GcInfoBuilder builder = AccessController.doPrivileged (new PrivilegedAction<GcInfoBuilder>() { | |
| 73 | + public GcInfoBuilder run() { | |
| 74 | + try { | |
| 75 | + Class cl = Class.forName("com.sun.management.GcInfo"); | |
| 76 | + Field f = cl.getDeclaredField("builder"); | |
| 77 | + f.setAccessible(true); | |
| 78 | + return (GcInfoBuilder)f.get(info); | |
| 79 | + } catch(ClassNotFoundException e) { | |
| 80 | + return null; | |
| 81 | + } catch(NoSuchFieldException e) { | |
| 82 | + return null; | |
| 83 | + } catch(IllegalAccessException e) { | |
| 84 | + return null; | |
| 85 | + } | |
| 86 | + } | |
| 87 | + }); | |
| 88 | + final Object[] extAttr = AccessController.doPrivileged (new PrivilegedAction<Object[]>() { | |
| 89 | + public Object[] run() { | |
| 90 | + try { | |
| 91 | + Class cl = Class.forName("com.sun.management.GcInfo"); | |
| 92 | + Field f = cl.getDeclaredField("extAttributes"); | |
| 93 | + f.setAccessible(true); | |
| 94 | + return (Object[])f.get(info); | |
| 95 | + } catch(ClassNotFoundException e) { | |
| 96 | + return null; | |
| 97 | + } catch(NoSuchFieldException e) { | |
| 98 | + return null; | |
| 99 | + } catch(IllegalAccessException e) { | |
| 100 | + return null; | |
| 101 | + } | |
| 102 | + } | |
| 103 | + }); | |
| 104 | + GcInfoCompositeData gcicd = | |
| 105 | + new GcInfoCompositeData(info,builder,extAttr); | |
| 106 | + return gcicd.getCompositeData(); | |
| 107 | + } | |
| 108 | + | |
| 109 | + protected CompositeData getCompositeData() { | |
| 110 | + // CONTENTS OF THIS ARRAY MUST BE SYNCHRONIZED WITH | |
| 111 | + // baseGcInfoItemNames! | |
| 112 | + final Object[] baseGcInfoItemValues; | |
| 113 | + | |
| 114 | + try { | |
| 115 | + baseGcInfoItemValues = new Object[] { | |
| 116 | + new Long(info.getId()), | |
| 117 | + new Long(info.getStartTime()), | |
| 118 | + new Long(info.getEndTime()), | |
| 119 | + new Long(info.getDuration()), | |
| 120 | + memoryUsageMapType.toOpenTypeData(info.getMemoryUsageBeforeGc()), | |
| 121 | + memoryUsageMapType.toOpenTypeData(info.getMemoryUsageAfterGc()), | |
| 122 | + }; | |
| 123 | + } catch (OpenDataException e) { | |
| 124 | + // Should never reach here | |
| 125 | + throw new AssertionError(e); | |
| 126 | + } | |
| 127 | + | |
| 128 | + // Get the item values for the extension attributes | |
| 129 | + final int gcExtItemCount = builder.getGcExtItemCount(); | |
| 130 | + if (gcExtItemCount == 0 && | |
| 131 | + gcExtItemValues != null && gcExtItemValues.length != 0) { | |
| 132 | + throw new AssertionError("Unexpected Gc Extension Item Values"); | |
| 133 | + } | |
| 134 | + | |
| 135 | + if (gcExtItemCount > 0 && (gcExtItemValues == null || | |
| 136 | + gcExtItemCount != gcExtItemValues.length)) { | |
| 137 | + throw new AssertionError("Unmatched Gc Extension Item Values"); | |
| 138 | + } | |
| 139 | + | |
| 140 | + Object[] values = new Object[baseGcInfoItemValues.length + | |
| 141 | + gcExtItemCount]; | |
| 142 | + System.arraycopy(baseGcInfoItemValues, 0, values, 0, | |
| 143 | + baseGcInfoItemValues.length); | |
| 144 | + | |
| 145 | + if (gcExtItemCount > 0) { | |
| 146 | + System.arraycopy(gcExtItemValues, 0, values, | |
| 147 | + baseGcInfoItemValues.length, gcExtItemCount); | |
| 148 | + } | |
| 149 | + | |
| 150 | + try { | |
| 151 | + return new CompositeDataSupport(builder.getGcInfoCompositeType(), | |
| 152 | + builder.getItemNames(), | |
| 153 | + values); | |
| 154 | + } catch (OpenDataException e) { | |
| 155 | + // Should never reach here | |
| 156 | + throw new AssertionError(e); | |
| 157 | + } | |
| 158 | + } | |
| 159 | + | |
| 160 | + private static final String ID = "id"; | |
| 161 | + private static final String START_TIME = "startTime"; | |
| 162 | + private static final String END_TIME = "endTime"; | |
| 163 | + private static final String DURATION = "duration"; | |
| 164 | + private static final String MEMORY_USAGE_BEFORE_GC = "memoryUsageBeforeGc"; | |
| 165 | + private static final String MEMORY_USAGE_AFTER_GC = "memoryUsageAfterGc"; | |
| 166 | + | |
| 167 | + private static final String[] baseGcInfoItemNames = { | |
| 168 | + ID, | |
| 169 | + START_TIME, | |
| 170 | + END_TIME, | |
| 171 | + DURATION, | |
| 172 | + MEMORY_USAGE_BEFORE_GC, | |
| 173 | + MEMORY_USAGE_AFTER_GC, | |
| 174 | + }; | |
| 175 | + | |
| 176 | + | |
| 177 | + private static MappedMXBeanType memoryUsageMapType; | |
| 178 | + static { | |
| 179 | + try { | |
| 180 | + Method m = GcInfo.class.getMethod("getMemoryUsageBeforeGc"); | |
| 181 | + memoryUsageMapType = | |
| 182 | + MappedMXBeanType.getMappedType(m.getGenericReturnType()); | |
| 183 | + } catch (NoSuchMethodException e) { | |
| 184 | + // Should never reach here | |
| 185 | + throw new AssertionError(e); | |
| 186 | + } catch (OpenDataException e) { | |
| 187 | + // Should never reach here | |
| 188 | + throw new AssertionError(e); | |
| 189 | + } | |
| 190 | + } | |
| 191 | + | |
| 192 | + static String[] getBaseGcInfoItemNames() { | |
| 193 | + return baseGcInfoItemNames; | |
| 194 | + } | |
| 195 | + | |
| 196 | + private static OpenType[] baseGcInfoItemTypes = null; | |
| 197 | + static synchronized OpenType[] getBaseGcInfoItemTypes() { | |
| 198 | + if (baseGcInfoItemTypes == null) { | |
| 199 | + OpenType<?> memoryUsageOpenType = memoryUsageMapType.getOpenType(); | |
| 200 | + baseGcInfoItemTypes = new OpenType[] { | |
| 201 | + SimpleType.LONG, | |
| 202 | + SimpleType.LONG, | |
| 203 | + SimpleType.LONG, | |
| 204 | + SimpleType.LONG, | |
| 205 | + | |
| 206 | + memoryUsageOpenType, | |
| 207 | + memoryUsageOpenType, | |
| 208 | + }; | |
| 209 | + } | |
| 210 | + return baseGcInfoItemTypes; | |
| 211 | + } | |
| 212 | + | |
| 213 | + public static long getId(CompositeData cd) { | |
| 214 | + return getLong(cd, ID); | |
| 215 | + } | |
| 216 | + public static long getStartTime(CompositeData cd) { | |
| 217 | + return getLong(cd, START_TIME); | |
| 218 | + } | |
| 219 | + public static long getEndTime(CompositeData cd) { | |
| 220 | + return getLong(cd, END_TIME); | |
| 221 | + } | |
| 222 | + | |
| 223 | + public static Map<String, MemoryUsage> | |
| 224 | + getMemoryUsageBeforeGc(CompositeData cd) { | |
| 225 | + try { | |
| 226 | + TabularData td = (TabularData) cd.get(MEMORY_USAGE_BEFORE_GC); | |
| 227 | + return cast(memoryUsageMapType.toJavaTypeData(td)); | |
| 228 | + } catch (InvalidObjectException e) { | |
| 229 | + // Should never reach here | |
| 230 | + throw new AssertionError(e); | |
| 231 | + } catch (OpenDataException e) { | |
| 232 | + // Should never reach here | |
| 233 | + throw new AssertionError(e); | |
| 234 | + } | |
| 235 | + } | |
| 236 | + | |
| 237 | + @SuppressWarnings("unchecked") | |
| 238 | + public static Map<String, MemoryUsage> cast(Object x) { | |
| 239 | + return (Map<String, MemoryUsage>) x; | |
| 240 | + } | |
| 241 | + public static Map<String, MemoryUsage> | |
| 242 | + getMemoryUsageAfterGc(CompositeData cd) { | |
| 243 | + try { | |
| 244 | + TabularData td = (TabularData) cd.get(MEMORY_USAGE_AFTER_GC); | |
| 245 | + //return (Map<String,MemoryUsage>) | |
| 246 | + return cast(memoryUsageMapType.toJavaTypeData(td)); | |
| 247 | + } catch (InvalidObjectException e) { | |
| 248 | + // Should never reach here | |
| 249 | + throw new AssertionError(e); | |
| 250 | + } catch (OpenDataException e) { | |
| 251 | + // Should never reach here | |
| 252 | + throw new AssertionError(e); | |
| 253 | + } | |
| 254 | + } | |
| 255 | + | |
| 256 | + /** | |
| 257 | + * Returns true if the input CompositeData has the expected | |
| 258 | + * CompositeType (i.e. contain all attributes with expected | |
| 259 | + * names and types). Otherwise, return false. | |
| 260 | + */ | |
| 261 | + public static void validateCompositeData(CompositeData cd) { | |
| 262 | + if (cd == null) { | |
| 263 | + throw new NullPointerException("Null CompositeData"); | |
| 264 | + } | |
| 265 | + | |
| 266 | + if (!isTypeMatched(getBaseGcInfoCompositeType(), | |
| 267 | + cd.getCompositeType())) { | |
| 268 | + throw new IllegalArgumentException( | |
| 269 | + "Unexpected composite type for GcInfo"); | |
| 270 | + } | |
| 271 | + } | |
| 272 | + | |
| 273 | + // This is only used for validation. | |
| 274 | + private static CompositeType baseGcInfoCompositeType = null; | |
| 275 | + static synchronized CompositeType getBaseGcInfoCompositeType() { | |
| 276 | + if (baseGcInfoCompositeType == null) { | |
| 277 | + try { | |
| 278 | + baseGcInfoCompositeType = | |
| 279 | + new CompositeType("sun.management.BaseGcInfoCompositeType", | |
| 280 | + "CompositeType for Base GcInfo", | |
| 281 | + getBaseGcInfoItemNames(), | |
| 282 | + getBaseGcInfoItemNames(), | |
| 283 | + getBaseGcInfoItemTypes()); | |
| 284 | + } catch (OpenDataException e) { | |
| 285 | + // shouldn't reach here | |
| 286 | + throw Util.newException(e); | |
| 287 | + } | |
| 288 | + } | |
| 289 | + return baseGcInfoCompositeType; | |
| 290 | + } | |
| 291 | + | |
| 292 | + private static final long serialVersionUID = -5716428894085882742L; | |
| 293 | +} | |