Search in sources :

Example 51 with ThreadMXBean

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++;
        }
    }
}
Also used : Read(suite.streamlet.Read) Set(java.util.Set) Service(suite.net.Service) HashMap(java.util.HashMap) Timer(java.util.Timer) ThreadMXBean(java.lang.management.ThreadMXBean) HashSet(java.util.HashSet) ThreadInfo(java.lang.management.ThreadInfo) String_(suite.util.String_) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Map(java.util.Map) As(suite.streamlet.As) ManagementFactory(java.lang.management.ManagementFactory) TimerTask(java.util.TimerTask) State(java.lang.Thread.State) ThreadMXBean(java.lang.management.ThreadMXBean) ThreadInfo(java.lang.management.ThreadInfo) Set(java.util.Set) HashSet(java.util.HashSet)

Example 52 with ThreadMXBean

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());
}
Also used : ThreadMXBean(java.lang.management.ThreadMXBean) ThreadInfo(java.lang.management.ThreadInfo) Test(org.junit.Test)

Example 53 with ThreadMXBean

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;
}
Also used : ThreadMXBean(java.lang.management.ThreadMXBean) MonitorInfo(java.lang.management.MonitorInfo) JSONArray(org.json.simple.JSONArray) ThreadInfo(java.lang.management.ThreadInfo) JSONObject(org.json.simple.JSONObject) LockInfo(java.lang.management.LockInfo) JSONObject(org.json.simple.JSONObject)

Example 54 with ThreadMXBean

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;
}
Also used : ThreadMXBean(java.lang.management.ThreadMXBean) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) HashMap(java.util.HashMap)

Example 55 with ThreadMXBean

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()));
}
Also used : ThreadMXBean(java.lang.management.ThreadMXBean)

Aggregations

ThreadMXBean (java.lang.management.ThreadMXBean)105 ThreadInfo (java.lang.management.ThreadInfo)56 HashMap (java.util.HashMap)9 RuntimeMXBean (java.lang.management.RuntimeMXBean)8 MemoryMXBean (java.lang.management.MemoryMXBean)7 IOException (java.io.IOException)6 OperatingSystemMXBean (java.lang.management.OperatingSystemMXBean)6 ArrayList (java.util.ArrayList)6 Map (java.util.Map)6 File (java.io.File)5 HashSet (java.util.HashSet)5 Test (org.junit.Test)5 ClassLoadingMXBean (java.lang.management.ClassLoadingMXBean)4 GarbageCollectorMXBean (java.lang.management.GarbageCollectorMXBean)4 MemoryUsage (java.lang.management.MemoryUsage)4 HealthCheckResultEntry (fish.payara.notification.healthcheck.HealthCheckResultEntry)3 HealthCheckResult (fish.payara.nucleus.healthcheck.HealthCheckResult)3 LockInfo (java.lang.management.LockInfo)3 ManagementFactory (java.lang.management.ManagementFactory)3 MonitorInfo (java.lang.management.MonitorInfo)3