This discussion is archived
6 Replies Latest reply: Sep 23, 2011 12:06 PM by jtahlborn RSS

Caluculate CPU utilization independent of OS

884257 Newbie
Currently Being Moderated
Hi All,
Previously our application is deployed in unix & we used to monitor CPU utilization using vmstat -1 4 as below
Process p = Runtime.getRuntime().exec("vmstat -1 4 ");

We will interpret the string and get the CPU usage by getting US column in vmstat output.

But recently our application is migrated to linux , and we are getting CPU usage as 98% every time as output of vmstat is different in linux. We had to change our code again for this.

Is there any way to caluculate CPU utilization independent of OS?.When i browsed , i got few solutions to use JNI which we are not interested.

Any help on this is greatly appreciated.

Thanks,
Suresh
  • 1. Re: Caluculate CPU utilization independent of OS
    796440 Guru
    Currently Being Moderated
    881254 wrote:
    Is there any way to caluculate CPU utilization independent of OS?
    No.

    The closest you might come is to use SNMP, but there's no guarantee that a given host will have an SNMP agent running, so if you can't enforce that, you're out of luck, and even if you can, the code to collect and interpret the data is non-trivial.
  • 2. Re: Caluculate CPU utilization independent of OS
    884257 Newbie
    Currently Being Moderated
    No .The closest you might come is to use SNMP

    Thanks. Is vmstat ( for unix & linux platforms) an efficient way to caluculate the CPU utilization , in java application
  • 3. Re: Caluculate CPU utilization independent of OS
    796440 Guru
    Currently Being Moderated
    881254 wrote:
    Is vmstat ( for unix & linux platforms) an efficient
    I have no idea. Why are you concerning yourself with "efficiency"? Will you be calculated CPU usage every millisecond?
  • 4. Re: Caluculate CPU utilization independent of OS
    jtahlborn Expert
    Currently Being Moderated
    is there a reason you can't use the support built into the java runtime?

    http://download.oracle.com/javase/6/docs/api/java/lang/management/OperatingSystemMXBean.html#getSystemLoadAverage%28%29
  • 5. Re: Caluculate CPU utilization independent of OS
    796440 Guru
    Currently Being Moderated
    jtahlborn wrote:
    is there a reason you can't use the support built into the java runtime?

    http://download.oracle.com/javase/6/docs/api/java/lang/management/OperatingSystemMXBean.html#getSystemLoadAverage%28%29
    Oh, cool! Didn't know about that one.
  • 6. Re: Caluculate CPU utilization independent of OS
    jtahlborn Expert
    Currently Being Moderated
    jverd wrote:
    jtahlborn wrote:
    is there a reason you can't use the support built into the java runtime?

    http://download.oracle.com/javase/6/docs/api/java/lang/management/OperatingSystemMXBean.html#getSystemLoadAverage%28%29
    Oh, cool! Didn't know about that one.
    note, as of jdk 6, it only works on *nix OSes, not windows.  dunno if that has been fixed in jdk 7.                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                       

Legend

  • Correct Answers - 10 points
  • Helpful Answers - 5 points