Search in sources :

Example 61 with ThreadInfo

use of java.lang.management.ThreadInfo in project jvm-tools by aragozin.

the class MBeanCpuUsageReporter method probe.

public void probe() {
    try {
        long[] ids = mbean.getAllThreadIds();
        ThreadInfo[] ti = mbean.getThreadInfo(ids);
        Map<Long, ThreadInfo> buf = new HashMap<Long, ThreadInfo>();
        for (ThreadInfo t : ti) {
            if (t != null) {
                buf.put(t.getThreadId(), t);
            }
        }
        for (Long key : threadDump.keySet()) {
            ThreadTrac tt = threadDump.get(key);
            ThreadInfo t = buf.remove(key);
            if (t != null) {
                tt.name = t.getThreadName();
                tt.lastThreadInfo = t;
            } else {
                tt.dead = true;
            }
        }
        for (ThreadInfo t : buf.values()) {
            ThreadTrac tt = new ThreadTrac();
            tt.name = t.getThreadName();
            tt.lastThreadInfo = t;
            threadDump.put(t.getThreadId(), tt);
        }
        if (threadAllocatedMemoryEnabled) {
            long[] alloc = ((ThreadMXBeanEx) mbean).getThreadAllocatedBytes(ids);
            for (int i = 0; i != ids.length; ++i) {
                if (threadDump.get(ids[i]) == null) {
                    continue;
                }
                threadDump.get(ids[i]).lastAllocatedBytes = alloc[i];
            }
        }
        if (bulkCpuEnabled) {
            long[] cpu = ((ThreadMXBeanEx) mbean).getThreadCpuTime(ids);
            long[] usr = ((ThreadMXBeanEx) mbean).getThreadUserTime(ids);
            for (int i = 0; i != ids.length; ++i) {
                if (threadDump.get(ids[i]) == null) {
                    continue;
                }
                threadDump.get(ids[i]).lastCpuTime = cpu[i];
                threadDump.get(ids[i]).lastUserTime = usr[i];
            }
        } else {
            for (long id : ids) {
                if (threadDump.get(id) == null) {
                    continue;
                }
                ThreadTrac tt = threadDump.get(id);
                tt.lastCpuTime = mbean.getThreadCpuTime(id);
                tt.lastUserTime = mbean.getThreadCpuTime(id);
            }
        }
    } catch (Exception e) {
        throw new RuntimeException(e);
    }
}
Also used : ThreadInfo(java.lang.management.ThreadInfo) HashMap(java.util.HashMap) ThreadMXBeanEx(org.gridkit.jvmtool.stacktrace.ThreadMXBeanEx) MalformedObjectNameException(javax.management.MalformedObjectNameException)

Example 62 with ThreadInfo

use of java.lang.management.ThreadInfo in project jvm-tools by aragozin.

the class ThreadStackSampler method prime.

public void prime() {
    ThreadInfo[] ti = threading.dumpAllThreads(false, false);
    long[] tids = new long[ti.length];
    int n = 0;
    for (ThreadInfo t : ti) {
        long tid = t.getThreadId();
        String name = t.getThreadName();
        if (threadFilter == null || threadFilter.matcher(name).matches()) {
            tids[n++] = tid;
        }
    }
    tids = Arrays.copyOf(tids, n);
    threadSet = tids;
}
Also used : ThreadInfo(java.lang.management.ThreadInfo)

Example 63 with ThreadInfo

use of java.lang.management.ThreadInfo in project jvm-tools by aragozin.

the class ThreadStackSampler method collect.

public void collect() {
    long timestamp = System.currentTimeMillis();
    ThreadInfo[] dump = threading.getThreadInfo(threadSet, Integer.MAX_VALUE);
    for (ThreadInfo ti : dump) {
        Trace trace = newTrace(timestamp, ti);
        if (trace != null) {
            traces.add(trace);
        }
    }
}
Also used : ThreadInfo(java.lang.management.ThreadInfo)

Example 64 with ThreadInfo

use of java.lang.management.ThreadInfo in project jvm-tools by aragozin.

the class ThreadDumpSampler method compactThreads.

private ThreadInfo[] compactThreads(ThreadInfo[] dumpAllThreads) {
    int n = 0;
    for (int i = 0; i != dumpAllThreads.length; ++i) {
        if (dumpAllThreads[i] != null) {
            ++n;
        }
    }
    if (n == dumpAllThreads.length) {
        return dumpAllThreads;
    } else {
        ThreadInfo[] result = new ThreadInfo[n];
        n = 0;
        for (ThreadInfo ti : dumpAllThreads) {
            if (ti != null) {
                result[n++] = ti;
            }
        }
        return result;
    }
}
Also used : ThreadInfo(java.lang.management.ThreadInfo)

Example 65 with ThreadInfo

use of java.lang.management.ThreadInfo in project jvm-tools by aragozin.

the class ThreadDumpSampler method filterThreads.

private ThreadInfo[] filterThreads(ThreadInfo[] dumpAllThreads) {
    if (threadFilter == null) {
        return compactThreads(dumpAllThreads);
    } else {
        Matcher m = threadFilter.matcher("");
        int n = 0;
        for (int i = 0; i != dumpAllThreads.length; ++i) {
            if (dumpAllThreads[i] != null) {
                m.reset(dumpAllThreads[i].getThreadName());
                if (m.matches()) {
                    ++n;
                } else {
                    dumpAllThreads[i] = null;
                }
            }
        }
        if (n == dumpAllThreads.length) {
            return dumpAllThreads;
        } else {
            ThreadInfo[] result = new ThreadInfo[n];
            n = 0;
            for (ThreadInfo ti : dumpAllThreads) {
                if (ti != null) {
                    result[n++] = ti;
                }
            }
            return result;
        }
    }
}
Also used : ThreadInfo(java.lang.management.ThreadInfo) Matcher(java.util.regex.Matcher)

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