View on GitHub

Hglm.github.io

hglm's blog

Optimizing system performance of flash-based Linux systems

Here are a few tips to improve interactive performance on flash (SD-card)-based devices running Linux, such as the Raspberry Pi, other ARM boards and tablets. These apply primarily to Debian and derived distros such as Raspbian, probably Ubuntu, and in general terms to most Linux distributions.

When the root filesystem used on the device is a flash memory card or flash memory as will often be the case, the performance bottleneck will often be write access to the root file system. It is therefore beneficial for overall system performance to reduce unnecessary writing by using methods such as ramdisk file systems, tmpfs, and reducing system logging activity.

Reducing system logging activity

In a default distro install, system logging is often configured fully, suitable for a server or multi-user system. However, on a single-user system the constant writing the many system log files will result in reduced interactive system performance, and reducing logging activity will be beneficial for performance as well as the lifetime of the flash memory.

In a Debian Wheezy installation, the default system logger is rsyslog, and it configuration file is /etc/rsyslog.conf. In the rules section, the following logs are often enabled by default:

 auth,authpriv.*                 /var/log/auth.log
 *.*;auth,authpriv.none         -/var/log/syslog
 cron.*                         /var/log/cron.log
 daemon.*                       -/var/log/daemon.log
 kern.*                         -/var/log/kern.log
 lpr.*                          -/var/log/lpr.log
 mail.*                         -/var/log/mail.log
 user.*                         -/var/log/user.log

There may also be rules for -/var/log/debug and -/var/log/messages, and |/dev/xconsole.

Note that kernel messages are logged in both kern.log and syslog, in addition to being available from the dmesg command from kernel memory. In a single user system, it is possible to disable most or all of these logs by placing a '#' character at the start of the corresponding lines. Logs can be re-enabled if it is necessary to debug a system problem.

When using the a X environment there are may also be a log-file called .xsession-errors in your home directory. This contains mostly irrelevant warnings and error messages from various applications and can grow quite large. Importantly, the constant writing to this file can really affect performance on a flash-based system. To disable it, edit the file /etc/X11/Xsession and change the line:

 exec >> "$ERRFILE" 2>&1
to
 exec >> /dev/null 2>&1

Optimizing cache directories

Applications like browsers and window managers that use a disk cache may conform to the XDG Base Directory Specification standard. In that case, the environment variable XDG_CACHE_HOME defines the directory where cache files are stored. By setting this variable to a ramdisk location, it is possible to greatly speed-up the performance of certain browsers that otherwise stall with heavy writing to the disk-cache in flash memory. For example, in a Debian Wheezy installation with LXDE, there may be a configuration file called /etc/alternatives/x-session-manager. By adding the line

 export XDG_CACHE_HOME="/dev/shm/.cache"

to the start of this file, programs running in X conforming to the standard will be using the ramdisk in /dev/shm to store cache files. One browser popular on light-weight configurations that benefits form this is Midori. The default disk cache size in Midori is 100MB, you can lower this if you don't have much system memory available, but you can configure Midori to clean the cache on exit and thus give back the memory to the system in Tools->Clear Private Data.

A better solution, and required if /etc/alternatives/x-sessions-manager is a binary file, is add the same line to a new file in /etc/profile.d/, for example /etc/profile.d/xdg_cache_home.sh, that will be executed at the start of every shell. The file should look like this:

 #!/bin/bash
export XDG_CACHE_HOME="/dev/shm/.cache"

Using tmpfs

To mount /tmp and /var/tmp (the directories for temporary files used by programs) on a ramdisk, you may be able to add the following lines to /etc/fstab:

 tmpfs    /tmp        tmpfs    defaults    0 0
 tmpfs    /var/tmp    tmpfs    defaults    0 0

Changing the ext4 root filesystem to write-back mode

If your root ext4 root filesystem is mounted in ordered data mode, it is possible to improve performance by changing the filesystem type to write-back, which speeds up write operations. The disadvantage of this is that it increases the chances of losing data in case of a (hard) system crash, although it seems work reasonably safely. With recent versions of ext4 you must change the filesystem mount type using the tune2fs utility. It is not necessary to change settings in /etc/inittab, except perhaps to add the "noatime" option to your root filesystem for performance reasons if it is not already enabled. To change the filesystem to writeback mode, which will take effect on the next reboot, run as root:

tune2fs -o journal_data_writeback /dev/sdXn
where /dev/sdXn is the device corresponding to your root partition.