26°

jmx 获取cpu时间

import java.lang.management.ManagementFactory;
import java.lang.management.OperatingSystemMXBean;
import java.lang.management.RuntimeMXBean;
import java.lang.management.ThreadInfo;
import java.lang.management.ThreadMXBean;
import java.util.HashMap;
import java.util.Map;

public class Test {

private int sampleTime = 10000;
private ThreadMXBean threadMxBean = ManagementFactory.getThreadMXBean();
private RuntimeMXBean runtimeMxBean = ManagementFactory.getRuntimeMXBean();
private OperatingSystemMXBean osMxBean = ManagementFactory.getOperatingSystemMXBean();
private Map<Long, Long> threadInitialCPU = new HashMap<Long, Long>();
private Map<Long, Float> threadCPUUsage = new HashMap<Long, Float>();
private long initialUptime = runtimeMxBean.getUptime();

public static void main(String[] args) {
	Thread thread = new Thread(new Runnable() {
		int j = 0;

		@Override
		public void run() {
			// TODO Auto-generated method stub
			for (int i = 0; i < 100000; i++) {
				j++;
				try {
					Thread.sleep(1);
				} catch (InterruptedException e) {
					e.printStackTrace();
				}
			}
			System.out.println("运行完成>>>>>>>>>>");
		}
	});
	thread.setName("test>>");
	thread.start();

	new Test().test();
}

public void test() {
	ThreadInfo[] threadInfos = threadMxBean.dumpAllThreads(false, false);
	for (ThreadInfo info : threadInfos) {
		threadInitialCPU.put(info.getThreadId(), threadMxBean.getThreadCpuTime(info.getThreadId()));
	}

	try {
		Thread.sleep(sampleTime);
	} catch (InterruptedException e) {
	}

	long upTime = runtimeMxBean.getUptime();

	Map<Long, Long> threadCurrentCPU = new HashMap<Long, Long>();
	threadInfos = threadMxBean.dumpAllThreads(false, false);
	for (ThreadInfo info : threadInfos) {
		threadCurrentCPU.put(info.getThreadId(), threadMxBean.getThreadCpuTime(info.getThreadId()));
	}

	// CPU over all processes
	// int nrCPUs = osMxBean.getAvailableProcessors();
	// total CPU: CPU % can be more than 100% (devided over multiple cpus)
	long nrCPUs = 1;
	// elapsedTime is in ms.
	long elapsedTime = (upTime - initialUptime);
	for (ThreadInfo info : threadInfos) {
		// elapsedCpu is in ns
		Long initialCPU = threadInitialCPU.get(info.getThreadId());
		if (initialCPU != null) {
			long elapsedCpu = threadCurrentCPU.get(info.getThreadId()) - initialCPU;
			float cpuUsage = elapsedCpu / (elapsedTime * 1000000F * nrCPUs);
			threadCPUUsage.put(info.getThreadId(), cpuUsage);
		}
	}

	// threadCPUUsage contains cpu % per thread
	System.out.println(threadCPUUsage);
	// You can use osMxBean.getThreadInfo(theadId) to get information on every
	// thread reported in threadCPUUsage and analyze the most CPU intentive threads
	
	for(Long l :threadCPUUsage.keySet()) {
		Float float1 = threadCPUUsage.get(l);
		if(float1 > 0.0) {
			ThreadInfo threadInfo = threadMxBean.getThreadInfo(l,1000);
			System.out.println("id="+l+"  cpu:"+float1 + "theadName:"+threadInfo.getThreadName());
			StackTraceElement[] stackTrace = threadInfo.getStackTrace();
			if(stackTrace!=null && stackTrace.length > 0) {
				for(StackTraceElement s:stackTrace) {
					//System.out.println("method: "+s.getMethodName()+" line:"+s.getLineNumber());
					System.out.println(s.toString());
				}
			}
		}
	}
}

}

 

本文由【xiaomin0322】发布于开源中国,原文链接:https://my.oschina.net/xiaominmin/blog/3159479

全部评论: 0

    我有话说: