Pradeep Singh | 1st Sep 2017
Swap space in Linux is used when the amount of physical memory (RAM) is full. If the system needs more memory resources and the RAM is full, inactive pages in memory are moved to the swap space.
Swap space can be a dedicated swap partition (recommended), a swap file, or a combination of swap partitions and swap files. For systems with up to 2 GB of physical RAM, recommended swap space size should be equal to 2 times the physical RAM; and never less than 32 MB.
Zswap is a Linux kernel feature providing a compressed write-back cache for swapped pages. Instead of moving memory pages to a swap device when they are to be swapped out, zswap performs their compression and then stores them into a memory pool dynamically allocated inside system’s RAM.
piCore’s Default Swap Space (Zswap):
By default piCore has a zlib compressed swap in RAM, automatically sized to 25% of available RAM. Zlib is a cross platform, lossless data-compression library based on gzip file compression program written by Jean-loup Gailly and Mark Adler.
Check the Default Swap Location and Size:
You can check the current Swap space and its size using “cat /proc/swaps” command. In the following screen output, you can see piCore is using “/dev/zram0” as default swap space –
tc@box:~$ cat /proc/swaps Filename Type Size Used Priority /dev/zram0 partition 228264 0 -1 tc@box:~$
You can check the Swap space size in Megabytes (MB) using “free -m” command. In the following command output, you can see, we have 923 MB RAM available in Raspberry Pi 3 and out of that approximately 25% i.e 222 MB is used for Zswap –
tc@box:~$ free -m total used free shared buffers cached Mem: 923 55 867 9 3 20 -/+ buffers/cache: 32 891 Swap: 222 0 222 tc@box:~$
Let’s explore some tweaking with piCore Swap. Here in Part-A, we will see how to add a new Swap Disk to piCore Swap. Then in Part-B, we will explore, how to disable zswap and use only the traditional swap partition.
PART A – Increase PiCore Swap Capacity with New Swap Disk
1. Create a New Swap Disk:
If you want to add an extra Swap Disk or replace the Zswap with local disk based swap, you need to create a new Swap partition on your SD Card.
I created a new Primary Partition with Partition ID 3 and with size approximately around 2 GB using “sudo fdisk -u /dev/mmcblk0” command (new disk partition name is /dev/mmcblk0p3) –
2. Reboot Raspberry Pi:
After partitioning the SD Card for a new Swap disk you need to reboot your Raspberry Pi so that piCore can detect the new Partition. You can use “sudo reboot” command to reboot the system.
3. Format the Swap Disk with ‘mkswap’:
Format the newly created swap partition using “mkswap /dev/mmcblk0p3” command (please change the partition name in this command as per your SD Card partitions) –
tc@box:~$ mkswap /dev/mmcblk0p3 Setting up swapspace version 1, size = 2094788608 bytes UUID=919549fb-e39b-4524-8838-c3589364d102 tc@box:~$
At this time, if you check your Swap details, you will not notice any difference as piCore is still running in RAM and it can’t detect the changes made on the physical media –
tc@box:~$ cat /proc/swaps Filename Type Size Used Priority /dev/zram0 partition 227884 0 -1 tc@box:~$ free -m total used free shared buffers cached Mem: 923 55 867 9 3 20 -/+ buffers/cache: 32 891 Swap: 222 0 222 tc@box:~$
4. Reboot Raspberry Pi Again:
Reboot your Raspberry Pi again so that piCore can detect new Swap Disk. You can use “sudo reboot” command to reboot the system.
5. Check the Swap Details:
Now after the system reboot, if you check your system Swap details again, you would notice, PiCore has detected and added a new Swap Disk and the total swap size has increased.
tc@box:~$ cat /proc/swaps Filename Type Size Used Priority /dev/zram0 partition 228236 0 -1 /dev/mmcblk0p3 partition 2045692 0 -2 tc@box:~$ free -m total used free shared buffers cached Mem: 923 56 866 9 3 20 -/+ buffers/cache: 33 889 Swap: 2220 0 2220 tc@box:~$
With this, you have an increased swap space on your Raspberry Pi.
PART B – Disable Zswap on PiCore
1. Mount ‘/dev/mmcblk0p1’:
During the boot process, PiCore OS is loaded into memory (RAM) from “/dev/mmcblk0p1” partition, and after that is unmounted. To make any changes in the OS configuration we need to mount this partition manually. You can mount this partition using the following command –
2. Find the “cmdline” File Name Used for Your Raspberry Pi Model:
There are different “cmdline” files for different Raspberry Pi models, For example, for Raspberry Pi, piCore would use “cmdline3.txt“. To find all the mappings check “config.txt” file contents using the following command –
sudo cat /mnt/mmcblk0p1/config.txt | more
3. Add the ‘nozswap’ Boot Code in ‘cmdline’ File:
To Disable Zswap on piCore you must add “nozswap” boot code in the cmdline file identified in the previous step. You can edit the file using “vi /mnt/mmcblk0p1/cmdline3.txt” command (I am editing “cmdline3.txt” file as I am using Raspberry Pi 3)
Following are the contents of my “cmdline3.txt” after making the changes –
tc@box:~$ cat /mnt/mmcblk0p1/cmdline3.txt dwc_otg.lpm_enable=0 console=ttyS0,115200 root=/dev/ram0 elevator=deadline rootwait quiet nortc loglevel=3 noembed waitusb=1 nozswap tc@box:~$
If you want to see the list of all the supported Boot Codes, you can visit this URL – http://tinycorelinux.net/faq.html#bootcodes
4. Reboot Raspberry Pi:
For piCore to read the new Boot Code, it is necessary to reboot the system. You can reboot the system using “sudo reboot” command.
5. Check the Swap Details:
After the reboot, piCore should stop using the zswap partition in RAM. If you check swap details you should see that piCore is now using only the swap partition that you created in Part-A of this article (for me its /dev/mmcblk0p3) –
tc@box:~$ cat /proc/swaps Filename Type Size Used Priority /dev/mmcblk0p3 partition 2045692 0 -1 tc@box:~$ free -m total used free shared buffers cached Mem: 923 55 867 9 3 20 -/+ buffers/cache: 32 891 Swap: 1997 0 1997 tc@box:~$
Zswap can significantly reduce I/O for Linux systems that require swapping. However, the tradeoff is the need for additional CPU cycles to perform the compression. Still, as a result of reduced I/O, zswap offers major performance advantages to the devices like Raspberry Pi etc that use a slow SD Card. Apart from this, zswap can also increase the lifespan of SD Card by limiting the Read/Write operations to the RAM disk.