Optimizing Octoprint/Pi

Optimize your pi and Octoprint

As an engineer, over the years, I always try to keep my systems running optimally. Whether it's hardware, operating system, programs, or network, I want to get out of it what I'm paying for. After running Octoprint on a raspberry pi for many years, these are the optimizations that I've made to make it run much better. If you go outside the boundaries of the information I am giving here, you are on your own. If you don't take my warnings, that is your problem. If you do not feel comfortable doing these changes, then don't do it. I am taking for granted that you know how to SSH in to your pi. Let's go.

Suggested free software
I use 'Putty' to SSH into my pi;
https://www.putty.org/

For more of a GUI interface file manager, WinSCP;

The pi... Suggestions...
The Raspberry Pi is an awesome piece of engineering. There are a few minor things that I have noticed over the years, playing with them, to make it better, faster, and last longer.
You may have a different model, but I have a raspberry pi 3B+. It draws 2.8 to 2.9 amps of current. They give you a 3 amp power supply. I suggest very strongly going up to a 4AMP power supply. You might need an adapter.
If you are running a pi 4 or 5 I suggest going to a 5 amp or higher.

I suggest using heat sinks on the chips.
image

Keeping things cool, run a 5v 25mm fan;
image
With the right connector you can plug this straight onto the pi.

Linux OS... the tune up...
I have found that force_turbo=1 does not always work. We are not overclocking it, but we can set the lower speed to the highest speed, and that will force it to run at maximum all the time. Note: On most machines, overclocking anything will not really show any great improvement.
Get current settings for your cpu;

> lscpu
You will see the following two settings:
image

> sudo nano /boot/config.txt
Mine runs at 1.4 gigahertz. Use your settings.
Scroll down and either alter or enter the following two lines (Enter your max frequency);
image

you don't need the .000
Note: If you don't have it, you can add 'force_turbo=1'
Save the file. (CTRL X, Y, ENTER)

Octoprint and gzip... big improvement...
Most web servers today use on the fly data compression. It is called gzip. When you install a Web server, by default, these are turned off. I noticed Octoprint loaded slowly, My webcam would freeze, And most pages would lag. Let's turn this on...

> sudo nano /etc/nginx/nginx.conf
Scroll down to:
image

Copy and paste in the following;

    gzip on;
    gzip_disable "msie6";

    gzip_vary on;
    gzip_proxied any;
    gzip_comp_level 6;
    gzip_buffers 16 8k;
    gzip_http_version 1.1;
    gzip_min_length 128;
    gzip_types
      application/atom+xml
      application/geo+json
      application/javascript
      application/x-javascript
      application/json
      application/ld+json
      application/manifest+json
      application/rdf+xml
      application/rss+xml
      application/vnd.api+json
      application/xml+rss
      application/xhtml+xml
      application/xml
      font/eot
      font/otf
      font/ttf
      image/svg+xml
      text/css
      text/javascript
      text/plain
      text/xml;

Save the file.

Give Octoprint and cameras a high priority... be renice...
There are times when I want to SSH into my pi to do other things, but I don't want to interrupt or slow down the print. I set my octoprint and cameras to a higher priority. This gives the apps more processor time. Even if you don't SSH into your pi and do other things, This will make it run faster. The settings are as follows;
Highest Default Lowest
-19 ---------- 0 -------- 20
I suggest not going higher than -10.

> sudo nano /etc/rc.local
Scroll to the bottom, just before the 'Exit 0', copy and paste;

sudo renice -10 -p $(pgrep 'octoprint')

sudo renice -7 -p $(pgrep 'camera-streamer')

Save the file.

Cameras using only 2 processors... Use all 4... share the load...
Only do this if you are running a quad processor from lscpu;
image

I don't normally suggest altering services, but this is the only way to do it;

> sudo nano /etc/systemd/system/camera-streamer-libcamera.service
alter allowedCPUs to;
image

Save file.

Note: If you have lowered your fps on your cameras because they kept freezing, this problem should go away with these settings.

I suggest getting the 'Resource Monitor' plugin;

The biggest thing, you can monitor all of your pi info, specifically the temperature;
image

When done, reboot your pi.

I'm not sure this will impact everyone, because the standard OctoPi installation is running it's own flask server, and that is front-ended by haproxy not nginx. I think nginx might be used for HLS camera feed though.

1 Like

OK.
have you tried it????

-JC

I have not yet, mainly because I've never experienced any slow downs on my pi 4 4gb model. I also don't use the new camera stack and therefore camera-streamer/nginx are not in my setup.

Cool. Sounds like an awesome setup.
Have you tried the renice?? That seemed to help with the webpages.
You can nice it in the services but I try to stay out of there.

-JC

Try:
sudo renice -10 -p $(pgrep 'haproxy')

-JC

so going along with your renice logic, I went ahead and added these to rc.local and it does seem a little quicker than before for initial page load.

sudo renice -10 -p $(pgrep 'octoprint')
sudo renice -10 -p $(pgrep 'haproxy')
sudo renice -7 -p $(pgrep 'mjpg_streamer')

Very cool.
That is why I used grep.

Thanks for taking a look at it.

-JC