java 线程 cpu,获取Java中的CPU线程使用情况

I have a question around getting CPU utilization for a given JNI block. I'm making some intensive CPU computation in the underlying C++ JNI native method. I'm in the process of optimizing this computation and want to benchmark it against varying inputs.

I need some guidance on how to go about measuring this. The alternatives I have considered so far are

Using JMX ThreadMXBean to measure system CPU usage for the current thread that invokes call into JNI method. However, I am not sure if JNI code is executed within the invoking thread context. What happens when the thread spawns more threads?

Using JMX OperatingSystemMXBean to get the CPU usage for the entire JVM. Ideally, this is not want I want as there could be parallel executions in JVM that might tweak the benchmarking.

Measure externally using getrusage(..). What I want to know here is that how is it different than using OperatingSystemMXBean.

解决方案

You can use ThreadMXBean to get cpu usage statistics from all running threads. In the example below the CPU usage per thread is calculated:

private int sampleTime = 10000;

private ThreadMXBean threadMxBean = ManagementFactory.getThreadMXBean();

private RuntimeMXBean runtimeMxBean = ManagementFactory.getRuntimeMXBean();

private OperatingSystemMXBean osMxBean = ManagementFactory.getOperatingSystemMXBean();

private Map threadInitialCPU = new HashMap();

private Map threadCPUUsage = new HashMap();

private long initialUptime = runtimeMxBean.getUptime();

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 threadCurrentCPU = new HashMap();

ThreadInfo[] 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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值