0N/A/*
4235N/A * Copyright (c) 2003, 2011, Oracle and/or its affiliates. All rights reserved.
0N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
0N/A *
0N/A * This code is free software; you can redistribute it and/or modify it
0N/A * under the terms of the GNU General Public License version 2 only, as
2362N/A * published by the Free Software Foundation. Oracle designates this
0N/A * particular file as subject to the "Classpath" exception as provided
2362N/A * by Oracle in the LICENSE file that accompanied this code.
0N/A *
0N/A * This code is distributed in the hope that it will be useful, but WITHOUT
0N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
0N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
0N/A * version 2 for more details (a copy is included in the LICENSE file that
0N/A * accompanied this code).
0N/A *
0N/A * You should have received a copy of the GNU General Public License version
0N/A * 2 along with this work; if not, write to the Free Software Foundation,
0N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
0N/A *
2362N/A * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
2362N/A * or visit www.oracle.com if you need additional information or have any
2362N/A * questions.
0N/A */
0N/A
0N/Apackage sun.management;
0N/A
0N/Aimport com.sun.management.GarbageCollectorMXBean;
4235N/Aimport com.sun.management.GarbageCollectionNotificationInfo;
0N/Aimport java.lang.management.ManagementFactory;
0N/Aimport java.lang.management.MemoryPoolMXBean;
0N/Aimport java.lang.management.MemoryUsage;
0N/A
0N/Aimport com.sun.management.GcInfo;
0N/Aimport javax.management.openmbean.CompositeData;
0N/Aimport javax.management.MBeanInfo;
0N/Aimport javax.management.MBeanAttributeInfo;
178N/Aimport javax.management.ObjectName;
4235N/Aimport javax.management.MBeanNotificationInfo;
4235N/Aimport javax.management.Notification;
4235N/Aimport javax.management.NotificationFilter;
4235N/Aimport javax.management.NotificationListener;
4235N/Aimport javax.management.ListenerNotFoundException;
0N/A
0N/Aimport java.util.List;
0N/Aimport java.util.ListIterator;
4235N/Aimport java.util.Map;
0N/A
0N/A/**
0N/A * Implementation class for the garbage collector.
0N/A * Standard and committed hotspot-specific metrics if any.
0N/A *
0N/A * ManagementFactory.getGarbageCollectorMXBeans() returns a list
0N/A * of instances of this class.
0N/A */
0N/Aclass GarbageCollectorImpl extends MemoryManagerImpl
0N/A implements GarbageCollectorMXBean {
0N/A
0N/A GarbageCollectorImpl(String name) {
0N/A super(name);
0N/A }
0N/A
0N/A public native long getCollectionCount();
0N/A public native long getCollectionTime();
0N/A
0N/A
0N/A // The memory pools are static and won't be changed.
0N/A // TODO: If the hotspot implementation begins to have pools
0N/A // dynamically created and removed, this needs to be modified.
0N/A private String[] poolNames = null;
0N/A synchronized String[] getAllPoolNames() {
0N/A if (poolNames == null) {
0N/A List pools = ManagementFactory.getMemoryPoolMXBeans();
0N/A poolNames = new String[pools.size()];
0N/A int i = 0;
0N/A for (ListIterator iter = pools.listIterator();
0N/A iter.hasNext();
0N/A i++) {
0N/A MemoryPoolMXBean p = (MemoryPoolMXBean) iter.next();
0N/A poolNames[i] = p.getName();
0N/A }
0N/A }
0N/A return poolNames;
0N/A }
0N/A
0N/A // Sun JDK extension
0N/A private GcInfoBuilder gcInfoBuilder;
4235N/A
4235N/A private synchronized GcInfoBuilder getGcInfoBuilder() {
4235N/A if(gcInfoBuilder == null) {
4235N/A gcInfoBuilder = new GcInfoBuilder(this, getAllPoolNames());
4235N/A }
4235N/A return gcInfoBuilder;
4235N/A }
4235N/A
0N/A public GcInfo getLastGcInfo() {
4235N/A GcInfo info = getGcInfoBuilder().getLastGcInfo();
4235N/A return info;
4235N/A }
4235N/A
4235N/A private final static String notifName =
4235N/A "javax.management.Notification";
4235N/A
4235N/A private final static String[] gcNotifTypes = {
4235N/A GarbageCollectionNotificationInfo.GARBAGE_COLLECTION_NOTIFICATION
4235N/A };
4235N/A
4235N/A private MBeanNotificationInfo[] notifInfo = null;
4235N/A public MBeanNotificationInfo[] getNotificationInfo() {
0N/A synchronized (this) {
4235N/A if (notifInfo == null) {
4235N/A notifInfo = new MBeanNotificationInfo[1];
4235N/A notifInfo[0] = new MBeanNotificationInfo(gcNotifTypes,
4235N/A notifName,
4235N/A "GC Notification");
0N/A }
0N/A }
4235N/A return notifInfo;
4235N/A }
0N/A
4235N/A private static long seqNumber = 0;
4235N/A private static long getNextSeqNumber() {
4235N/A return ++seqNumber;
4235N/A }
4235N/A
4235N/A void createGCNotification(long timestamp,
4235N/A String gcName,
4235N/A String gcAction,
4235N/A String gcCause,
4235N/A GcInfo gcInfo) {
4235N/A
4235N/A if (!hasListeners()) {
4235N/A return;
4235N/A }
4235N/A
4235N/A Notification notif = new Notification(GarbageCollectionNotificationInfo.GARBAGE_COLLECTION_NOTIFICATION,
4235N/A getObjectName(),
4235N/A getNextSeqNumber(),
4235N/A timestamp,
4235N/A gcName);
4235N/A GarbageCollectionNotificationInfo info =
4235N/A new GarbageCollectionNotificationInfo(gcName,
4235N/A gcAction,
4235N/A gcCause,
4235N/A gcInfo);
4235N/A
4235N/A CompositeData cd =
4235N/A GarbageCollectionNotifInfoCompositeData.toCompositeData(info);
4235N/A notif.setUserData(cd);
4235N/A sendNotification(notif);
4235N/A }
4235N/A
4235N/A public synchronized void addNotificationListener(NotificationListener listener,
4235N/A NotificationFilter filter,
4235N/A Object handback)
4235N/A {
4235N/A boolean before = hasListeners();
4235N/A super.addNotificationListener(listener, filter, handback);
4235N/A boolean after = hasListeners();
4235N/A if (!before && after) {
4235N/A setNotificationEnabled(this, true);
4235N/A }
4235N/A }
4235N/A
4235N/A public synchronized void removeNotificationListener(NotificationListener listener)
4235N/A throws ListenerNotFoundException {
4235N/A boolean before = hasListeners();
4235N/A super.removeNotificationListener(listener);
4235N/A boolean after = hasListeners();
4235N/A if (before && !after) {
4235N/A setNotificationEnabled(this,false);
4235N/A }
4235N/A }
4235N/A
4235N/A public synchronized void removeNotificationListener(NotificationListener listener,
4235N/A NotificationFilter filter,
4235N/A Object handback)
4235N/A throws ListenerNotFoundException
4235N/A {
4235N/A boolean before = hasListeners();
4235N/A super.removeNotificationListener(listener,filter,handback);
4235N/A boolean after = hasListeners();
4235N/A if (before && !after) {
4235N/A setNotificationEnabled(this,false);
4235N/A }
0N/A }
0N/A
178N/A public ObjectName getObjectName() {
178N/A return Util.newObjectName(ManagementFactory.GARBAGE_COLLECTOR_MXBEAN_DOMAIN_TYPE, getName());
178N/A }
178N/A
4235N/A native void setNotificationEnabled(GarbageCollectorMXBean gc,
4235N/A boolean enabled);
4235N/A
0N/A}