I would like to use the percentage of CPU load in a program to discover wether or not it has an affect on some other sensors but i need to be able to get that number as an integer in python. I looked online and found this which looked helpful but was very confusing. (probably because i understand the SO format but not **traditional** forums) I am using a RPi 2 with the latest and greatest Raspbian.

Firstly, is this possible?

Secondly, if it is how can i do it?

  • You can get it by doing some math with a clock on output from /proc/stat (described in man proc) which is the horse's mouth from with tools like top take measurements, I believe. But if it is not very performance critical then then popen() on such a tool is easier.
    – goldilocks
    Jun 27, 2016 at 20:19

3 Answers 3


(disclamer: I just answered a similar question in Unix & Linux )

The contents of /proc/stat are cumulative from boot. To use /proc/stat, you must poll the content of /proc/stat twice. Then the differences between the first poll and second poll results provide enough data to calculate the current load. There must be some form of delay to make this calculation. /proc/stat contains entries for the cpu as a whole and for each specific core.

For each core, adding all the fields together yields the total time slices. Then adding column 4 (idle) and 5 (IO wait) yields the idle slices. After a small delay, take another look at /proc/stat, and do the calculations again.

The formula should then be,

( (total_2 - total1) – (idle_2 - idle_1 ) / (total_2 – total_1)


c_total = (total_2 – total1)
c_idle = (idle_2 - idle_1 ) 

(c_total – c_idle) / c_total = current_load

look at the man 5 proc as well


The utilization is usually calculated using top/htop commands. I did check the link that you had mentioned where the methods getRAMinfo and getCPUuse are used to gather the information.

You can use os.popen as suggested by the forum or use subprocess module in python to fetch the details from the shell. There is good tutorial for quick start on subprocess module here. Hope this helps. Let me know if you need more information.

  • This is by far the simplest method. Open a terminal window and type htop
    – SDsolar
    May 20, 2017 at 10:40

Since you want to analyse the performance of a particular program, the right tool to do this would be perf, which is included in linux-tools package. It is a complex and powerful tool which ought to be learned before you can use it in full. However, obtaining CPU load statistics is easy:

$ perf stat ls /usr/bin
 Performance counter stats for 'ls /usr/bin':

    149.787143 task-clock                #    0.535 CPUs utilized
         3,648 context-switches          #    0.024 M/sec
             1 CPU-migrations            #    0.000 M/sec
           266 page-faults               #    0.002 M/sec
    78,313,233 cycles                    #    0.523 GHz
    47,984,906 instructions              #    0.61  insns per cycle
     9,067,524 branches                  #   60.536 M/sec
     1,113,291 branch-misses             #   12.28% of all branches

   0.279785985 seconds time elapsed

As you can see, ls was executed in 0.28 seconds, consuming 0.535% of my two-core CPU (so, essentially loading 1 core at 100%). An average of 0.61 instructions per cycle suggests it's not heavily optimized, and so does the 12% of branch misses.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.