2 Replies Latest reply on Jul 12, 2006 11:00 AM by clebert.suconic

    Calculating the Percent CPU Utilization for a block of code

      I thnk this code closely calculates the CPU utilization of a block of executed code using standard plaform independent Java 1.5, although I suppose it may be susceptible to the vaguaries of different CPU architectures.

      The sample code calculates a block of 10000 prime numbers can then determines the CPU utilization. The test is repeated a number of times to allow me to monitor a native CPU monitor.

      The premise is this:

      1. Collect the start time in nanos.
      2. Collect the CPU start time in nanos.
      3. Run the block of code.
      4. Collect the end time in nanos.
      5. Collect the CPU end time in nanos.
      6. Calculate the elapsed time in nanos and multiply it by the number of processors.
      7. Calculate the actual elapsed used CPU time.
      8. The elapsed used CPU time as a percentage of the the total available elapsed time (multiplied by the number of processors) is the percent CPU utilization of the block of code averaged out for the duration of the execution.

      Does this make sense ?
      Anyone see any flaws ?









      import java.lang.management.ManagementFactory;
      import java.util.Random;
      
      public class CPUTest {
      
       public static void main(String[] args) {
       for(int i = 0; i < 30; i++) {
       long start = System.nanoTime();
       int cpuCount = ManagementFactory.getOperatingSystemMXBean().getAvailableProcessors();
       Random random = new Random(start);
       int seed = Math.abs(random.nextInt());
       log("Starting Test with " + cpuCount + " CPUs and random number:" + seed);
       int primes = 10000;
       long startCPUTime = ManagementFactory.getThreadMXBean().getCurrentThreadCpuTime();
       start = System.nanoTime();
       while(primes != 0) {
       if(isPrime(seed)) {
       primes--;
       }
       seed++;
      
       }
       float cpuPercent = calcCPU(startCPUTime, start, cpuCount);
       log("CPU Util:" + cpuPercent);
       try {Thread.sleep(10000);} catch (InterruptedException e) {}
       }
       }
      
       public static void log(Object message) {
       System.out.println(message);
       }
      
       public static int calcCPU(long cpuStartTime, long elapsedStartTime, int cpuCount) {
       long end = System.nanoTime();
       long totalAvailCPUTime = cpuCount * (end-elapsedStartTime);
       long totalUsedCPUTime = ManagementFactory.getThreadMXBean().getCurrentThreadCpuTime()-cpuStartTime;
       log("Total CPU Time:" + totalUsedCPUTime + " ns.");
       log("Total Avail CPU Time:" + totalAvailCPUTime + " ns.");
       float per = ((float)totalUsedCPUTime*100)/(float)totalAvailCPUTime;
       log("%:" + per);
       return (int)per;
       }
      
       static boolean isPrime(int n) {
       // 2 is the smallest prime
       if (n <= 2) {
       return n == 2;
       }
       // even numbers other than 2 are not prime
       if (n % 2 == 0) {
       return false;
       }
       // check odd divisors from 3
       // to the square root of n
       for (int i = 3, end = (int)Math.sqrt(n);
       i <= end; i += 2) {
       if (n % i == 0) {
       return false;
       }
       }
       return true;
       }
      }