Search in sources :

Example 76 with ThreadMXBean

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

the class DeployedJarJUnitTest method testMultiThreadingDoesNotCauseDeadlock.

@Test
public void testMultiThreadingDoesNotCauseDeadlock() throws Exception {
    // Add two JARs to the classpath
    byte[] jarBytes = this.classBuilder.createJarFromName("JarClassLoaderJUnitA");
    ClassPathLoader.getLatest().getJarDeployer().deploy("JarClassLoaderJUnitA.jar", jarBytes);
    jarBytes = this.classBuilder.createJarFromClassContent("com/jcljunit/JarClassLoaderJUnitB", "package com.jcljunit; public class JarClassLoaderJUnitB {}");
    ClassPathLoader.getLatest().getJarDeployer().deploy("JarClassLoaderJUnitB.jar", jarBytes);
    String[] classNames = new String[] { "JarClassLoaderJUnitA", "com.jcljunit.JarClassLoaderJUnitB", "NON-EXISTENT CLASS" };
    final int threadCount = 10;
    ExecutorService executorService = Executors.newFixedThreadPool(threadCount);
    for (int i = 0; i < threadCount; i++) {
        executorService.submit(new ForNameExerciser(classNames));
    }
    executorService.shutdown();
    Awaitility.await().atMost(60, TimeUnit.SECONDS).until(executorService::isTerminated);
    ThreadMXBean threadMXBean = ManagementFactory.getThreadMXBean();
    long[] threadIds = threadMXBean.findDeadlockedThreads();
    if (threadIds != null) {
        StringBuilder deadLockTrace = new StringBuilder();
        for (long threadId : threadIds) {
            ThreadInfo threadInfo = threadMXBean.getThreadInfo(threadId, 100);
            deadLockTrace.append(threadInfo.getThreadName()).append("\n");
            for (StackTraceElement stackTraceElem : threadInfo.getStackTrace()) {
                deadLockTrace.append("\t").append(stackTraceElem).append("\n");
            }
        }
        System.out.println(deadLockTrace);
    }
    assertThat(threadIds).isNull();
}
Also used : ThreadMXBean(java.lang.management.ThreadMXBean) ThreadInfo(java.lang.management.ThreadInfo) ExecutorService(java.util.concurrent.ExecutorService) Test(org.junit.Test) IntegrationTest(org.apache.geode.test.junit.categories.IntegrationTest)

Example 77 with ThreadMXBean

use of java.lang.management.ThreadMXBean in project gerrit by GerritCodeReview.

the class GetSummary method getThreadSummary.

private ThreadSummaryInfo getThreadSummary() {
    Runtime r = Runtime.getRuntime();
    ThreadSummaryInfo threadInfo = new ThreadSummaryInfo();
    threadInfo.cpus = r.availableProcessors();
    threadInfo.threads = toInteger(ManagementFactory.getThreadMXBean().getThreadCount());
    List<String> prefixes = Arrays.asList("H2", "HTTP", "IntraLineDiff", "ReceiveCommits", "SSH git-receive-pack", "SSH git-upload-pack", "SSH-Interactive-Worker", "SSH-Stream-Worker", "SshCommandStart", "sshd-SshServer");
    String other = "Other";
    ThreadMXBean threadMXBean = ManagementFactory.getThreadMXBean();
    threadInfo.counts = new HashMap<>();
    for (long id : threadMXBean.getAllThreadIds()) {
        ThreadInfo info = threadMXBean.getThreadInfo(id);
        if (info == null) {
            continue;
        }
        String name = info.getThreadName();
        Thread.State state = info.getThreadState();
        String group = other;
        for (String p : prefixes) {
            if (name.startsWith(p)) {
                group = p;
                break;
            }
        }
        Map<Thread.State, Integer> counts = threadInfo.counts.get(group);
        if (counts == null) {
            counts = new HashMap<>();
            threadInfo.counts.put(group, counts);
        }
        Integer c = counts.get(state);
        counts.put(state, c != null ? c + 1 : 1);
    }
    return threadInfo;
}
Also used : ThreadMXBean(java.lang.management.ThreadMXBean) ThreadInfo(java.lang.management.ThreadInfo)

Example 78 with ThreadMXBean

use of java.lang.management.ThreadMXBean in project symmetric-ds by JumpMind.

the class MonitorTypeCpu method getNotificationMessage.

protected String getNotificationMessage(long value, long threshold, long period) {
    ThreadInfo[] infos = new ThreadInfo[TOP_THREADS];
    long[] cpuUsages = new long[TOP_THREADS];
    ThreadMXBean threadBean = ManagementFactory.getThreadMXBean();
    for (long threadId : threadBean.getAllThreadIds()) {
        ThreadInfo info = threadBean.getThreadInfo(threadId);
        if (info.getThreadState() != Thread.State.TERMINATED) {
            rankTopUsage(infos, cpuUsages, info, threadBean.getThreadCpuTime(threadId));
        }
    }
    String text = "CPU usage is at " + value;
    for (int i = 0; i < infos.length; i++) {
        text += "Top #" + (i + 1) + " CPU thread " + infos[i].getThreadId() + " is using " + (cpuUsages[i] / 1000000000f) + "s";
        text += logStackTrace(threadBean.getThreadInfo(infos[i].getThreadId(), MAX_STACK_DEPTH));
    }
    return text;
}
Also used : ThreadMXBean(java.lang.management.ThreadMXBean) ThreadInfo(java.lang.management.ThreadInfo) IBuiltInExtensionPoint(org.jumpmind.extension.IBuiltInExtensionPoint)

Example 79 with ThreadMXBean

use of java.lang.management.ThreadMXBean in project symmetric-ds by JumpMind.

the class MonitorTypeMemory method getMessage.

public String getMessage(long value, long threshold, long period) {
    long maxMemory = tenuredPool.getUsage().getMax();
    long usedMemory = tenuredPool.getUsage().getUsed();
    String text = "Memory threshold exceeded, " + usedMemory + " of " + maxMemory;
    ThreadInfo[] infos = new ThreadInfo[TOP_THREADS];
    long[] byteUsages = new long[TOP_THREADS];
    ThreadMXBean threadBean = ManagementFactory.getThreadMXBean();
    for (long threadId : threadBean.getAllThreadIds()) {
        ThreadInfo info = threadBean.getThreadInfo(threadId);
        if (info.getThreadState() != Thread.State.TERMINATED) {
            rankTopUsage(infos, byteUsages, info, getThreadAllocatedBytes(threadBean, threadId));
        }
    }
    for (int i = 0; i < infos.length; i++) {
        text += "Top #" + (i + 1) + " memory thread " + infos[i].getThreadId() + " is using " + String.format("%.1f", ((double) byteUsages[i] / 1048576f)) + "MB";
        text += logStackTrace(threadBean.getThreadInfo(infos[i].getThreadId(), MAX_STACK_DEPTH));
    }
    return text;
}
Also used : ThreadMXBean(java.lang.management.ThreadMXBean) ThreadInfo(java.lang.management.ThreadInfo) IBuiltInExtensionPoint(org.jumpmind.extension.IBuiltInExtensionPoint)

Example 80 with ThreadMXBean

use of java.lang.management.ThreadMXBean in project symmetric-ds by JumpMind.

the class ProcessInfo method getThreadData.

public static ThreadData getThreadData(long threadId) {
    ThreadMXBean threadBean = ManagementFactory.getThreadMXBean();
    ThreadInfo info = threadBean.getThreadInfo(threadId, 100);
    if (info != null) {
        String threadName = info.getThreadName();
        return new ThreadData(threadName, AppUtils.formatStackTrace(info.getStackTrace()));
    } else {
        return null;
    }
}
Also used : ThreadMXBean(java.lang.management.ThreadMXBean) ThreadInfo(java.lang.management.ThreadInfo)

Aggregations

ThreadMXBean (java.lang.management.ThreadMXBean)105 ThreadInfo (java.lang.management.ThreadInfo)56 HashMap (java.util.HashMap)9 RuntimeMXBean (java.lang.management.RuntimeMXBean)8 MemoryMXBean (java.lang.management.MemoryMXBean)7 IOException (java.io.IOException)6 OperatingSystemMXBean (java.lang.management.OperatingSystemMXBean)6 ArrayList (java.util.ArrayList)6 Map (java.util.Map)6 File (java.io.File)5 HashSet (java.util.HashSet)5 Test (org.junit.Test)5 ClassLoadingMXBean (java.lang.management.ClassLoadingMXBean)4 GarbageCollectorMXBean (java.lang.management.GarbageCollectorMXBean)4 MemoryUsage (java.lang.management.MemoryUsage)4 HealthCheckResultEntry (fish.payara.notification.healthcheck.HealthCheckResultEntry)3 HealthCheckResult (fish.payara.nucleus.healthcheck.HealthCheckResult)3 LockInfo (java.lang.management.LockInfo)3 ManagementFactory (java.lang.management.ManagementFactory)3 MonitorInfo (java.lang.management.MonitorInfo)3