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