train-departure-display icon indicating copy to clipboard operation
train-departure-display copied to clipboard

Raspberry Pi Zero WH maxes out at 100% constantly

Open mattanorak opened this issue 2 years ago • 32 comments

Looking in the dashboard, I see that my RPi Zero WH is constantly maxing out at 99-100% CPU and the effective FPS is anywhere from about 2.2 to 2.22 - consequently the refresh on the screen (when text scrolls) is very slow and jumpy rather than smooth as with the Pi3. I also notice that the clock at the bottom sometimes misses a second or catches up after a pause (especially at the time when scrolling begins on the 'calling at' stations).

It could well be that the Pi Zero WH is just not powerful enough for this, but wanted to raise it in case it's something else causing an issue with this device.

This is only for the Pi Zero WH - the Pi3 is fine.

mattanorak avatar Nov 04 '21 09:11 mattanorak

This is something that's bugged me for a long time, and I have had a few different debugging sessions with no real improvement. I did the original development work on a Pi 3 and like you found it to be a lot smoother, but had to sacrifice the performance for the convenient form factor of the Zero. However with the recent release of the Zero 2 I am hoping we can have the best of both worlds! I have one arriving in a few days and will put it to work in my sign when I get a chance.

chrisys avatar Nov 05 '21 00:11 chrisys

I've got one coming next week as well. What device would it be under in Balena Cloud?

mattanorak avatar Nov 06 '21 10:11 mattanorak

I have a zero 2 and am seeing 78 - 82% CPU utilisation. I'm running the python code directly as it doesn't seem to be supported under Balena Cloud as yet.

LeePorte avatar Nov 07 '21 14:11 LeePorte

We're still working on adding the device type to balenaCloud but it shouldn't be too much longer. This is the issue to follow for updates: https://github.com/balena-os/balena-raspberrypi/pull/725

chrisys avatar Nov 09 '21 20:11 chrisys

@LeePorte what effective frame rate does the code report in the logs when you're running on the Zero 2?

chrisys avatar Nov 09 '21 20:11 chrisys

I've seen highs of 17.91 and lows of 10.86

Nov 08 11:14:52 train-sign python3[2778]: Effective FPS: 10.86
Nov 08 11:14:52 train-sign python3[2778]: Effective FPS: 10.88
Nov 09 08:48:29 train-sign python3[10163]: Effective FPS: 17.9
Nov 09 08:48:29 train-sign python3[10163]: Effective FPS: 17.91

LeePorte avatar Nov 09 '21 21:11 LeePorte

Excellent - might give that a try while I'm waiting for the device type to appear on BalenaCloud (physical device arrived today).

mattanorak avatar Nov 10 '21 21:11 mattanorak

If it would help I can post up the systemd service file.

LeePorte avatar Nov 10 '21 22:11 LeePorte

Exellent - if you can!

mattanorak avatar Nov 11 '21 00:11 mattanorak

Here's my service file to use as a base

[Unit]
Description=Train Sign Service
After=multi-user.target

StartLimitIntervalSec=500
StartLimitBurst=5

[Service]
Type=idle
WorkingDirectory=/home/pi/uk-train-departure-display
Restart=on-failure
RestartSec=5s
Environment="TZ=Europe/London"
Environment="departureStation=<STATION_CODE>"
Environment="refreshTime=120"
Environment="screenRotation=2"
Environment="apiKey=<API_KEY>"
Environment="operatingHours=8-22"
Environment="screenBlankHours=1-6"
Environment="outOfHoursName=<STATION_NAME>"
ExecStart=/usr/bin/python3 src/main.py

[Install]
WantedBy=multi-user.target

LeePorte avatar Nov 11 '21 15:11 LeePorte

Perfect - thank you. I'll give it a go later tonight on the Zero2 and let you know how I get on.

mattanorak avatar Nov 11 '21 17:11 mattanorak

Thanks, @LeePorte - For some reason I couldn't get the service file to work, so instead I edited config.py (I also had to put VERSION in the home directory (I think that was a Path statement thing though - or lack thereof). What I did was just create a cron entry to run on boot and that did the trick. Before I could get it to work, I had to do a few things like install the dependencies in the requirements.txt file, but that was easy! Thanks for the tip! Currently getting about 19.88 FPS standalone on the Zero2W.

mattanorak avatar Nov 14 '21 00:11 mattanorak

@LeePorte - Odd, I've noticed that this seems to stop running after a while (after 30 mins this morning). I checked using: ps-ef |grep main and I didn't see the main.py process running as I normally do, so this seems to have terminated. I am running the command with: python3 /home/pi/uk-train-departure-display/src/main.py & It's running in the cron as an @reboot, so rebooting fixes it - temporarily.

Anyt thoughts on this?

mattanorak avatar Nov 15 '21 08:11 mattanorak

I had seen some network timeouts that caused it to die. Though since using the systemd service file, I've not had that issue as it has restart on failure set.

I used the following to create and enable the systemd service file

sudo vi /lib/systemd/system/train-sign.service 
sudo systemctl daemon-reload 
sudo service train-sign start

What method did you use to try and install it?

LeePorte avatar Nov 15 '21 12:11 LeePorte

I seem to remember doing something similar (with nano, though, not vi). what did you put in train-sign.service? I copied your system file (but used my settings) - and it didn't do anything. Maybe I'll give it another go.

mattanorak avatar Nov 15 '21 12:11 mattanorak

Nano is fine, use your editor of choice

The contents of train-sign.service was the following with some of the details adjusted (<STATION_CODE>, <API_KEY> and <STATION_NAME>) to suit my info.

[Unit]
Description=Train Sign Service
After=multi-user.target

StartLimitIntervalSec=500
StartLimitBurst=5

[Service]
Type=idle
WorkingDirectory=/home/pi/uk-train-departure-display
Restart=on-failure
RestartSec=5s
Environment="TZ=Europe/London"
Environment="departureStation=<STATION_CODE>"
Environment="refreshTime=120"
Environment="screenRotation=2"
Environment="apiKey=<API_KEY>"
Environment="operatingHours=8-22"
Environment="screenBlankHours=1-6"
Environment="outOfHoursName=<STATION_NAME>"
ExecStart=/usr/bin/python3 src/main.py

[Install]
WantedBy=multi-user.target

It might we worth checking your python3 path with which python3 and making sure it matches.

LeePorte avatar Nov 15 '21 12:11 LeePorte

Yep, I set python3 to run as default and in the script anyway, I call python3 .... I also found that running it like this will also restart the process (I think) if it terminates: setsid python3 /home/pi/uk-departure-display/src/main.py < /dev/zero &> /home/pi/log.txt &

Next, if this keeps terminating (which it seems to be OK up to now), I'll try belt and braces in a shell script like this:

!/bin/bash if [ $(ps aux | grep 'yourscript.py' | grep -v grep | wc -l | tr -s "\n") -eq 0 ]; then /your/path/to/yourscript.py; fi &

Messy, but it looks like it would work :-)

mattanorak avatar Nov 15 '21 12:11 mattanorak

OK, I got it working as a service now. I'll see how long it stays up. The only difference I used here (and that seemed to get it working) was instead of: After=multi-user.target I used: Requires=network.target and then in the main section I added: User=pi

Fingers crossed....

mattanorak avatar Nov 15 '21 13:11 mattanorak

@mattanorak How's it been holding up?

LeePorte avatar Nov 15 '21 19:11 LeePorte

Interestingly CPU usage seems to be a little lower under Bullseye

bullseye-cpu

LeePorte avatar Nov 15 '21 20:11 LeePorte

Seems to be OK - stayed up all yesterday and is still there this morning :-) CPU seems roughly the same as yours. image

Thanks for the assistance - will keep an eye on it and see how it goes.

mattanorak avatar Nov 16 '21 08:11 mattanorak

Hey @LeePorte @mattanorak since balena now has support for the Pi Zero 2 I wonder if you guys have given it a try?

chrisys avatar Dec 06 '21 16:12 chrisys

I confess i have not. I've been working on the dual display. If I get a chance I'll give it a spin in the next few days.

LeePorte avatar Dec 06 '21 19:12 LeePorte

Hi - also not had chance to test. I'll see if I can do that this week. I've got a couple of Zero W 2's now and some spare memory cards, so if I get some time I'll give it a whirl.

mattanorak avatar Dec 07 '21 11:12 mattanorak

Just a heads up guys, I haven't attached a display to my Zero 2 yet but I did try building the project for the Zero 2 and it fails. The way to get it to work at the moment is to set the default device type for the balenaCloud application to the Zero 1 (as you would have done before), then add a Zero 2 to the Zero 1 application. This way the builders build for the 32 bit architecture of the Zero 1 but it still runs on the Zero 2 (64 bit) as it's backward compatible. I will look into the changes necessary to update the project to make it build for 64 bit architectures.

chrisys avatar Dec 09 '21 23:12 chrisys

Scratch that, it should work now due to the workaround in #56

chrisys avatar Dec 10 '21 00:12 chrisys

@mattanorak what is the performance like now with the latest version?

chrisys avatar Jul 07 '23 18:07 chrisys

@chrisys I've just updated and it's running much faster. Possibly a bit too fast. Though that may just be me being used to how it was before. I'll try and grab a video.

LeePorte avatar Jul 07 '23 18:07 LeePorte

Must admit I've not tried it in the dashboard for a while, I've been running standalone - I'll plug my spare in and give it a go.

mattanorak avatar Jul 08 '23 09:07 mattanorak

Here's a video of it running on a PI zero 2W

And the resulting htop

train-sign-htop

Definitely much lower resource use

LeePorte avatar Jul 08 '23 19:07 LeePorte