Search in sources :

Example 46 with ThreadInfo

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

the class MemberMBeanBridge method fetchJvmThreads.

/**
   * @return JVM thread list
   */
public String[] fetchJvmThreads() {
    long[] threadIds = threadMXBean.getAllThreadIds();
    ThreadInfo[] threadInfos = threadMXBean.getThreadInfo(threadIds, 0);
    if (threadInfos == null || threadInfos.length < 1) {
        return ManagementConstants.NO_DATA_STRING;
    }
    ArrayList<String> thrdStr = new ArrayList<String>(threadInfos.length);
    for (ThreadInfo thInfo : threadInfos) {
        if (thInfo != null) {
            thrdStr.add(thInfo.getThreadName());
        }
    }
    String[] result = new String[thrdStr.size()];
    return thrdStr.toArray(result);
}
Also used : ThreadInfo(java.lang.management.ThreadInfo) ArrayList(java.util.ArrayList)

Example 47 with ThreadInfo

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

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

the class ThreadDeadlockDetectorTest method returnsASetOfThreadsIfAnyAreDeadlocked.

@Test
public void returnsASetOfThreadsIfAnyAreDeadlocked() {
    final ThreadInfo thread1 = mock(ThreadInfo.class);
    when(thread1.getThreadName()).thenReturn("thread1");
    when(thread1.getLockName()).thenReturn("lock2");
    when(thread1.getLockOwnerName()).thenReturn("thread2");
    when(thread1.getStackTrace()).thenReturn(new StackTraceElement[] { new StackTraceElement("Blah", "bloo", "Blah.java", 150), new StackTraceElement("Blah", "blee", "Blah.java", 100) });
    final ThreadInfo thread2 = mock(ThreadInfo.class);
    when(thread2.getThreadName()).thenReturn("thread2");
    when(thread2.getLockName()).thenReturn("lock1");
    when(thread2.getLockOwnerName()).thenReturn("thread1");
    when(thread2.getStackTrace()).thenReturn(new StackTraceElement[] { new StackTraceElement("Blah", "blee", "Blah.java", 100), new StackTraceElement("Blah", "bloo", "Blah.java", 150) });
    final long[] ids = { 1, 2 };
    when(threads.findDeadlockedThreads()).thenReturn(ids);
    when(threads.getThreadInfo(eq(ids), anyInt())).thenReturn(new ThreadInfo[] { thread1, thread2 });
    assertThat(detector.getDeadlockedThreads()).containsOnly(String.format(Locale.US, "thread1 locked on lock2 (owned by thread2):%n" + "\t at Blah.bloo(Blah.java:150)%n" + "\t at Blah.blee(Blah.java:100)%n"), String.format(Locale.US, "thread2 locked on lock1 (owned by thread1):%n" + "\t at Blah.blee(Blah.java:100)%n" + "\t at Blah.bloo(Blah.java:150)%n"));
}
Also used : ThreadInfo(java.lang.management.ThreadInfo) Test(org.junit.Test)

Example 49 with ThreadInfo

use of java.lang.management.ThreadInfo in project suite by stupidsing.

the class Profiler method recordStats.

private void recordStats() {
    long currentThreadId = Thread.currentThread().getId();
    ThreadMXBean mx = ManagementFactory.getThreadMXBean();
    long[] threadIds = mx.getAllThreadIds();
    ThreadInfo[] threadInfos = mx.getThreadInfo(threadIds, stackTraceDepth);
    count.getAndIncrement();
    for (ThreadInfo threadInfo : threadInfos) if (// 
    threadInfo != null && // 
    threadInfo.getThreadId() != currentThreadId && // 
    threadInfo.getThreadState() == State.RUNNABLE && !String_.equals(threadInfo.getThreadName(), "ReaderThread")) {
        StackTraceElement[] stackTrace = threadInfo.getStackTrace();
        Set<String> elements = new HashSet<>();
        int i = stackTrace.length;
        Call call = callRoot;
        // anonymous classes
        while (0 < i) {
            StackTraceElement elem = stackTrace[--i];
            String fileName = elem.getFileName();
            int lineNumber = elem.getLineNumber();
            String mn = elem.getClassName() + "." + elem.getMethodName();
            String fn = fileName != null ? " " + fileName + (1 < lineNumber ? ":" + lineNumber : "") : "<unknown>";
            String name = mn + fn;
            (call = call.callees.computeIfAbsent(mn, any -> new Call())).count++;
            if (elements.add(name))
                records.computeIfAbsent(name, any -> new Record()).count++;
        }
    }
}
Also used : Read(suite.streamlet.Read) Set(java.util.Set) Service(suite.net.Service) HashMap(java.util.HashMap) Timer(java.util.Timer) ThreadMXBean(java.lang.management.ThreadMXBean) HashSet(java.util.HashSet) ThreadInfo(java.lang.management.ThreadInfo) String_(suite.util.String_) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Map(java.util.Map) As(suite.streamlet.As) ManagementFactory(java.lang.management.ManagementFactory) TimerTask(java.util.TimerTask) State(java.lang.Thread.State) ThreadMXBean(java.lang.management.ThreadMXBean) ThreadInfo(java.lang.management.ThreadInfo) Set(java.util.Set) HashSet(java.util.HashSet)

Example 50 with ThreadInfo

use of java.lang.management.ThreadInfo in project suite by stupidsing.

the class ThreadMxBeanTest method test.

@Test
public void test() {
    ThreadMXBean threadBean = ManagementFactory.getThreadMXBean();
    System.out.println(threadBean.getThreadCount());
    ThreadInfo[] threadInfos = threadBean.dumpAllThreads(false, false);
    for (ThreadInfo threadInfo : threadInfos) if (threadInfo.getThreadState() == State.RUNNABLE)
        System.out.println(threadInfo.toString());
}
Also used : ThreadMXBean(java.lang.management.ThreadMXBean) ThreadInfo(java.lang.management.ThreadInfo) Test(org.junit.Test)

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