Page 1 of 1

CPU load on computer

Posted: Thu Jan 14, 2016 1:54 am
by Bhavani
Hi all,

I need some idea on writing C program that visualizes the current CPU load of the RaspberryPi computer on the
Gertboard LEDs with certain conditons on. The conditions are as follows:

1. On full load of the CPU, 10 LEDS should be switched on.
2. On zero load of the CPU, no LEDshould be switched on.
3. Partial loads should be shown accordingly with partial LED lighting,
for example, 50% load should be visualized by activating only 5 adjacent LEDs, starting
from B3.
4. program should constantly monitor and display the CPU status for 10s and
terminate then.
5. The display should update at least every second.

Also, The Makefile must use the cross-compiler named arm-linux-gnueabihf-gcc. The compiled
binary must get the name cpuload, and must be executable on a RaspberryPi computer.

Kindly input your ideas. I am finding it difficult as I am new to this.

Re: CPU load on computer

Posted: Thu Jan 14, 2016 12:53 pm
by jahboater
Is this a Raspberry Pi2 or one of the other Pi's. The Pi2 has four CPU's so a program using 100% of its CPU may be considered a 25% overall CPU load, but on a B+ it would be 100% overall load. Experiment with top!

A more useful value is the load average (again see top, or uptime) from /proc/loadavg which is the run queue length averaged over 1, 5, or 15 mins. This is meaningful regardless of the number of CPU cores. A high CPU utilization with low load average figures is not usually a bad thing (the machine will still be responsive).

Low CPU utilisation does not necessarily mean the Pi is happy - over-committed memory for example will lower the CPU utilisation while increasing disk i/o, and generally slow the machine to a crawl. Look at swap usage.

Also remember that 20% CPU usage really means 100% CPU usage for 20% of the time.

Re: CPU load on computer

Posted: Thu Jan 14, 2016 1:16 pm
by plugwash
jahboater wrote:Is this a Raspberry Pi2 or one of the other Pi's. The Pi2 has four CPU's so a program using 100% of its CPU may be considered a 25% overall CPU load, but on a B+ it would be 100% overall load. Experiment with top!
Do note that there are two conventions for measuring CPU usage on multicore systems, percentage of a single CPU and percentage of all CPUs.

conventions can vary between different tools and even within one tool.

top seems to use percentage of one CPU for individual processes but percentage of all CPUs for the totals at the top.
munin seems to use percentage of one CPU for total usage

Re: CPU load on computer

Posted: Thu Jan 14, 2016 1:41 pm
by jahboater
top seems to use percentage of one CPU for individual processes but percentage of all CPUs for the totals at the top.
munin seems to use percentage of one CPU for total usage
I prefer top's convention. munin would say the Pi2 CPU is 100% utilized when in fact 75% of the CPU resource is idle. I presume munin says 400% when all four cores are in use.

Re: CPU load on computer

Posted: Thu Jan 14, 2016 1:43 pm
by rpdom
plugwash wrote:top seems to use percentage of one CPU for individual processes but percentage of all CPUs for the totals at the top.
munin seems to use percentage of one CPU for total usage
Pressing "1" while running top will give individual CPU stats.

The cpu usage can be calculated by looking at /proc/stat and comparing successive "cpu" lines to check the difference in cumulative cpu time.

Re: CPU load on computer

Posted: Thu Jan 14, 2016 2:07 pm
by plugwash
jahboater wrote: I prefer top's convention. munin would say the Pi2 CPU is 100% utilized when in fact 75% of the CPU resource is idle. I presume munin says 400% when all four cores are in use.
Yeah, it's not too confusing because munin shows all the categories including "idle" so you get graphs like

Image

(not sure why they don't quite add up to the expected 800% though, maybe there are some cateegoires munin doesn't cover or maybe there is some sort of power management going on shutting cores down)

Re: CPU load on computer

Posted: Thu Jan 14, 2016 4:45 pm
by Bhavani
Thank you so much ! It cleared my doubts! :)

It will be helpful if you could provide some similar codes in C as it will be more helpful in analysing my flow of code .

Re: CPU load on computer

Posted: Thu Jan 14, 2016 5:23 pm
by jahboater
/proc is your friend. See "man 5 proc" and look down for /proc/stat which will give you details of each field.

The file "/proc/stat" appears as a normal text file. It may be opened in the usual way (fopen) (you can also look at it with your favourite editor for example) and read with scanf. Read and save the values of interest to you. Sleep() for 1 second and re-read the file. Subtract the previous values from the current values to derive the usage in the interval.

Re: CPU load on computer

Posted: Thu Jan 14, 2016 6:19 pm
by Bhavani
Thank you !!! :)

Re: CPU load on computer

Posted: Fri May 11, 2018 5:16 pm
by BMardle
Very useful thread! I'm trying much the same thing (displaying processor load on an LED bargraph).
At the moment I'm reading /proc/stat once a second and displaying the change in (system time + user time)/CLK_TCK.
I'm not sure that's actually right... but it does seem to bear some resemblance to what xload displays (once xload has settled down).