001: /*
002: * Copyright 2004 Sun Microsystems, Inc. All Rights Reserved.
003: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
004: *
005: * This code is free software; you can redistribute it and/or modify it
006: * under the terms of the GNU General Public License version 2 only, as
007: * published by the Free Software Foundation. Sun designates this
008: * particular file as subject to the "Classpath" exception as provided
009: * by Sun in the LICENSE file that accompanied this code.
010: *
011: * This code is distributed in the hope that it will be useful, but WITHOUT
012: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
013: * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
014: * version 2 for more details (a copy is included in the LICENSE file that
015: * accompanied this code).
016: *
017: * You should have received a copy of the GNU General Public License version
018: * 2 along with this work; if not, write to the Free Software Foundation,
019: * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
020: *
021: * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
022: * CA 95054 USA or visit www.sun.com if you need additional information or
023: * have any questions.
024: */
025:
026: package sun.tools.jconsole;
027:
028: import javax.management.ObjectName;
029: import java.lang.management.ManagementFactory;
030: import java.lang.management.MemoryPoolMXBean;
031: import java.lang.management.MemoryUsage;
032: import com.sun.management.GarbageCollectorMXBean;
033: import com.sun.management.GcInfo;
034: import java.util.HashMap;
035: import java.util.Set;
036: import java.util.Map;
037: import java.util.Map.Entry;
038:
039: import static java.lang.management.ManagementFactory.*;
040:
041: public class MemoryPoolProxy {
042: private String poolName;
043: private ProxyClient client;
044: private ObjectName objName;
045: private MemoryPoolMXBean pool;
046: private Map<ObjectName, Long> gcMBeans;
047: private GcInfo lastGcInfo;
048:
049: public MemoryPoolProxy(ProxyClient client, ObjectName poolName)
050: throws java.io.IOException {
051: this .client = client;
052: this .objName = objName;
053: this .pool = client.getMXBean(poolName, MemoryPoolMXBean.class);
054: this .poolName = this .pool.getName();
055: this .gcMBeans = new HashMap<ObjectName, Long>();
056: this .lastGcInfo = null;
057:
058: String[] mgrNames = pool.getMemoryManagerNames();
059: for (String name : mgrNames) {
060: try {
061: ObjectName mbeanName = new ObjectName(
062: GARBAGE_COLLECTOR_MXBEAN_DOMAIN_TYPE + ",name="
063: + name);
064: if (client.isRegistered(mbeanName)) {
065: gcMBeans.put(mbeanName, new Long(0));
066: }
067: } catch (Exception e) {
068: assert false;
069: }
070:
071: }
072: }
073:
074: public boolean isCollectedMemoryPool() {
075: return (gcMBeans.size() != 0);
076: }
077:
078: public ObjectName getObjectName() {
079: return objName;
080: }
081:
082: public MemoryPoolStat getStat() throws java.io.IOException {
083: long usageThreshold = (pool.isUsageThresholdSupported() ? pool
084: .getUsageThreshold() : -1);
085: long collectThreshold = (pool
086: .isCollectionUsageThresholdSupported() ? pool
087: .getCollectionUsageThreshold() : -1);
088: long lastGcStartTime = 0;
089: long lastGcEndTime = 0;
090: MemoryUsage beforeGcUsage = null;
091: MemoryUsage afterGcUsage = null;
092: long gcId = 0;
093: if (lastGcInfo != null) {
094: gcId = lastGcInfo.getId();
095: lastGcStartTime = lastGcInfo.getStartTime();
096: lastGcEndTime = lastGcInfo.getEndTime();
097: beforeGcUsage = lastGcInfo.getMemoryUsageBeforeGc().get(
098: poolName);
099: afterGcUsage = lastGcInfo.getMemoryUsageAfterGc().get(
100: poolName);
101: }
102:
103: Set<Map.Entry<ObjectName, Long>> set = gcMBeans.entrySet();
104: for (Map.Entry<ObjectName, Long> e : set) {
105: GarbageCollectorMXBean gc = client.getMXBean(e.getKey(),
106: com.sun.management.GarbageCollectorMXBean.class);
107: Long gcCount = e.getValue();
108: Long newCount = gc.getCollectionCount();
109: if (newCount > gcCount) {
110: gcMBeans.put(e.getKey(), new Long(newCount));
111: lastGcInfo = gc.getLastGcInfo();
112: if (lastGcInfo.getEndTime() > lastGcEndTime) {
113: gcId = lastGcInfo.getId();
114: lastGcStartTime = lastGcInfo.getStartTime();
115: lastGcEndTime = lastGcInfo.getEndTime();
116: beforeGcUsage = lastGcInfo.getMemoryUsageBeforeGc()
117: .get(poolName);
118: afterGcUsage = lastGcInfo.getMemoryUsageAfterGc()
119: .get(poolName);
120: assert (beforeGcUsage != null);
121: assert (afterGcUsage != null);
122: }
123: }
124: }
125:
126: MemoryUsage usage = pool.getUsage();
127: return new MemoryPoolStat(poolName, usageThreshold, usage,
128: gcId, lastGcStartTime, lastGcEndTime, collectThreshold,
129: beforeGcUsage, afterGcUsage);
130: }
131: }
|