Search in sources :

Example 1 with ThreadMXBeanEx

use of org.gridkit.jvmtool.stacktrace.ThreadMXBeanEx 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)

Aggregations

ThreadInfo (java.lang.management.ThreadInfo)1 HashMap (java.util.HashMap)1 MalformedObjectNameException (javax.management.MalformedObjectNameException)1 ThreadMXBeanEx (org.gridkit.jvmtool.stacktrace.ThreadMXBeanEx)1