List of usage examples for java.lang.management ThreadInfo getStackTrace
public StackTraceElement[] getStackTrace()
From source file:org.batoo.jpa.benchmark.BenchmarkTest.java
private void measureTime(long worked, ThreadInfo threadInfo) { TimeElement child = this.element; boolean gotStart = false; boolean last = false; boolean inDb = false; if (threadInfo == null) { return;/*from w w w .ja v a 2s .c o m*/ } for (int i = threadInfo.getStackTrace().length - 1; i >= 0; i--) { final StackTraceElement stElement = threadInfo.getStackTrace()[i]; if (this.isInDb(stElement)) { inDb = true; break; } } for (int i = threadInfo.getStackTrace().length - 1; i >= 0; i--) { final StackTraceElement stElement = threadInfo.getStackTrace()[i]; if (!gotStart && !stElement.getMethodName().startsWith("singleTest")) { continue; } gotStart = true; final String key = BenchmarkTest.SUMMARIZE ? // stElement.getClassName() + "." + stElement.getMethodName() : // stElement.getClassName() + "." + stElement.getMethodName() + "." + stElement.getLineNumber(); child = child.get(key); TimeElement child2 = this.elements.get(key); if (child2 == null) { this.elements.put(key, child2 = new TimeElement(key)); } if (this.isInDb(stElement) || (i == 0)) { child.addTime(worked, true, inDb); child2.addTime(worked, true, inDb); last = true; } else { child.addTime(worked, false, inDb); child2.addTime(worked, false, inDb); } if (last) { break; } } }
From source file:com.thoughtworks.go.server.service.support.ThreadInformationProvider.java
private Map<String, Object> getDeadLockThreadInformation(ThreadMXBean threadMXBean) { LinkedHashMap<String, Object> json = new LinkedHashMap<>(); long[] deadlockedThreads = threadMXBean.findDeadlockedThreads(); if (deadlockedThreads != null && deadlockedThreads.length > 0) { json.put("Count", deadlockedThreads.length); for (long deadlockedThread : deadlockedThreads) { LinkedHashMap<String, Object> threadsInfo = new LinkedHashMap<>(); LinkedHashMap<String, Object> lockedMonitorsInfo = new LinkedHashMap<>(); LinkedHashMap<String, Object> stackTrackInfo = new LinkedHashMap<>(); ThreadInfo threadInfo = threadMXBean.getThreadInfo(deadlockedThread); LockInfo lockInfo = threadInfo.getLockInfo(); if (lockInfo != null) { threadsInfo.put(threadInfo.getThreadName(), lockInfo); } else { threadsInfo.put(threadInfo.getThreadName(), "This thread is not waiting for any locks"); }//from w w w . ja v a2s.c om MonitorInfo[] lockedMonitors = threadInfo.getLockedMonitors(); for (MonitorInfo lockedMonitor : lockedMonitors) { lockedMonitorsInfo.put("Monitor for class " + lockedMonitor.getClassName(), "taken at stack frame " + lockedMonitor.getLockedStackFrame()); } stackTrackInfo.put(Long.toString(deadlockedThread), Arrays.toString(threadInfo.getStackTrace())); json.put("Thread Information", threadsInfo); json.put("Monitor Information Stack Frame where locks were taken", lockedMonitorsInfo); json.put("Stack Trace Of DeadLock Threads", stackTrackInfo); } } return json; }
From source file:org.apache.storm.utils.Utils.java
/** * Gets some information, including stack trace, for a running thread. * @return A human-readable string of the dump. */// w w w . j a v a 2 s.c o m public static String threadDump() { final StringBuilder dump = new StringBuilder(); final java.lang.management.ThreadMXBean threadMXBean = ManagementFactory.getThreadMXBean(); final java.lang.management.ThreadInfo[] threadInfos = threadMXBean .getThreadInfo(threadMXBean.getAllThreadIds(), 100); for (java.lang.management.ThreadInfo threadInfo : threadInfos) { dump.append('"'); dump.append(threadInfo.getThreadName()); dump.append("\" "); dump.append("\n lock: "); dump.append(threadInfo.getLockName()); dump.append(" owner: "); dump.append(threadInfo.getLockOwnerName()); final Thread.State state = threadInfo.getThreadState(); dump.append("\n java.lang.Thread.State: "); dump.append(state); final StackTraceElement[] stackTraceElements = threadInfo.getStackTrace(); for (final StackTraceElement stackTraceElement : stackTraceElements) { dump.append("\n at "); dump.append(stackTraceElement); } dump.append("\n\n"); } return dump.toString(); }
From source file:com.streamsets.datacollector.bundles.content.SdcInfoContentGenerator.java
public void threadDump(BundleWriter writer) throws IOException { writer.markStartOfFile("runtime/threads.txt"); ThreadMXBean threadMXBean = ManagementFactory.getThreadMXBean(); ThreadInfo[] threads = threadMXBean.dumpAllThreads(true, true); // Sadly we can't easily do info.toString() as the implementation is hardcoded to cut the stack trace only to 8 // items which does not serve our purpose well. Hence we have custom implementation that prints entire stack trace // for all threads. for (ThreadInfo info : threads) { StringBuilder sb = new StringBuilder( "\"" + info.getThreadName() + "\"" + " Id=" + info.getThreadId() + " " + info.getThreadState()); if (info.getLockName() != null) { sb.append(" on " + info.getLockName()); }/* w ww .ja va 2s. c o m*/ 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)"); } sb.append('\n'); int i = 0; for (StackTraceElement ste : info.getStackTrace()) { if (i == 0 && info.getLockInfo() != null) { Thread.State ts = info.getThreadState(); switch (ts) { case BLOCKED: sb.append("\t- blocked on " + info.getLockInfo()); sb.append('\n'); break; case WAITING: sb.append("\t- waiting on " + info.getLockInfo()); sb.append('\n'); break; case TIMED_WAITING: sb.append("\t- waiting on " + info.getLockInfo()); sb.append('\n'); break; default: } } sb.append("\tat " + ste.toString()); sb.append('\n'); i++; for (MonitorInfo mi : info.getLockedMonitors()) { if (mi.getLockedStackDepth() == i) { sb.append("\t- locked " + mi); sb.append('\n'); } } } LockInfo[] locks = info.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'); writer.write(sb.toString()); } writer.markEndOfFile(); }
From source file:com.thoughtworks.go.server.service.support.ServerRuntimeInformationProvider.java
private void threadInfo(ThreadMXBean threadMXBean, InformationStringBuilder builder) { builder.addSection("Thread information"); builder.append(String.format("Current: %s, Total: %s, Daemon: %s, Peak: %s\n", threadMXBean.getThreadCount(), threadMXBean.getTotalStartedThreadCount(), threadMXBean.getDaemonThreadCount(), threadMXBean.getPeakThreadCount())); long[] deadlockedThreads = threadMXBean.findDeadlockedThreads(); if (deadlockedThreads != null && deadlockedThreads.length > 0) { builder.append(String.format("Found %s dead locked threads. Here is there information.\n", deadlockedThreads.length)); for (long deadlockedThread : deadlockedThreads) { ThreadInfo threadInfo = threadMXBean.getThreadInfo(deadlockedThread); LockInfo lockInfo = threadInfo.getLockInfo(); if (lockInfo != null) { builder.append(String.format("LockInfo: %s", lockInfo)); } else { builder.append("This thread is not waiting for any locks\n"); }/*from w w w . j ava 2 s . com*/ builder.append(String.format("Monitor Info - Stack Frame where locks were taken.\n")); MonitorInfo[] lockedMonitors = threadInfo.getLockedMonitors(); for (MonitorInfo lockedMonitor : lockedMonitors) { builder.append(String.format("Monitor for class '%s' taken at stack frame '%s'.", lockedMonitor.getClassName(), lockedMonitor.getLockedStackFrame())); } builder.append("The stack trace of the deadlocked thread\n"); builder.append(Arrays.toString(threadInfo.getStackTrace())); } } builder.addSubSection("All thread stacktraces"); ThreadInfo[] threadInfos = threadMXBean.dumpAllThreads(true, true); for (ThreadInfo threadInfo : threadInfos) { builder.append(String.format("%s, %s, %s\n", threadInfo.getThreadId(), threadInfo.getThreadName(), threadInfo.getThreadState())); MonitorInfo[] lockedMonitors = threadInfo.getLockedMonitors(); builder.append("Locked Monitors:\n"); for (MonitorInfo lockedMonitor : lockedMonitors) { builder.append(String.format("%s at %s", lockedMonitor, lockedMonitor.getLockedStackFrame())); } LockInfo[] lockedSynchronizers = threadInfo.getLockedSynchronizers(); builder.append("Locked Synchronizers:\n"); for (LockInfo lockedSynchronizer : lockedSynchronizers) { builder.append(lockedSynchronizer); } builder.append("Stacktrace:\n "); builder.append(StringUtils.join(threadInfo.getStackTrace(), "\n ")); builder.append("\n\n"); } }
From source file:com.thoughtworks.go.server.service.support.ThreadInformationProvider.java
@Override public void appendInformation(InformationStringBuilder builder) { ThreadMXBean threadMXBean = ManagementFactory.getThreadMXBean(); builder.addSection("Thread information"); builder.append(String.format("Current: %s, Total: %s, Daemon: %s, Peak: %s\n", threadMXBean.getThreadCount(), threadMXBean.getTotalStartedThreadCount(), threadMXBean.getDaemonThreadCount(), threadMXBean.getPeakThreadCount())); long[] deadlockedThreads = threadMXBean.findDeadlockedThreads(); if (deadlockedThreads != null && deadlockedThreads.length > 0) { builder.append(String.format("Found %s dead locked threads. Here is there information.\n", deadlockedThreads.length)); for (long deadlockedThread : deadlockedThreads) { ThreadInfo threadInfo = threadMXBean.getThreadInfo(deadlockedThread); LockInfo lockInfo = threadInfo.getLockInfo(); if (lockInfo != null) { builder.append(String.format("LockInfo: %s", lockInfo)); } else { builder.append("This thread is not waiting for any locks\n"); }//from w w w.j a v a2 s .c o m builder.append(String.format("Monitor Info - Stack Frame where locks were taken.\n")); MonitorInfo[] lockedMonitors = threadInfo.getLockedMonitors(); for (MonitorInfo lockedMonitor : lockedMonitors) { builder.append(String.format("Monitor for class '%s' taken at stack frame '%s'.", lockedMonitor.getClassName(), lockedMonitor.getLockedStackFrame())); } builder.append("The stack trace of the deadlocked thread\n"); builder.append(Arrays.toString(threadInfo.getStackTrace())); } } builder.addSubSection("All thread stacktraces"); ThreadInfo[] threadInfos = threadMXBean.dumpAllThreads(true, true); for (ThreadInfo threadInfo : threadInfos) { builder.append(String.format("%s, %s, %s\n", threadInfo.getThreadId(), threadInfo.getThreadName(), threadInfo.getThreadState())); MonitorInfo[] lockedMonitors = threadInfo.getLockedMonitors(); builder.append("Locked Monitors:\n"); for (MonitorInfo lockedMonitor : lockedMonitors) { builder.append(String.format("%s at %s", lockedMonitor, lockedMonitor.getLockedStackFrame())); } LockInfo[] lockedSynchronizers = threadInfo.getLockedSynchronizers(); builder.append("Locked Synchronizers:\n"); for (LockInfo lockedSynchronizer : lockedSynchronizers) { builder.append(lockedSynchronizer); } builder.append("Stacktrace:\n "); builder.append(StringUtils.join(threadInfo.getStackTrace(), "\n ")); builder.append("\n\n"); } }
From source file:org.apache.tajo.worker.TajoWorker.java
public void dumpThread(Writer writer) { PrintWriter stream = new PrintWriter(writer); int STACK_DEPTH = 20; boolean contention = threadBean.isThreadContentionMonitoringEnabled(); long[] threadIds = threadBean.getAllThreadIds(); stream.println("Process Thread Dump: Tajo Worker"); stream.println(threadIds.length + " active threads"); for (long tid : threadIds) { ThreadInfo info = threadBean.getThreadInfo(tid, STACK_DEPTH); if (info == null) { stream.println(" Inactive"); continue; }//from ww w . j ava 2s . c o m stream.println("Thread " + getThreadTaskName(info.getThreadId(), info.getThreadName()) + ":"); Thread.State state = info.getThreadState(); stream.println(" State: " + state + ", Blocked count: " + info.getBlockedCount() + ", Waited count: " + info.getWaitedCount()); if (contention) { stream.println( " Blocked time: " + info.getBlockedTime() + ", Waited time: " + info.getWaitedTime()); } if (state == Thread.State.WAITING) { stream.println(" Waiting on " + info.getLockName()); } else if (state == Thread.State.BLOCKED) { stream.println(" Blocked on " + info.getLockName() + ", Blocked by " + getThreadTaskName(info.getLockOwnerId(), info.getLockOwnerName())); } stream.println(" Stack:"); for (StackTraceElement frame : info.getStackTrace()) { stream.println(" " + frame.toString()); } stream.println(""); } }
From source file:org.apache.tajo.master.TajoMaster.java
public void dumpThread(Writer writer) { PrintWriter stream = new PrintWriter(writer); int STACK_DEPTH = 20; boolean contention = threadBean.isThreadContentionMonitoringEnabled(); long[] threadIds = threadBean.getAllThreadIds(); stream.println("Process Thread Dump: Tajo Worker"); stream.println(threadIds.length + " active threads"); for (long tid : threadIds) { ThreadInfo info = threadBean.getThreadInfo(tid, STACK_DEPTH); if (info == null) { stream.println(" Inactive"); continue; }/*from w w w .j ava 2 s .c o m*/ stream.println("Thread " + getThreadTaskName(info.getThreadId(), info.getThreadName()) + ":"); Thread.State state = info.getThreadState(); stream.println(" State: " + state + ", Blocked count: " + info.getBlockedCount() + ", Waited count: " + info.getWaitedCount()); if (contention) { stream.println( " Blocked time: " + info.getBlockedTime() + ", Waited time: " + info.getWaitedTime()); } if (state == Thread.State.WAITING) { stream.println(" Waiting on " + info.getLockName()); } else if (state == Thread.State.BLOCKED) { stream.println(" Blocked on " + info.getLockName() + ", Blocked by " + getThreadTaskName(info.getLockOwnerId(), info.getLockOwnerName())); } stream.println(" Stack:"); for (StackTraceElement frame : info.getStackTrace()) { stream.println(" " + frame.toString()); } stream.println(""); } }
From source file:org.ScripterRon.JavaBitcoin.RpcHandler.java
/** * Process 'getstacktraces' request/*w ww. j av a2 s .c o m*/ * * Response parameters: * locks - An array of lock objects for locks with waiters * threads - An array of thread objects * * Lock object: * name - Lock class name * hash - Lock identity hash code * thread - Identifier of thread holding the lock * * Monitor object: * name - Monitor class name * hash - Monitor identity hash * depth - Stack depth where monitor locked * trace - Stack element where monitor locked * * Thread object: * blocked - Lock object if thread is waiting on a lock * id - Thread identifier * locks - Array of monitor objects for locks held by this thread * name - Thread name * state - Thread state * trace - Array of stack trace elements * * @return Response as a JSONObject */ private JSONObject getStackTraces() { JSONArray threadsJSON = new JSONArray(); JSONArray locksJSON = new JSONArray(); ThreadMXBean tmxBean = ManagementFactory.getThreadMXBean(); boolean tmxMI = tmxBean.isObjectMonitorUsageSupported(); ThreadInfo[] tList = tmxBean.dumpAllThreads(tmxMI, false); // // Generate the response // for (ThreadInfo tInfo : tList) { JSONObject threadJSON = new JSONObject(); // // General thread information // threadJSON.put("id", tInfo.getThreadId()); threadJSON.put("name", tInfo.getThreadName()); threadJSON.put("state", tInfo.getThreadState().toString()); // // Gather lock usage // if (tmxMI) { MonitorInfo[] mList = tInfo.getLockedMonitors(); if (mList.length > 0) { JSONArray monitorsJSON = new JSONArray(); for (MonitorInfo mInfo : mList) { JSONObject lockJSON = new JSONObject(); lockJSON.put("name", mInfo.getClassName()); lockJSON.put("hash", mInfo.getIdentityHashCode()); lockJSON.put("depth", mInfo.getLockedStackDepth()); lockJSON.put("trace", mInfo.getLockedStackFrame().toString()); monitorsJSON.add(lockJSON); } threadJSON.put("locks", monitorsJSON); } if (tInfo.getThreadState() == Thread.State.BLOCKED) { LockInfo lInfo = tInfo.getLockInfo(); if (lInfo != null) { JSONObject lockJSON = new JSONObject(); lockJSON.put("name", lInfo.getClassName()); lockJSON.put("hash", lInfo.getIdentityHashCode()); lockJSON.put("thread", tInfo.getLockOwnerId()); threadJSON.put("blocked", lockJSON); boolean addLock = true; for (Object lock : locksJSON) { if (((String) ((JSONObject) lock).get("name")).equals(lInfo.getClassName())) { addLock = false; break; } } if (addLock) locksJSON.add(lockJSON); } } } // // Add the stack trace // StackTraceElement[] elements = tInfo.getStackTrace(); JSONArray traceJSON = new JSONArray(); for (StackTraceElement element : elements) traceJSON.add(element.toString()); threadJSON.put("trace", traceJSON); // // Add the thread to the response // threadsJSON.add(threadJSON); } // // Return the response // JSONObject response = new JSONObject(); response.put("threads", threadsJSON); response.put("locks", locksJSON); return response; }
From source file:com.thoughtworks.go.server.service.support.ThreadInformationProvider.java
private TreeMap<Long, Map<String, Object>> getStackTraceInformation(ThreadMXBean threadMXBean) { TreeMap<Long, Map<String, Object>> traces = new TreeMap<>(); ThreadInfo[] threadInfos = threadMXBean.dumpAllThreads(true, true); for (ThreadInfo threadInfo : threadInfos) { LinkedHashMap<String, Object> threadStackTrace = new LinkedHashMap<>(); threadStackTrace.put("Id", threadInfo.getThreadId()); threadStackTrace.put("Name", threadInfo.getThreadName()); threadStackTrace.put("State", threadInfo.getThreadState()); LinkedHashMap<String, Object> lockMonitorInfo = new LinkedHashMap<>(); MonitorInfo[] lockedMonitors = threadInfo.getLockedMonitors(); ArrayList<Map<String, Object>> lockedMonitorsJson = new ArrayList<>(); for (MonitorInfo lockedMonitor : lockedMonitors) { LinkedHashMap<String, Object> lockedMonitorJson = new LinkedHashMap<>(); lockedMonitorJson.put("Class", lockedMonitor.getClassName()); lockedMonitorJson.put("IdentityHashCode", lockedMonitor.getIdentityHashCode()); lockedMonitorJson.put("LockedStackDepth", lockedMonitor.getLockedStackDepth()); lockedMonitorJson.put("StackFrame", lockedMonitor.getLockedStackFrame().toString()); lockedMonitorsJson.add(lockedMonitorJson); }/*from w ww . jav a 2 s . com*/ lockMonitorInfo.put("Locked Monitors", lockedMonitorsJson); lockMonitorInfo.put("Locked Synchronizers", asJSON(threadInfo.getLockedSynchronizers())); threadStackTrace.put("Lock Monitor Info", lockMonitorInfo); LinkedHashMap<String, Object> blockedInfo = new LinkedHashMap<>(); blockedInfo.put("Blocked Time", threadInfo.getBlockedTime() == -1 ? null : threadInfo.getBlockedTime()); blockedInfo.put("Blocked Count", threadInfo.getBlockedCount()); threadStackTrace.put("Blocked Info", blockedInfo); LinkedHashMap<String, Object> timeInfo = new LinkedHashMap<>(); timeInfo.put("Waited Time", threadInfo.getWaitedTime() == -1 ? null : threadInfo.getWaitedTime()); timeInfo.put("Waited Count", threadInfo.getWaitedCount()); threadStackTrace.put("Time Info", timeInfo); LinkedHashMap<String, Object> lockInfoMap = new LinkedHashMap<>(); LockInfo lockInfo = threadInfo.getLockInfo(); lockInfoMap.put("Locked On", asJSON(lockInfo)); lockInfoMap.put("Lock Owner Thread Id", threadInfo.getLockOwnerId() == -1 ? null : threadInfo.getLockOwnerId()); lockInfoMap.put("Lock Owner Thread Name", threadInfo.getLockOwnerName()); threadStackTrace.put("Lock Info", lockInfoMap); LinkedHashMap<String, Object> stateInfo = new LinkedHashMap<>(); stateInfo.put("Suspended", threadInfo.isSuspended()); stateInfo.put("InNative", threadInfo.isInNative()); threadStackTrace.put("State Info", stateInfo); threadStackTrace.put("Stack Trace", asJSON(threadInfo.getStackTrace())); traces.put(threadInfo.getThreadId(), threadStackTrace); } return traces; }