I would like to monitor total CPU utilization percentage as a counter. The reason I would like it as a counter is that data won't be lost between samples (and I can have the graphing side calculate the rate).
My initial approach was to use /proc/uptime
with the formula (uptime-(idle_time/num_core))*100
. This generally seems to be accurate across a large number of servers (something like 98% of the time), but sometimes I seem to get erroneous results. For example the following seems to suggest that there was negative CPU usage, which doesn't really make sense:
[root@ny-lb05 ~]# echo -e "scale=10\n ($(cut -f1 -d' ' /proc/uptime)-($(cut -f2 -d' ' /proc/uptime)/16))*100" | bc
5646895.3750000000
[root@ny-lb05 ~]# echo -e "scale=10\n ($(cut -f1 -d' ' /proc/uptime)-($(cut -f2 -d' ' /proc/uptime)/16))*100" | bc
5646891.5625000000
On this server I'm running:
Linux ny-lb05.ds.stackexchange.com 2.6.32-431.11.2.el6.x86_64 #1 SMP Tue Mar 25 19:59:55 UTC 2014 x86_64 x86_64 x86_64 GNU/Linux
Does someone see an error in this method of calculation? Is there a better way to get CPU utilization as a counter?
Update:
So what I'm after is the total utilization time as a monotonically increasing counter. I would expect that total utilization should never decrease. But that seems to be the case with the following:
[root@ny-lb05 ~]# read uptime idle </proc/uptime; echo -e "scale=1000\n ($uptime*16-($idle))" | bc
903874.23
[root@ny-lb05 ~]# read uptime idle </proc/uptime; echo -e "scale=1000\n ($uptime*16-($idle))" | bc
903870.29
Also, according to /proc/cpuinfo, cores=siblings so I believe HT is not enabled.
Update 2:
TLDR; /proc/uptime is bugged, use /proc/stat instead.
uptime * num_core - idle_time = total active processor seconds
. Doesn't it logically follow that total active processor seconds should never decrease?top
, I've done this in the past using fields from/proc/stat
; the firstcpu
is a combined total, which is useful (then you have breakdowns for each individual core). You then need two samples to determine usage in relation to a unit of time.