Search in sources :

Example 1 with ThreadStat

use of com.shulie.instrument.simulator.module.model.thread.ThreadStat in project LinkAgent by shulieTech.

the class ThreadModule method processBlockingThread.

private CommandResponse processBlockingThread(Integer samplingInterval) {
    ThreadUtil.BlockingLockInfo blockingLockInfo = ThreadUtil.findMostBlockingLock();
    if (blockingLockInfo.threadInfo == null) {
        CommandResponse commandResponse = new CommandResponse();
        commandResponse.setSuccess(false);
        commandResponse.setMessage("No most blocking thread found!\n");
        return commandResponse;
    } else {
        CommandResponse commandResponse = new CommandResponse();
        commandResponse.setSuccess(true);
        Map<Long, ThreadStat> allThreadStats = ThreadUtil.getAllThreadStats(samplingInterval);
        commandResponse.setResult(ThreadUtil.getThreadInfo(blockingLockInfo, allThreadStats.get(blockingLockInfo.threadInfo.getThreadId())));
        return commandResponse;
    }
}
Also used : CommandResponse(com.shulie.instrument.simulator.api.CommandResponse) ThreadUtil(com.shulie.instrument.simulator.module.util.ThreadUtil) ThreadStat(com.shulie.instrument.simulator.module.model.thread.ThreadStat)

Example 2 with ThreadStat

use of com.shulie.instrument.simulator.module.model.thread.ThreadStat in project LinkAgent by shulieTech.

the class ThreadModule method getAllThreadStats.

private CommandResponse getAllThreadStats(String state, Integer sampleInterval) {
    Map<Long, ThreadStat> allThreadStats = ThreadUtil.getAllThreadStats(state, sampleInterval);
    ThreadInfo[] threadInfos = threadMXBean.getThreadInfo(ArrayUtils.toPrimitive(allThreadStats.keySet().toArray(new Long[0])), lockedMonitors, lockedSynchronizers);
    if (threadInfos == null) {
        return CommandResponse.failure("no thread found!\n");
    } else {
        List<com.shulie.instrument.simulator.module.model.thread.ThreadInfo> list = new ArrayList<com.shulie.instrument.simulator.module.model.thread.ThreadInfo>();
        for (ThreadInfo info : threadInfos) {
            com.shulie.instrument.simulator.module.model.thread.ThreadInfo stacktrace = ThreadUtil.getThreadInfo(info, allThreadStats.get(info.getThreadId()));
            list.add(stacktrace);
        }
        return CommandResponse.success(list);
    }
}
Also used : ThreadStat(com.shulie.instrument.simulator.module.model.thread.ThreadStat) ThreadInfo(java.lang.management.ThreadInfo)

Example 3 with ThreadStat

use of com.shulie.instrument.simulator.module.model.thread.ThreadStat in project LinkAgent by shulieTech.

the class ThreadModule method processTopBusyThreads.

private CommandResponse processTopBusyThreads(Integer sampleInterval, Integer topNBusy) {
    Map<Long, ThreadStat> allThreadStats = ThreadUtil.getAllThreadStats(sampleInterval);
    List<ThreadStat> coll = new ArrayList<ThreadStat>(allThreadStats.values());
    Collections.sort(coll, new Comparator<ThreadStat>() {

        @Override
        public int compare(ThreadStat o1, ThreadStat o2) {
            long l1 = o1.getCpuUsage();
            long l2 = o2.getCpuUsage();
            if (l1 < l2) {
                return 1;
            } else if (l1 > l2) {
                return -1;
            } else {
                return 0;
            }
        }
    });
    List<ThreadStat> topThreads = topNBusy > 0 && topNBusy <= coll.size() ? coll.subList(0, topNBusy) : coll;
    List<Long> tids = new ArrayList<Long>();
    for (ThreadStat threadStat : topThreads) {
        tids.add(threadStat.getThreadId());
    }
    ThreadInfo[] threadInfos = threadMXBean.getThreadInfo(ArrayUtils.toPrimitive(tids.toArray(new Long[tids.size()])), true, true);
    if (threadInfos == null) {
        return CommandResponse.failure("thread do not exist! id: " + tids + "\n");
    } else {
        List<com.shulie.instrument.simulator.module.model.thread.ThreadInfo> list = new ArrayList<com.shulie.instrument.simulator.module.model.thread.ThreadInfo>();
        for (ThreadInfo info : threadInfos) {
            com.shulie.instrument.simulator.module.model.thread.ThreadInfo stacktrace = ThreadUtil.getThreadInfo(info, allThreadStats.get(info.getThreadId()));
            list.add(stacktrace);
        }
        return CommandResponse.success(list);
    }
}
Also used : ThreadStat(com.shulie.instrument.simulator.module.model.thread.ThreadStat) ThreadInfo(java.lang.management.ThreadInfo)

Example 4 with ThreadStat

use of com.shulie.instrument.simulator.module.model.thread.ThreadStat in project LinkAgent by shulieTech.

the class ThreadUtil method getAllThreadStats.

public static Map<Long, ThreadStat> getAllThreadStats(String state, Integer sampleInterval) {
    Map<String, Thread> threads = ThreadUtil.getThreads();
    // 统计各种线程状态
    Map<Thread.State, Integer> stateCountMap = new HashMap<Thread.State, Integer>();
    for (Thread.State s : Thread.State.values()) {
        stateCountMap.put(s, 0);
    }
    for (Thread thread : threads.values()) {
        Thread.State threadState = thread.getState();
        Integer count = stateCountMap.get(threadState);
        stateCountMap.put(threadState, count + 1);
    }
    Collection<Thread> resultThreads = new ArrayList<Thread>();
    if (StringUtils.isNotBlank(state)) {
        state = state.toUpperCase();
        if (states.contains(state)) {
            for (Thread thread : threads.values()) {
                if (state.equals(thread.getState().name())) {
                    resultThreads.add(thread);
                }
            }
        }
    } else {
        resultThreads = threads.values();
    }
    // Compute cpu
    final HashMap<Thread, Long> cpus = new HashMap<Thread, Long>(threads.size());
    // Compute time cost
    final Map<Long, Long> times = new HashMap<Long, Long>(threads.size());
    if (sampleInterval != null && sampleInterval > 0) {
        Map<Long, Long> times1 = new HashMap<Long, Long>();
        for (Thread thread : resultThreads) {
            long cpu = threadMXBean.getThreadCpuTime(thread.getId());
            times1.put(thread.getId(), cpu);
        }
        try {
            Thread.sleep(sampleInterval);
        } catch (InterruptedException e) {
            Thread.currentThread().interrupt();
        }
        Map<Long, Long> times2 = new HashMap<Long, Long>(threads.size());
        for (Thread thread : resultThreads) {
            long cpu = threadMXBean.getThreadCpuTime(thread.getId());
            times2.put(thread.getId(), cpu);
        }
        long total = 0;
        for (Long id : times2.keySet()) {
            long time1 = times1.get(id);
            long time2 = times2.get(id);
            long delta = 0;
            if (time1 != time2) {
                if (time1 == -1) {
                    time1 = time2;
                } else if (time2 == -1) {
                    time2 = time1;
                }
                delta = time2 - time1;
            }
            times.put(id, delta);
            total += delta;
        }
        for (Thread thread : resultThreads) {
            long cpu = total == 0 ? 0 : Math.round((times.get(thread.getId()) * 100) / total);
            cpus.put(thread, cpu);
        }
    }
    Map<Long, ThreadStat> threadStatMap = new HashMap<Long, ThreadStat>();
    for (Thread t : resultThreads) {
        ThreadStat threadStat = new ThreadStat();
        threadStat.setThread(t);
        threadStat.setThreadId(t.getId());
        threadStat.setInterrupted(t.isInterrupted());
        threadStat.setDaemon(t.isDaemon());
        threadStat.setPriority(t.getPriority());
        threadStat.setThreadName(t.getName());
        threadStat.setGroupName(t.getThreadGroup() == null ? "main" : t.getThreadGroup().getName());
        if (cpus.containsKey(t)) {
            threadStat.setCpuUsage(cpus.get(t));
        } else {
            threadStat.setCpuUsage(0L);
        }
        if (times.containsKey(t.getId())) {
            threadStat.setCpuTime(times.get(t.getId()));
        } else {
            threadStat.setCpuTime(0L);
        }
        threadStatMap.put(t.getId(), threadStat);
    }
    return threadStatMap;
}
Also used : ThreadStat(com.shulie.instrument.simulator.module.model.thread.ThreadStat)

Aggregations

ThreadStat (com.shulie.instrument.simulator.module.model.thread.ThreadStat)4 ThreadInfo (java.lang.management.ThreadInfo)2 CommandResponse (com.shulie.instrument.simulator.api.CommandResponse)1 ThreadUtil (com.shulie.instrument.simulator.module.util.ThreadUtil)1