Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Raspberry Pi OS/Debian] psutil.cpu_percent() shows wrong percentage, sort of #2030

Closed
Crilum opened this issue Dec 7, 2021 · 2 comments
Closed

Comments

@Crilum
Copy link

Crilum commented Dec 7, 2021

Summary

  • OS: { Raspberry Pi OS Bullseye (which is based off Debian) }
  • Architecture: { 32bit ARM (armhf) }
  • Psutil version: { 5.8.0 }
  • Python version: { Python 3.9.2 }
  • Type: { not sure, just imported for use in a script }

Description

I'm editing a script that displays CPU Temp and Time on an LCD screen, and instead of having CPU Temp and Time, I wanted to have CPU usage in percent, and the CPU frequency, so I made a function for both:

def get_cpu_percent(): # get CPU usage
    cpuper = psutil.cpu_percent(interval=0.5)
    return '{:.2f}'.format( float(cpuper)/1000 ) + ' %'
def get_cpu_frequency():
    return str(psutil.cpu_freq().current)

and called it in a loop:

def loop():
    mcp.output(3,1)     # turn on LCD backlight
    lcd.begin(16,2)     # set number of LCD lines and columns
    while(True):         
        #lcd.clear()
        lcd.setCursor(0,0)  # set cursor position
        #lcd.message( 'CPU Temp: ' + get_cpu_temp()+'\n' )# display CPU temperature
        lcd.message( 'CPU %: ' + get_cpu_percent() + '\n' )
        lcd.message( 'CPU MHz: ' + get_cpu_frequency() )
        #lcd.message( get_time_now() )   # display the time
        sleep(1)

and ran the loop.

And it worked, but the get_cpu_percent() function shows CPU %: 0.03on the LCD when called in the loop. The 3 is an echo of every 10%, so if the CPU were running at 50%, the LCD would show CPU %: 0.05, and if the CPU were running at 100%, the LCD would show CPU %: 0.10 (theoretically, I haven't used the CPU at 100% to test..), and so on and so forth..
The frequency function worked fine.

If this is a user error, sorry.. I'm new to python..

This is the whole script:

#!/usr/bin/env python3
from PCF8574 import PCF8574_GPIO
from Adafruit_LCD1602 import Adafruit_CharLCD

from time import sleep, strftime
from datetime import datetime
import psutil
 
def get_cpu_temp():     # get CPU temperature and store it into file "/sys/class/thermal/thermal_zone0/temp"
    tmp = open('/sys/class/thermal/thermal_zone0/temp')
    cpu = tmp.read()
    tmp.close()
    return '{:.2f}'.format( float(cpu)/1000 ) + 'C'
 
def get_cpu_percent(): # get CPU usage
    cpuper = psutil.cpu_percent(interval=0.5)
    return '{:.2f}'.format( float(cpuper)/1000 ) + ' %'
    print(cpuper())

def get_cpu_frequency():
    return str(psutil.cpu_freq().current)

def get_time_now():     # get system time
    return datetime.now().strftime('Time: %H:%M:%S')
    
def loop():
    mcp.output(3,1)     # turn on LCD backlight
    lcd.begin(16,2)     # set number of LCD lines and columns
    while(True):         
        #lcd.clear()
        lcd.setCursor(0,0)  # set cursor position
        #lcd.message( 'CPU Temp: ' + get_cpu_temp()+'\n' )# display CPU temperature
        lcd.message( 'CPU %: ' + get_cpu_percent() + '\n' )
        lcd.message( 'CPU MHz: ' + get_cpu_frequency() )
        #lcd.message( get_time_now() )   # display the time
        sleep(1)
def destroy():
    print('Aborting!')
    lcd.clear()
    
PCF8574_address = 0x27  # I2C address of the PCF8574 chip.
PCF8574A_address = 0x3F  # I2C address of the PCF8574A chip.
# Create PCF8574 GPIO adapter.
try:
    mcp = PCF8574_GPIO(PCF8574_address)
except:
    try:
        mcp = PCF8574_GPIO(PCF8574A_address)
    except:
        print ('I2C Address Error !')
        exit(1)
# Create LCD, passing in MCP GPIO adapter.
lcd = Adafruit_CharLCD(pin_rs=0, pin_e=2, pins_db=[4,5,6,7], GPIO=mcp)

if __name__ == '__main__':
    print ('Program is starting ... ')
    try:
        loop()
    except KeyboardInterrupt:
        destroy()
@Crilum Crilum added the bug label Dec 7, 2021
@github-actions github-actions bot added the linux label Dec 7, 2021
@n-ext
Copy link

n-ext commented Dec 17, 2021

Seems like your error. Just remove /1000 from get_cpu_percent().

def get_cpu_percent(): # get CPU usage
    cpuper = psutil.cpu_percent(interval=0.5)  # <-- It returns value in %, so for 50% it returns 50.0 etc
    return '{:.2f}'.format( float(cpuper)/1000 ) + ' %'  # <-- Here you are dividing by 1000, so your 50.0 becomes 0.05

@Crilum
Copy link
Author

Crilum commented Dec 17, 2021

Ahh, thanks!

@Crilum Crilum closed this as completed Dec 17, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants