Search in sources :

Example 56 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 57 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 58 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)

Example 59 with ThreadInfo

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

the class ThreadDumpSampler method prime.

/**
	 * Find threads according to thread name filters and memorise their IDs.
	 * <br/>
	 * Optional method to avoid dump all threads at every collection.
	 */
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 60 with ThreadInfo

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

the class ThreadDumpSampler method collect.

public void collect(StackTraceWriter writer) throws IOException {
    long timestamp = System.currentTimeMillis();
    ThreadInfo[] dump;
    if (threadSet != null) {
        dump = compactThreads(threading.getThreadInfo(threadSet, Integer.MAX_VALUE));
    } else {
        dump = filterThreads(threading.dumpAllThreads(false, false));
    }
    long[] ids = new long[dump.length];
    for (int i = 0; i != dump.length; ++i) {
        ids[i] = dump[i].getThreadId();
    }
    for (CounterCollector cc : collectors) {
        try {
            cc.collect(ids);
        } catch (Exception e) {
        // ignore
        }
    }
    ThreadCapture ts = new ThreadCapture();
    for (ThreadInfo ti : dump) {
        ts.reset();
        ts.timestamp = timestamp;
        ts.copyFrom(ti);
        for (CounterCollector cc : collectors) {
            try {
                cc.fillIntoSnapshot(ts);
            } catch (Exception e) {
            // ignore
            }
        }
        writer.write(ts);
    }
}
Also used : ThreadInfo(java.lang.management.ThreadInfo) IOException(java.io.IOException)

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