## Get pi CPU temp and load

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

### Get pi CPU temp and load

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.

DougieLawson
Posts: 38536
Joined: Sun Jun 16, 2013 11:19 pm
Location: A small cave in deepest darkest Basingstoke, UK

### Re: Get pi CPU temp and load

Code: Select all

``````    t = float(f.readline ())
t = t / 1000``````
Note: Any requirement to use a crystal ball or mind reading will result in me ignoring your question.

I'll do your homework for you for a suitable fee.

Any DMs sent on Twitter will be answered next month.
All non-medical doctors are on my foes list.

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

### Re: Get pi CPU temp and load

Code: Select all

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

while True:

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: 273
Joined: Mon Jan 01, 2018 2:14 pm

### Re: Get pi CPU temp and load

Thanks,

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

### Re: Get pi CPU temp and load

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: 273
Joined: Mon Jan 01, 2018 2:14 pm

### Re: Get pi CPU temp and load

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: 4933
Joined: Sun Aug 05, 2012 3:12 pm

### Re: Get pi CPU temp and load

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

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

### Re: Get pi CPU temp and load

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: 273
Joined: Mon Jan 01, 2018 2:14 pm

### Re: Get pi CPU temp and load

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: 4933
Joined: Sun Aug 05, 2012 3:12 pm

### Re: Get pi CPU temp and load

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: 507
Joined: Fri Aug 25, 2017 2:58 pm
Location: Blackstone River Valley, MA, USA
Contact: Website

### Re: Get pi CPU temp and load

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

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

### Re: Get pi CPU temp and load

Thanks
I will take a look at this

PeterO
Posts: 5736
Joined: Sun Jul 22, 2012 4:14 pm

### Re: Get pi CPU temp and load

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 (14.5 KiB) Viewed 2300 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

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

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

### Re: Get pi CPU temp and load

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,

jahboater
Posts: 5475
Joined: Wed Feb 04, 2015 6:38 pm
Location: West Dorset

### Re: Get pi CPU temp and load

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.

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

### Re: Get pi CPU temp and load

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 ,