Search in sources :

Example 41 with ThreadInfo

use of java.lang.management.ThreadInfo in project processdash by dtuma.

the class ThreadMonitor method findDeadlock.

/**
     * Checks if any threads are deadlocked. If any, print
     * the thread dump information.
     */
public boolean findDeadlock() {
    long[] tids = tmbean.findMonitorDeadlockedThreads();
    if (tids == null) {
        return false;
    } else {
        System.out.println("Deadlock found :-");
        ThreadInfo[] tinfos = tmbean.getThreadInfo(tids, Integer.MAX_VALUE);
        for (ThreadInfo ti : tinfos) {
            printThreadInfo(ti);
        }
        return true;
    }
}
Also used : ThreadInfo(java.lang.management.ThreadInfo)

Example 42 with ThreadInfo

use of java.lang.management.ThreadInfo in project jdk8u_jdk by JetBrains.

the class AnnotationTypeDeadlockTest method main.

public static void main(String[] args) throws Exception {
    CountDownLatch prepareLatch = new CountDownLatch(2);
    AtomicInteger goLatch = new AtomicInteger(1);
    Task taskA = new Task(prepareLatch, goLatch, AnnA.class);
    Task taskB = new Task(prepareLatch, goLatch, AnnB.class);
    taskA.start();
    taskB.start();
    // wait until both threads start-up
    prepareLatch.await();
    // let them go
    goLatch.set(0);
    // obtain ThreadMXBean
    ThreadMXBean threadBean = ManagementFactory.getThreadMXBean();
    // wait for threads to finish or dead-lock
    while (taskA.isAlive() || taskB.isAlive()) {
        // attempt to join threads
        taskA.join(500L);
        taskB.join(500L);
        // detect dead-lock
        long[] deadlockedIds = threadBean.findMonitorDeadlockedThreads();
        if (deadlockedIds != null && deadlockedIds.length > 0) {
            StringBuilder sb = new StringBuilder("deadlock detected:\n\n");
            for (ThreadInfo ti : threadBean.getThreadInfo(deadlockedIds, Integer.MAX_VALUE)) {
                sb.append(ti);
            }
            throw new IllegalStateException(sb.toString());
        }
    }
}
Also used : ThreadMXBean(java.lang.management.ThreadMXBean) ThreadInfo(java.lang.management.ThreadInfo) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) CountDownLatch(java.util.concurrent.CountDownLatch)

Example 43 with ThreadInfo

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

the class DeadlockDetector method collectFromDependencyMonitor.

private static Set<Dependency> collectFromDependencyMonitor(ThreadMXBean bean, Serializable locality, Map<Long, ThreadInfo> threadInfos) {
    HashSet<Dependency> results = new HashSet<Dependency>();
    // Convert the held resources into serializable dependencies
    Set<Dependency<Serializable, Thread>> heldResources = DependencyMonitorManager.getHeldResources();
    for (Dependency<Serializable, Thread> dep : heldResources) {
        Thread thread = dep.getDependsOn();
        Serializable resource = dep.getDepender();
        ThreadInfo info = threadInfos.get(thread.getId());
        if (info == null) {
            info = bean.getThreadInfo(thread.getId());
        }
        if (info != null) {
            results.add(new Dependency(resource, new LocalThread(locality, info)));
        }
    }
    Set<Dependency<Thread, Serializable>> blockedThreads = DependencyMonitorManager.getBlockedThreads();
    // Convert the blocked threads into serializable dependencies
    for (Dependency<Thread, Serializable> dep : blockedThreads) {
        Thread thread = dep.getDepender();
        ThreadInfo info = threadInfos.get(thread.getId());
        if (info == null) {
            info = bean.getThreadInfo(thread.getId());
        }
        final Serializable resource = dep.getDependsOn();
        results.add(new Dependency(new LocalThread(locality, info), resource));
    }
    return results;
}
Also used : Serializable(java.io.Serializable) ThreadInfo(java.lang.management.ThreadInfo) HashSet(java.util.HashSet) LinkedHashSet(java.util.LinkedHashSet)

Example 44 with ThreadInfo

use of java.lang.management.ThreadInfo 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 45 with ThreadInfo

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

the class VMStats50 method refreshThreads.

private void refreshThreads() {
    if (!THREAD_STATS_ENABLED) {
        return;
    }
    if (this.allThreadIds == null || newThreadsStarted()) {
        this.allThreadIds = threadBean.getAllThreadIds();
        this.threadStartCount = threadBean.getTotalStartedThreadCount();
    }
    ThreadInfo[] threadInfos = threadBean.getThreadInfo(this.allThreadIds, 0);
    for (int i = 0; i < threadInfos.length; i++) {
        long id = this.allThreadIds[i];
        ThreadInfo item = threadInfos[i];
        if (item != null) {
            ThreadStatInfo tsi = threadMap.get(id);
            if (tsi == null) {
                threadMap.put(id, new ThreadStatInfo(item, this.f.createStatistics(threadType, item.getThreadName() + '-' + item.getThreadId(), this.id)));
            } else {
                tsi.ti = item;
            }
        } else {
            ThreadStatInfo tsi = threadMap.remove(id);
            if (tsi != null) {
                tsi.s.close();
            }
        }
    }
    Iterator<Map.Entry<Long, ThreadStatInfo>> it = threadMap.entrySet().iterator();
    while (it.hasNext()) {
        Map.Entry<Long, ThreadStatInfo> me = it.next();
        long id = me.getKey();
        ThreadStatInfo tsi = me.getValue();
        ThreadInfo ti = tsi.ti;
        Statistics s = tsi.s;
        s.setLong(thread_blockedId, ti.getBlockedCount());
        s.setLong(thread_lockOwnerId, ti.getLockOwnerId());
        s.setLong(thread_waitedId, ti.getWaitedCount());
        s.setInt(thread_inNativeId, ti.isInNative() ? 1 : 0);
        s.setInt(thread_suspendedId, ti.isSuspended() ? 1 : 0);
        if (threadBean.isThreadContentionMonitoringSupported() && threadBean.isThreadContentionMonitoringEnabled()) {
            s.setLong(thread_blockedTimeId, ti.getBlockedTime());
            s.setLong(thread_waitedTimeId, ti.getWaitedTime());
        }
        if (threadBean.isThreadCpuTimeSupported() && threadBean.isThreadCpuTimeEnabled()) {
            s.setLong(thread_cpuTimeId, threadBean.getThreadCpuTime(id));
            s.setLong(thread_userTimeId, threadBean.getThreadUserTime(id));
        }
    }
}
Also used : ThreadInfo(java.lang.management.ThreadInfo) HashMap(java.util.HashMap) Map(java.util.Map) Statistics(org.apache.geode.Statistics)

Aggregations

ThreadInfo (java.lang.management.ThreadInfo)124 ThreadMXBean (java.lang.management.ThreadMXBean)56 HashMap (java.util.HashMap)10 IOException (java.io.IOException)8 ArrayList (java.util.ArrayList)8 HashSet (java.util.HashSet)7 Map (java.util.Map)7 LockInfo (java.lang.management.LockInfo)6 MonitorInfo (java.lang.management.MonitorInfo)6 Test (org.junit.Test)5 Date (java.util.Date)4 CountDownLatch (java.util.concurrent.CountDownLatch)4 FileOutputStream (java.io.FileOutputStream)3 PrintStream (java.io.PrintStream)3 PrintWriter (java.io.PrintWriter)3 Method (java.lang.reflect.Method)3 LinkedHashSet (java.util.LinkedHashSet)3 TreeMap (java.util.TreeMap)3 ExecutorService (java.util.concurrent.ExecutorService)3 HealthCheckResultEntry (fish.payara.notification.healthcheck.HealthCheckResultEntry)2