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 26 package sun.tools.jconsole; 27 28 import javax.management.ObjectName; 29 import java.lang.management.ManagementFactory; 30 import java.lang.management.MemoryPoolMXBean; 31 import java.lang.management.MemoryUsage; 32 import com.sun.management.GarbageCollectorMXBean; 33 import com.sun.management.GcInfo; 34 import java.util.HashMap; 35 import java.util.Set; 36 import java.util.Map; 37 import java.util.Map.Entry; 38 39 import static java.lang.management.ManagementFactory.*; 40 41 public class MemoryPoolProxy { 42 private String poolName; 43 private ProxyClient client; 44 private ObjectName objName; 45 private MemoryPoolMXBean pool; 46 private Map<ObjectName,Long> gcMBeans; 47 private GcInfo lastGcInfo; 48 49 public MemoryPoolProxy(ProxyClient client, ObjectName poolName) throws java.io.IOException { 50 this.client = client; 51 this.objName = objName; 52 this.pool = client.getMXBean(poolName, MemoryPoolMXBean.class); 53 this.poolName = this.pool.getName(); 54 this.gcMBeans = new HashMap<ObjectName,Long>(); 55 this.lastGcInfo = null; 56 57 String[] mgrNames = pool.getMemoryManagerNames(); 58 for (String name : mgrNames) { 59 try { 60 ObjectName mbeanName = new ObjectName(GARBAGE_COLLECTOR_MXBEAN_DOMAIN_TYPE + 61 ",name=" + name); 62 if (client.isRegistered(mbeanName)) { 63 gcMBeans.put(mbeanName, new Long(0)); 64 } 65 } catch (Exception e) { 66 assert false; 67 } 68 69 } 70 } 71 72 public boolean isCollectedMemoryPool() { 73 return (gcMBeans.size() != 0); 74 } 75 76 public ObjectName getObjectName() { 77 return objName; 78 } 79 80 public MemoryPoolStat getStat() throws java.io.IOException { 81 long usageThreshold = (pool.isUsageThresholdSupported() 82 ? pool.getUsageThreshold() 83 : -1); 84 long collectThreshold = (pool.isCollectionUsageThresholdSupported() 85 ? pool.getCollectionUsageThreshold() 86 : -1); 87 long lastGcStartTime = 0; 88 long lastGcEndTime = 0; 89 MemoryUsage beforeGcUsage = null; 90 MemoryUsage afterGcUsage = null; 91 long gcId = 0; 92 if (lastGcInfo != null) { 93 gcId = lastGcInfo.getId(); 94 lastGcStartTime = lastGcInfo.getStartTime(); 95 lastGcEndTime = lastGcInfo.getEndTime(); 96 beforeGcUsage = lastGcInfo.getMemoryUsageBeforeGc().get(poolName); 97 afterGcUsage = lastGcInfo.getMemoryUsageAfterGc().get(poolName); 98 } 99 | 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 26 package sun.tools.jconsole; 27 28 import javax.management.ObjectName; 29 import java.lang.management.MemoryPoolMXBean; 30 import java.lang.management.MemoryUsage; 31 import com.sun.management.GarbageCollectorMXBean; 32 import com.sun.management.GcInfo; 33 import java.util.HashMap; 34 import java.util.Set; 35 import java.util.Map; 36 37 import static java.lang.management.ManagementFactory.*; 38 39 public 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, new Long(0)); 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 |