1/* 2 * Copyright (c) 2004, 2012, 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 26package sun.tools.jconsole; 27 28import javax.management.ObjectName; 29import java.lang.management.MemoryPoolMXBean; 30import java.lang.management.MemoryUsage; 31import com.sun.management.GarbageCollectorMXBean; 32import com.sun.management.GcInfo; 33import java.util.HashMap; 34import java.util.Set; 35import java.util.Map; 36 37import static java.lang.management.ManagementFactory.*; 38 39public class MemoryPoolProxy { 40 private String poolName; 41 private ProxyClient client; 42 private MemoryPoolMXBean pool; 43 private Map<ObjectName,Long> gcMBeans; 44 private GcInfo lastGcInfo; 45 46 public MemoryPoolProxy(ProxyClient client, ObjectName poolName) throws java.io.IOException { 47 this.client = client; 48 this.pool = client.getMXBean(poolName, MemoryPoolMXBean.class); 49 this.poolName = this.pool.getName(); 50 this.gcMBeans = new HashMap<ObjectName,Long>(); 51 this.lastGcInfo = null; 52 53 String[] mgrNames = pool.getMemoryManagerNames(); 54 for (String name : mgrNames) { 55 try { 56 ObjectName mbeanName = new ObjectName(GARBAGE_COLLECTOR_MXBEAN_DOMAIN_TYPE + 57 ",name=" + name); 58 if (client.isRegistered(mbeanName)) { 59 gcMBeans.put(mbeanName, 0L); 60 } 61 } catch (Exception e) { 62 assert false; 63 } 64 65 } 66 } 67 68 public boolean isCollectedMemoryPool() { 69 return (gcMBeans.size() != 0); 70 } 71 72 public MemoryPoolStat getStat() throws java.io.IOException { 73 long usageThreshold = (pool.isUsageThresholdSupported() 74 ? pool.getUsageThreshold() 75 : -1); 76 long collectThreshold = (pool.isCollectionUsageThresholdSupported() 77 ? pool.getCollectionUsageThreshold() 78 : -1); 79 long lastGcStartTime = 0; 80 long lastGcEndTime = 0; 81 MemoryUsage beforeGcUsage = null; 82 MemoryUsage afterGcUsage = null; 83 long gcId = 0; 84 if (lastGcInfo != null) { 85 gcId = lastGcInfo.getId(); 86 lastGcStartTime = lastGcInfo.getStartTime(); 87 lastGcEndTime = lastGcInfo.getEndTime(); 88 beforeGcUsage = lastGcInfo.getMemoryUsageBeforeGc().get(poolName); 89 afterGcUsage = lastGcInfo.getMemoryUsageAfterGc().get(poolName); 90 } 91 92 Set<Map.Entry<ObjectName,Long>> set = gcMBeans.entrySet(); 93 for (Map.Entry<ObjectName,Long> e : set) { 94 GarbageCollectorMXBean gc = 95 client.getMXBean(e.getKey(), 96 com.sun.management.GarbageCollectorMXBean.class); 97 Long gcCount = e.getValue(); 98 Long newCount = gc.getCollectionCount(); 99 if (newCount > gcCount) { 100 gcMBeans.put(e.getKey(), newCount); 101 lastGcInfo = gc.getLastGcInfo(); 102 if (lastGcInfo.getEndTime() > lastGcEndTime) { 103 gcId = lastGcInfo.getId(); 104 lastGcStartTime = lastGcInfo.getStartTime(); 105 lastGcEndTime = lastGcInfo.getEndTime(); 106 beforeGcUsage = lastGcInfo.getMemoryUsageBeforeGc().get(poolName); 107 afterGcUsage = lastGcInfo.getMemoryUsageAfterGc().get(poolName); 108 assert(beforeGcUsage != null); 109 assert(afterGcUsage != null); 110 } 111 } 112 } 113 114 MemoryUsage usage = pool.getUsage(); 115 return new MemoryPoolStat(poolName, 116 usageThreshold, 117 usage, 118 gcId, 119 lastGcStartTime, 120 lastGcEndTime, 121 collectThreshold, 122 beforeGcUsage, 123 afterGcUsage); 124 } 125} 126