I guess the question is, could I somehow calculate the CPU utilization percentage just by reading the /proc/stat once?
# head -1 /proc/stat
cpu 67891300 39035 6949171 2849641614 118251644 365498 2341854 0
I am thinking about summarizing the columns except the IOWait (I was reading somewhere that it is counted in the idle) and that would give me the 100% and each individual column could be turned into percentage by (column/100_percent)*100.
- user: normal processes executing in user mode
- nice: niced processes executing in user mode
- system: processes executing in kernel mode
- idle: twiddling thumbs
- iowait: waiting for I/O to complete
- irq: servicing interrupts
- softirq: servicing softirqs
- steal: involuntary wait
- guest: running a normal guest
- guest_nice: running a niced guest
Is this a viable approach or i am totally off the track?
Best Answer
You are on the right track,
top
uses this file for this purpose. But you need to read it more than once.Utilization is a is a measure of use over time. You can read it once providing you know the uptime in seconds of the host, then divide by that, but that would give you a utility rate of the host since the machine was booted.
If you want a rate over 5 seconds, you would read the file once, sleep for 5 seconds, read it again, obtain the difference of the calculations and divide by 5.