david_1234
Posts: 153
Joined: Mon Jan 01, 2018 2:14 pm

Get pi CPU temp and load

Sun Aug 04, 2019 9:52 am

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 ,
Last edited by david_1234 on Sun Aug 04, 2019 10:03 am, edited 1 time in total.

User avatar
DougieLawson
Posts: 36123
Joined: Sun Jun 16, 2013 11:19 pm
Location: Basingstoke, UK
Contact: Website Twitter

Re: Get pi CPU temp and load

Sun Aug 04, 2019 10:01 am

Code: Select all

    t = float(f.readline ())
    t = t / 1000
Note: Having anything humorous in your signature is completely banned on this forum. Wear a tin-foil hat and you'll get a ban.

Any DMs sent on Twitter will be answered next month.

This is a doctor free zone.

gordon77
Posts: 4165
Joined: Sun Aug 05, 2012 3:12 pm

Re: Get pi CPU temp and load

Sun Aug 04, 2019 10:03 am

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))

david_1234
Posts: 153
Joined: Mon Jan 01, 2018 2:14 pm

Re: Get pi CPU temp and load

Sun Aug 04, 2019 10:04 am

Thanks,

and about the cpu load?

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

Re: Get pi CPU temp and load

Sun Aug 04, 2019 10:06 am

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.

david_1234
Posts: 153
Joined: Mon Jan 01, 2018 2:14 pm

Re: Get pi CPU temp and load

Sun Aug 04, 2019 10:12 am

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 ,

gordon77
Posts: 4165
Joined: Sun Aug 05, 2012 3:12 pm

Re: Get pi CPU temp and load

Sun Aug 04, 2019 10:21 am

You could just run uptime from python and store the resultant line, or the load average figures

david_1234
Posts: 153
Joined: Mon Jan 01, 2018 2:14 pm

Re: Get pi CPU temp and load

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 .

david_1234
Posts: 153
Joined: Mon Jan 01, 2018 2:14 pm

Re: Get pi CPU temp and load

Sun Aug 04, 2019 10:34 am

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,
Last edited by david_1234 on Sun Aug 04, 2019 10:51 am, edited 1 time in total.

gordon77
Posts: 4165
Joined: Sun Aug 05, 2012 3:12 pm

Re: Get pi CPU temp and load

Sun Aug 04, 2019 10:39 am

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.

Paul Hutch
Posts: 385
Joined: Fri Aug 25, 2017 2:58 pm
Location: Blackstone River Valley, MA, USA
Contact: Website

Re: Get pi CPU temp and load

Tue Aug 06, 2019 12:18 pm

I've had good luck using the psutil library for getting CPU load.
https://pypi.org/project/psutil/

david_1234
Posts: 153
Joined: Mon Jan 01, 2018 2:14 pm

Re: Get pi CPU temp and load

Tue Aug 13, 2019 12:07 pm

Thanks
I will take a look at this

User avatar
PeterO
Posts: 5006
Joined: Sun Jul 22, 2012 4:14 pm

Re: Get pi CPU temp and load

Tue Aug 13, 2019 12:18 pm

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 1435 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
Discoverer of the PI2 XENON DEATH FLASH!
Interests: C,Python,PIC,Electronics,Ham Radio (G0DZB),1960s British Computers.
"The primary requirement (as we've always seen in your examples) is that the code is readable. " Dougie Lawson

Return to “Python”