|
1 /* |
|
2 * Copyright (c) 2003, 2015, 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 com.sun.management.internal; |
|
27 |
|
28 import com.sun.management.GarbageCollectionNotificationInfo; |
|
29 import com.sun.management.GarbageCollectorMXBean; |
|
30 import com.sun.management.GcInfo; |
|
31 import java.lang.management.ManagementFactory; |
|
32 import java.lang.management.MemoryPoolMXBean; |
|
33 import java.util.List; |
|
34 import javax.management.ListenerNotFoundException; |
|
35 import javax.management.MBeanNotificationInfo; |
|
36 import javax.management.Notification; |
|
37 import javax.management.NotificationFilter; |
|
38 import javax.management.NotificationListener; |
|
39 import javax.management.openmbean.CompositeData; |
|
40 import sun.management.GarbageCollectorImpl; |
|
41 |
|
42 /** |
|
43 * Implementation class for the garbage collector. |
|
44 * Standard and committed hotspot-specific metrics if any. |
|
45 * |
|
46 * ManagementFactory.getGarbageCollectorMXBeans() returns a list |
|
47 * of instances of this class. |
|
48 */ |
|
49 public class GarbageCollectorExtImpl extends GarbageCollectorImpl |
|
50 implements GarbageCollectorMXBean { |
|
51 |
|
52 public GarbageCollectorExtImpl(String name) { |
|
53 super(name); |
|
54 } |
|
55 |
|
56 // The memory pools are static and won't be changed. |
|
57 // TODO: If the hotspot implementation begins to have pools |
|
58 // dynamically created and removed, this needs to be modified. |
|
59 private String[] poolNames = null; |
|
60 private synchronized String[] getAllPoolNames() { |
|
61 if (poolNames == null) { |
|
62 List<MemoryPoolMXBean> pools = ManagementFactory.getMemoryPoolMXBeans(); |
|
63 poolNames = new String[pools.size()]; |
|
64 int i = 0; |
|
65 for (MemoryPoolMXBean m : pools) { |
|
66 poolNames[i++] = m.getName(); |
|
67 } |
|
68 } |
|
69 return poolNames; |
|
70 } |
|
71 |
|
72 public GcInfo getLastGcInfo() { |
|
73 GcInfo info = getGcInfoBuilder().getLastGcInfo(); |
|
74 return info; |
|
75 } |
|
76 |
|
77 private final static String notifName = |
|
78 "javax.management.Notification"; |
|
79 |
|
80 private final static String[] gcNotifTypes = { |
|
81 GarbageCollectionNotificationInfo.GARBAGE_COLLECTION_NOTIFICATION |
|
82 }; |
|
83 |
|
84 private MBeanNotificationInfo[] notifInfo = null; |
|
85 public MBeanNotificationInfo[] getNotificationInfo() { |
|
86 synchronized (this) { |
|
87 if (notifInfo == null) { |
|
88 notifInfo = new MBeanNotificationInfo[1]; |
|
89 notifInfo[0] = new MBeanNotificationInfo(gcNotifTypes, |
|
90 notifName, |
|
91 "GC Notification"); |
|
92 } |
|
93 } |
|
94 return notifInfo; |
|
95 } |
|
96 |
|
97 private static long seqNumber = 0; |
|
98 private static long getNextSeqNumber() { |
|
99 return ++seqNumber; |
|
100 } |
|
101 |
|
102 protected void createGCNotification(long timestamp, |
|
103 String gcName, |
|
104 String gcAction, |
|
105 String gcCause, |
|
106 GcInfo gcInfo) { |
|
107 if (!hasListeners()) { |
|
108 return; |
|
109 } |
|
110 Notification notif = new Notification(GarbageCollectionNotificationInfo.GARBAGE_COLLECTION_NOTIFICATION, |
|
111 getObjectName(), |
|
112 getNextSeqNumber(), |
|
113 timestamp, |
|
114 gcName); |
|
115 GarbageCollectionNotificationInfo info = |
|
116 new GarbageCollectionNotificationInfo(gcName, |
|
117 gcAction, |
|
118 gcCause, |
|
119 gcInfo); |
|
120 |
|
121 CompositeData cd = |
|
122 GarbageCollectionNotifInfoCompositeData.toCompositeData(info); |
|
123 notif.setUserData(cd); |
|
124 sendNotification(notif); |
|
125 } |
|
126 |
|
127 public synchronized void addNotificationListener(NotificationListener listener, |
|
128 NotificationFilter filter, |
|
129 Object handback) |
|
130 { |
|
131 boolean before = hasListeners(); |
|
132 super.addNotificationListener(listener, filter, handback); |
|
133 boolean after = hasListeners(); |
|
134 if (!before && after) { |
|
135 setNotificationEnabled(this, true); |
|
136 } |
|
137 } |
|
138 |
|
139 public synchronized void removeNotificationListener(NotificationListener listener) |
|
140 throws ListenerNotFoundException { |
|
141 boolean before = hasListeners(); |
|
142 super.removeNotificationListener(listener); |
|
143 boolean after = hasListeners(); |
|
144 |
|
145 if (before && !after) { |
|
146 setNotificationEnabled(this,false); |
|
147 } |
|
148 } |
|
149 |
|
150 public synchronized void removeNotificationListener(NotificationListener listener, |
|
151 NotificationFilter filter, |
|
152 Object handback) |
|
153 throws ListenerNotFoundException |
|
154 { |
|
155 boolean before = hasListeners(); |
|
156 super.removeNotificationListener(listener,filter,handback); |
|
157 boolean after = hasListeners(); |
|
158 if (before && !after) { |
|
159 setNotificationEnabled(this,false); |
|
160 } |
|
161 } |
|
162 |
|
163 private GcInfoBuilder gcInfoBuilder; |
|
164 // Invoked also by the VM |
|
165 private synchronized GcInfoBuilder getGcInfoBuilder() { |
|
166 if(gcInfoBuilder == null) { |
|
167 gcInfoBuilder = new GcInfoBuilder(this, getAllPoolNames()); |
|
168 } |
|
169 return gcInfoBuilder; |
|
170 } |
|
171 |
|
172 private native void setNotificationEnabled(GarbageCollectorMXBean gc, |
|
173 boolean enabled); |
|
174 |
|
175 // Invoked by the VM |
|
176 private static java.lang.management.GarbageCollectorMXBean |
|
177 createGarbageCollector(String name, String type) { |
|
178 |
|
179 return new GarbageCollectorExtImpl(name); |
|
180 } |
|
181 } |