/** * Formats the thread dump header for one thread. * * @param ti the ThreadInfo describing the thread * @return the formatted thread dump header */ private static String getThreadDumpHeader(ThreadInfo ti) { StringBuilder sb = new StringBuilder("\"" + ti.getThreadName() + "\""); sb.append(" Id=" + ti.getThreadId()); sb.append(" cpu=" + threadMXBean.getThreadCpuTime(ti.getThreadId()) + " ns"); sb.append(" usr="******" ns"); sb.append(" blocked " + ti.getBlockedCount() + " for " + ti.getBlockedTime() + " ms"); sb.append(" waited " + ti.getWaitedCount() + " for " + ti.getWaitedTime() + " ms"); if (ti.isSuspended()) { sb.append(" (suspended)"); } if (ti.isInNative()) { sb.append(" (running in native)"); } sb.append(CRLF); sb.append(INDENT3 + "java.lang.Thread.State: " + ti.getThreadState()); sb.append(CRLF); return sb.toString(); }
private static SimpleOrderedMap<Object> getThreadInfo(ThreadInfo ti, ThreadMXBean tmbean) { SimpleOrderedMap<Object> info = new SimpleOrderedMap<Object>(); long tid = ti.getThreadId(); info.add("id", tid); info.add("name", ti.getThreadName()); info.add("state", ti.getThreadState().toString()); if (ti.getLockName() != null) { info.add("lock", ti.getLockName()); } if (ti.isSuspended()) { info.add("suspended", true); } if (ti.isInNative()) { info.add("native", true); } if (tmbean.isThreadCpuTimeSupported()) { info.add("cpuTime", formatNanos(tmbean.getThreadCpuTime(tid))); info.add("userTime", formatNanos(tmbean.getThreadUserTime(tid))); } if (ti.getLockOwnerName() != null) { SimpleOrderedMap<Object> owner = new SimpleOrderedMap<Object>(); owner.add("name", ti.getLockOwnerName()); owner.add("id", ti.getLockOwnerId()); } // Add the stack trace int i = 0; String[] trace = new String[ti.getStackTrace().length]; for (StackTraceElement ste : ti.getStackTrace()) { trace[i++] = ste.toString(); } info.add("stackTrace", trace); return info; }
private static void printThread(ThreadInfo threadInfo, StringBuilder threadDump) { StringBuilder sb = new StringBuilder( "\"" + threadInfo.getThreadName() + "\"" + " nid=" + threadInfo.getThreadId() + " state=" + threadInfo.getThreadState()); if (threadInfo.getLockName() != null && threadInfo.getThreadState() != Thread.State.BLOCKED) { String[] lockInfo = threadInfo.getLockName().split("@"); sb.append("\n" + INDENT + "- waiting on <0x" + lockInfo[1] + "> (a " + lockInfo[0] + ")"); sb.append("\n" + INDENT + "- locked <0x" + lockInfo[1] + "> (a " + lockInfo[0] + ")"); } else if (threadInfo.getLockName() != null && threadInfo.getThreadState() == Thread.State.BLOCKED) { String[] lockInfo = threadInfo.getLockName().split("@"); sb.append( "\n" + INDENT + "- waiting to lock <0x" + lockInfo[1] + "> (a " + lockInfo[0] + ")"); } if (threadInfo.isSuspended()) sb.append(" (suspended)"); if (threadInfo.isInNative()) sb.append(" (running in native)"); threadDump.append(sb.toString()); threadDump.append("\n"); if (threadInfo.getLockOwnerName() != null) { threadDump.append( INDENT + " owned by " + threadInfo.getLockOwnerName() + " id=" + threadInfo.getLockOwnerId()); threadDump.append("\n"); } }
/** * スレッドの状態をStringBufferにして返します。<br> * * @param info {@link ThreadInfo}オブジェクト * @return スレッドの状態をStringBufferにして返したもの。 */ private static StringBuilder getThreadInfoBuffer(ThreadInfo info) { StringBuilder sb = new StringBuilder( "\"" + info.getThreadName() + "\"" + " Id=" + info.getThreadId() + " " + info.getThreadState()); if (info.getLockName() != null) { sb.append(" on " + info.getLockName()); } if (info.getLockOwnerName() != null) { sb.append(" owned by \"" + info.getLockOwnerName() + "\" Id=" + info.getLockOwnerId()); } if (info.isSuspended()) { sb.append(" (suspended)"); } if (info.isInNative()) { sb.append(" (in native)"); } return sb; }
// ThreadInfo.toString() truncates the stack trace by first 8, so needed my own version @IgnoreJRERequirement public static String dumpThreadInfo(ThreadInfo ti) { StringBuilder sb = new StringBuilder( "\"" + ti.getThreadName() + "\"" + " Id=" + ti.getThreadId() + " " + ti.getThreadState()); if (ti.getLockName() != null) { sb.append(" on " + ti.getLockName()); } if (ti.getLockOwnerName() != null) { sb.append(" owned by \"" + ti.getLockOwnerName() + "\" Id=" + ti.getLockOwnerId()); } if (ti.isSuspended()) { sb.append(" (suspended)"); } if (ti.isInNative()) { sb.append(" (in native)"); } sb.append('\n'); StackTraceElement[] stackTrace = ti.getStackTrace(); for (int i = 0; i < stackTrace.length; i++) { StackTraceElement ste = stackTrace[i]; sb.append("\tat " + ste.toString()); sb.append('\n'); if (i == 0 && ti.getLockInfo() != null) { Thread.State ts = ti.getThreadState(); switch (ts) { case BLOCKED: sb.append("\t- blocked on " + ti.getLockInfo()); sb.append('\n'); break; case WAITING: sb.append("\t- waiting on " + ti.getLockInfo()); sb.append('\n'); break; case TIMED_WAITING: sb.append("\t- waiting on " + ti.getLockInfo()); sb.append('\n'); break; default: } } for (MonitorInfo mi : ti.getLockedMonitors()) { if (mi.getLockedStackDepth() == i) { sb.append("\t- locked " + mi); sb.append('\n'); } } } LockInfo[] locks = ti.getLockedSynchronizers(); if (locks.length > 0) { sb.append("\n\tNumber of locked synchronizers = " + locks.length); sb.append('\n'); for (LockInfo li : locks) { sb.append("\t- " + li); sb.append('\n'); } } sb.append('\n'); return sb.toString(); }