[glugot] FINDING CPU UTILIZATION OF ALL LINUX PROCESSES

dhana sekar yoursdhana at gmail.com
Tue Dec 16 23:53:00 IST 2008


hi ,
  i have to calculate the cumulative CPU utilization of all the linux
processes(including kernel processes)...i am using the
"PS" command's output to do that..but when i compare it with the result  of
top command the the result is differs...
for example if my program calculates 40.5% , then "TOP COMMAND" shows that
34% only...

 where is the mistake....which one is best..TOP OR PS COMMAND.....i tried
with top but i failed in getting the updated values....if you know, please
improve the following code and send me...

import java.io.*;
 class Test1
{
 Test1()
  {   }
 public void calc()
   {
        String temp=null;
    try{
    Process p = Runtime.getRuntime().exec("ps -A -eo %cpu,pid"); // i tried
with TOP but i cant get updated result....

    BufferedReader br = new BufferedReader(new
InputStreamReader(p.getInputStream()));
    temp="";
        int i=0;
        float count=0;
    System.out.println("in");
    while((temp = br.readLine())!= null)
       {
          if( (++i>1))
        {
               String s1=temp.substring(0,4);
        System.out.println(s1);
               count+=Float.parseFloat(s1);

          }
     }
        System.out.println("usage->"+count);
    p = Runtime.getRuntime().exec("top -b -n 1");
    p = Runtime.getRuntime().exec("ls");
        p.destroy();
    System.out.println("out");
     br.close();
       }catch(Exception e){ }

    }

  }
public class sample
 {
  public static void main(String args[])throws Exception
   {
    Test1 t=new Test1();
    while(true)
     {
       t.calc();
       Thread.currentThread().sleep(1000);
    // t.calc();
       }

    }
}

thank you,,,,,,
-------------- next part --------------
An HTML attachment was scrubbed...
URL: https://lists.tce.edu/pipermail/glugot/attachments/20081216/c2306099/attachment.htm 


More information about the GLUGOT mailing list