use of java.lang.management.ThreadInfo in project tomcat by apache.
the class OneLineFormatter method getThreadName.
/**
* LogRecord has threadID but no thread name.
* LogRecord uses an int for thread ID but thread IDs are longs.
* If the real thread ID > (Integer.MAXVALUE / 2) LogRecord uses it's own
* ID in an effort to avoid clashes due to overflow.
* <p>
* Words fail me to describe what I think of the design decision to use an
* int in LogRecord for a long value and the resulting mess that follows.
*/
private static String getThreadName(int logRecordThreadId) {
Map<Integer, String> cache = threadNameCache.get();
String result = null;
if (logRecordThreadId > (Integer.MAX_VALUE / 2)) {
result = cache.get(Integer.valueOf(logRecordThreadId));
}
if (result != null) {
return result;
}
if (logRecordThreadId > Integer.MAX_VALUE / 2) {
result = UNKNOWN_THREAD_NAME + logRecordThreadId;
} else {
// Double checked locking OK as threadMxBean is volatile
if (threadMxBean == null) {
synchronized (threadMxBeanLock) {
if (threadMxBean == null) {
threadMxBean = ManagementFactory.getThreadMXBean();
}
}
}
ThreadInfo threadInfo = threadMxBean.getThreadInfo(logRecordThreadId);
if (threadInfo == null) {
return Long.toString(logRecordThreadId);
}
result = threadInfo.getThreadName();
}
cache.put(Integer.valueOf(logRecordThreadId), result);
return result;
}
use of java.lang.management.ThreadInfo in project hazelcast by hazelcast.
the class AbstractHazelcastClassRunner method generateThreadDump.
private String generateThreadDump() {
StringBuilder dump = new StringBuilder();
ThreadMXBean threadMXBean = ManagementFactory.getThreadMXBean();
ThreadInfo[] threadInfos = threadMXBean.dumpAllThreads(true, true);
long currentThreadId = Thread.currentThread().getId();
for (ThreadInfo threadInfo : threadInfos) {
long threadId = threadInfo.getThreadId();
if (threadId == currentThreadId) {
continue;
}
dump.append('"');
dump.append(threadInfo.getThreadName());
dump.append("\" ");
Thread.State state = threadInfo.getThreadState();
dump.append("\n\tjava.lang.Thread.State: ");
dump.append(state);
if (threadInfo.getLockName() != null) {
dump.append(", on lock=").append(threadInfo.getLockName());
}
if (threadInfo.getLockOwnerName() != null) {
dump.append(", owned by ").append(threadInfo.getLockOwnerName());
dump.append(", id=").append(threadInfo.getLockOwnerId());
}
if (THREAD_CPU_TIME_INFO_AVAILABLE) {
dump.append(", cpu=").append(threadMXBean.getThreadCpuTime(threadId)).append(" nsecs");
dump.append(", usr=").append(threadMXBean.getThreadUserTime(threadId)).append(" nsecs");
}
if (THREAD_CONTENTION_INFO_AVAILABLE) {
dump.append(", blocked=").append(threadInfo.getBlockedTime()).append(" msecs");
dump.append(", waited=").append(threadInfo.getWaitedTime()).append(" msecs");
}
StackTraceElement[] stackTraceElements = threadInfo.getStackTrace();
for (StackTraceElement stackTraceElement : stackTraceElements) {
dump.append("\n\t\tat ");
dump.append(stackTraceElement);
}
dump.append("\n\n");
}
return dump.toString();
}
use of java.lang.management.ThreadInfo in project deeplearning4j by deeplearning4j.
the class ReflectionUtils method printThreadInfo.
/**
* Print all of the thread's information and stack traces.
*
* @param stream the stream to
* @param title a string title for the stack trace
*/
public static void printThreadInfo(PrintWriter stream, String title) {
final int STACK_DEPTH = 20;
boolean contention = threadBean.isThreadContentionMonitoringEnabled();
long[] threadIds = threadBean.getAllThreadIds();
stream.println("Process Thread Dump: " + title);
stream.println(threadIds.length + " active threads");
for (long tid : threadIds) {
ThreadInfo info = threadBean.getThreadInfo(tid, STACK_DEPTH);
if (info == null) {
stream.println(" Inactive");
continue;
}
stream.println("Thread " + getTaskName(info.getThreadId(), info.getThreadName()) + ":");
Thread.State state = info.getThreadState();
stream.println(" State: " + state);
stream.println(" Blocked count: " + info.getBlockedCount());
stream.println(" Waited count: " + info.getWaitedCount());
if (contention) {
stream.println(" Blocked time: " + info.getBlockedTime());
stream.println(" 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());
stream.println(" Blocked by " + getTaskName(info.getLockOwnerId(), info.getLockOwnerName()));
}
stream.println(" Stack:");
for (StackTraceElement frame : info.getStackTrace()) {
stream.println(" " + frame.toString());
}
}
stream.flush();
}
use of java.lang.management.ThreadInfo in project heron by twitter.
the class JVMMetrics method getThreadsMetrics.
private void getThreadsMetrics() {
// Set the cpu usage for every single thread
if (threadMXBean.isThreadCpuTimeSupported()) {
threadMXBean.setThreadCpuTimeEnabled(true);
long tmpOtherThreadsCpuTime = 0;
long tmpOtherThreadsUserCpuTime = 0;
for (long id : threadMXBean.getAllThreadIds()) {
long cpuTime = threadMXBean.getThreadCpuTime(id);
long cpuUserTime = threadMXBean.getThreadUserTime(id);
ThreadInfo threadInfo = threadMXBean.getThreadInfo(id);
if (threadInfo != null) {
String threadName = threadInfo.getThreadName();
if (threadName.equals(ThreadNames.THREAD_GATEWAY_NAME) || threadName.equals(ThreadNames.THREAD_SLAVE_NAME)) {
threadsCPUTimeNs.scope(threadName).setValue(cpuTime);
threadsUserCPUTimeNs.scope(threadName).setValue(cpuUserTime);
} else {
tmpOtherThreadsCpuTime += cpuTime;
tmpOtherThreadsUserCpuTime += cpuUserTime;
}
}
}
otherThreadsCPUTimeNs.setValue(tmpOtherThreadsCpuTime);
otherThreadsUserCPUTimeNs.setValue(tmpOtherThreadsUserCpuTime);
jvmThreadCount.setValue(threadMXBean.getThreadCount());
jvmDaemonThreadCount.setValue(threadMXBean.getDaemonThreadCount());
}
}
use of java.lang.management.ThreadInfo in project commons by twitter.
the class ContentionPrinter method getLines.
@Override
public Iterable<String> getLines(HttpServletRequest request) {
List<String> lines = Lists.newLinkedList();
ThreadMXBean bean = ManagementFactory.getThreadMXBean();
Map<Long, StackTraceElement[]> threadStacks = Maps.newHashMap();
for (Map.Entry<Thread, StackTraceElement[]> entry : Thread.getAllStackTraces().entrySet()) {
threadStacks.put(entry.getKey().getId(), entry.getValue());
}
Set<Long> lockOwners = Sets.newHashSet();
lines.add("Locked threads:");
for (ThreadInfo t : bean.getThreadInfo(bean.getAllThreadIds())) {
switch(t.getThreadState()) {
case BLOCKED:
case WAITING:
case TIMED_WAITING:
lines.addAll(getThreadInfo(t, threadStacks.get(t.getThreadId())));
if (t.getLockOwnerId() != -1)
lockOwners.add(t.getLockOwnerId());
break;
}
}
if (lockOwners.size() > 0) {
lines.add("\nLock Owners");
for (ThreadInfo t : bean.getThreadInfo(Longs.toArray(lockOwners))) {
lines.addAll(getThreadInfo(t, threadStacks.get(t.getThreadId())));
}
}
return lines;
}
Aggregations