author | mchung |
Thu, 10 Apr 2008 10:47:13 -0700 | |
changeset 401 | ef01e0dccd63 |
parent 2 | 90ce3da70b43 |
child 715 | f16baef3a20e |
permissions | -rw-r--r-- |
2 | 1 |
/* |
2 |
* Copyright 2003-2005 Sun Microsystems, Inc. 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. Sun designates this |
|
8 |
* particular file as subject to the "Classpath" exception as provided |
|
9 |
* by Sun 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 Sun Microsystems, Inc., 4150 Network Circle, Santa Clara, |
|
22 |
* CA 95054 USA or visit www.sun.com if you need additional information or |
|
23 |
* have any questions. |
|
24 |
*/ |
|
25 |
||
26 |
package sun.management; |
|
27 |
||
401
ef01e0dccd63
6610094: Add generic support for platform MXBeans of any type (also fixed 6681031)
mchung
parents:
2
diff
changeset
|
28 |
import java.lang.management.ManagementFactory; |
2 | 29 |
import java.lang.management.MemoryMXBean; |
30 |
import java.lang.management.MemoryUsage; |
|
31 |
import java.lang.management.MemoryNotificationInfo; |
|
32 |
import java.lang.management.MemoryManagerMXBean; |
|
33 |
import java.lang.management.MemoryPoolMXBean; |
|
34 |
import javax.management.ObjectName; |
|
35 |
import javax.management.MBeanNotificationInfo; |
|
36 |
import javax.management.Notification; |
|
37 |
import javax.management.NotificationEmitter; |
|
38 |
import javax.management.NotificationFilter; |
|
39 |
import javax.management.NotificationListener; |
|
40 |
import javax.management.ListenerNotFoundException; |
|
41 |
import javax.management.openmbean.CompositeData; |
|
42 |
import java.util.List; |
|
43 |
import java.util.ArrayList; |
|
44 |
import java.util.ListIterator; |
|
45 |
import java.util.Collections; |
|
46 |
||
47 |
/** |
|
48 |
* Implementation class for the memory subsystem. |
|
49 |
* Standard and committed hotspot-specific metrics if any. |
|
50 |
* |
|
51 |
* ManagementFactory.getMemoryMXBean() returns an instance |
|
52 |
* of this class. |
|
53 |
*/ |
|
54 |
class MemoryImpl extends NotificationEmitterSupport |
|
55 |
implements MemoryMXBean { |
|
56 |
||
57 |
private final VMManagement jvm; |
|
58 |
||
59 |
private static MemoryPoolMXBean[] pools = null; |
|
60 |
private static MemoryManagerMXBean[] mgrs = null; |
|
61 |
||
62 |
/** |
|
63 |
* Constructor of MemoryImpl class |
|
64 |
*/ |
|
65 |
MemoryImpl(VMManagement vm) { |
|
66 |
this.jvm = vm; |
|
67 |
} |
|
68 |
||
69 |
public int getObjectPendingFinalizationCount() { |
|
70 |
return sun.misc.VM.getFinalRefCount(); |
|
71 |
} |
|
72 |
||
73 |
public void gc() { |
|
74 |
Runtime.getRuntime().gc(); |
|
75 |
} |
|
76 |
||
77 |
// Need to make a VM call to get coherent value |
|
78 |
public MemoryUsage getHeapMemoryUsage() { |
|
79 |
return getMemoryUsage0(true); |
|
80 |
} |
|
81 |
||
82 |
public MemoryUsage getNonHeapMemoryUsage() { |
|
83 |
return getMemoryUsage0(false); |
|
84 |
} |
|
85 |
||
86 |
public boolean isVerbose() { |
|
87 |
return jvm.getVerboseGC(); |
|
88 |
} |
|
89 |
||
90 |
public void setVerbose(boolean value) { |
|
401
ef01e0dccd63
6610094: Add generic support for platform MXBeans of any type (also fixed 6681031)
mchung
parents:
2
diff
changeset
|
91 |
Util.checkControlAccess(); |
2 | 92 |
|
93 |
setVerboseGC(value); |
|
94 |
} |
|
95 |
||
96 |
// The current Hotspot implementation does not support |
|
97 |
// dynamically add or remove memory pools & managers. |
|
98 |
static synchronized MemoryPoolMXBean[] getMemoryPools() { |
|
99 |
if (pools == null) { |
|
100 |
pools = getMemoryPools0(); |
|
101 |
} |
|
102 |
return pools; |
|
103 |
} |
|
104 |
static synchronized MemoryManagerMXBean[] getMemoryManagers() { |
|
105 |
if (mgrs == null) { |
|
106 |
mgrs = getMemoryManagers0(); |
|
107 |
} |
|
108 |
return mgrs; |
|
109 |
} |
|
110 |
private static native MemoryPoolMXBean[] getMemoryPools0(); |
|
111 |
private static native MemoryManagerMXBean[] getMemoryManagers0(); |
|
112 |
private native MemoryUsage getMemoryUsage0(boolean heap); |
|
113 |
private native void setVerboseGC(boolean value); |
|
114 |
||
115 |
private final static String notifName = |
|
116 |
"javax.management.Notification"; |
|
117 |
private final static String[] notifTypes = { |
|
118 |
MemoryNotificationInfo.MEMORY_THRESHOLD_EXCEEDED, |
|
119 |
MemoryNotificationInfo.MEMORY_COLLECTION_THRESHOLD_EXCEEDED |
|
120 |
}; |
|
121 |
private final static String[] notifMsgs = { |
|
122 |
"Memory usage exceeds usage threshold", |
|
123 |
"Memory usage exceeds collection usage threshold" |
|
124 |
}; |
|
125 |
||
126 |
private MBeanNotificationInfo[] notifInfo = null; |
|
127 |
public MBeanNotificationInfo[] getNotificationInfo() { |
|
128 |
synchronized (this) { |
|
129 |
if (notifInfo == null) { |
|
130 |
notifInfo = new MBeanNotificationInfo[1]; |
|
131 |
notifInfo[0] = new MBeanNotificationInfo(notifTypes, |
|
132 |
notifName, |
|
133 |
"Memory Notification"); |
|
134 |
} |
|
135 |
} |
|
136 |
return notifInfo; |
|
137 |
} |
|
138 |
||
139 |
private static String getNotifMsg(String notifType) { |
|
140 |
for (int i = 0; i < notifTypes.length; i++) { |
|
141 |
if (notifType == notifTypes[i]) { |
|
142 |
return notifMsgs[i]; |
|
143 |
} |
|
144 |
} |
|
145 |
return "Unknown message"; |
|
146 |
} |
|
147 |
||
148 |
private static long seqNumber = 0; |
|
149 |
private static long getNextSeqNumber() { |
|
150 |
return ++seqNumber; |
|
151 |
} |
|
152 |
||
153 |
static void createNotification(String notifType, |
|
154 |
String poolName, |
|
155 |
MemoryUsage usage, |
|
156 |
long count) { |
|
157 |
MemoryImpl mbean = (MemoryImpl) ManagementFactory.getMemoryMXBean(); |
|
158 |
if (!mbean.hasListeners()) { |
|
159 |
// if no listener is registered. |
|
160 |
return; |
|
161 |
} |
|
162 |
long timestamp = System.currentTimeMillis(); |
|
163 |
String msg = getNotifMsg(notifType); |
|
164 |
Notification notif = new Notification(notifType, |
|
401
ef01e0dccd63
6610094: Add generic support for platform MXBeans of any type (also fixed 6681031)
mchung
parents:
2
diff
changeset
|
165 |
mbean.getObjectName(), |
2 | 166 |
getNextSeqNumber(), |
167 |
timestamp, |
|
168 |
msg); |
|
169 |
MemoryNotificationInfo info = |
|
170 |
new MemoryNotificationInfo(poolName, |
|
171 |
usage, |
|
172 |
count); |
|
173 |
CompositeData cd = |
|
174 |
MemoryNotifInfoCompositeData.toCompositeData(info); |
|
175 |
notif.setUserData(cd); |
|
176 |
mbean.sendNotification(notif); |
|
177 |
} |
|
178 |
||
401
ef01e0dccd63
6610094: Add generic support for platform MXBeans of any type (also fixed 6681031)
mchung
parents:
2
diff
changeset
|
179 |
public ObjectName getObjectName() { |
ef01e0dccd63
6610094: Add generic support for platform MXBeans of any type (also fixed 6681031)
mchung
parents:
2
diff
changeset
|
180 |
return Util.newObjectName(ManagementFactory.MEMORY_MXBEAN_NAME); |
ef01e0dccd63
6610094: Add generic support for platform MXBeans of any type (also fixed 6681031)
mchung
parents:
2
diff
changeset
|
181 |
} |
ef01e0dccd63
6610094: Add generic support for platform MXBeans of any type (also fixed 6681031)
mchung
parents:
2
diff
changeset
|
182 |
|
2 | 183 |
} |