0N/A/*
2362N/A * Copyright (c) 2004, 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.tools.jconsole;
0N/A
0N/Aimport javax.management.ObjectName;
0N/Aimport java.lang.management.MemoryPoolMXBean;
0N/Aimport java.lang.management.MemoryUsage;
0N/Aimport com.sun.management.GarbageCollectorMXBean;
0N/Aimport com.sun.management.GcInfo;
0N/Aimport java.util.HashMap;
0N/Aimport java.util.Set;
0N/Aimport java.util.Map;
0N/A
0N/Aimport static java.lang.management.ManagementFactory.*;
0N/A
0N/Apublic class MemoryPoolProxy {
0N/A private String poolName;
0N/A private ProxyClient client;
0N/A private MemoryPoolMXBean pool;
0N/A private Map<ObjectName,Long> gcMBeans;
0N/A private GcInfo lastGcInfo;
0N/A
0N/A public MemoryPoolProxy(ProxyClient client, ObjectName poolName) throws java.io.IOException {
0N/A this.client = client;
0N/A this.pool = client.getMXBean(poolName, MemoryPoolMXBean.class);
0N/A this.poolName = this.pool.getName();
0N/A this.gcMBeans = new HashMap<ObjectName,Long>();
0N/A this.lastGcInfo = null;
0N/A
0N/A String[] mgrNames = pool.getMemoryManagerNames();
0N/A for (String name : mgrNames) {
0N/A try {
0N/A ObjectName mbeanName = new ObjectName(GARBAGE_COLLECTOR_MXBEAN_DOMAIN_TYPE +
0N/A ",name=" + name);
0N/A if (client.isRegistered(mbeanName)) {
0N/A gcMBeans.put(mbeanName, new Long(0));
0N/A }
0N/A } catch (Exception e) {
0N/A assert false;
0N/A }
0N/A
0N/A }
0N/A }
0N/A
0N/A public boolean isCollectedMemoryPool() {
0N/A return (gcMBeans.size() != 0);
0N/A }
0N/A
0N/A public MemoryPoolStat getStat() throws java.io.IOException {
0N/A long usageThreshold = (pool.isUsageThresholdSupported()
0N/A ? pool.getUsageThreshold()
0N/A : -1);
0N/A long collectThreshold = (pool.isCollectionUsageThresholdSupported()
0N/A ? pool.getCollectionUsageThreshold()
0N/A : -1);
0N/A long lastGcStartTime = 0;
0N/A long lastGcEndTime = 0;
0N/A MemoryUsage beforeGcUsage = null;
0N/A MemoryUsage afterGcUsage = null;
0N/A long gcId = 0;
0N/A if (lastGcInfo != null) {
0N/A gcId = lastGcInfo.getId();
0N/A lastGcStartTime = lastGcInfo.getStartTime();
0N/A lastGcEndTime = lastGcInfo.getEndTime();
0N/A beforeGcUsage = lastGcInfo.getMemoryUsageBeforeGc().get(poolName);
0N/A afterGcUsage = lastGcInfo.getMemoryUsageAfterGc().get(poolName);
0N/A }
0N/A
0N/A Set<Map.Entry<ObjectName,Long>> set = gcMBeans.entrySet();
0N/A for (Map.Entry<ObjectName,Long> e : set) {
0N/A GarbageCollectorMXBean gc =
0N/A client.getMXBean(e.getKey(),
0N/A com.sun.management.GarbageCollectorMXBean.class);
0N/A Long gcCount = e.getValue();
0N/A Long newCount = gc.getCollectionCount();
0N/A if (newCount > gcCount) {
0N/A gcMBeans.put(e.getKey(), new Long(newCount));
0N/A lastGcInfo = gc.getLastGcInfo();
0N/A if (lastGcInfo.getEndTime() > lastGcEndTime) {
0N/A gcId = lastGcInfo.getId();
0N/A lastGcStartTime = lastGcInfo.getStartTime();
0N/A lastGcEndTime = lastGcInfo.getEndTime();
0N/A beforeGcUsage = lastGcInfo.getMemoryUsageBeforeGc().get(poolName);
0N/A afterGcUsage = lastGcInfo.getMemoryUsageAfterGc().get(poolName);
0N/A assert(beforeGcUsage != null);
0N/A assert(afterGcUsage != null);
0N/A }
0N/A }
0N/A }
0N/A
0N/A MemoryUsage usage = pool.getUsage();
0N/A return new MemoryPoolStat(poolName,
0N/A usageThreshold,
0N/A usage,
0N/A gcId,
0N/A lastGcStartTime,
0N/A lastGcEndTime,
0N/A collectThreshold,
0N/A beforeGcUsage,
0N/A afterGcUsage);
0N/A }
0N/A}