View Single Post
Posts: 10 | Thanked: 8 times | Joined on Jan 2011 @ Malaysia
#106
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.

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.

Code:
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.

Configure microSD card for swapping

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.

Using microSD partition as swap

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

Please Take Note On Below Explaination, This I don't Want You All Simply KeyIn Script Without Understanding What It Means
by
- mmcblk0
- mmcblk0p1
- mmcblk0p2
- mmcblk0p3
- mmcblk1
- mmcblk1p1
- mmcblk1p2


* /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:

Code:
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.

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:

Code:
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:

Code:
/home/user # cat /proc/swaps
Result:
Code:
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.

Credit To - Original Author Unknown
 

The Following 6 Users Say Thank You to izzuddinmeister For This Useful Post: