Page 1 of 1

Get pi CPU temp and load

Posted: Sun Aug 04, 2019 9:52 am
by david_1234
Hello ,
I'm trying to get the cpu information
I couldn't find the cpu load

the cpu temp I'm getting using :

Code: Select all

f = open("/sys/class/thermal/thermal_zone0/temp", "r")
    t = f.readline ()
    CPU_Temp_Int = int(t)
    CPU_Temp = (CPU_Temp_Int / 1000)
    
but where is the cpu load?

Thanks ,

Re: Get pi CPU temp and load

Posted: Sun Aug 04, 2019 10:01 am
by DougieLawson

Code: Select all

    t = float(f.readline ())
    t = t / 1000

Re: Get pi CPU temp and load

Posted: Sun Aug 04, 2019 10:03 am
by gordon77

Code: Select all

#!/usr/bin/python3
import sys
import Adafruit_DHT
import io

while True:

    humidity, temperature = Adafruit_DHT.read_retry(11, 4)
    f = open("/sys/class/thermal/thermal_zone0/temp", "r")
    t = int(f.readline ())/1000

    print 'Outside Temp: {0:0.1f} C  Humidity: {1:0.1f} '.format(temperature, humidity)
    print ('CPU Temp is - ' + str(t))

Re: Get pi CPU temp and load

Posted: Sun Aug 04, 2019 10:04 am
by david_1234
Thanks,

and about the cpu load?

Re: Get pi CPU temp and load

Posted: Sun Aug 04, 2019 10:06 am
by rpdom
The CPU/GPU temp of "47236" means 47.236°C
(I see you edited your post while I was typing this)

CPU load is a bit harder to work out. The "top" command will show you an overview. You can toggle between overall usage and per-cpu usage by pressing "1". Press "q" to quit".

The load average figures are possibly more interesting. If the figure goes above 1 x CPU cores (so 1 for Pi 1 or Pi 0, 4 for any other Pi) the Pi is a bit overloaded. This figure can be found in /proc/loadavg.

Just for reference, I've managed servers where the load average has gone over 30 - they struggled a lot with that load! On some of them it was just that the server was not up to handling the load. On others it was bad software. I had to identify which of the two it was and make recommendations or kill things to get stuff up and running again.

Re: Get pi CPU temp and load

Posted: Sun Aug 04, 2019 10:12 am
by david_1234
this is what I wanr\need to do
run my code and see how the PI handle with it...
for now just for report(save the data into a file ).

Thanks ,

Re: Get pi CPU temp and load

Posted: Sun Aug 04, 2019 10:21 am
by gordon77
You could just run uptime from python and store the resultant line, or the load average figures

Re: Get pi CPU temp and load

Posted: Sun Aug 04, 2019 10:27 am
by david_1234
but it will just give me the usage of the python , no?

I want to see of the all system

or I didn't understand you?

Thanks .

Re: Get pi CPU temp and load

Posted: Sun Aug 04, 2019 10:34 am
by david_1234
I found this command

Code: Select all

# Return % of CPU used by user as a character string                                
def getCPUuse():
    return(str(os.popen("top -n1 | awk '/Cpu\(s\):/ {print $2}'").readline().strip()))
but the resualt is in the range of 2.8 - 3.5
when I look at hte icon of the cpu from the gui I can see it changing from 30-60%

how do I get this ?

maybe something from this part?

Code: Select all

cat /proc/stat 
cpu  59661 19 10181 1693018 2582 0 658 0 0 0
cpu0 16032 4 2754 417182 709 0 589 0 0 0
cpu1 14537 2 2708 424932 592 0 33 0 0 0
cpu2 15427 7 2479 424454 589 0 12 0 0 0
cpu3 13665 6 2240 426448 690 0 24 0 0 0
intr 1372397 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 2888 115 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 281 0 0 0 341 13316 0 0 0 0 0 0 0 0 0 0 518533 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 3786 6983 0 0 0 0 3450 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 388040 0 0 0 1 0 3
ctxt 1745002
btime 1564911386
processes 1956
procs_running 2
procs_blocked 0
softirq 963864 103224 234155 5752 51293 0 0 176583 209456 243 183158

Thanks,

Re: Get pi CPU temp and load

Posted: Sun Aug 04, 2019 10:39 am
by gordon77
david_1234 wrote:
Sun Aug 04, 2019 10:27 am
but it will just give me the usage of the python , no?

I want to see of the all system

or I didn't understand you?

Thanks .
It will give you the same average figure as top AFAIK, which is overall.

Re: Get pi CPU temp and load

Posted: Tue Aug 06, 2019 12:18 pm
by Paul Hutch
I've had good luck using the psutil library for getting CPU load.
https://pypi.org/project/psutil/

Re: Get pi CPU temp and load

Posted: Tue Aug 13, 2019 12:07 pm
by david_1234
Thanks
I will take a look at this

Re: Get pi CPU temp and load

Posted: Tue Aug 13, 2019 12:18 pm
by PeterO
glibtop provides an easy API to get cpu loads etc. calling glibtop_get_cpu (glibtop_cpu *buf) gets you one of these :

Code: Select all

struct glibtop_cpu {
	guint64 flags;
	guint64 total;				/* GLIBTOP_CPU_TOTAL		*/
	guint64 user;				/* GLIBTOP_CPU_USER		*/
	guint64 nice;				/* GLIBTOP_CPU_NICE		*/
	guint64 sys;				/* GLIBTOP_CPU_SYS		*/
	guint64 idle;				/* GLIBTOP_CPU_IDLE		*/
	guint64 iowait;				/* GLIBTOP_CPU_IOWAIT		*/
	guint64 irq;				/* GLIBTOP_CPU_IRQ		*/
	guint64 softirq;			/* GLIBTOP_CPU_SOFTIRQ		*/
	guint64 frequency;			/* GLIBTOP_CPU_FREQUENCY */
	guint64 xcpu_total [GLIBTOP_NCPU]; /* GLIBTOP_XCPU_TOTAL		*/
	guint64 xcpu_user [GLIBTOP_NCPU]; /* GLIBTOP_XCPU_USER		*/
	guint64 xcpu_nice [GLIBTOP_NCPU]; /* GLIBTOP_XCPU_NICE		*/
	guint64 xcpu_sys  [GLIBTOP_NCPU]; /* GLIBTOP_XCPU_SYS		*/
	guint64 xcpu_idle [GLIBTOP_NCPU]; /* GLIBTOP_XCPU_IDLE		*/
	guint64 xcpu_iowait [GLIBTOP_NCPU]; /* GLIBTOP_XCPU_IOWAIT		*/
	guint64 xcpu_irq [GLIBTOP_NCPU]; /* GLIBTOP_XCPU_IRQ		*/
	guint64 xcpu_softirq  [GLIBTOP_NCPU]; /* GLIBTOP_XCPU_SOFTIRQ		*/
	guint64 xcpu_flags;	                /* GLIBTOP_XCPU_IDLE		*/
};
I use it to drive this "Load meter" :
Thrust1.png
Thrust1.png (14.5 KiB) Viewed 2083 times
I'm looking at how to use this in python... stay tuned....

Hmmmmmm ..... It doesn't seem to be supported by gintrospection so looks like it's not useable in python3 :cry:

PeterO

Re: Get pi CPU temp and load

Posted: Thu Nov 07, 2019 1:50 pm
by david_1234
Sorry for the bump

I still can't find an answer for this , how to get the CPU Load.
and it can't be that hard - becuase you can see it in the menu as "CPU Usage Monitor", so the number and calculation is there.

so it must be a way to reach and get it , no?

I understadn correctly this command

Code: Select all

 top -n 1 | grep "%Cpu"
%Cpu(s):  6.8 us,  6.8 sy,  0.0 ni, 86.5 id,  0.0 wa,  0.0 hi,  0.0 si,  0.0 st
it mean that currently I'm using 6.8+6.8 = 13.4 "cpu" , and idle is 100-13.4=86.6 which is like 86.5 id
but when I look on the graphic monitor - the number is not 13 but run between 3-8 (I have run this command maybe 20 times to be check the resault )
can it be ?

Thanks,

Re: Get pi CPU temp and load

Posted: Thu Nov 07, 2019 2:24 pm
by jahboater
david_1234 wrote:
Thu Nov 07, 2019 1:50 pm
Sorry for the bump

I still can't find an answer for this , how to get the CPU Load.
and it can't be that hard - becuase you can see it in the menu as "CPU Usage Monitor", so the number and calculation is there.

so it must be a way to reach and get it , no?
uptime and top give you the "load averages".
These are the average number of jobs in the run queue and is an indication the system load. Averaged over 1, 5 and 15 minutes. That is, the number of runnable jobs held up waiting for an available CPU.

Actual CPU load is 100% all the time it is executing anything (of course). Therefore the percentage often given is the percentage of time the CPU is executing something within the measurement interval.
100% load for half the time is 50% utilization.

Re: Get pi CPU temp and load

Posted: Thu Nov 07, 2019 3:09 pm
by david_1234
OK
so can I "trust" the answer I'm getting?
meaning that if I get

Code: Select all

%Cpu(s):  6.8 us,  6.8 sy,  0.0 ni, 86.5 id,  0.0 wa,  0.0 hi,  0.0 si,  0.0 st
which I take from there only the idle "86.5" meaning beaing use 13.5%

the real number is not that importent ,
I just want to make sure the pi is not "kiling" itself and working on 80% CPU load all the time .
then I will know there is a problem .

Thanks ,