List of usage examples for java.lang.management ThreadMXBean getThreadCpuTime
public long getThreadCpuTime(long id);
From source file:org.batoo.jpa.benchmark.BenchmarkTest.java
private void measureTimes(final ThreadMXBean mxBean, ArrayList<Runnable> profilingQueue) { final ThreadInfo[] threadInfos = mxBean.getThreadInfo(this.threadIds, Integer.MAX_VALUE); for (int i = 0; i < this.threadIds.length; i++) { final long id = this.threadIds[i]; final ThreadInfo threadInfo = threadInfos[i]; final long newThreadTime = mxBean.getThreadCpuTime(id); final long worked = Math.abs(newThreadTime - this.currentThreadTimes[i]); this.currentThreadTimes[i] = newThreadTime; profilingQueue.add(new Runnable() { @Override//ww w . ja va 2 s. c om public void run() { BenchmarkTest.this.measureTime(worked, threadInfo); } }); } }
From source file:org.kaaproject.kaa.server.common.thrift.cli.server.BaseCliThriftService.java
/** * Dump service threads information./*from www . ja v a2 s . c om*/ * * @param writer the writer to output threads information */ private void dumpThreads(PrintWriter writer) { writer.println("THREADS DUMP:"); writer.println(); ThreadMXBean threadMxBean = ManagementFactory.getThreadMXBean(); ThreadInfo[] threads = threadMxBean.dumpAllThreads(false, false); Map<Long, ThreadStruct> threadsMap = new HashMap<>(); for (ThreadInfo ti : threads) { ThreadStruct ts = new ThreadStruct(); ts.ti = ti; threadsMap.put(ti.getThreadId(), ts); } ThreadGroup root = Thread.currentThread().getThreadGroup(); ThreadGroup parent; parent = root.getParent(); while (parent != null) { root = parent; parent = parent.getParent(); } allThreadsFromGroup(root, threadsMap); Collection<ThreadStruct> threadValues = threadsMap.values(); List<ThreadStruct> threadList = new ArrayList<>(threadValues); Collections.sort(threadList); Map<State, Integer> threadStatistics = new LinkedHashMap<>(); threadStatistics.put(State.NEW, 0); threadStatistics.put(State.RUNNABLE, 0); threadStatistics.put(State.BLOCKED, 0); threadStatistics.put(State.WAITING, 0); threadStatistics.put(State.TIMED_WAITING, 0); threadStatistics.put(State.TERMINATED, 0); int maxGroup = 0; int maxName = 0; for (ThreadStruct thread : threadList) { maxName = Math.max(thread.ti.getThreadName().length(), maxName); maxGroup = Math.max(thread.getGroupName().length(), maxGroup); int count = threadStatistics.get(thread.ti.getThreadState()); count++; threadStatistics.put(thread.ti.getThreadState(), count); } StringBuffer header = new StringBuffer(); header.append("ID"); int idColumnLength = 4; int length = idColumnLength; header.append(createPadding(length - header.length())); header.append("GROUP"); int groupColumnLength = maxGroup + 1; length += groupColumnLength; header.append(createPadding(length - header.length())); header.append("NAME"); int nameColumnLength = maxName + 1; length += nameColumnLength; header.append(createPadding(length - header.length())); header.append("PRIORITY"); int priorityColumnLength = 10; length += priorityColumnLength; header.append(createPadding(length - header.length())); header.append("STATE"); int stateColumnLength = 14; length += stateColumnLength; header.append(createPadding(length - header.length())); header.append("DAEMON"); int daemonColumnLengh = 7; length += daemonColumnLengh; header.append(createPadding(length - header.length())); header.append("ALIVE"); int aliveColumnLengh = 6; length += aliveColumnLengh; header.append(createPadding(length - header.length())); header.append("CPU TIME (SEC)"); int cpuTimeColumnLengh = 14; length += cpuTimeColumnLengh; header.append(createPadding(length - header.length())); writer.println(header); int maxRowLength = header.length(); writer.println(createPadding(maxRowLength, '-')); NumberFormat format = new DecimalFormat("0.#"); for (ThreadStruct thread : threadList) { StringBuffer row = new StringBuffer(); row.append(thread.ti.getThreadId()); int rowLength = idColumnLength; row.append(createPadding(rowLength - row.length())); row.append(thread.getGroupName()); rowLength += groupColumnLength; row.append(createPadding(rowLength - row.length())); row.append(thread.ti.getThreadName()); rowLength += nameColumnLength; row.append(createPadding(rowLength - row.length())); row.append(thread.getPriority()); rowLength += priorityColumnLength; row.append(createPadding(rowLength - row.length())); row.append(thread.ti.getThreadState()); rowLength += stateColumnLength; row.append(createPadding(rowLength - row.length())); row.append(thread.isDaemon()); rowLength += daemonColumnLengh; row.append(createPadding(rowLength - row.length())); row.append(thread.isAlive()); rowLength += aliveColumnLengh; row.append(createPadding(rowLength - row.length())); double cpuTimeSec = (double) threadMxBean.getThreadCpuTime(thread.ti.getThreadId()) / (double) (1000 * 1000 * 1000); row.append(format.format(cpuTimeSec)); writer.println(row); } writer.println(createPadding(maxRowLength, '-')); writer.println("SUMMARY:"); writer.println(createPadding(maxRowLength, '-')); for (State state : threadStatistics.keySet()) { int count = threadStatistics.get(state); if (count > 0) { StringBuffer row = new StringBuffer(); row.append(state.toString()); row.append(createPadding(stateColumnLength - row.length())); row.append(count); writer.println(row); } } writer.println(createPadding(maxRowLength, '-')); StringBuffer row = new StringBuffer(); row.append("TOTAL"); row.append(createPadding(stateColumnLength - row.length())); row.append(threadList.size()); writer.println(row); writer.println(createPadding(maxRowLength, '-')); }