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);
}
}
}
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;
}
}
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;
}
}
}
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;
}
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);
}
}
Aggregations