benchmarking - Obtaining CPU thread usage in Java -


i have question around getting cpu utilization given jni block. i'm making intensive cpu computation in underlying c++ jni native method. i'm in process of optimizing computation , want benchmark against varying inputs.

need guidance on how go measuring this. alternatives have considered far are

  • using jmx threadmxbean measure system cpu usage current thread invokes call jni method. however, not sure if jni code executed within invoking thread context. happens when thread spawns more threads?

  • using jmx operatingsystemmxbean cpu usage entire jvm. ideally, not want want there parallel executions in jvm might tweak benchmarking.

  • measure externally using getrusage(..). want know here how different using operatingsystemmxbean.

you can use threadmxbean cpu usage statistics running threads. in example below cpu usage per thread calculated:

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();  threadinfo[] threadinfos = threadmxbean.dumpallthreads(false, false); (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>(); threadinfo[] threadinfos = threadmxbean.dumpallthreads(false, false); (threadinfo info : threadinfos) {     threadcurrentcpu.put(info.getthreadid(), threadmxbean.getthreadcputime(info.getthreadid())); }  // cpu on processes //int nrcpus = osmxbean.getavailableprocessors(); // total cpu: cpu % can more 100% (devided on multiple cpus) long nrcpus = 1; // elapsedtime in ms. long elapsedtime = (uptime - initialuptime); (threadinfo info : threadinfos) {     // elapsedcpu 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); // can use osmxbean.getthreadinfo(theadid) information on every thread reported in threadcpuusage , analyze cpu intentive threads 

Comments

Popular posts from this blog

java - Spring Data JPA: Why findOne(id) executing delete query internally? -

python - Mongodb How to add addtional information when aggregating? -

java - Incorrect order of records in M-M relationship in hibernate -