1 23 24 29 30 package com.sun.enterprise.admin.mbeans.jvm; 31 32 import com.sun.enterprise.admin.common.constant.AdminConstants; 33 import com.sun.enterprise.util.i18n.StringManager; 34 import java.lang.management.ManagementFactory ; 35 import java.lang.management.RuntimeMXBean ; 36 import java.lang.management.ThreadInfo ; 37 import java.lang.management.ThreadMXBean ; 38 import java.util.logging.Logger ; 39 import javax.management.MBeanServerConnection ; 40 41 43 class ThreadMonitor { 44 45 private final MBeanServerConnection mbsc; 46 private static final Logger logger = Logger.getLogger(AdminConstants.kLoggerName); 47 private final StringManager sm = StringManager.getManager(ThreadMonitor.class); 48 public ThreadMonitor(final MBeanServerConnection mbsc) { 49 this.mbsc = mbsc; 50 } 51 public final String getThreadDump() { 52 final long start = System.currentTimeMillis(); 53 final StringBuilder sb = new StringBuilder (); 54 final StringBuilderNewLineAppender td = new StringBuilderNewLineAppender(sb); 55 try { 56 final ThreadMXBean tmx = ManagementFactory.newPlatformMXBeanProxy(mbsc, ManagementFactory.THREAD_MXBEAN_NAME, ThreadMXBean .class); 57 final String title = getTitle(); 58 td.append(title); 59 final long[] tids = tmx.getAllThreadIds(); 60 td.append(sm.getString("thread.no", tmx.getThreadCount())); 61 td.append(sm.getString("daemon.threads.no", tmx.getDaemonThreadCount())); 62 final ThreadInfo [] tinfos = tmx.getThreadInfo(tids, Integer.MAX_VALUE); 63 70 for (final ThreadInfo ti : tinfos) { 71 td.append(dumpThread(ti)); 72 } 73 sb.append(getDeadlockInfo(tmx)); 74 return ( td.toString() ); 75 } catch(final Exception e) { 76 throw new RuntimeException (e); 77 } 78 finally { 79 final long end = System.currentTimeMillis(); 80 final double time = (end/1000.0) - (start/1000.0); 81 logger.info("Time in seconds to get the jvm thread dump: " + time); 82 } 83 } 84 private String dumpThread(final ThreadInfo ti) { 85 final long ids = ti.getThreadId(); 86 final String ss = ti.getThreadState().toString(); 87 String msg = sm.getString("thread.title", quote(ti.getThreadName()), ids, ss); 89 final StringBuilder sb = new StringBuilder (msg); 90 if (ti.getLockName() != null) { 91 msg = sm.getString("thread.waiting.on", ti.getLockName()); 92 sb.append(msg); 93 } 94 if (ti.isSuspended()) { 95 msg = sm.getString("thread.suspended"); 96 sb.append(msg); 97 } 98 if (ti.isInNative()) { 99 msg = sm.getString("thread.in.native"); 100 sb.append(msg); 101 } 102 sb.append(System.getProperty("line.separator")); 103 if (ti.getLockOwnerName() != null) { 104 msg = sm.getString("thread.owner", ti.getLockOwnerName(), ti.getLockOwnerId()); 105 sb.append(msg); 106 } 107 for (final StackTraceElement ste : ti.getStackTrace()) { 108 msg = sm.getString("thread.stack.element", ste.toString()); 109 sb.append(msg); 110 sb.append(System.getProperty("line.separator")); 111 } 112 sb.append(System.getProperty("line.separator")); 113 return ( sb.toString() ); 114 } 115 116 private String getTitle() throws Exception { 117 final RuntimeMXBean rt = ManagementFactory.newPlatformMXBeanProxy(mbsc, ManagementFactory.RUNTIME_MXBEAN_NAME, RuntimeMXBean .class); 118 final String vmname = rt.getVmName(); 119 final String vmversion = rt.getVmVersion(); 120 final String vmvendor = rt.getVmVendor(); 121 final String title = sm.getString("td.title", vmname, vmversion, vmvendor); 122 123 return ( title ); 124 } 125 126 private String quote(final String uq) { 127 final StringBuilder sb = new StringBuilder ("\""); 128 sb.append(uq).append("\""); 129 return ( sb.toString() ); 130 } 131 132 private String getDeadlockInfo(final ThreadMXBean tmx) { 133 final StringBuilderNewLineAppender sb = new StringBuilderNewLineAppender(new StringBuilder ()); 134 final long[] dts = tmx.findMonitorDeadlockedThreads(); 135 if (dts == null) { 136 sb.append(sm.getString("no.deadlock")); 137 } 138 else { 139 sb.append(sm.getString("deadlocks.found")); 140 for (final long dt : dts) { 141 final ThreadInfo ti = tmx.getThreadInfo(dt); 142 sb.append(this.dumpThread(ti)); 143 } 144 } 145 return ( sb.toString() ); 146 } 147 } 148 | Popular Tags |