Sunday, June 10, 2012

RPi Tuning: boost your RPi!!

Basically you can gain up to 25% performance simply modifing some text file :)
There are lot of thing to do, to cut down RAM usage, tune the sd card and hack a bit the CPU. Here the complete list of improvment:
RAM usage: remove not needed services and daemon, change start.elf file
Disk tuning: we use an sdcard so everything is writing/reading from our sdcard will drop our (already very low) performance. There are lot of improvment we can use to minimize the hidden i/o and it can be integrated in the kernel or not. So, we have to remove unecessary log from /etc/rsyslog.conf, change the disk scheduler to deadline, add noatime/relatime, noadirtime, writeback to ext4 partition and use squashfs+aufs as filesystem for /usr
CPU Hack: just change in config.txt the arm_freq, gpu_freq and sdram_freq... at your own risk!!!

All of this improvment is aimed to speedup RPi to be an thin client, but generally it can be used for every use!

RAM usage:
apt-get install sysv-rc-conf
remove not used service (i remove nfs)
vi /etc/inittab (remove all getty spawn)
modify /boot/cmdline.txt and remove: console=ttyAMA0,115200 kgdboc=ttyAMA0,115200 (we dont need console)
dpkg-reconfigure dash
dpkg-reconfigure insserv
/etc/xdg/lxsession/LXDE/autostart (remove line xscreensaver)
apt-get remove gvfs
apt-get remove gnome
apt-get autoremove

Disk tuning:
- Create partitions aligned on Flash blocks:
printcsd.py to find the erase block size (most sd have 128K)
fdisk -H 224 -S 56 /dev/mmcblk0
mke2fs -t ext4 -E stripe-width=32 -m 0 /dev/mmcblk0p1
- Format partitions with journalling disabled
mkfs.ext4 -O ^has_journal -L PiBoot /dev/mmcblk0p1
fsck.ext4 -f /dev/mmcblk0p1
remove unecessary log from /etc/rsyslog.conf
and put -/var/log/messages (-) in the used log daemon
- Disk scheduler elevator=deadline on cmdline.txt (or rebuild kernel)
- noatime (no access time), nodiratime (same for directories instead of files) and data=writeback
put on cmdline.txt: rootflags=data=writeback,commit=120 quiet
Squashfs+aufs (kernel rebuild is needed!!):
sudo apt-get install gcc-4.6-arm-linux-gnueabi
git clone https://github.com/raspberrypi/linux.git
cd linux
git clone git://aufs.git.sourceforge.net/gitroot/aufs/aufs3-standalone.git
cd aufs3-standalone
git checkout origin/aufs3.1
cp -rp *.patch ../
cp -rp fs ../
cp -rp Documentation/ ../
cp -rp include/ ../
cd ..
patch -p1 < aufs3-kbuild.patch
patch -p1 < aufs3-base.patch
patch -p1 < aufs3-proc_map.patch
patch -p1 < aufs3-standalone.patch

sudo ln -s  /usr/bin/arm-linux-gnueabi-gcc-4.6 /usr/bin/arm-linux-gnueabi-gcc
cp arch/arm/configs/bcmrpi_defconfig .config
make ARCH=arm CROSS_COMPILE=/usr/bin/arm-linux-gnueabi- menuconfig
remove anything you don't need and add AUFS on FileSystem group.
make ARCH=arm CROSS_COMPILE=/usr/bin/arm-linux-gnueabi- -k
mkdir /tmp/modules
make ARCH=arm modules_install INSTALL_MOD_PATH=/tmp/modules
cp arch/arm/boot/Image /boot/kernel.img
cp -rp /tmp/modules/lib/ /
reboot

sudo aptitude install squashfs-tools aufs-tools
sudo mksquashfs /usr /.usr.sqfs
init 1
cd /
mv /usr /usr-old
mkdir /usr
mkdir -p /var/squashed/{ro,rw}
nano -w /etc/fstab
/.usr.sqfs  /var/squashed/ro  squashfs  loop,ro  0 0
aufs  /usr  aufs  br:/var/squashed/rw=rw:/var/squashed/ro=ro  0 0
mount -a

CPU Hack: use at your own risk!!! (all tested and working)
this is my default:
/boot/config.txt:
arm_freq=800

high hack:
/boot/config.txt:
arm_freq=900
gpu_freq=250
sdram_freq=500

extreme hack:
/boot/config.txt
arm_freq=1000
core_freq=500
sdram_freq=500
over_voltage=6


printcsd.py:
#!/usr/bin/env python
import sys

def unstuff(x, start, size):
    return (x >> start) & (2**size - 1)

def main(name, args):
    if len(args) != 1:
        print "Syntax: %s <card>" % (name, )
        print "Example: %s mmcblk0" % (name, )
        return 100

    card = args[0]
    dev = "/sys/class/block/%s/device/csd" % (card, )
    csd = int(file(dev).read(), 16)
    write_block_size = 2**unstuff(csd,22,4)
    erase_block_size = write_block_size*(unstuff(csd,39,7)+1)
    print "Erase block size of %s is %d bytes." % (card, erase_block_size)

sys.exit(main(sys.argv[0], sys.argv[1:]))

2 comments:

  1. I get the following output:

    Erase block size of mmcblk0 is 65536 bytes.

    What do I do with it now?

    ReplyDelete