1 23 24 29 30 package com.sun.enterprise.admin.mbeans.jvm; 31 32 import com.sun.enterprise.util.i18n.StringManager; 33 import java.lang.management.GarbageCollectorMXBean ; 34 import java.lang.management.ManagementFactory ; 35 import java.lang.management.MemoryMXBean ; 36 import java.lang.management.MemoryPoolMXBean ; 37 import java.lang.management.MemoryUsage ; 38 import java.lang.management.RuntimeMXBean ; 39 import java.util.ArrayList ; 40 import java.util.Iterator ; 41 import java.util.List ; 42 import java.util.Set ; 43 import javax.management.MBeanServerConnection ; 44 import javax.management.ObjectName ; 45 46 48 class MemoryReporter { 49 private final MBeanServerConnection mbsc; 50 private RuntimeMXBean rmbean; 51 private MemoryMXBean mmbean; 52 private List <MemoryPoolMXBean > pools; 53 private List <GarbageCollectorMXBean > gcmbeans; 54 private final static StringManager sm = StringManager.getManager(MemoryReporter.class); 55 56 public MemoryReporter(final MBeanServerConnection mbsc) { 57 this.mbsc = mbsc; 58 } 59 public String getMemoryReport() { 60 init(); 61 final StringBuilderNewLineAppender sb = new StringBuilderNewLineAppender(new StringBuilder ()); 62 sb.append(getMemoryPoolReport()); 63 sb.append(getGarbageCollectionReport()); 64 sb.append(getMemoryMXBeanReport()); 65 return ( sb.toString() ); 66 } 67 68 private void init() throws RuntimeException { 69 try { 70 this.rmbean = ManagementFactory.newPlatformMXBeanProxy(mbsc, 71 ManagementFactory.RUNTIME_MXBEAN_NAME, 72 RuntimeMXBean .class); 73 this.mmbean = ManagementFactory.newPlatformMXBeanProxy(mbsc, 74 ManagementFactory.MEMORY_MXBEAN_NAME, 75 MemoryMXBean .class); 76 ObjectName poolName = new ObjectName (ManagementFactory.MEMORY_POOL_MXBEAN_DOMAIN_TYPE+",*");; 77 ObjectName gcName = new ObjectName (ManagementFactory.GARBAGE_COLLECTOR_MXBEAN_DOMAIN_TYPE+",*"); 78 Set mbeans = mbsc.queryNames(poolName, null); 79 if (mbeans != null) { 80 pools = new ArrayList <MemoryPoolMXBean >(); 81 Iterator iterator = mbeans.iterator(); 82 MemoryPoolMXBean p = null; 83 while (iterator.hasNext()) { 84 ObjectName objName = (ObjectName ) iterator.next(); 85 p = ManagementFactory.newPlatformMXBeanProxy(mbsc, 86 objName.getCanonicalName(), 87 MemoryPoolMXBean .class); 88 pools.add(p); 89 } 90 } 91 mbeans = mbsc.queryNames(gcName, null); 92 if (mbeans != null) { 93 gcmbeans = new ArrayList <GarbageCollectorMXBean >(); 94 Iterator iterator = mbeans.iterator(); 95 GarbageCollectorMXBean gc = null; 96 while (iterator.hasNext()) { 97 ObjectName objName = (ObjectName ) iterator.next(); 98 gc = ManagementFactory.newPlatformMXBeanProxy(mbsc, 99 objName.getCanonicalName(), 100 GarbageCollectorMXBean .class); 101 gcmbeans.add(gc); 102 } 103 } 104 } catch (final Exception e) { 105 throw new RuntimeException (e); 106 } 107 } 108 109 private String getMemoryPoolReport() { 110 final StringBuilderNewLineAppender sb = new StringBuilderNewLineAppender(new StringBuilder ()); 111 final long millis = rmbean.getUptime(); 112 final String uptime = sm.getString("uptime", JVMInformationCollector.millis2HoursMinutesSeconds(millis)); 113 sb.append(uptime); 114 for (final MemoryPoolMXBean m : pools) { 115 final String n = m.getName(); 116 sb.append(sm.getString("memory.pool.name", n)); 117 MemoryUsage mu = m.getUsage(); 118 sb.append(mu2String(mu)); 119 } 120 return ( sb.toString() ); 121 } 122 private String mu2String(final MemoryUsage mu) { 123 final StringBuilderNewLineAppender sb = new StringBuilderNewLineAppender(new StringBuilder ()); 124 final String init = JVMInformationCollector.formatLong(mu.getInit()); 125 sb.append(sm.getString("memory.usage.init", init)); 126 final String comm = JVMInformationCollector.formatLong(mu.getCommitted()); 127 sb.append(sm.getString("memory.usage.comm", comm)); 128 final String max = JVMInformationCollector.formatLong(mu.getMax()); 129 sb.append(sm.getString("memory.usage.max", max)); 130 final String used = JVMInformationCollector.formatLong(mu.getUsed()); 131 sb.append(sm.getString("memory.usage.used", used)); 132 return ( sb.toString() ); 133 } 134 private String getGarbageCollectionReport() { 135 final StringBuilderNewLineAppender sb = new StringBuilderNewLineAppender(new StringBuilder ()); 136 for (final GarbageCollectorMXBean m : gcmbeans) { 137 final String name = sm.getString("gc.name", m.getName()); 138 sb.append(name); 139 final String cc = sm.getString("gc.numcol", JVMInformationCollector.formatLong(m.getCollectionCount())); 140 sb.append(cc); 141 final String gct = sm.getString("gc.coltime", JVMInformationCollector.millis2SecondsMillis(m.getCollectionTime())); 142 sb.append(gct); 143 } 144 return ( sb.toString() ); 145 } 146 private String getMemoryMXBeanReport() { 147 final StringBuilderNewLineAppender sb = new StringBuilderNewLineAppender(new StringBuilder ()); 148 sb.append(sm.getString("heap.mem.usage")); 149 sb.append(mu2String(mmbean.getHeapMemoryUsage())); 150 sb.append(sm.getString("nonheap.mem.usage")); 151 sb.append(mu2String(mmbean.getNonHeapMemoryUsage())); 152 sb.append(sm.getString("obj.fin.pending", mmbean.getObjectPendingFinalizationCount())); 153 return ( sb.toString() ); 154 } 155 } | Popular Tags |