Skip to content

Instantly share code, notes, and snippets.

@hernan43
Last active June 21, 2019 19:24
Show Gist options
  • Select an option

  • Save hernan43/3a89c45410413619f1b51c58afe8e225 to your computer and use it in GitHub Desktop.

Select an option

Save hernan43/3a89c45410413619f1b51c58afe8e225 to your computer and use it in GitHub Desktop.

Optimizing the setup

Works with any Raspberry based system like Pi0/1/2/3 whatever revision, and also or CM3 8-)

Ok, lets save some RAM, some CPU, some Watts :D **

707919EE-FBCD-40C3-9A3C-7451C1C5974B.jpeg

1) personnaly, i don't need Samba so -----> OUT Launch Retropie config sudo ~/RetroPie-Setup/retropie_setup.sh Choose C : Configuration / tools Choose 828 : Samba Choose 2 : Remove Samba Shares Choose 5 : Remove Samba

**2) i will never use Romservice so i'd better desactivate it to save :mrgreen: ** Launch Retropie config sudo ~/RetroPie-Setup/retropie_setup.sh Choose C : Configuration / tools Choose 835 : usbromservice Choose 2 : disable usbromservice scripts

3) If you are not HDMI, disabling the circuitry will help save current Shut HDMI Off /usr/bin/tvservice -o(-p to re-enable) Then disable it permanently sudo nano /etc/rc.local and add the code above to the end of the file. CTRL o to write Out the file, CTRL x to eXit the file

**Do not use that code if you are using composite screen or something relying on fbcp because you will go black screen lol. Sorry i didn’t mention this before. It’s still ok if you’ve encountering a black screen. Simply ssh to your pi and delete the entry on rc.local and reboot. It’s safe to use with spi and dpi screens **

4) Lets save more and disable the Pi Status LED sudo nano /boot/config.txt Add this lines to the end of the file

##########################
#### DISABLE PI STATUS LED ######
##########################
dtparam=act_led_trigger=none
dtparam=act_led_activelow=on

CTRL o to write Out the file, CTRL x to eXit the file

5) I/O Scheduler to improve SD performance sudo nano /boot/cmdline.txt change 'elevator=deadline' to 'elevator=noop' (be careful not to destroy the line by adding any breaklines) CTRL o to write Out the file, CTRL x to eXit the file

6) To further extend SD life and reduce power consumptions, we are going to change swappiness to 1; This will allow swap to happen only when we've completely run out of RAM. We do not need to remove swap entirely. sudo nano /etc/sysctl.conf Add this line to the end of the file vm.swappiness = 1 CTRL o to write Out the file, CTRL x to eXit the file

7) We are setting noatime to fat partition. This allows file system to stop writing last time files were accessed and thus saving I/O to the SD (thus expanding life and saving power ) sudo nano /etc/fstab add 'noatime' to /boot Example of my file

proc            /proc           proc    defaults          0       0
PARTUUID=3d24ca30-01  /boot           vfat    defaults,noatime,nodiratime       0       2
PARTUUID=3d24ca30-02  /               ext4    defaults,noatime,nodiratime  0       1
# a swapfile is not a swap partition, no line here
#   use  dphys-swapfile swap[on|off]  for that

CTRL o to write Out the file, CTRL x to eXit the file

8)We need to mount temp and logs to RAM to save SD writes and to save power sudo nano /etc/fstab add those lines to the end of the file tmpfs /var/log tmpfs defaults,size=20m,noatime,nodev,nosuid,mode=1777 0 0 tmpfs /tmp tmpfs defaults,size=10m,noatime,nodev,nosuid,mode=1777 0 0 CTRL o to write Out the file, CTRL x to eXit the file

9)When logging to RAM, we need to setup dummy directories that some services need otherwise, they will not run very good or not run (referenced from: http://raspberrypi.stackexchange.com/a/14094) sudo nano /etc/init.d/prepare-dirs Copy and paste this #!/bin/bash # ### BEGIN INIT INFO # Provides: prepare-dirs # Default-Start: 2 3 4 5 # Default-Stop: 0 1 6 # Required-Start: # Required-Stop: # Short-Description: Create needed directories on /var/log/ for tmpfs at startup # Description: Create needed directories on /var/log/ for tmpfs at startup ### END INIT INFO # needed Dirs DIR[0]=/var/log/apt DIR[1]=/var/log/ConsoleKit DIR[2]=/var/log/fsck DIR[3]=/var/log/ntpstats DIR[4]=/var/log/lastlog DIR[5]=/var/log/watchdog DIR[6]=/var/log/proftpd case "${1:-''}" in start) typeset -i i=0 max=${#DIR[*]} while (( i < max )) do mkdir ${DIR[$i]} chmod 755 ${DIR[$i]} i=i+1 done # set rights chown www-data.www-data ${DIR[0]} ;; stop) ;; restart) ;; reload|force-reload) ;; status) ;; *) echo "Usage: $SELF start" exit 1 ;; esac CTRL o to write Out the file, CTRL x to eXit the file We need to "activate" the script file sudo chmod 755 /etc/init.d/prepare-dirs sudo update-rc.d prepare-dirs defaults 01 99 We make the script executable, and we make sure that the script will be started first on boot before your daemons start

10) Remove avahi daemon. This will be lighter on resources By doing this, you lose ability to refer to 'pi.local' while referecing the Pi, so you will need to use host name or ip address instead sudo apt-get purge avahi-daemon -y

**11) The onboard Bluetooth device of the Raspberry Pi Zero W can be disabled by adding dtoverlay=pi3-disable-bt to the config.txt file. Wifi is disabled with dtoverlay=pi3-disable-wifi. I need wifi so i'll just disable Bluetooth :D (and kite's SAIO allows mode button combination to disable it ** sudo nano /boot/config.txt Add the lines to the end of the file

##########################
#### DISABLE BLUETOOTH ######
##########################
dtoverlay=pi3-disable-bt

CTRL o to write Out the file, CTRL x to eXit the file

12) Disable wait for LAN at boot, will speed up boot time and disable all the interface kernels sudo raspi-config While here in he raspi-config, do the following for your own safety : 1 change password and Define hostname to your like (option 2) (this will prevent someone to easily find you and hack you, better be safe than sorry lol) 3. Boot options -> B2 Wait for network at boot (disable it) 5. Interfacing options Disable Camera Disable VNC Disable SPI Disable I2C Disable Serial Disable 1-wire Then make some other stuff clean also, go to 4. Localization option, set your language and timezone and also the Wifi country (for legal stuff)

13) DISABLE MESSAGES AND LOGO / BOOT for a cleaner look and also to have a fancy look if you are using a spashscreen video sudo nano /boot/cmdline.txt You will see a single line with all the boot options. Scroll along and change the following making sure not to add any linebreaks: - Replace "console=tty1" by "console=tty3" to redirect boot messages to the third console. - Add "loglevel=3" to disable non-critical kernel log messages. (should be default option normally) - Add "logo.nologo" to the end of the line to remove the Raspberry PI logos from displaying CTRL o to write Out the file, CTRL x to eXit the file The logo.nologo option is what turns off the raspberries on boot. sudo nano /boot/config.txt You can also disable the rainbow splash at the beginning (not to be confused with the underpowered rainbow square that appears in the top right corner of your screen indicating you have an insufficient power supply)

##########################
#### DISABLE Rainbow SPLASH ######
##########################
disable_splash=1

14) We are going to replace OpenSSH with DropBear; lighter on resources (minus 10mb RAM used :D ) (referenced from http://pingbin.com/2011/07/how-to-replace-openssh-and-install-dropbear/)

sudo apt-get install dropbear
sudo nano /etc/default/dropbear

At the beginning of the file, you have to set 'NO_START' to '0' to enable DropBear CTRL o to write Out the file, CTRL x to eXit the file

sudo /etc/init.d/ssh stop
sudo /etc/init.d/dropbear start
sudo apt-get purge openssh-server -y

This will stop ssh, start DropBear and uninstall openssh-server.

**15) Now we disable triggerhappy that is used for keyboard hotkeys and thus unnecessary to us (saves a few RAM) **

sudo systemctl disable triggerhappy
sudo systemctl stop triggerhappy

16) If you don't use ipV6, then desactivate it, you will free some RAM and save CPU thus power

sudo nano /etc/sysctl.conf

Add this line to the end of the file net.ipv6.conf.all.disable_ipv6 = 1 CTRL o to write Out the file, CTRL x to eXit the file For the change to take effect without rebooting: sudo sysctl -p

Verify that IPv6 address does not show up in ifconfig. ifconfig You must not see any entries with inet6 addr:...

  1. We need to cleanup the system by removing the unused packages sudo apt-get autoremove --purge

EDIT : i had an issue after clean up, the openssh-sftp-server was judged orphan so deleted by the purge command. I wasn't anymore able to use winscp to connect over ssh to my Pi which is a big issue for transferring roms.

If this is your case, just** sudo apt-get install openssh-sftp-server

and you will be ready to go

18) More fine tuning to the filesystem I/O, optimizing the cache writes

Inspired by https://lonesysadmin.net/2013/12/22/better-linux-disk-caching-performance-vm-dirty_ratio/ (i advise you to read in order to fully understand what you are doing) This tuning will optimize the cache writings to the SD card but be aware that this is unsafe for the data being cached. In our particular usage, we don't really care of cache data being lost, we are caring about extending our SD card life. What worst can happen ? The data in cache is lost due to power fault but what we loose ? Logs ? We do not write them as we want less SD I/O ! Opened documents ? We don't work on documents, this a console, not a workstation... We just play lol so no care of all this. So we loose nothing critical. Once setup, the system is in standalone mode and is only intended to play. You can go with this optimization.

sudo nano /etc/sysctl.conf

add those lines to the end of the file

##########################
#### FINE TUNE CACHING  ########
##########################
vm.dirty_background_ratio=20
vm.dirty_ratio=40
vm.dirty_writeback_centisecs=12000
vm.dirty_expire_centisecs=12000

CTRL o to write Out the file, CTRL x to eXit the file and reboot for the changes to be applied.

*vm.dirty_background_ratio is the percentage of system memory that can be filled with “dirty” pages — memory pages that still need to be written to disk — before the pdflush/flush/kdmflush background processes kick in to write it to disk. My example is 10%, so if my virtual server has 32 GB of memory that’s 3.2 GB of data that can be sitting in RAM before something is done.

vm.dirty_ratio is the absolute maximum amount of system memory that can be filled with dirty pages before everything must get committed to disk. When the system gets to this point all new I/O blocks until dirty pages have been written to disk. This is often the source of long I/O pauses, but is a safeguard against too much data being cached unsafely in memory.

vm.dirty_writeback_centisecs is how often the pdflush/flush/kdmflush processes wake up and check to see if work needs to be done.

vm.dirty_expire_centisecs is how long something can be in cache before it needs to be written. In this case it’s 30 seconds. When the pdflush/flush/kdmflush processes kick in they will check to see how old a dirty page is, and if it’s older than this value it’ll be written asynchronously to disk. Since holding a dirty page in memory is unsafe this is also a safeguard against data loss.*

Those 4 paragraphs are taken directly from https://lonesysadmin.net/2013/12/22/better-linux-disk-caching-performance-vm-dirty_ratio/ and all the credits goes to the author.

In my settings, we push the limits further to really save writes, so we are using 20% or RAm usage for dirty pages, ultimately 40%, and we write data every 2 minutes and check every 2 minutes if something needs to be written.

**## UPDATE 17/03/2018 - Creating a system menu in Emulation Station to apply my optimization settings :mrgreen: **

Yeah, thanks to Somatorio, you can now apply some of my optimization settings right from Emulation Station by accessing a menu system like if you were browsing roms :lol: Somatorio did a huge work on that, there are some minor stuff to improve but the code is perfectly working and the settings are now accessible via a menu system, that means you no longer have to type code in terminal to aply them :mrgreen:

So, please say thank you to Somatorio :D

Here is how to.

  1. SSH into your Game Boy (Pi3 or CM3 or Zero based) read here for instructions on how to https://www.sudomod.com/forum/viewtopic.php?f=44&t=3567

  2. Normally you start on home directory, but just to be sure to not mess up your system by writing stuffs where you must not write, navigate to your home folder cd ~

  3. get the installer wget http://gbz-menu.somatorio.org/

  4. rename the file to something more useful and make the installer executable mv index.html gbz-menu-install chmod +x gbz-menu-install

  5. launch installer sudo ./gbz-menu-install

  6. go have a coffee :lol:

  7. reboot and enjoy sudo reboot

you can see in this video how it looks like :D

[youtube]iZPMvavlxdY[/youtube]

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment