author | sjiang |
Fri, 17 Apr 2015 09:40:02 +0200 | |
changeset 30355 | e37c7eba132f |
parent 25859 | jdk/src/java.management/share/classes/sun/management/GcInfoCompositeData.java@3317bb8137f4 |
permissions | -rw-r--r-- |
2 | 1 |
/* |
30355
e37c7eba132f
8042901: Allow com.sun.management to be in a different module to java.lang.management
sjiang
parents:
25859
diff
changeset
|
2 |
* Copyright (c) 2004, 2015, 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 |
||
30355
e37c7eba132f
8042901: Allow com.sun.management to be in a different module to java.lang.management
sjiang
parents:
25859
diff
changeset
|
26 |
package com.sun.management.internal; |
2 | 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.Map; |
32 |
import java.io.InvalidObjectException; |
|
33 |
import javax.management.openmbean.CompositeType; |
|
34 |
import javax.management.openmbean.CompositeData; |
|
35 |
import javax.management.openmbean.CompositeDataSupport; |
|
36 |
import javax.management.openmbean.TabularData; |
|
37 |
import javax.management.openmbean.SimpleType; |
|
38 |
import javax.management.openmbean.OpenType; |
|
39 |
import javax.management.openmbean.OpenDataException; |
|
40 |
import com.sun.management.GcInfo; |
|
9698
8b66cd6c5ebc
7036199: Adding a notification to the implementation of GarbageCollectorMXBeans
fparain
parents:
5506
diff
changeset
|
41 |
import java.security.AccessController; |
8b66cd6c5ebc
7036199: Adding a notification to the implementation of GarbageCollectorMXBeans
fparain
parents:
5506
diff
changeset
|
42 |
import java.security.PrivilegedAction; |
30355
e37c7eba132f
8042901: Allow com.sun.management to be in a different module to java.lang.management
sjiang
parents:
25859
diff
changeset
|
43 |
import sun.management.LazyCompositeData; |
e37c7eba132f
8042901: Allow com.sun.management to be in a different module to java.lang.management
sjiang
parents:
25859
diff
changeset
|
44 |
import static sun.management.LazyCompositeData.getLong; |
e37c7eba132f
8042901: Allow com.sun.management to be in a different module to java.lang.management
sjiang
parents:
25859
diff
changeset
|
45 |
import sun.management.MappedMXBeanType; |
e37c7eba132f
8042901: Allow com.sun.management to be in a different module to java.lang.management
sjiang
parents:
25859
diff
changeset
|
46 |
import sun.management.Util; |
2 | 47 |
|
48 |
/** |
|
49 |
* A CompositeData for GcInfo for the local management support. |
|
50 |
* This class avoids the performance penalty paid to the |
|
51 |
* construction of a CompositeData use in the local case. |
|
52 |
*/ |
|
53 |
public class GcInfoCompositeData extends LazyCompositeData { |
|
54 |
private final GcInfo info; |
|
55 |
private final GcInfoBuilder builder; |
|
56 |
private final Object[] gcExtItemValues; |
|
57 |
||
58 |
public GcInfoCompositeData(GcInfo info, |
|
59 |
GcInfoBuilder builder, |
|
60 |
Object[] gcExtItemValues) { |
|
61 |
this.info = info; |
|
62 |
this.builder = builder; |
|
63 |
this.gcExtItemValues = gcExtItemValues; |
|
64 |
} |
|
65 |
||
66 |
public GcInfo getGcInfo() { |
|
67 |
return info; |
|
68 |
} |
|
69 |
||
9698
8b66cd6c5ebc
7036199: Adding a notification to the implementation of GarbageCollectorMXBeans
fparain
parents:
5506
diff
changeset
|
70 |
public static CompositeData toCompositeData(final GcInfo info) { |
8b66cd6c5ebc
7036199: Adding a notification to the implementation of GarbageCollectorMXBeans
fparain
parents:
5506
diff
changeset
|
71 |
final GcInfoBuilder builder = AccessController.doPrivileged (new PrivilegedAction<GcInfoBuilder>() { |
8b66cd6c5ebc
7036199: Adding a notification to the implementation of GarbageCollectorMXBeans
fparain
parents:
5506
diff
changeset
|
72 |
public GcInfoBuilder run() { |
8b66cd6c5ebc
7036199: Adding a notification to the implementation of GarbageCollectorMXBeans
fparain
parents:
5506
diff
changeset
|
73 |
try { |
25405
aa7f539c3569
8049820: Fix raw and unchecked lint warnings in sun.management
darcy
parents:
25186
diff
changeset
|
74 |
Class<?> cl = Class.forName("com.sun.management.GcInfo"); |
9698
8b66cd6c5ebc
7036199: Adding a notification to the implementation of GarbageCollectorMXBeans
fparain
parents:
5506
diff
changeset
|
75 |
Field f = cl.getDeclaredField("builder"); |
8b66cd6c5ebc
7036199: Adding a notification to the implementation of GarbageCollectorMXBeans
fparain
parents:
5506
diff
changeset
|
76 |
f.setAccessible(true); |
8b66cd6c5ebc
7036199: Adding a notification to the implementation of GarbageCollectorMXBeans
fparain
parents:
5506
diff
changeset
|
77 |
return (GcInfoBuilder)f.get(info); |
11530
a9d059c15b80
7117570: Warnings in sun.mangement.* and its subpackages
mchung
parents:
9698
diff
changeset
|
78 |
} catch(ClassNotFoundException | NoSuchFieldException | IllegalAccessException e) { |
9698
8b66cd6c5ebc
7036199: Adding a notification to the implementation of GarbageCollectorMXBeans
fparain
parents:
5506
diff
changeset
|
79 |
return null; |
8b66cd6c5ebc
7036199: Adding a notification to the implementation of GarbageCollectorMXBeans
fparain
parents:
5506
diff
changeset
|
80 |
} |
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 |
final Object[] extAttr = AccessController.doPrivileged (new PrivilegedAction<Object[]>() { |
8b66cd6c5ebc
7036199: Adding a notification to the implementation of GarbageCollectorMXBeans
fparain
parents:
5506
diff
changeset
|
84 |
public Object[] run() { |
8b66cd6c5ebc
7036199: Adding a notification to the implementation of GarbageCollectorMXBeans
fparain
parents:
5506
diff
changeset
|
85 |
try { |
25405
aa7f539c3569
8049820: Fix raw and unchecked lint warnings in sun.management
darcy
parents:
25186
diff
changeset
|
86 |
Class<?> cl = Class.forName("com.sun.management.GcInfo"); |
9698
8b66cd6c5ebc
7036199: Adding a notification to the implementation of GarbageCollectorMXBeans
fparain
parents:
5506
diff
changeset
|
87 |
Field f = cl.getDeclaredField("extAttributes"); |
8b66cd6c5ebc
7036199: Adding a notification to the implementation of GarbageCollectorMXBeans
fparain
parents:
5506
diff
changeset
|
88 |
f.setAccessible(true); |
8b66cd6c5ebc
7036199: Adding a notification to the implementation of GarbageCollectorMXBeans
fparain
parents:
5506
diff
changeset
|
89 |
return (Object[])f.get(info); |
11530
a9d059c15b80
7117570: Warnings in sun.mangement.* and its subpackages
mchung
parents:
9698
diff
changeset
|
90 |
} catch(ClassNotFoundException | NoSuchFieldException | IllegalAccessException e) { |
9698
8b66cd6c5ebc
7036199: Adding a notification to the implementation of GarbageCollectorMXBeans
fparain
parents:
5506
diff
changeset
|
91 |
return null; |
8b66cd6c5ebc
7036199: Adding a notification to the implementation of GarbageCollectorMXBeans
fparain
parents:
5506
diff
changeset
|
92 |
} |
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 |
GcInfoCompositeData gcicd = |
8b66cd6c5ebc
7036199: Adding a notification to the implementation of GarbageCollectorMXBeans
fparain
parents:
5506
diff
changeset
|
96 |
new GcInfoCompositeData(info,builder,extAttr); |
8b66cd6c5ebc
7036199: Adding a notification to the implementation of GarbageCollectorMXBeans
fparain
parents:
5506
diff
changeset
|
97 |
return gcicd.getCompositeData(); |
8b66cd6c5ebc
7036199: Adding a notification to the implementation of GarbageCollectorMXBeans
fparain
parents:
5506
diff
changeset
|
98 |
} |
8b66cd6c5ebc
7036199: Adding a notification to the implementation of GarbageCollectorMXBeans
fparain
parents:
5506
diff
changeset
|
99 |
|
2 | 100 |
protected CompositeData getCompositeData() { |
101 |
// CONTENTS OF THIS ARRAY MUST BE SYNCHRONIZED WITH |
|
102 |
// baseGcInfoItemNames! |
|
103 |
final Object[] baseGcInfoItemValues; |
|
104 |
||
105 |
try { |
|
106 |
baseGcInfoItemValues = new Object[] { |
|
25186
63e1a2ec30f5
8048267: Replace uses of 'new Long()' with appropriate alternative across core classes
prappo
parents:
14342
diff
changeset
|
107 |
info.getId(), |
63e1a2ec30f5
8048267: Replace uses of 'new Long()' with appropriate alternative across core classes
prappo
parents:
14342
diff
changeset
|
108 |
info.getStartTime(), |
63e1a2ec30f5
8048267: Replace uses of 'new Long()' with appropriate alternative across core classes
prappo
parents:
14342
diff
changeset
|
109 |
info.getEndTime(), |
63e1a2ec30f5
8048267: Replace uses of 'new Long()' with appropriate alternative across core classes
prappo
parents:
14342
diff
changeset
|
110 |
info.getDuration(), |
2 | 111 |
memoryUsageMapType.toOpenTypeData(info.getMemoryUsageBeforeGc()), |
112 |
memoryUsageMapType.toOpenTypeData(info.getMemoryUsageAfterGc()), |
|
113 |
}; |
|
114 |
} catch (OpenDataException e) { |
|
115 |
// Should never reach here |
|
401
ef01e0dccd63
6610094: Add generic support for platform MXBeans of any type (also fixed 6681031)
mchung
parents:
2
diff
changeset
|
116 |
throw new AssertionError(e); |
2 | 117 |
} |
118 |
||
119 |
// Get the item values for the extension attributes |
|
120 |
final int gcExtItemCount = builder.getGcExtItemCount(); |
|
121 |
if (gcExtItemCount == 0 && |
|
122 |
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
|
123 |
throw new AssertionError("Unexpected Gc Extension Item Values"); |
2 | 124 |
} |
125 |
||
126 |
if (gcExtItemCount > 0 && (gcExtItemValues == null || |
|
127 |
gcExtItemCount != gcExtItemValues.length)) { |
|
401
ef01e0dccd63
6610094: Add generic support for platform MXBeans of any type (also fixed 6681031)
mchung
parents:
2
diff
changeset
|
128 |
throw new AssertionError("Unmatched Gc Extension Item Values"); |
2 | 129 |
} |
130 |
||
131 |
Object[] values = new Object[baseGcInfoItemValues.length + |
|
132 |
gcExtItemCount]; |
|
133 |
System.arraycopy(baseGcInfoItemValues, 0, values, 0, |
|
134 |
baseGcInfoItemValues.length); |
|
135 |
||
136 |
if (gcExtItemCount > 0) { |
|
137 |
System.arraycopy(gcExtItemValues, 0, values, |
|
138 |
baseGcInfoItemValues.length, gcExtItemCount); |
|
139 |
} |
|
140 |
||
141 |
try { |
|
142 |
return new CompositeDataSupport(builder.getGcInfoCompositeType(), |
|
143 |
builder.getItemNames(), |
|
144 |
values); |
|
145 |
} catch (OpenDataException e) { |
|
146 |
// Should never reach here |
|
401
ef01e0dccd63
6610094: Add generic support for platform MXBeans of any type (also fixed 6681031)
mchung
parents:
2
diff
changeset
|
147 |
throw new AssertionError(e); |
2 | 148 |
} |
149 |
} |
|
150 |
||
151 |
private static final String ID = "id"; |
|
152 |
private static final String START_TIME = "startTime"; |
|
153 |
private static final String END_TIME = "endTime"; |
|
154 |
private static final String DURATION = "duration"; |
|
155 |
private static final String MEMORY_USAGE_BEFORE_GC = "memoryUsageBeforeGc"; |
|
156 |
private static final String MEMORY_USAGE_AFTER_GC = "memoryUsageAfterGc"; |
|
157 |
||
158 |
private static final String[] baseGcInfoItemNames = { |
|
159 |
ID, |
|
160 |
START_TIME, |
|
161 |
END_TIME, |
|
162 |
DURATION, |
|
163 |
MEMORY_USAGE_BEFORE_GC, |
|
164 |
MEMORY_USAGE_AFTER_GC, |
|
165 |
}; |
|
166 |
||
167 |
||
168 |
private static MappedMXBeanType memoryUsageMapType; |
|
169 |
static { |
|
170 |
try { |
|
171 |
Method m = GcInfo.class.getMethod("getMemoryUsageBeforeGc"); |
|
172 |
memoryUsageMapType = |
|
173 |
MappedMXBeanType.getMappedType(m.getGenericReturnType()); |
|
11530
a9d059c15b80
7117570: Warnings in sun.mangement.* and its subpackages
mchung
parents:
9698
diff
changeset
|
174 |
} catch (NoSuchMethodException | OpenDataException e) { |
2 | 175 |
// Should never reach here |
401
ef01e0dccd63
6610094: Add generic support for platform MXBeans of any type (also fixed 6681031)
mchung
parents:
2
diff
changeset
|
176 |
throw new AssertionError(e); |
2 | 177 |
} |
178 |
} |
|
179 |
||
180 |
static String[] getBaseGcInfoItemNames() { |
|
181 |
return baseGcInfoItemNames; |
|
182 |
} |
|
183 |
||
25405
aa7f539c3569
8049820: Fix raw and unchecked lint warnings in sun.management
darcy
parents:
25186
diff
changeset
|
184 |
private static OpenType<?>[] baseGcInfoItemTypes = null; |
aa7f539c3569
8049820: Fix raw and unchecked lint warnings in sun.management
darcy
parents:
25186
diff
changeset
|
185 |
static synchronized OpenType<?>[] getBaseGcInfoItemTypes() { |
2 | 186 |
if (baseGcInfoItemTypes == null) { |
187 |
OpenType<?> memoryUsageOpenType = memoryUsageMapType.getOpenType(); |
|
11530
a9d059c15b80
7117570: Warnings in sun.mangement.* and its subpackages
mchung
parents:
9698
diff
changeset
|
188 |
baseGcInfoItemTypes = new OpenType<?>[] { |
2 | 189 |
SimpleType.LONG, |
190 |
SimpleType.LONG, |
|
191 |
SimpleType.LONG, |
|
192 |
SimpleType.LONG, |
|
193 |
||
194 |
memoryUsageOpenType, |
|
195 |
memoryUsageOpenType, |
|
196 |
}; |
|
197 |
} |
|
198 |
return baseGcInfoItemTypes; |
|
199 |
} |
|
200 |
||
201 |
public static long getId(CompositeData cd) { |
|
202 |
return getLong(cd, ID); |
|
203 |
} |
|
204 |
public static long getStartTime(CompositeData cd) { |
|
205 |
return getLong(cd, START_TIME); |
|
206 |
} |
|
207 |
public static long getEndTime(CompositeData cd) { |
|
208 |
return getLong(cd, END_TIME); |
|
209 |
} |
|
210 |
||
211 |
public static Map<String, MemoryUsage> |
|
212 |
getMemoryUsageBeforeGc(CompositeData cd) { |
|
213 |
try { |
|
214 |
TabularData td = (TabularData) cd.get(MEMORY_USAGE_BEFORE_GC); |
|
215 |
return cast(memoryUsageMapType.toJavaTypeData(td)); |
|
11530
a9d059c15b80
7117570: Warnings in sun.mangement.* and its subpackages
mchung
parents:
9698
diff
changeset
|
216 |
} catch (InvalidObjectException | OpenDataException e) { |
2 | 217 |
// Should never reach here |
401
ef01e0dccd63
6610094: Add generic support for platform MXBeans of any type (also fixed 6681031)
mchung
parents:
2
diff
changeset
|
218 |
throw new AssertionError(e); |
2 | 219 |
} |
220 |
} |
|
221 |
||
222 |
@SuppressWarnings("unchecked") |
|
223 |
public static Map<String, MemoryUsage> cast(Object x) { |
|
224 |
return (Map<String, MemoryUsage>) x; |
|
225 |
} |
|
226 |
public static Map<String, MemoryUsage> |
|
227 |
getMemoryUsageAfterGc(CompositeData cd) { |
|
228 |
try { |
|
229 |
TabularData td = (TabularData) cd.get(MEMORY_USAGE_AFTER_GC); |
|
230 |
//return (Map<String,MemoryUsage>) |
|
231 |
return cast(memoryUsageMapType.toJavaTypeData(td)); |
|
11530
a9d059c15b80
7117570: Warnings in sun.mangement.* and its subpackages
mchung
parents:
9698
diff
changeset
|
232 |
} catch (InvalidObjectException | OpenDataException e) { |
2 | 233 |
// Should never reach here |
401
ef01e0dccd63
6610094: Add generic support for platform MXBeans of any type (also fixed 6681031)
mchung
parents:
2
diff
changeset
|
234 |
throw new AssertionError(e); |
2 | 235 |
} |
236 |
} |
|
237 |
||
238 |
/** |
|
239 |
* Returns true if the input CompositeData has the expected |
|
240 |
* CompositeType (i.e. contain all attributes with expected |
|
241 |
* names and types). Otherwise, return false. |
|
242 |
*/ |
|
243 |
public static void validateCompositeData(CompositeData cd) { |
|
244 |
if (cd == null) { |
|
245 |
throw new NullPointerException("Null CompositeData"); |
|
246 |
} |
|
247 |
||
248 |
if (!isTypeMatched(getBaseGcInfoCompositeType(), |
|
249 |
cd.getCompositeType())) { |
|
250 |
throw new IllegalArgumentException( |
|
251 |
"Unexpected composite type for GcInfo"); |
|
252 |
} |
|
253 |
} |
|
254 |
||
255 |
// This is only used for validation. |
|
256 |
private static CompositeType baseGcInfoCompositeType = null; |
|
9698
8b66cd6c5ebc
7036199: Adding a notification to the implementation of GarbageCollectorMXBeans
fparain
parents:
5506
diff
changeset
|
257 |
static synchronized CompositeType getBaseGcInfoCompositeType() { |
2 | 258 |
if (baseGcInfoCompositeType == null) { |
259 |
try { |
|
260 |
baseGcInfoCompositeType = |
|
261 |
new CompositeType("sun.management.BaseGcInfoCompositeType", |
|
262 |
"CompositeType for Base GcInfo", |
|
263 |
getBaseGcInfoItemNames(), |
|
264 |
getBaseGcInfoItemNames(), |
|
265 |
getBaseGcInfoItemTypes()); |
|
266 |
} catch (OpenDataException e) { |
|
267 |
// shouldn't reach here |
|
30355
e37c7eba132f
8042901: Allow com.sun.management to be in a different module to java.lang.management
sjiang
parents:
25859
diff
changeset
|
268 |
throw new RuntimeException(e); |
2 | 269 |
} |
270 |
} |
|
271 |
return baseGcInfoCompositeType; |
|
272 |
} |
|
273 |
||
4173
d01972926813
6895875: Missing serialVersionUID in sun.management classes
mchung
parents:
715
diff
changeset
|
274 |
private static final long serialVersionUID = -5716428894085882742L; |
2 | 275 |
} |