views:

530

answers:

5

I am trying to get total cpu usage in %. First I should start by saying that "top" will simply not do, as there is a delay between cpu dumps, it requires 2 dumps and several seconds, which hangs my program (I do not want to give it its own thread)

next thing what I tried is "ps" which is instant but always gives very high number in total (20+) and when I actually got my cpu to do something it stayed at about 20...

Is there any other way that I could get total cpu usage? It does not matter if it is over one second or longer periods of time... Longer periods would be more useful, though.

+1  A: 

cat /proc/stat

http://www.linuxhowtos.org/System/procstat.htm

aaa
This looks more like it could work... but what is the total capacity of cpu per one second? should I calculate with clock of the cpu? Or how do I know what total increment of, lets say 125, translates into usage?
@dav compute elapsed cpu time, compute time spent in user/system/whatever mode, get ratio , for example cpu_user/cpu_ticks.
aaa
+3  A: 

Try reading /proc/loadavg. The first three numbers are the number of processes actually running (i.e., using a CPU), averaged over the last 1, 5, and 15 minutes, respectively.

http://www.linuxinsight.com/proc_loadavg.html

wdebeaum
That won't do... I am looking for an actual percentage. I do not see how I could calculate it from that
A: 

I suggest two files to starting...

/proc/stat and /proc/cpuinfo.

http://www.mjmwired.net/kernel/Documentation/filesystems/proc.txt

olarva
+1  A: 

Read /proc/cpuinfo to find the number of CPU/cores available to the systems. Call the getloadavg() (or alternatively read the /proc/loadavg), take the first value, multiply it by 100 (to convert to percents), divide by number of CPU/cores. If the value is greater than 100, truncate it to 100. Done.

Relevant documentation: man getloadavg and man 5 proc

N.B. Load average, usual to *NIX systems, can be more than 100% (per CPU/core) because it actually measures number of processes ready to be run by scheduler. With Windows-like CPU metric, when load is at 100% you do not really know whether it is optimal use of CPU resources or system is overloaded. Under *NIX, optimal use of CPU loadavg would give you value ~1.0 (or 2.0 for dual system). If the value is much greater than number CPU/cores, then you might want to plug extra CPUs into the box.

Otherwise, dig the /proc file system.

Dummy00001
Interesting, I have just let the computer Idle for one minute, With top at 70 second delay. Top showed 95% idle over that minute. and When I read loadavg it showed me 0.20 which is when divided 10% of usage, This method is way too unprecise for me. Most I can afford is 1% error...
+7  A: 

cat /proc/stat

http://www.linuxhowtos.org/System/procstat.htm

I agree with this answer above. The cpu line in this file gives the total number of "jiffies" your system has spent doing different types of processing.

What you need to do is take 2 readings of this file, seperated by whatever interval of time you require. The numbers are increasing values (subject to integer rollover) so to get the %cpu you need to calculate how many jiffies have elapsed over your interval, versus how many jiffies were spend doing work.

e.g. Suppose at 14:00:00 you have

cpu 4698 591 262 8953 916 449 531

total_jiffies_1 = (sum of all values) = 16400

work_jiffies_1 = (sum of user,nice,system = the first 3 values) = 5551

and at 14:00:05 you have

cpu 4739 591 289 9961 936 449 541

total_jiffies_2 = 17506

work_jiffies_2 = 5619

So the %cpu usage over this period is:

work_over_period = work_jiffies_2 - work_jiffies_1 = 68

total_over_period = total_jiffies_2 - total_jiffies_1 = 1106

%cpu = work_over_period / total_over_period * 100 = 6.1%

Hope that helps a bit.

Hitobat
Just finished the function, works great. Thanks