Search in sources :

Example 11 with ThreadInfo

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

the class ThreadDeadlockDetectorTest method returnsASetOfThreadsIfAnyAreDeadlocked.

@Test
public void returnsASetOfThreadsIfAnyAreDeadlocked() throws Exception {
    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 12 with ThreadInfo

use of java.lang.management.ThreadInfo in project elasticsearch by elastic.

the class SimpleThreadPoolIT method testThreadNames.

public void testThreadNames() throws Exception {
    ThreadMXBean threadBean = ManagementFactory.getThreadMXBean();
    Set<String> preNodeStartThreadNames = new HashSet<>();
    for (long l : threadBean.getAllThreadIds()) {
        ThreadInfo threadInfo = threadBean.getThreadInfo(l);
        if (threadInfo != null) {
            preNodeStartThreadNames.add(threadInfo.getThreadName());
        }
    }
    logger.info("pre node threads are {}", preNodeStartThreadNames);
    String node = internalCluster().startNode();
    logger.info("do some indexing, flushing, optimize, and searches");
    int numDocs = randomIntBetween(2, 100);
    IndexRequestBuilder[] builders = new IndexRequestBuilder[numDocs];
    for (int i = 0; i < numDocs; ++i) {
        builders[i] = client().prepareIndex("idx", "type").setSource(jsonBuilder().startObject().field("str_value", "s" + i).array("str_values", new String[] { "s" + (i * 2), "s" + (i * 2 + 1) }).field("l_value", i).array("l_values", new int[] { i * 2, i * 2 + 1 }).field("d_value", i).array("d_values", new double[] { i * 2, i * 2 + 1 }).endObject());
    }
    indexRandom(true, builders);
    int numSearches = randomIntBetween(2, 100);
    for (int i = 0; i < numSearches; i++) {
        assertNoFailures(client().prepareSearch("idx").setQuery(QueryBuilders.termQuery("str_value", "s" + i)).get());
        assertNoFailures(client().prepareSearch("idx").setQuery(QueryBuilders.termQuery("l_value", i)).get());
    }
    Set<String> threadNames = new HashSet<>();
    for (long l : threadBean.getAllThreadIds()) {
        ThreadInfo threadInfo = threadBean.getThreadInfo(l);
        if (threadInfo != null) {
            threadNames.add(threadInfo.getThreadName());
        }
    }
    logger.info("post node threads are {}", threadNames);
    threadNames.removeAll(preNodeStartThreadNames);
    logger.info("post node *new* threads are {}", threadNames);
    for (String threadName : threadNames) {
        // or the ones that are occasionally come up from ESSingleNodeTestCase
        if (// TODO: this can't possibly be right! single node and integ test are unrelated!
        threadName.contains("[node_s_0]") || threadName.contains("Keep-Alive-Timer")) {
            continue;
        }
        String nodePrefix = "(" + Pattern.quote(InternalTestCluster.TRANSPORT_CLIENT_PREFIX) + ")?(" + Pattern.quote(ESIntegTestCase.SUITE_CLUSTER_NODE_PREFIX) + "|" + Pattern.quote(ESIntegTestCase.TEST_CLUSTER_NODE_PREFIX) + "|" + Pattern.quote("node_tribe2") + ")";
        assertThat(threadName, RegexMatcher.matches("\\[" + nodePrefix + "\\d+\\]"));
    }
}
Also used : IndexRequestBuilder(org.elasticsearch.action.index.IndexRequestBuilder) ThreadMXBean(java.lang.management.ThreadMXBean) ThreadInfo(java.lang.management.ThreadInfo) Matchers.containsString(org.hamcrest.Matchers.containsString) HashSet(java.util.HashSet)

Example 13 with ThreadInfo

use of java.lang.management.ThreadInfo in project jetty.project by eclipse.

the class JavaMonitorTools method sampleThreads.

private synchronized void sampleThreads() {
    if ((lastSampled + 50L) < System.currentTimeMillis()) {
        lastSampled = System.currentTimeMillis();
        for (final Thread.State state : Thread.State.values()) {
            states.put(state, 0);
        }
        for (final ThreadInfo thread : threadMXBean.getThreadInfo(threadMXBean.getAllThreadIds())) {
            if (thread != null) {
                final Thread.State state = thread.getThreadState();
                states.put(state, states.get(state) + 1);
            } else {
                states.put(Thread.State.TERMINATED, states.get(Thread.State.TERMINATED) + 1);
            }
        }
    }
}
Also used : ThreadInfo(java.lang.management.ThreadInfo)

Example 14 with ThreadInfo

use of java.lang.management.ThreadInfo in project disruptor by LMAX-Exchange.

the class BasicExecutor method dumpThreadInfo.

private String dumpThreadInfo() {
    final StringBuilder sb = new StringBuilder();
    final ThreadMXBean threadMXBean = ManagementFactory.getThreadMXBean();
    for (Thread t : threads) {
        ThreadInfo threadInfo = threadMXBean.getThreadInfo(t.getId());
        sb.append("{");
        sb.append("name=").append(t.getName()).append(",");
        sb.append("id=").append(t.getId()).append(",");
        sb.append("state=").append(threadInfo.getThreadState()).append(",");
        sb.append("lockInfo=").append(threadInfo.getLockInfo());
        sb.append("}");
    }
    return sb.toString();
}
Also used : ThreadMXBean(java.lang.management.ThreadMXBean) ThreadInfo(java.lang.management.ThreadInfo)

Example 15 with ThreadInfo

use of java.lang.management.ThreadInfo in project Openfire by igniterealtime.

the class Threading method sampleThreads.

private synchronized void sampleThreads() {
    if ((lastSampled + 50L) < System.currentTimeMillis()) {
        lastSampled = System.currentTimeMillis();
        for (final Thread.State state : Thread.State.values()) {
            states.put(state, 0);
        }
        for (final ThreadInfo thread : threadMXBean.getThreadInfo(threadMXBean.getAllThreadIds())) {
            if (thread != null) {
                final Thread.State state = thread.getThreadState();
                states.put(state, states.get(state) + 1);
            } else {
                states.put(Thread.State.TERMINATED, states.get(Thread.State.TERMINATED) + 1);
            }
        }
    }
}
Also used : ThreadInfo(java.lang.management.ThreadInfo)

Aggregations

ThreadInfo (java.lang.management.ThreadInfo)107 ThreadMXBean (java.lang.management.ThreadMXBean)43 HashMap (java.util.HashMap)9 IOException (java.io.IOException)8 ArrayList (java.util.ArrayList)8 HashSet (java.util.HashSet)6 Map (java.util.Map)6 LockInfo (java.lang.management.LockInfo)5 MonitorInfo (java.lang.management.MonitorInfo)5 Date (java.util.Date)4 CountDownLatch (java.util.concurrent.CountDownLatch)4 Test (org.junit.Test)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 ByteArrayOutputStream (java.io.ByteArrayOutputStream)2