jdk/src/share/classes/sun/management/MemoryImpl.java
author mchung
Thu, 10 Apr 2008 10:47:13 -0700
changeset 401 ef01e0dccd63
parent 2 90ce3da70b43
child 715 f16baef3a20e
permissions -rw-r--r--
6610094: Add generic support for platform MXBeans of any type (also fixed 6681031) Summary: Add new methods in ManagementFactory class to obtain platform MXBeans Reviewed-by: alanb, dfuchs, emcmanus
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
     2
 * Copyright 2003-2005 Sun Microsystems, Inc.  All Rights Reserved.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     4
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
90ce3da70b43 Initial load
duke
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
90ce3da70b43 Initial load
duke
parents:
diff changeset
     7
 * published by the Free Software Foundation.  Sun designates this
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
90ce3da70b43 Initial load
duke
parents:
diff changeset
     9
 * by Sun in the LICENSE file that accompanied this code.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    10
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
90ce3da70b43 Initial load
duke
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
90ce3da70b43 Initial load
duke
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
90ce3da70b43 Initial load
duke
parents:
diff changeset
    15
 * accompanied this code).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    16
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
90ce3da70b43 Initial load
duke
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    20
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    21
 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    22
 * CA 95054 USA or visit www.sun.com if you need additional information or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    23
 * have any questions.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    24
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    25
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
package sun.management;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    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
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
import java.lang.management.MemoryMXBean;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
import java.lang.management.MemoryUsage;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
import java.lang.management.MemoryNotificationInfo;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
import java.lang.management.MemoryManagerMXBean;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
import java.lang.management.MemoryPoolMXBean;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
import javax.management.ObjectName;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
import javax.management.MBeanNotificationInfo;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
import javax.management.Notification;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
import javax.management.NotificationEmitter;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
import javax.management.NotificationFilter;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
import javax.management.NotificationListener;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
import javax.management.ListenerNotFoundException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
import javax.management.openmbean.CompositeData;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
import java.util.List;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
import java.util.ArrayList;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
import java.util.ListIterator;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
import java.util.Collections;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
 * Implementation class for the memory subsystem.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
 * Standard and committed hotspot-specific metrics if any.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
 * ManagementFactory.getMemoryMXBean() returns an instance
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
 * of this class.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
class MemoryImpl extends NotificationEmitterSupport
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
                 implements MemoryMXBean {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
    private final VMManagement jvm;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
    private static MemoryPoolMXBean[] pools = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
    private static MemoryManagerMXBean[] mgrs = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
     * Constructor of MemoryImpl class
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
    MemoryImpl(VMManagement vm) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
        this.jvm = vm;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
    public int getObjectPendingFinalizationCount() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
        return sun.misc.VM.getFinalRefCount();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
    public void gc() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
        Runtime.getRuntime().gc();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
    // Need to make a VM call to get coherent value
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
    public MemoryUsage getHeapMemoryUsage() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
        return getMemoryUsage0(true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
    public MemoryUsage getNonHeapMemoryUsage() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
        return getMemoryUsage0(false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
    public boolean isVerbose() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
        return jvm.getVerboseGC();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
90ce3da70b43 Initial load
duke
parents:
diff changeset
    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
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
        setVerboseGC(value);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
    // The current Hotspot implementation does not support
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
    // dynamically add or remove memory pools & managers.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
    static synchronized MemoryPoolMXBean[] getMemoryPools() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
        if (pools == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
            pools = getMemoryPools0();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
        return pools;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
    static synchronized MemoryManagerMXBean[] getMemoryManagers() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
        if (mgrs == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
            mgrs = getMemoryManagers0();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
        return mgrs;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
    private static native MemoryPoolMXBean[] getMemoryPools0();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
    private static native MemoryManagerMXBean[] getMemoryManagers0();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
    private native MemoryUsage getMemoryUsage0(boolean heap);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
    private native void setVerboseGC(boolean value);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
    private final static String notifName =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
        "javax.management.Notification";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
    private final static String[] notifTypes = {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
        MemoryNotificationInfo.MEMORY_THRESHOLD_EXCEEDED,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
        MemoryNotificationInfo.MEMORY_COLLECTION_THRESHOLD_EXCEEDED
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
    };
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
    private final static String[] notifMsgs  = {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
        "Memory usage exceeds usage threshold",
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
        "Memory usage exceeds collection usage threshold"
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
    };
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
    private MBeanNotificationInfo[] notifInfo = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
    public MBeanNotificationInfo[] getNotificationInfo() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
        synchronized (this) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
            if (notifInfo == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
                 notifInfo = new MBeanNotificationInfo[1];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
                 notifInfo[0] = new MBeanNotificationInfo(notifTypes,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
                                                          notifName,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
                                                          "Memory Notification");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
        return notifInfo;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
    private static String getNotifMsg(String notifType) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
        for (int i = 0; i < notifTypes.length; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
            if (notifType == notifTypes[i]) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
                return notifMsgs[i];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
        return "Unknown message";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
    private static long seqNumber = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
    private static long getNextSeqNumber() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
        return ++seqNumber;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
    static void createNotification(String notifType,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
                                   String poolName,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
                                   MemoryUsage usage,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
                                   long count) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
        MemoryImpl mbean = (MemoryImpl) ManagementFactory.getMemoryMXBean();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
        if (!mbean.hasListeners()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
            // if no listener is registered.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
            return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
        long timestamp = System.currentTimeMillis();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
        String msg = getNotifMsg(notifType);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   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
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
                                              getNextSeqNumber(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
                                              timestamp,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
                                              msg);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
        MemoryNotificationInfo info =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
            new MemoryNotificationInfo(poolName,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
                                       usage,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
                                       count);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
        CompositeData cd =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
            MemoryNotifInfoCompositeData.toCompositeData(info);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
        notif.setUserData(cd);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
        mbean.sendNotification(notif);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   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
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
}