Search in sources :

Example 96 with ThreadMXBean

use of java.lang.management.ThreadMXBean in project JGroups by belaban.

the class JChannelProbeHandler method handleProbe.

public Map<String, String> handleProbe(String... keys) {
    Map<String, String> map = new TreeMap<>();
    for (String key : keys) {
        if (key.startsWith("jmx")) {
            handleJmx(map, key);
            continue;
        }
        if (key.startsWith("reset-stats")) {
            resetAllStats();
            continue;
        }
        if (key.startsWith("invoke") || key.startsWith("op")) {
            int index = key.indexOf('=');
            if (index != -1) {
                try {
                    handleOperation(map, key.substring(index + 1));
                } catch (Throwable throwable) {
                    log.error(Util.getMessage("OperationInvocationFailure"), key.substring(index + 1), throwable);
                }
            }
            continue;
        }
        if (key.startsWith("threads")) {
            ThreadMXBean bean = ManagementFactory.getThreadMXBean();
            boolean cpu_supported = bean.isThreadCpuTimeSupported();
            boolean contention_supported = bean.isThreadContentionMonitoringSupported();
            int max_name = 0;
            long[] ids = bean.getAllThreadIds();
            List<ThreadEntry> entries = new ArrayList<>(ids.length);
            for (long id : ids) {
                ThreadInfo info = bean.getThreadInfo(id);
                if (info == null)
                    continue;
                String thread_name = info.getThreadName();
                max_name = Math.max(max_name, thread_name.length());
                Thread.State state = info.getThreadState();
                long blocked = info.getBlockedCount();
                long blocked_time = contention_supported ? info.getBlockedTime() : -1;
                long waited = info.getWaitedCount();
                long waited_time = contention_supported ? info.getWaitedTime() : -1;
                double cpu_time = cpu_supported ? bean.getThreadCpuTime(id) : -1;
                if (cpu_time > 0)
                    cpu_time /= 1_000_000;
                double user_time = cpu_supported ? bean.getThreadUserTime(id) : -1;
                if (user_time > 0)
                    user_time /= 1_000_000;
                ThreadEntry entry = new ThreadEntry(state, thread_name, blocked, waited, blocked_time, waited_time, cpu_time, user_time);
                entries.add(entry);
            }
            int index = key.indexOf('=');
            if (index >= 0) {
                Comparator<ThreadEntry> comp = Comparator.comparing(e -> e.thread_name);
                String val = key.substring(index + 1);
                if (val.startsWith("state"))
                    comp = Comparator.comparing(e -> e.state);
                else if (val.startsWith("cpu"))
                    comp = Comparator.comparing((ThreadEntry e) -> e.cpu_time).reversed();
                else if (val.startsWith("user"))
                    comp = Comparator.comparing((ThreadEntry e) -> e.user_time).reversed();
                else if (val.startsWith("block"))
                    comp = Comparator.comparing((ThreadEntry e) -> e.blocks).reversed();
                else if (val.startsWith("btime"))
                    comp = Comparator.comparing((ThreadEntry e) -> e.block_time).reversed();
                else if (val.startsWith("wait"))
                    comp = Comparator.comparing((ThreadEntry e) -> e.waits).reversed();
                else if (val.startsWith("wtime"))
                    comp = Comparator.comparing((ThreadEntry e) -> e.wait_time).reversed();
                entries.sort(comp);
            }
            // see if we need to limit the displayed data
            index = key.indexOf('=', index + 1);
            int limit = 0;
            if (index >= 0) {
                String val = key.substring(index + 1);
                limit = Integer.valueOf(val);
            }
            max_name = Math.min(max_name, 50) + 1;
            String title = "\n[%s]   \t%-" + max_name + "s: %10s %10s %6s %9s %10s %10s\n";
            String line = "[%s]\t%-" + max_name + "s: %,8.0f %,8.0f %,10d %,9.0f %,10d %,10.0f\n";
            StringBuilder sb = new StringBuilder(String.format(title, "state", "thread-name", "cpu (ms)", "user (ms)", "block", "btime (ms)", "wait", "wtime (ms)"));
            Stream<ThreadEntry> stream = entries.stream();
            if (limit > 0)
                stream = stream.limit(limit);
            stream.forEach(e -> sb.append(e.print(line)));
            map.put(key, sb.toString());
            continue;
        }
        if (key.equals("enable-cpu")) {
            map.put(key, enable(1, true));
            continue;
        }
        if (key.startsWith("enable-cont")) {
            map.put(key, enable(2, true));
            continue;
        }
        if (key.equals("disable-cpu")) {
            map.put(key, enable(1, false));
            continue;
        }
        if (key.startsWith("disable-cont")) {
            map.put(key, enable(2, false));
        }
    }
    return map;
}
Also used : ThreadMXBean(java.lang.management.ThreadMXBean) ThreadInfo(java.lang.management.ThreadInfo)

Example 97 with ThreadMXBean

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

the class ErrorUtils method generateThreadDump.

public static String generateThreadDump() {
    final StringBuilder dump = new StringBuilder();
    final ThreadMXBean threadMXBean = ManagementFactory.getThreadMXBean();
    final ThreadInfo[] threadInfos = threadMXBean.getThreadInfo(threadMXBean.getAllThreadIds(), 100);
    for (ThreadInfo threadInfo : threadInfos) {
        dump.append('"');
        dump.append(threadInfo.getThreadName());
        dump.append("\" ");
        final Thread.State state = threadInfo.getThreadState();
        dump.append("\n   java.lang.Thread.State: ");
        dump.append(state);
        final StackTraceElement[] stackTraceElements = threadInfo.getStackTrace();
        for (final StackTraceElement stackTraceElement : stackTraceElements) {
            dump.append("\n        at ");
            dump.append(stackTraceElement);
        }
        dump.append("\n\n");
    }
    return dump.toString();
}
Also used : ThreadMXBean(java.lang.management.ThreadMXBean) ThreadInfo(java.lang.management.ThreadInfo)

Example 98 with ThreadMXBean

use of java.lang.management.ThreadMXBean in project JGroups by belaban.

the class Util method dumpThreads.

public static String dumpThreads() {
    StringBuilder sb = new StringBuilder();
    ThreadMXBean bean = ManagementFactory.getThreadMXBean();
    long[] ids = bean.getAllThreadIds();
    _printThreads(bean, ids, sb);
    long[] deadlocks = bean.findDeadlockedThreads();
    if (deadlocks != null && deadlocks.length > 0) {
        sb.append("deadlocked threads:\n");
        _printThreads(bean, deadlocks, sb);
    }
    deadlocks = bean.findMonitorDeadlockedThreads();
    if (deadlocks != null && deadlocks.length > 0) {
        sb.append("monitor deadlocked threads:\n");
        _printThreads(bean, deadlocks, sb);
    }
    return sb.toString();
}
Also used : ThreadMXBean(java.lang.management.ThreadMXBean)

Example 99 with ThreadMXBean

use of java.lang.management.ThreadMXBean in project jPOS by jpos.

the class SystemMonitor method generateFrozenDump.

private String generateFrozenDump(String indent) {
    RuntimeMXBean runtimeMXBean = ManagementFactory.getRuntimeMXBean();
    ThreadMXBean mxBean = ManagementFactory.getThreadMXBean();
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    PrintStream p = new PrintStream(baos);
    String newIndent = indent + "  ";
    Runtime r = getRuntimeInstance();
    ZoneId zi = ZoneId.systemDefault();
    Instant instant = Instant.now();
    File cwd = new File(".");
    String freeSpace = ISOUtil.readableFileSize(cwd.getFreeSpace());
    String usableSpace = ISOUtil.readableFileSize(cwd.getUsableSpace());
    p.printf("%s           OS: %s (%s)%n", indent, System.getProperty("os.name"), System.getProperty("os.version"));
    int maxKeyLength = 0;
    try {
        maxKeyLength = Cipher.getMaxAllowedKeyLength("AES");
    } catch (NoSuchAlgorithmException ignored) {
    }
    p.printf("%s         Java: %s (%s) AES-%s%n", indent, System.getProperty("java.version"), System.getProperty("java.vendor"), maxKeyLength == Integer.MAX_VALUE ? "secure" : Integer.toString(maxKeyLength));
    p.printf("%s process name: %s%n", indent, runtimeMXBean.getName());
    p.printf("%s         host: %s%n", indent, getLocalHost());
    p.printf("%s          cwd: %s%n", indent, System.getProperty("user.dir"));
    p.printf("%s   free space: %s%n", indent, freeSpace);
    if (!freeSpace.equals(usableSpace))
        p.printf("%s usable space: %s%n", indent, usableSpace);
    p.printf("%s      version: %s (%s)%n", indent, Q2.getVersion(), getRevision());
    p.printf("%s     instance: %s%n", indent, getInstanceIdAsString());
    p.printf("%s       uptime: %s (%f)%n", indent, ISOUtil.millisToString(getServerUptimeAsMillisecond()), loadAverage());
    p.printf("%s   processors: %d%n", indent, r.availableProcessors());
    p.printf("%s       drift : %d%n", indent, delay);
    p.printf("%smemory(t/u/f): %d/%d/%d%n", indent, r.totalMemory() / MB, (r.totalMemory() - r.freeMemory()) / MB, r.freeMemory() / MB);
    p.printf("%s     encoding: %s%n", indent, Charset.defaultCharset());
    p.printf("%s     timezone: %s (%s) %s%n", indent, zi, zi.getDisplayName(TextStyle.FULL, Locale.getDefault()), zi.getRules().getOffset(instant).toString());
    p.printf("%swatch service: %s%n", indent, getServer().getWatchServiceClassname());
    List<ZoneOffsetTransitionRule> l = zi.getRules().getTransitionRules();
    for (ZoneOffsetTransitionRule tr : l) {
        p.printf("%s         rule: %s%n", indent, tr.toString());
    }
    ZoneOffsetTransition tran = zi.getRules().nextTransition(instant);
    if (tran != null) {
        Instant in = tran.getInstant();
        p.printf("%s   transition: %s (%s)%n", indent, in, in.atZone(zi));
    }
    p.printf("%s        clock: %d %s%n", indent, System.currentTimeMillis() / 1000L, instant);
    if (hasSecurityManager())
        p.printf("%s  sec-manager: %s%n", indent, getSecurityManager());
    p.printf("%s thread count: %d%n", indent, mxBean.getThreadCount());
    p.printf("%s peak threads: %d%n", indent, mxBean.getPeakThreadCount());
    p.printf("%s user threads: %d%n", indent, Thread.activeCount());
    showThreadGroup(Thread.currentThread().getThreadGroup(), p, newIndent);
    NameRegistrar.getInstance().dump(p, indent, detailRequired);
    for (String s : scripts) {
        p.printf("%s%s:%n", indent, s);
        exec(s, p, newIndent);
    }
    return baos.toString();
}
Also used : ZoneOffsetTransitionRule(java.time.zone.ZoneOffsetTransitionRule) ThreadMXBean(java.lang.management.ThreadMXBean) ZoneId(java.time.ZoneId) Instant(java.time.Instant) RuntimeMXBean(java.lang.management.RuntimeMXBean) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) ZoneOffsetTransition(java.time.zone.ZoneOffsetTransition)

Example 100 with ThreadMXBean

use of java.lang.management.ThreadMXBean in project kernel by exoplatform.

the class TestJVMManagement method testThreadManagement.

public void testThreadManagement() {
    ThreadMXBean threadBean = (ThreadMXBean) RootContainer.getComponent(ThreadMXBean.class);
    if (threadBean == null)
        return;
    ThreadInfo[] infos = threadBean.getThreadInfo(threadBean.getAllThreadIds());
}
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