Search in sources :

Example 11 with LockInfo

use of java.lang.management.LockInfo in project geode by apache.

the class DeadlockDetector method collectAllDependencies.

/**
   * Collect all of the dependencies that exist between threads in this VM, using java management
   * beans and the {@link DependencyMonitor}.
   * 
   * Threads may depend on locks, or on other resources that are tracked by the
   * {@link DependencyMonitor}.
   * 
   * @param locality a name tag to stick on entities to help associate them with this JVM and
   *        distinguish them from entities from other jvms
   * 
   * @return All of the dependencies between threads and locks or other resources on this VM.
   */
public static Set<Dependency> collectAllDependencies(Serializable locality) {
    ThreadMXBean bean = ManagementFactory.getThreadMXBean();
    ThreadInfo[] infos = bean.dumpAllThreads(true, true);
    Set<Dependency> results = new HashSet<Dependency>();
    Map<Long, ThreadInfo> threadInfos = new HashMap<Long, ThreadInfo>();
    for (ThreadInfo info : infos) {
        // This can happen if the thread died.
        if (info == null) {
            continue;
        }
        for (LockInfo monitor : info.getLockedMonitors()) {
            Dependency dependency = new Dependency(new LocalLockInfo(locality, monitor), new LocalThread(locality, info));
            results.add(dependency);
        }
        for (LockInfo sync : info.getLockedSynchronizers()) {
            Dependency dependency = new Dependency(new LocalLockInfo(locality, sync), new LocalThread(locality, info));
            results.add(dependency);
        }
        LockInfo waitingFor = info.getLockInfo();
        if (waitingFor != null) {
            Dependency dependency = new Dependency(new LocalThread(locality, info), new LocalLockInfo(locality, waitingFor));
            results.add(dependency);
        }
        threadInfos.put(info.getThreadId(), info);
    }
    Set<Dependency> monitoredDependencies = collectFromDependencyMonitor(bean, locality, threadInfos);
    results.addAll(monitoredDependencies);
    return results;
}
Also used : ThreadMXBean(java.lang.management.ThreadMXBean) ThreadInfo(java.lang.management.ThreadInfo) HashMap(java.util.HashMap) LockInfo(java.lang.management.LockInfo) HashSet(java.util.HashSet) LinkedHashSet(java.util.LinkedHashSet)

Example 12 with LockInfo

use of java.lang.management.LockInfo in project metrics by dropwizard.

the class ThreadDump method dump.

/**
 * Dumps all of the threads' current information to an output stream.
 *
 * @param out an output stream
 */
public void dump(OutputStream out) {
    final ThreadInfo[] threads = this.threadMXBean.dumpAllThreads(true, true);
    final PrintWriter writer = new PrintWriter(new OutputStreamWriter(out, UTF_8));
    for (int ti = threads.length - 1; ti >= 0; ti--) {
        final ThreadInfo t = threads[ti];
        writer.printf("\"%s\" id=%d state=%s", t.getThreadName(), t.getThreadId(), t.getThreadState());
        final LockInfo lock = t.getLockInfo();
        if (lock != null && t.getThreadState() != Thread.State.BLOCKED) {
            writer.printf("%n    - waiting on <0x%08x> (a %s)", lock.getIdentityHashCode(), lock.getClassName());
            writer.printf("%n    - locked <0x%08x> (a %s)", lock.getIdentityHashCode(), lock.getClassName());
        } else if (lock != null && t.getThreadState() == Thread.State.BLOCKED) {
            writer.printf("%n    - waiting to lock <0x%08x> (a %s)", lock.getIdentityHashCode(), lock.getClassName());
        }
        if (t.isSuspended()) {
            writer.print(" (suspended)");
        }
        if (t.isInNative()) {
            writer.print(" (running in native)");
        }
        writer.println();
        if (t.getLockOwnerName() != null) {
            writer.printf("     owned by %s id=%d%n", t.getLockOwnerName(), t.getLockOwnerId());
        }
        final StackTraceElement[] elements = t.getStackTrace();
        final MonitorInfo[] monitors = t.getLockedMonitors();
        for (int i = 0; i < elements.length; i++) {
            final StackTraceElement element = elements[i];
            writer.printf("    at %s%n", element);
            for (int j = 1; j < monitors.length; j++) {
                final MonitorInfo monitor = monitors[j];
                if (monitor.getLockedStackDepth() == i) {
                    writer.printf("      - locked %s%n", monitor);
                }
            }
        }
        writer.println();
        final LockInfo[] locks = t.getLockedSynchronizers();
        if (locks.length > 0) {
            writer.printf("    Locked synchronizers: count = %d%n", locks.length);
            for (LockInfo l : locks) {
                writer.printf("      - %s%n", l);
            }
            writer.println();
        }
    }
    writer.println();
    writer.flush();
}
Also used : ThreadInfo(java.lang.management.ThreadInfo) MonitorInfo(java.lang.management.MonitorInfo) OutputStreamWriter(java.io.OutputStreamWriter) LockInfo(java.lang.management.LockInfo) PrintWriter(java.io.PrintWriter)

Example 13 with LockInfo

use of java.lang.management.LockInfo in project knime-core by knime.

the class GUIDeadlockDetector method fillStackFromThread.

private static void fillStackFromThread(final ThreadInfo ti, final StringBuilder buf) {
    buf.append("\"" + ti.getThreadName() + "\" Id=" + ti.getThreadId() + " " + ti.getThreadState());
    if (ti.getLockName() != null) {
        buf.append(" on " + ti.getLockName());
    }
    if (ti.getLockOwnerName() != null) {
        buf.append(" owned by \"" + ti.getLockOwnerName() + "\" Id=" + ti.getLockOwnerId());
    }
    if (ti.isSuspended()) {
        buf.append(" (suspended)");
    }
    if (ti.isInNative()) {
        buf.append(" (in native)");
    }
    buf.append('\n');
    int i = 0;
    for (StackTraceElement ste : ti.getStackTrace()) {
        buf.append("\tat " + ste.toString());
        buf.append('\n');
        if ((i == 0) && (ti.getLockInfo() != null)) {
            Thread.State ts = ti.getThreadState();
            switch(ts) {
                case BLOCKED:
                    buf.append("\t-  blocked on " + ti.getLockInfo());
                    buf.append('\n');
                    break;
                case WAITING:
                    buf.append("\t-  waiting on " + ti.getLockInfo());
                    buf.append('\n');
                    break;
                case TIMED_WAITING:
                    buf.append("\t-  waiting on " + ti.getLockInfo());
                    buf.append('\n');
                    break;
                default:
            }
        }
        for (MonitorInfo mi : ti.getLockedMonitors()) {
            if (mi.getLockedStackDepth() == i) {
                buf.append("\t-  locked " + mi);
                buf.append('\n');
            }
        }
        i++;
    }
    LockInfo[] locks = ti.getLockedSynchronizers();
    if (locks.length > 0) {
        buf.append("\n\tNumber of locked synchronizers = " + locks.length);
        buf.append('\n');
        for (LockInfo li : locks) {
            buf.append("\t- " + li);
            buf.append('\n');
        }
    }
    buf.append('\n');
}
Also used : MonitorInfo(java.lang.management.MonitorInfo) LockInfo(java.lang.management.LockInfo)

Example 14 with LockInfo

use of java.lang.management.LockInfo in project parseq by linkedin.

the class ThreadDumper method dumpThreadInfoWithLocks.

private void dumpThreadInfoWithLocks(StringBuilder out) {
    ThreadInfo[] tinfos = tmbean.dumpAllThreads(true, true);
    for (ThreadInfo ti : tinfos) {
        dumpThreadInfo(ti, out);
        LockInfo[] syncs = ti.getLockedSynchronizers();
        dumpLockInfo(syncs, out);
    }
    out.append("\n");
    findDeadlock(out);
}
Also used : ThreadInfo(java.lang.management.ThreadInfo) LockInfo(java.lang.management.LockInfo)

Example 15 with LockInfo

use of java.lang.management.LockInfo in project Mindroid.java by Himmele.

the class ConsoleService method addThreadInformation.

@SuppressWarnings("incomplete-switch")
private static void addThreadInformation(StringBuilder builder, ThreadInfo threadInfo) {
    builder.append('"').append(threadInfo.getThreadName()).append('"').append(threadInfo.isDaemon() ? " daemon" : "").append(" prio=").append(threadInfo.getPriority()).append(" id=").append(threadInfo.getThreadId()).append(" ").append(threadInfo.getThreadState());
    if (threadInfo.getLockName() != null) {
        builder.append(" on ").append(threadInfo.getLockName());
    }
    if (threadInfo.getLockOwnerName() != null) {
        builder.append(" owned by \"").append(threadInfo.getLockOwnerName()).append("\" id=").append(threadInfo.getLockOwnerId());
    }
    if (threadInfo.isSuspended()) {
        builder.append(" (suspended)");
    }
    if (threadInfo.isInNative()) {
        builder.append(" (in native)");
    }
    builder.append('\n');
    StackTraceElement[] stackTrace = threadInfo.getStackTrace();
    MonitorInfo[] lockedMonitors = threadInfo.getLockedMonitors();
    for (int i = 0; i < stackTrace.length; ++i) {
        StackTraceElement ste = stackTrace[i];
        builder.append("\tat ").append(ste.toString());
        builder.append('\n');
        if (i == 0 && threadInfo.getLockInfo() != null) {
            Thread.State ts = threadInfo.getThreadState();
            switch(ts) {
                case BLOCKED:
                    builder.append("\t-  blocked on ").append(threadInfo.getLockInfo());
                    builder.append('\n');
                    break;
                case WAITING:
                case TIMED_WAITING:
                    builder.append("\t-  waiting on ").append(threadInfo.getLockInfo());
                    builder.append('\n');
                    break;
            }
        }
        for (MonitorInfo mi : lockedMonitors) {
            if (mi.getLockedStackDepth() == i) {
                builder.append("\t-  locked ").append(mi);
                builder.append('\n');
            }
        }
    }
    LockInfo[] locks = threadInfo.getLockedSynchronizers();
    if (locks.length > 0) {
        builder.append("\n\tLocked synchronizers count = ").append(locks.length);
        builder.append('\n');
        for (LockInfo li : locks) {
            builder.append("\t- ").append(li);
            builder.append('\n');
        }
    }
    builder.append('\n');
}
Also used : MonitorInfo(java.lang.management.MonitorInfo) LockInfo(java.lang.management.LockInfo)

Aggregations

LockInfo (java.lang.management.LockInfo)69 MonitorInfo (java.lang.management.MonitorInfo)41 ThreadInfo (java.lang.management.ThreadInfo)17 ThreadMXBean (java.lang.management.ThreadMXBean)9 OutputStreamWriter (java.io.OutputStreamWriter)6 Test (org.testng.annotations.Test)6 ArrayList (java.util.ArrayList)4 BufferedWriter (java.io.BufferedWriter)3 PrintWriter (java.io.PrintWriter)3 CompositeData (javax.management.openmbean.CompositeData)3 TMonitorInfo (com.navercorp.pinpoint.thrift.dto.command.TMonitorInfo)2 TThreadDump (com.navercorp.pinpoint.thrift.dto.command.TThreadDump)2 State (java.lang.Thread.State)2 HashSet (java.util.HashSet)2 CompositeDataSupport (javax.management.openmbean.CompositeDataSupport)2 OpenDataException (javax.management.openmbean.OpenDataException)2 ByteArrayOutputStream (java.io.ByteArrayOutputStream)1 InvocationTargetException (java.lang.reflect.InvocationTargetException)1 HashMap (java.util.HashMap)1 LinkedHashSet (java.util.LinkedHashSet)1