author | mchung |
Tue, 17 Jan 2012 15:55:40 -0800 | |
changeset 11530 | a9d059c15b80 |
parent 9698 | 8b66cd6c5ebc |
child 14342 | 8435a30053c1 |
permissions | -rw-r--r-- |
2 | 1 |
/* |
9698
8b66cd6c5ebc
7036199: Adding a notification to the implementation of GarbageCollectorMXBeans
fparain
parents:
5506
diff
changeset
|
2 |
* Copyright (c) 2004, 2011, Oracle and/or its affiliates. All rights reserved. |
2 | 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 |
|
5506 | 7 |
* published by the Free Software Foundation. Oracle designates this |
2 | 8 |
* particular file as subject to the "Classpath" exception as provided |
5506 | 9 |
* by Oracle in the LICENSE file that accompanied this code. |
2 | 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 |
* |
|
5506 | 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. |
|
2 | 24 |
*/ |
25 |
||
26 |
package sun.management; |
|
27 |
||
28 |
import java.lang.management.MemoryUsage; |
|
29 |
import java.lang.reflect.Method; |
|
9698
8b66cd6c5ebc
7036199: Adding a notification to the implementation of GarbageCollectorMXBeans
fparain
parents:
5506
diff
changeset
|
30 |
import java.lang.reflect.Field; |
2 | 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; |
|
9698
8b66cd6c5ebc
7036199: Adding a notification to the implementation of GarbageCollectorMXBeans
fparain
parents:
5506
diff
changeset
|
45 |
import com.sun.management.GarbageCollectionNotificationInfo; |
8b66cd6c5ebc
7036199: Adding a notification to the implementation of GarbageCollectorMXBeans
fparain
parents:
5506
diff
changeset
|
46 |
import java.security.AccessController; |
8b66cd6c5ebc
7036199: Adding a notification to the implementation of GarbageCollectorMXBeans
fparain
parents:
5506
diff
changeset
|
47 |
import java.security.PrivilegedAction; |
2 | 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 |
||
9698
8b66cd6c5ebc
7036199: Adding a notification to the implementation of GarbageCollectorMXBeans
fparain
parents:
5506
diff
changeset
|
71 |
public static CompositeData toCompositeData(final GcInfo info) { |
8b66cd6c5ebc
7036199: Adding a notification to the implementation of GarbageCollectorMXBeans
fparain
parents:
5506
diff
changeset
|
72 |
final GcInfoBuilder builder = AccessController.doPrivileged (new PrivilegedAction<GcInfoBuilder>() { |
8b66cd6c5ebc
7036199: Adding a notification to the implementation of GarbageCollectorMXBeans
fparain
parents:
5506
diff
changeset
|
73 |
public GcInfoBuilder run() { |
8b66cd6c5ebc
7036199: Adding a notification to the implementation of GarbageCollectorMXBeans
fparain
parents:
5506
diff
changeset
|
74 |
try { |
8b66cd6c5ebc
7036199: Adding a notification to the implementation of GarbageCollectorMXBeans
fparain
parents:
5506
diff
changeset
|
75 |
Class cl = Class.forName("com.sun.management.GcInfo"); |
8b66cd6c5ebc
7036199: Adding a notification to the implementation of GarbageCollectorMXBeans
fparain
parents:
5506
diff
changeset
|
76 |
Field f = cl.getDeclaredField("builder"); |
8b66cd6c5ebc
7036199: Adding a notification to the implementation of GarbageCollectorMXBeans
fparain
parents:
5506
diff
changeset
|
77 |
f.setAccessible(true); |
8b66cd6c5ebc
7036199: Adding a notification to the implementation of GarbageCollectorMXBeans
fparain
parents:
5506
diff
changeset
|
78 |
return (GcInfoBuilder)f.get(info); |
11530
a9d059c15b80
7117570: Warnings in sun.mangement.* and its subpackages
mchung
parents:
9698
diff
changeset
|
79 |
} catch(ClassNotFoundException | NoSuchFieldException | IllegalAccessException e) { |
9698
8b66cd6c5ebc
7036199: Adding a notification to the implementation of GarbageCollectorMXBeans
fparain
parents:
5506
diff
changeset
|
80 |
return null; |
8b66cd6c5ebc
7036199: Adding a notification to the implementation of GarbageCollectorMXBeans
fparain
parents:
5506
diff
changeset
|
81 |
} |
8b66cd6c5ebc
7036199: Adding a notification to the implementation of GarbageCollectorMXBeans
fparain
parents:
5506
diff
changeset
|
82 |
} |
8b66cd6c5ebc
7036199: Adding a notification to the implementation of GarbageCollectorMXBeans
fparain
parents:
5506
diff
changeset
|
83 |
}); |
8b66cd6c5ebc
7036199: Adding a notification to the implementation of GarbageCollectorMXBeans
fparain
parents:
5506
diff
changeset
|
84 |
final Object[] extAttr = AccessController.doPrivileged (new PrivilegedAction<Object[]>() { |
8b66cd6c5ebc
7036199: Adding a notification to the implementation of GarbageCollectorMXBeans
fparain
parents:
5506
diff
changeset
|
85 |
public Object[] run() { |
8b66cd6c5ebc
7036199: Adding a notification to the implementation of GarbageCollectorMXBeans
fparain
parents:
5506
diff
changeset
|
86 |
try { |
8b66cd6c5ebc
7036199: Adding a notification to the implementation of GarbageCollectorMXBeans
fparain
parents:
5506
diff
changeset
|
87 |
Class cl = Class.forName("com.sun.management.GcInfo"); |
8b66cd6c5ebc
7036199: Adding a notification to the implementation of GarbageCollectorMXBeans
fparain
parents:
5506
diff
changeset
|
88 |
Field f = cl.getDeclaredField("extAttributes"); |
8b66cd6c5ebc
7036199: Adding a notification to the implementation of GarbageCollectorMXBeans
fparain
parents:
5506
diff
changeset
|
89 |
f.setAccessible(true); |
8b66cd6c5ebc
7036199: Adding a notification to the implementation of GarbageCollectorMXBeans
fparain
parents:
5506
diff
changeset
|
90 |
return (Object[])f.get(info); |
11530
a9d059c15b80
7117570: Warnings in sun.mangement.* and its subpackages
mchung
parents:
9698
diff
changeset
|
91 |
} catch(ClassNotFoundException | NoSuchFieldException | IllegalAccessException e) { |
9698
8b66cd6c5ebc
7036199: Adding a notification to the implementation of GarbageCollectorMXBeans
fparain
parents:
5506
diff
changeset
|
92 |
return null; |
8b66cd6c5ebc
7036199: Adding a notification to the implementation of GarbageCollectorMXBeans
fparain
parents:
5506
diff
changeset
|
93 |
} |
8b66cd6c5ebc
7036199: Adding a notification to the implementation of GarbageCollectorMXBeans
fparain
parents:
5506
diff
changeset
|
94 |
} |
8b66cd6c5ebc
7036199: Adding a notification to the implementation of GarbageCollectorMXBeans
fparain
parents:
5506
diff
changeset
|
95 |
}); |
8b66cd6c5ebc
7036199: Adding a notification to the implementation of GarbageCollectorMXBeans
fparain
parents:
5506
diff
changeset
|
96 |
GcInfoCompositeData gcicd = |
8b66cd6c5ebc
7036199: Adding a notification to the implementation of GarbageCollectorMXBeans
fparain
parents:
5506
diff
changeset
|
97 |
new GcInfoCompositeData(info,builder,extAttr); |
8b66cd6c5ebc
7036199: Adding a notification to the implementation of GarbageCollectorMXBeans
fparain
parents:
5506
diff
changeset
|
98 |
return gcicd.getCompositeData(); |
8b66cd6c5ebc
7036199: Adding a notification to the implementation of GarbageCollectorMXBeans
fparain
parents:
5506
diff
changeset
|
99 |
} |
8b66cd6c5ebc
7036199: Adding a notification to the implementation of GarbageCollectorMXBeans
fparain
parents:
5506
diff
changeset
|
100 |
|
2 | 101 |
protected CompositeData getCompositeData() { |
102 |
// CONTENTS OF THIS ARRAY MUST BE SYNCHRONIZED WITH |
|
103 |
// baseGcInfoItemNames! |
|
104 |
final Object[] baseGcInfoItemValues; |
|
105 |
||
106 |
try { |
|
107 |
baseGcInfoItemValues = new Object[] { |
|
108 |
new Long(info.getId()), |
|
109 |
new Long(info.getStartTime()), |
|
110 |
new Long(info.getEndTime()), |
|
111 |
new Long(info.getDuration()), |
|
112 |
memoryUsageMapType.toOpenTypeData(info.getMemoryUsageBeforeGc()), |
|
113 |
memoryUsageMapType.toOpenTypeData(info.getMemoryUsageAfterGc()), |
|
114 |
}; |
|
115 |
} catch (OpenDataException e) { |
|
116 |
// Should never reach here |
|
401
ef01e0dccd63
6610094: Add generic support for platform MXBeans of any type (also fixed 6681031)
mchung
parents:
2
diff
changeset
|
117 |
throw new AssertionError(e); |
2 | 118 |
} |
119 |
||
120 |
// Get the item values for the extension attributes |
|
121 |
final int gcExtItemCount = builder.getGcExtItemCount(); |
|
122 |
if (gcExtItemCount == 0 && |
|
123 |
gcExtItemValues != null && gcExtItemValues.length != 0) { |
|
401
ef01e0dccd63
6610094: Add generic support for platform MXBeans of any type (also fixed 6681031)
mchung
parents:
2
diff
changeset
|
124 |
throw new AssertionError("Unexpected Gc Extension Item Values"); |
2 | 125 |
} |
126 |
||
127 |
if (gcExtItemCount > 0 && (gcExtItemValues == null || |
|
128 |
gcExtItemCount != gcExtItemValues.length)) { |
|
401
ef01e0dccd63
6610094: Add generic support for platform MXBeans of any type (also fixed 6681031)
mchung
parents:
2
diff
changeset
|
129 |
throw new AssertionError("Unmatched Gc Extension Item Values"); |
2 | 130 |
} |
131 |
||
132 |
Object[] values = new Object[baseGcInfoItemValues.length + |
|
133 |
gcExtItemCount]; |
|
134 |
System.arraycopy(baseGcInfoItemValues, 0, values, 0, |
|
135 |
baseGcInfoItemValues.length); |
|
136 |
||
137 |
if (gcExtItemCount > 0) { |
|
138 |
System.arraycopy(gcExtItemValues, 0, values, |
|
139 |
baseGcInfoItemValues.length, gcExtItemCount); |
|
140 |
} |
|
141 |
||
142 |
try { |
|
143 |
return new CompositeDataSupport(builder.getGcInfoCompositeType(), |
|
144 |
builder.getItemNames(), |
|
145 |
values); |
|
146 |
} catch (OpenDataException e) { |
|
147 |
// Should never reach here |
|
401
ef01e0dccd63
6610094: Add generic support for platform MXBeans of any type (also fixed 6681031)
mchung
parents:
2
diff
changeset
|
148 |
throw new AssertionError(e); |
2 | 149 |
} |
150 |
} |
|
151 |
||
152 |
private static final String ID = "id"; |
|
153 |
private static final String START_TIME = "startTime"; |
|
154 |
private static final String END_TIME = "endTime"; |
|
155 |
private static final String DURATION = "duration"; |
|
156 |
private static final String MEMORY_USAGE_BEFORE_GC = "memoryUsageBeforeGc"; |
|
157 |
private static final String MEMORY_USAGE_AFTER_GC = "memoryUsageAfterGc"; |
|
158 |
||
159 |
private static final String[] baseGcInfoItemNames = { |
|
160 |
ID, |
|
161 |
START_TIME, |
|
162 |
END_TIME, |
|
163 |
DURATION, |
|
164 |
MEMORY_USAGE_BEFORE_GC, |
|
165 |
MEMORY_USAGE_AFTER_GC, |
|
166 |
}; |
|
167 |
||
168 |
||
169 |
private static MappedMXBeanType memoryUsageMapType; |
|
170 |
static { |
|
171 |
try { |
|
172 |
Method m = GcInfo.class.getMethod("getMemoryUsageBeforeGc"); |
|
173 |
memoryUsageMapType = |
|
174 |
MappedMXBeanType.getMappedType(m.getGenericReturnType()); |
|
11530
a9d059c15b80
7117570: Warnings in sun.mangement.* and its subpackages
mchung
parents:
9698
diff
changeset
|
175 |
} catch (NoSuchMethodException | OpenDataException e) { |
2 | 176 |
// Should never reach here |
401
ef01e0dccd63
6610094: Add generic support for platform MXBeans of any type (also fixed 6681031)
mchung
parents:
2
diff
changeset
|
177 |
throw new AssertionError(e); |
2 | 178 |
} |
179 |
} |
|
180 |
||
181 |
static String[] getBaseGcInfoItemNames() { |
|
182 |
return baseGcInfoItemNames; |
|
183 |
} |
|
184 |
||
185 |
private static OpenType[] baseGcInfoItemTypes = null; |
|
186 |
static synchronized OpenType[] getBaseGcInfoItemTypes() { |
|
187 |
if (baseGcInfoItemTypes == null) { |
|
188 |
OpenType<?> memoryUsageOpenType = memoryUsageMapType.getOpenType(); |
|
11530
a9d059c15b80
7117570: Warnings in sun.mangement.* and its subpackages
mchung
parents:
9698
diff
changeset
|
189 |
baseGcInfoItemTypes = new OpenType<?>[] { |
2 | 190 |
SimpleType.LONG, |
191 |
SimpleType.LONG, |
|
192 |
SimpleType.LONG, |
|
193 |
SimpleType.LONG, |
|
194 |
||
195 |
memoryUsageOpenType, |
|
196 |
memoryUsageOpenType, |
|
197 |
}; |
|
198 |
} |
|
199 |
return baseGcInfoItemTypes; |
|
200 |
} |
|
201 |
||
202 |
public static long getId(CompositeData cd) { |
|
203 |
return getLong(cd, ID); |
|
204 |
} |
|
205 |
public static long getStartTime(CompositeData cd) { |
|
206 |
return getLong(cd, START_TIME); |
|
207 |
} |
|
208 |
public static long getEndTime(CompositeData cd) { |
|
209 |
return getLong(cd, END_TIME); |
|
210 |
} |
|
211 |
||
212 |
public static Map<String, MemoryUsage> |
|
213 |
getMemoryUsageBeforeGc(CompositeData cd) { |
|
214 |
try { |
|
215 |
TabularData td = (TabularData) cd.get(MEMORY_USAGE_BEFORE_GC); |
|
216 |
return cast(memoryUsageMapType.toJavaTypeData(td)); |
|
11530
a9d059c15b80
7117570: Warnings in sun.mangement.* and its subpackages
mchung
parents:
9698
diff
changeset
|
217 |
} catch (InvalidObjectException | OpenDataException e) { |
2 | 218 |
// Should never reach here |
401
ef01e0dccd63
6610094: Add generic support for platform MXBeans of any type (also fixed 6681031)
mchung
parents:
2
diff
changeset
|
219 |
throw new AssertionError(e); |
2 | 220 |
} |
221 |
} |
|
222 |
||
223 |
@SuppressWarnings("unchecked") |
|
224 |
public static Map<String, MemoryUsage> cast(Object x) { |
|
225 |
return (Map<String, MemoryUsage>) x; |
|
226 |
} |
|
227 |
public static Map<String, MemoryUsage> |
|
228 |
getMemoryUsageAfterGc(CompositeData cd) { |
|
229 |
try { |
|
230 |
TabularData td = (TabularData) cd.get(MEMORY_USAGE_AFTER_GC); |
|
231 |
//return (Map<String,MemoryUsage>) |
|
232 |
return cast(memoryUsageMapType.toJavaTypeData(td)); |
|
11530
a9d059c15b80
7117570: Warnings in sun.mangement.* and its subpackages
mchung
parents:
9698
diff
changeset
|
233 |
} catch (InvalidObjectException | OpenDataException e) { |
2 | 234 |
// Should never reach here |
401
ef01e0dccd63
6610094: Add generic support for platform MXBeans of any type (also fixed 6681031)
mchung
parents:
2
diff
changeset
|
235 |
throw new AssertionError(e); |
2 | 236 |
} |
237 |
} |
|
238 |
||
239 |
/** |
|
240 |
* Returns true if the input CompositeData has the expected |
|
241 |
* CompositeType (i.e. contain all attributes with expected |
|
242 |
* names and types). Otherwise, return false. |
|
243 |
*/ |
|
244 |
public static void validateCompositeData(CompositeData cd) { |
|
245 |
if (cd == null) { |
|
246 |
throw new NullPointerException("Null CompositeData"); |
|
247 |
} |
|
248 |
||
249 |
if (!isTypeMatched(getBaseGcInfoCompositeType(), |
|
250 |
cd.getCompositeType())) { |
|
251 |
throw new IllegalArgumentException( |
|
252 |
"Unexpected composite type for GcInfo"); |
|
253 |
} |
|
254 |
} |
|
255 |
||
256 |
// This is only used for validation. |
|
257 |
private static CompositeType baseGcInfoCompositeType = null; |
|
9698
8b66cd6c5ebc
7036199: Adding a notification to the implementation of GarbageCollectorMXBeans
fparain
parents:
5506
diff
changeset
|
258 |
static synchronized CompositeType getBaseGcInfoCompositeType() { |
2 | 259 |
if (baseGcInfoCompositeType == null) { |
260 |
try { |
|
261 |
baseGcInfoCompositeType = |
|
262 |
new CompositeType("sun.management.BaseGcInfoCompositeType", |
|
263 |
"CompositeType for Base GcInfo", |
|
264 |
getBaseGcInfoItemNames(), |
|
265 |
getBaseGcInfoItemNames(), |
|
266 |
getBaseGcInfoItemTypes()); |
|
267 |
} catch (OpenDataException e) { |
|
268 |
// shouldn't reach here |
|
269 |
throw Util.newException(e); |
|
270 |
} |
|
271 |
} |
|
272 |
return baseGcInfoCompositeType; |
|
273 |
} |
|
274 |
||
4173
d01972926813
6895875: Missing serialVersionUID in sun.management classes
mchung
parents:
715
diff
changeset
|
275 |
private static final long serialVersionUID = -5716428894085882742L; |
2 | 276 |
} |