Swap on microSD

Contents

[edit] Swap

Swap is a virtual memory which runs off storage devices (HDD, SSD, eMMC, microSD...). It is in a form of a file on a drive (currently this method does not work on Maemo 5) or as a seperate partition (this is actually a better method). Its advantages are that it expands available memory, but it is of course very slow compared to thousand times faster RAM and is therefore used by the system to drop off data which is currently not needed. That means that you can run many programs at once, but they will be running slowly. Virtually every desktop OS does that (for example Windows has page file).

Enabling additional swap is not needed on the N900 for expanding available memory, because it has already 256 MB of RAM and 768 MB of swap space on internal 32 GB eMMC module, resulting in 1 GB of total available memory, which is enough for a device like that. It does however help to offload the work from internal eMMC if there is additional swap space enabled on microSD, because kernel uses both swap partitions and is therefore swapping bandwidth increased which results in higher performance.

[edit] Swappiness

Swappiness is a kernel parameter which tells it how aggressively to swap. The more aggressive it is, more RAM is freed, but there is a performance impact, because more data needs to be swapped (written to the slower drive). Swappiness value is between 0 and 100 with 100 being the least aggressive. Maemo 4 had swappiness set to 1 by default while Maemo 5 sets it to 100. The best value is somewhere in between. 256 MB of N900's RAM is enough for a few open programs and there is also enhanced battery life because eMMC doesn't need to write so much swap data. So swappiness should be set somewhere between 30 and 50. It is set with the following command which has to be run as root.

echo 30 > /proc/sys/vm/swappiness

You can set swappiness at each boot automatically by using a startup script.

(EDIT by egoshin):

In case of N900 you should check that with lower swappiness values you still have an acceptable time of answering calls by phone application. With lower swappiness your N900 more likely meet the situation then there is no enough memory to load phone application for incoming calls - kernel needs to swap-out some applications and you may miss a call. Maximum swap-out speed is around 18 MBytes/sec into eMMC.

With maximum swappiness the kernel just discards the memory pages of some applications because it is already in swap space and cut a time. I believe it is a reason why Nokia set 100 by default for swappiness.

(EDIT by eero):

The situation will also change after a while because swap becomes fragmented (parts of processes' memory are spread all over swap in little pieces). I.e. testing the speed only for few hours after boot isn't enough, new setting needs to be tested for several days.

[edit] Swap Fragmentation

Swap fragmentation starts to occur, when total number of megabytes written during swap's life time exceed it's maximum size. Until that, swap is written in purely sequential manner (blocks that are no longer needed, are marked as free, but never used) - however, after reaching end of swap space, previously used (freed) blocks are rewritten, using new content. This means no longer writing sequentially, causing massive slowdowns, to already slow (compared to RAM) swap. A workaround to this is to disable and enable swap again - linuxswap filesystem content doesn't live through restarts, and once re-enabled, is written sequentially again.

Swapping, Disabling and Re-enabling swap spaces can take upto a few minutes.

Information about your current swap spaces can be found by entering

cat /proc/swaps

in a Terminal

[edit] Solutions to Fragmentation

[edit] Script

Moved to Talk:Swap_on_microSD Options currently in discussion.

[edit] Flopswap

A GUI application Flopswap is available. This application doesn't depend on any packages and can check used blocks and refresh swap.

More information is available on the Flopswap wiki

[edit] Ereswap

An application Ereswap is available. This application as well as checking for defragmention and refreshing swap, also edits /etc/event.d/rcS-late. This edit makes sure your primary swap partition on the Micro SD is available during booting. More information is available on the Ereswap wiki

[edit] Manually configure microSD card for swapping on the device

The microSD card must be split into at least 2 partitions. The first one should be FAT32 (vfat) formatted and the second one must be formatted for linux swap (type 82) and about 384 MB in size. You can change the size of it, but this value should be suitable for most people.

You can repartition and reformat the card with standard Linux tools from the N900 terminal, but the easiest way is to connect the device in mass storage mode to a Linux machine (which can also be a virtual machine if you use other OS as your primary one) and you can do the partitioning with GUI tools.

[edit] Using microSD partition as swap

Reboot after the repartitioning and after the boot, this is seen in /dev.

  • /dev/mmcblk0 (internal eMMC itself)
  • /dev/mmcblk0p1 (27 GB partition mounted on /home/user/MyDocs)
  • /dev/mmcblk0p2 (2 GB partition mounted on /home)
  • /dev/mmcblk0p3 (768 MB swap partition on eMMC)
  • /dev/mmcblk1 (microSD itself)
  • /dev/mmcblk1p1 (FAT32 partition on your microSD mounted on /media/mmc1)
  • /dev/mmcblk1p2 (swap partition on your microSD)
  1. mmc part defines type of storage device
  2. blkX defines number of storage device (0 is internal eMMC, 1 is microSD)
  3. pY defines partition number

The swap partitions are not activated, but you can enable swapping on microSD partition with:

swapon /dev/mmcblk1p2

If you have repartitioned microSD differently you have to change the partition number accordingly. The last part is setting the swapon command to run at every boot and you can do that with a startup script.

There is an additional warning and that is that while the device is running and swap is turned on on microSD partition, you shouldn't take the card out. It would be the same as taking out a RAM stick while running.

[edit] Swap priorities

In order to utilize both swap partitions to balance the I/O load (striping), they should have the same priority. The /bin/swapon command shipped with Maemo is the busybox version that does not support -p option. If you don't have Easy Debian environment installed, download the stock Debian mount package for the armel architecture and extract (-x) the swapon binary from it, and copy it somewhere e.g. /sbin/swapon.debian.

It seems that it's not possible to use -p -1 to match the priority of the default eMMC swap partition. I use the following trick:

  1. activate the microSD swap: swapon.debian -p 0 /dev/mmcblk1p2
  2. temporarily deactivate the eMMC swap: swapoff /dev/mmcblk0p3
  3. activate it again with matching priority: swapon.debian -p 0 /dev/mmcblk0p3

After that your swaps should look like this:

/home/user # cat /proc/swaps
Filename				Type		Size	Used	Priority
/dev/mmcblk0p3                          partition	786424	65464	0
/dev/mmcblk1p2                          partition	393272	97908	0

Depending on the performance of your microSD card, your N900 might now feel much faster when swapping takes place, about the same, or even slower - YMMV. My N900 definitely seems to have less those total I/O trashing episodes lasting 5-10 minutes, where you might think your N900 just died, after doing heavy I/O (e.g. download a 100 MB podcast with gPodder) after activating this dual-swap scheme.

Note: In my testing my N900 crashed when I tried to fill the VFAT data partition on the microSD with something like dd from /dev/null to a file. It's propably best to dedicate the whole microSD card for swap, if possible.