use of java.lang.management.ThreadMXBean in project suite by stupidsing.
the class Profiler method recordStats.
private void recordStats() {
long currentThreadId = Thread.currentThread().getId();
ThreadMXBean mx = ManagementFactory.getThreadMXBean();
long[] threadIds = mx.getAllThreadIds();
ThreadInfo[] threadInfos = mx.getThreadInfo(threadIds, stackTraceDepth);
count.getAndIncrement();
for (ThreadInfo threadInfo : threadInfos) if (//
threadInfo != null && //
threadInfo.getThreadId() != currentThreadId && //
threadInfo.getThreadState() == State.RUNNABLE && !String_.equals(threadInfo.getThreadName(), "ReaderThread")) {
StackTraceElement[] stackTrace = threadInfo.getStackTrace();
Set<String> elements = new HashSet<>();
int i = stackTrace.length;
Call call = callRoot;
// anonymous classes
while (0 < i) {
StackTraceElement elem = stackTrace[--i];
String fileName = elem.getFileName();
int lineNumber = elem.getLineNumber();
String mn = elem.getClassName() + "." + elem.getMethodName();
String fn = fileName != null ? " " + fileName + (1 < lineNumber ? ":" + lineNumber : "") : "<unknown>";
String name = mn + fn;
(call = call.callees.computeIfAbsent(mn, any -> new Call())).count++;
if (elements.add(name))
records.computeIfAbsent(name, any -> new Record()).count++;
}
}
}
use of java.lang.management.ThreadMXBean in project suite by stupidsing.
the class ThreadMxBeanTest method test.
@Test
public void test() {
ThreadMXBean threadBean = ManagementFactory.getThreadMXBean();
System.out.println(threadBean.getThreadCount());
ThreadInfo[] threadInfos = threadBean.dumpAllThreads(false, false);
for (ThreadInfo threadInfo : threadInfos) if (threadInfo.getThreadState() == State.RUNNABLE)
System.out.println(threadInfo.toString());
}
use of java.lang.management.ThreadMXBean in project elastic-core-maven by OrdinaryDude.
the class GetStackTraces method processRequest.
/**
* Process the GetStackTraces API request
*
* @param req API request
* @return API response
*/
@Override
protected JSONStreamAware processRequest(HttpServletRequest req) {
String value;
//
// Get the number of trace lines to return
//
int depth;
value = req.getParameter("depth");
if (value != null)
depth = Math.max(Integer.valueOf(value), 1);
else
depth = Integer.MAX_VALUE;
//
// Get the thread information
//
JSONArray threadsJSON = new JSONArray();
JSONArray locksJSON = new JSONArray();
ThreadMXBean tmxBean = ManagementFactory.getThreadMXBean();
boolean tmxMI = tmxBean.isObjectMonitorUsageSupported();
ThreadInfo[] tList = tmxBean.dumpAllThreads(tmxMI, false);
//
for (ThreadInfo tInfo : tList) {
JSONObject threadJSON = new JSONObject();
//
// General thread information
//
threadJSON.put("id", tInfo.getThreadId());
threadJSON.put("name", tInfo.getThreadName());
threadJSON.put("state", tInfo.getThreadState().toString());
//
if (tmxMI) {
MonitorInfo[] mList = tInfo.getLockedMonitors();
if (mList.length > 0) {
JSONArray monitorsJSON = new JSONArray();
for (MonitorInfo mInfo : mList) {
JSONObject lockJSON = new JSONObject();
lockJSON.put("name", mInfo.getClassName());
lockJSON.put("hash", mInfo.getIdentityHashCode());
lockJSON.put("depth", mInfo.getLockedStackDepth());
lockJSON.put("trace", mInfo.getLockedStackFrame().toString());
monitorsJSON.add(lockJSON);
}
threadJSON.put("locks", monitorsJSON);
}
if (tInfo.getThreadState() == Thread.State.BLOCKED) {
LockInfo lInfo = tInfo.getLockInfo();
if (lInfo != null) {
JSONObject lockJSON = new JSONObject();
lockJSON.put("name", lInfo.getClassName());
lockJSON.put("hash", lInfo.getIdentityHashCode());
lockJSON.put("thread", tInfo.getLockOwnerId());
threadJSON.put("blocked", lockJSON);
boolean addLock = true;
for (Object lock : locksJSON) {
if (((JSONObject) lock).get("name").equals(lInfo.getClassName())) {
addLock = false;
break;
}
}
if (addLock)
locksJSON.add(lockJSON);
}
}
}
//
// Add the stack trace
//
StackTraceElement[] elements = tInfo.getStackTrace();
JSONArray traceJSON = new JSONArray();
int ix = 0;
for (StackTraceElement element : elements) {
traceJSON.add(element.toString());
if (++ix == depth)
break;
}
threadJSON.put("trace", traceJSON);
//
// Add the thread to the response
//
threadsJSON.add(threadJSON);
}
//
// Return the response
//
JSONObject response = new JSONObject();
response.put("threads", threadsJSON);
response.put("locks", locksJSON);
return response;
}
use of java.lang.management.ThreadMXBean in project gocd by gocd.
the class DaemonThreadStatsCollector method statsFor.
public Map<String, Object> statsFor(long threadId) {
if (!cpuInfoConcurrentHashMap.containsKey(threadId)) {
return null;
}
ThreadMXBean threadMXBean = ManagementFactory.getThreadMXBean();
long end = threadMXBean.getThreadCpuTime(threadId);
Info info = cpuInfoConcurrentHashMap.get(threadId);
Long start = info.time;
HashMap<String, Object> map = new HashMap<>();
map.put("CPUTime(nanoseconds)", end - start);
map.put("UUID", info.uuid);
return map;
}
use of java.lang.management.ThreadMXBean in project gocd by gocd.
the class DaemonThreadStatsCollector method captureStats.
public void captureStats(long threadId) {
ThreadMXBean threadMXBean = ManagementFactory.getThreadMXBean();
long threadCpuTime = threadMXBean.getThreadCpuTime(threadId);
cpuInfoConcurrentHashMap.put(threadId, new Info(threadCpuTime, UUID.randomUUID().toString()));
}
Aggregations