List of usage examples for java.lang.management ThreadInfo getThreadId
public long getThreadId()
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; }/*www. ja v a 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 w w .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: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; }/*w w w .j a va 2s.co 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.kaaproject.kaa.server.common.thrift.cli.server.BaseCliThriftService.java
/** * Dump service threads information./*from w w w . j a v a 2s .co m*/ * * @param writer the writer to output threads information */ private void dumpThreads(PrintWriter writer) { writer.println("THREADS DUMP:"); writer.println(); ThreadMXBean threadMxBean = ManagementFactory.getThreadMXBean(); ThreadInfo[] threads = threadMxBean.dumpAllThreads(false, false); Map<Long, ThreadStruct> threadsMap = new HashMap<>(); for (ThreadInfo ti : threads) { ThreadStruct ts = new ThreadStruct(); ts.ti = ti; threadsMap.put(ti.getThreadId(), ts); } ThreadGroup root = Thread.currentThread().getThreadGroup(); ThreadGroup parent; parent = root.getParent(); while (parent != null) { root = parent; parent = parent.getParent(); } allThreadsFromGroup(root, threadsMap); Collection<ThreadStruct> threadValues = threadsMap.values(); List<ThreadStruct> threadList = new ArrayList<>(threadValues); Collections.sort(threadList); Map<State, Integer> threadStatistics = new LinkedHashMap<>(); threadStatistics.put(State.NEW, 0); threadStatistics.put(State.RUNNABLE, 0); threadStatistics.put(State.BLOCKED, 0); threadStatistics.put(State.WAITING, 0); threadStatistics.put(State.TIMED_WAITING, 0); threadStatistics.put(State.TERMINATED, 0); int maxGroup = 0; int maxName = 0; for (ThreadStruct thread : threadList) { maxName = Math.max(thread.ti.getThreadName().length(), maxName); maxGroup = Math.max(thread.getGroupName().length(), maxGroup); int count = threadStatistics.get(thread.ti.getThreadState()); count++; threadStatistics.put(thread.ti.getThreadState(), count); } StringBuffer header = new StringBuffer(); header.append("ID"); int idColumnLength = 4; int length = idColumnLength; header.append(createPadding(length - header.length())); header.append("GROUP"); int groupColumnLength = maxGroup + 1; length += groupColumnLength; header.append(createPadding(length - header.length())); header.append("NAME"); int nameColumnLength = maxName + 1; length += nameColumnLength; header.append(createPadding(length - header.length())); header.append("PRIORITY"); int priorityColumnLength = 10; length += priorityColumnLength; header.append(createPadding(length - header.length())); header.append("STATE"); int stateColumnLength = 14; length += stateColumnLength; header.append(createPadding(length - header.length())); header.append("DAEMON"); int daemonColumnLengh = 7; length += daemonColumnLengh; header.append(createPadding(length - header.length())); header.append("ALIVE"); int aliveColumnLengh = 6; length += aliveColumnLengh; header.append(createPadding(length - header.length())); header.append("CPU TIME (SEC)"); int cpuTimeColumnLengh = 14; length += cpuTimeColumnLengh; header.append(createPadding(length - header.length())); writer.println(header); int maxRowLength = header.length(); writer.println(createPadding(maxRowLength, '-')); NumberFormat format = new DecimalFormat("0.#"); for (ThreadStruct thread : threadList) { StringBuffer row = new StringBuffer(); row.append(thread.ti.getThreadId()); int rowLength = idColumnLength; row.append(createPadding(rowLength - row.length())); row.append(thread.getGroupName()); rowLength += groupColumnLength; row.append(createPadding(rowLength - row.length())); row.append(thread.ti.getThreadName()); rowLength += nameColumnLength; row.append(createPadding(rowLength - row.length())); row.append(thread.getPriority()); rowLength += priorityColumnLength; row.append(createPadding(rowLength - row.length())); row.append(thread.ti.getThreadState()); rowLength += stateColumnLength; row.append(createPadding(rowLength - row.length())); row.append(thread.isDaemon()); rowLength += daemonColumnLengh; row.append(createPadding(rowLength - row.length())); row.append(thread.isAlive()); rowLength += aliveColumnLengh; row.append(createPadding(rowLength - row.length())); double cpuTimeSec = (double) threadMxBean.getThreadCpuTime(thread.ti.getThreadId()) / (double) (1000 * 1000 * 1000); row.append(format.format(cpuTimeSec)); writer.println(row); } writer.println(createPadding(maxRowLength, '-')); writer.println("SUMMARY:"); writer.println(createPadding(maxRowLength, '-')); for (State state : threadStatistics.keySet()) { int count = threadStatistics.get(state); if (count > 0) { StringBuffer row = new StringBuffer(); row.append(state.toString()); row.append(createPadding(stateColumnLength - row.length())); row.append(count); writer.println(row); } } writer.println(createPadding(maxRowLength, '-')); StringBuffer row = new StringBuffer(); row.append("TOTAL"); row.append(createPadding(stateColumnLength - row.length())); row.append(threadList.size()); writer.println(row); writer.println(createPadding(maxRowLength, '-')); }
From source file:DeadLockDetector.java
/** * Check if there is a DeadLock./*from w w w .j a va 2s . c o m*/ */ @Override public final void run() { boolean deadlock = false; while (!deadlock) { try { long[] ids = tmx.findDeadlockedThreads(); if (ids != null) { /** deadlock found :/ */ deadlock = true; ThreadInfo[] tis = tmx.getThreadInfo(ids, true, true); String info = "DeadLock Found!\n"; for (ThreadInfo ti : tis) { info += ti.toString(); } for (ThreadInfo ti : tis) { LockInfo[] locks = ti.getLockedSynchronizers(); MonitorInfo[] monitors = ti.getLockedMonitors(); if (locks.length == 0 && monitors.length == 0) { /** this thread is deadlocked but its not guilty */ continue; } ThreadInfo dl = ti; info += "Java-level deadlock:\n"; info += "\t" + dl.getThreadName() + " is waiting to lock " + dl.getLockInfo().toString() + " which is held by " + dl.getLockOwnerName() + "\n"; while ((dl = tmx.getThreadInfo(new long[] { dl.getLockOwnerId() }, true, true)[0]) .getThreadId() != ti.getThreadId()) { info += "\t" + dl.getThreadName() + " is waiting to lock " + dl.getLockInfo().toString() + " which is held by " + dl.getLockOwnerName() + "\n"; } } System.out.println(info); if (doWhenDL == RESTART) { System.exit(0); } } Thread.sleep(sleepTime); } catch (Exception e) { System.out.println(e); } } }
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"); }/* www . ja va 2 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: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"); }/*w ww .ja 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"); } }