Get CPU utilization stats from C program
Solution 1
You want to read the first few lines of /proc/stat
. You'll need to read it twice, a measured time apart, and subtract the first set of numbers from the second. The lines look like this:
cpu 1526724 408013 600675 541100340 2861417 528 14531 0 0 0
cpu0 344507 77818 251244 134816146 1119991 324 13283 0 0 0
cpu1 502614 324065 179301 133991407 1631824 136 906 0 0 0
cpu2 299080 3527 79456 136144067 103208 59 255 0 0 0
cpu3 380521 2602 90672 136148719 6393 7 86 0 0 0
intr 2111239193 344878476 16943 ...
The first line is aggregate for all cores. The next lines show each core. When you see the line that start with intr
, you know to stop parsing.
Each number is the amount of time the CPU has spent in a particular state. The units are typically hundredths of a second. The fields are user
, nice
, system
, idle
, iowait
, irq
, softirq
, steal
, guest
, and guest_nice
.
The authoritative documentation is, of course, the source code. If you have a copy of the Linux kernel source handy, look at fs/proc/stat.c
, particularly the show_stat
function.
Solution 2
There is some example on the web that shows how to read /proc/pid/stat
in C.
You could read utime
or stime
values at two distinct moments and compute the desired cpu utilization stats. (I guess top
uses this raw data too.)
(I forgot: this is linux specific.)
Related videos on Youtube
0xFF
Updated on September 18, 2022Comments
-
0xFF over 1 year
I want to read the CPU utilization stats from a C program, I am interested in the percentage of use of CPU, steal time etc. These stats are shown in the 3rd row from the
top
command.I tried to parse
top
's output withawk
(top -n 1 -b | awk '{print $0}'
), but it seems thattop
gives always the same 'fictional' values before it starts showing correct stats.Is there a way from code, or by parsing some command's output to get the CPU utilization stats?
Edit:
The platform is Linux
Thank you.
-
jlliagre over 12 yearsSure, but there is no standard way. Precise the target OS(es).
-
-
0xFF over 12 yearsDo you know at what frequency the proc/stat is being updated? like once every 1ms or something, thank you.
-
ChrisCornwall over 12 yearsThey're not real files. Their contents are calculated when you open them.