use of java.lang.management.ThreadInfo in project Openfire by igniterealtime.
the class Threading method stacktraces.
private String stacktraces(final ThreadInfo[] threads, final int i) {
if (i >= threads.length) {
return "";
}
final ThreadInfo thread = threads[i];
final StringBuilder trace = new StringBuilder();
for (int stack_i = 0; stack_i < Math.min(thread.getStackTrace().length, MAX_STACK); stack_i++) {
if (stack_i == (MAX_STACK - 1)) {
trace.append(" ...");
} else {
trace.append(" at ").append(thread.getStackTrace()[stack_i]).append("\n");
}
}
return "\"" + thread.getThreadName() + "\", id " + thread.getThreadId() + " is " + thread.getThreadState() + " on " + thread.getLockName() + ", owned by " + thread.getLockOwnerName() + ", id " + thread.getLockOwnerId() + "\n" + trace + "\n\n" + stacktraces(threads, i + 1);
}
use of java.lang.management.ThreadInfo in project netty by netty.
the class TestUtils method dumpThreads.
private static void dumpThreads(File file) {
final String filename = file.toString();
OutputStream out = null;
try {
logger.info("Dumping threads: {}", filename);
final StringBuilder buf = new StringBuilder(8192);
try {
for (ThreadInfo info : ManagementFactory.getThreadMXBean().dumpAllThreads(true, true)) {
buf.append(info);
}
buf.append('\n');
} catch (UnsupportedOperationException ignored) {
logger.warn("Can't dump threads: ThreadMXBean.dumpAllThreads() unsupported");
return;
}
out = new FileOutputStream(file);
out.write(buf.toString().getBytes(CharsetUtil.UTF_8));
} catch (Exception e) {
logger.warn("Failed to dump threads: {}", filename, e);
} finally {
if (out != null) {
try {
out.close();
} catch (IOException ignored) {
// Ignore.
}
}
}
}
use of java.lang.management.ThreadInfo in project presto by prestodb.
the class ThreadResource method getThreadInfo.
@GET
@Path("/v1/thread")
@Produces(MediaType.APPLICATION_JSON)
public static List<Info> getThreadInfo() {
ThreadMXBean mbean = ManagementFactory.getThreadMXBean();
ImmutableList.Builder<Info> builder = ImmutableList.builder();
for (ThreadInfo info : mbean.getThreadInfo(mbean.getAllThreadIds(), Integer.MAX_VALUE)) {
builder.add(new Info(info.getThreadId(), info.getThreadName(), info.getThreadState().name(), info.getLockOwnerId() == -1 ? null : info.getLockOwnerId(), toStackTrace(info.getStackTrace())));
}
return Ordering.from(byName()).sortedCopy(builder.build());
}
use of java.lang.management.ThreadInfo in project randomizedtesting by randomizedtesting.
the class ThreadLeakControl method formatThreadStacksFull.
/** Dump thread state. */
private String formatThreadStacksFull() {
try {
StringBuilder b = new StringBuilder();
b.append("\n==== jstack at approximately timeout time ====\n");
for (ThreadInfo ti : ManagementFactory.getThreadMXBean().dumpAllThreads(true, true)) {
Threads.append(b, ti);
}
b.append("^^==============================================\n");
return b.toString();
} catch (Throwable e) {
// Ignore, perhaps not available.
}
return formatThreadStacks(getThreadsWithTraces());
}
use of java.lang.management.ThreadInfo in project jstorm by alibaba.
the class MetricJstack method dumpThread.
public String dumpThread() throws Exception {
StringBuilder writer = new StringBuilder();
ThreadMXBean threadMXBean = ManagementFactory.getThreadMXBean();
boolean contention = threadMXBean.isThreadContentionMonitoringEnabled();
long[] threadIds = threadMXBean.getAllThreadIds();
writer.append(threadIds.length + " active threads:");
for (long tid : threadIds) {
writer.append(tid).append(" ");
}
writer.append("\n");
long[] deadLockTids = threadMXBean.findDeadlockedThreads();
if (deadLockTids != null) {
writer.append(threadIds.length + " deadlocked threads:");
for (long tid : deadLockTids) {
writer.append(tid).append(" ");
}
writer.append("\n");
}
long[] deadLockMonitorTids = threadMXBean.findMonitorDeadlockedThreads();
if (deadLockMonitorTids != null) {
writer.append(threadIds.length + " deadlocked monitor threads:");
for (long tid : deadLockMonitorTids) {
writer.append(tid).append(" ");
}
writer.append("\n");
}
for (long tid : threadIds) {
ThreadInfo info = threadMXBean.getThreadInfo(tid, Integer.MAX_VALUE);
if (info == null) {
writer.append(" Inactive").append("\n");
continue;
}
writer.append("Thread " + getTaskName(info.getThreadId(), info.getThreadName()) + ":").append("\n");
Thread.State state = info.getThreadState();
writer.append(" State: " + state).append("\n");
writer.append(" Blocked count: " + info.getBlockedCount()).append("\n");
writer.append(" Waited count: " + info.getWaitedCount()).append("\n");
writer.append(" Cpu time:").append(threadMXBean.getThreadCpuTime(tid) / 1000000).append("ms").append("\n");
writer.append(" User time:").append(threadMXBean.getThreadUserTime(tid) / 1000000).append("ms").append("\n");
if (contention) {
writer.append(" Blocked time: " + info.getBlockedTime()).append("\n");
writer.append(" Waited time: " + info.getWaitedTime()).append("\n");
}
if (state == Thread.State.WAITING) {
writer.append(" Waiting on " + info.getLockName()).append("\n");
} else if (state == Thread.State.BLOCKED) {
writer.append(" Blocked on " + info.getLockName()).append("\n");
writer.append(" Blocked by " + getTaskName(info.getLockOwnerId(), info.getLockOwnerName())).append("\n");
}
}
for (long tid : threadIds) {
ThreadInfo info = threadMXBean.getThreadInfo(tid, Integer.MAX_VALUE);
if (info == null) {
writer.append(" Inactive").append("\n");
continue;
}
writer.append("Thread " + getTaskName(info.getThreadId(), info.getThreadName()) + ": Stack").append("\n");
for (StackTraceElement frame : info.getStackTrace()) {
writer.append(" " + frame.toString()).append("\n");
}
}
return writer.toString();
}
Aggregations