Bhavani
Posts: 3
Joined: Thu Jan 14, 2016 1:35 am

CPU load on computer

Thu Jan 14, 2016 1:54 am

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.

jahboater
Posts: 4832
Joined: Wed Feb 04, 2015 6:38 pm

Re: CPU load on computer

Thu Jan 14, 2016 12:53 pm

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.
Last edited by jahboater on Thu Jan 14, 2016 1:21 pm, edited 1 time in total.

plugwash
Forum Moderator
Forum Moderator
Posts: 3475
Joined: Wed Dec 28, 2011 11:45 pm

Re: CPU load on computer

Thu Jan 14, 2016 1:16 pm

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

jahboater
Posts: 4832
Joined: Wed Feb 04, 2015 6:38 pm

Re: CPU load on computer

Thu Jan 14, 2016 1:41 pm

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.

User avatar
rpdom
Posts: 15577
Joined: Sun May 06, 2012 5:17 am
Location: Chelmsford, Essex, UK

Re: CPU load on computer

Thu Jan 14, 2016 1:43 pm

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.

plugwash
Forum Moderator
Forum Moderator
Posts: 3475
Joined: Wed Dec 28, 2011 11:45 pm

Re: CPU load on computer

Thu Jan 14, 2016 2:07 pm

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)

Bhavani
Posts: 3
Joined: Thu Jan 14, 2016 1:35 am

Re: CPU load on computer

Thu Jan 14, 2016 4:45 pm

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 .

jahboater
Posts: 4832
Joined: Wed Feb 04, 2015 6:38 pm

Re: CPU load on computer

Thu Jan 14, 2016 5:23 pm

/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.

Bhavani
Posts: 3
Joined: Thu Jan 14, 2016 1:35 am

Re: CPU load on computer

Thu Jan 14, 2016 6:19 pm

Thank you !!! :)

BMardle
Posts: 112
Joined: Wed Feb 13, 2013 4:00 pm
Location: Isle of Wight

Re: CPU load on computer

Fri May 11, 2018 5:16 pm

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).
Bruce Mardle. "You know I yearn for a simpler time of barn dances and buggy rides before life was cheapened by heartless machines."

Return to “C/C++”