CPU usage: /proc/stat, etc?

January 9, 2009 on 2:26 pm | By | In bluefreesky.com | CPU usage: /proc/stat, etc?
  • Under linux, how do I get an approximate percentage of the current CPU usage? I have two cores. /proc/stat gives me the load average over the last few time periods, but i need a way to measure the CPU usage (preferably without ugly system calls like popen'ing ps for example) altogether at any given instant.

    Is there a way?


  • u can use sar comman-

    sar -u 6 5

    here CPU checkthe utilization 6 times in each 5 second.


  • Top command

    Type the top command:
    $ top

    Output:

    You can see Linux CPU utilization under CPU stats. The taskâ ™s share of the elapsed CPU time since the last screen update, expressed as a percentage of total CPU time. In a true SMP environment (multiple CPUS), top will operate in number of CPUs. Please note that you need to type q key to exit the top command display.


  • /proc/stat, the first line, which says "cpu" is the combined stats for all CPUs.

    You just need to sum the 2nd (user), 3rd (nice) and 4th (system) row, and divide it by all the available time (5th row is the idle time). Pseudo-code example:

    tmp = $2 + $3 + $4
    usage = $tmp / ($tmp + $5)

    Simple, isn't it? ;-).







  • #If you have any other info about this subject , Please add it free.#
    Your name:
    E-mail:
    Telphone:

    Your comments:


    If you have any other info about CPU usage: /proc/stat, etc? , Please add it free.