Most, if not all, Linux users have swap partitions or swap files on their desktops and servers. Swap is used to store less frequently used memory pages on a device with a designated swap partition, or in a dedicated swap file. The amount of memory available to the running system is the sum of the size of physical RAM and the system swap. While swap may be not as important these days as it was in the past, with desktops having even as much as 16GB of RAM, it is still recommended to have it configured on the system. This article has more information on Linux swap.
There is one major drawback to having swap on disk - in real life hard disks have two orders of magnitude less bandwidth than RAM and six orders of magnitude bigger latency. Usually when the system runs out of physical memory and starts swapping heavily, the whole machine can grind to a halt while waiting for I/O subsystem. Disk I/O is a major bottleneck and there is not much that can be done about it, except maybe placing the swap on a RAID (1+0 or 0+1) array made with very fast disks (e.g. WD Raptor), but this is in no way a solution to the problem.
There is however a solution that can improve swap performance. Compcache is an open source project implementing an innovative approach to swap. This has been done before, but not for swap. Users of DOS and early Windows versions will remember DoubleSpace/DriveSpace, which was used to virtually expand available disk space, by storing files in a compressed form. Compcache does exactly that, but for swap, by creating a new block device in the system which interfaces with the special compressed memory region in RAM. On the plus side, Compcache can also be configured to use an alternative swap device when the RAM swap area is full.
On my desktop, a Dell Precision S390 with 2GB DDR2 RAM and a Maxtor Diamond Max 9 80GB drive, I am getting the following hdparm results (average of three runs) for my disk swap, and my compcache swap:
So how can you get Compcache on your system? Installing it is relatively simple. First, ensure that you have kernel headers installed for the kernel version your system is running, then get the Compcache source code for version 0.6 and uncompress it in /usr/src/. Next, all you need to do is make the binaries and install them. Here's how (on Ubuntu 9.10):
If the make finishes without errors, you are ready to install:
You should now have a system ready to use Compcache, but it still needs a few things before proceeding. First, make sure to disable all disk swap devices present in /etc/fstab. We will use one of them as alternative swap. Next, download my Compcache init.d script and its config file.
By default my config will set Compcache to use 20% of RAM for the cache, and use /dev/sda2 as alternative swap. It will also increase system "swappiness" to 30, from the default 60. Please adjust the configuration to your liking and system layout by editing /etc/default/compcache. When done, run:
If Compcache starts without errors, you should see something like the following after issuing swapon -s:
Now it's time to make it run at system boot:
Reboot, then run swapon -s again and see if /dev/ramzswap0 is there. If so, your shiny new Compcache swap is up and running!
UPDATE: Apperently people have experienced problems with Compcache on Ubuntu in the past - but that was version 0.4.x, which was used on LiveCD to increase the amount of memory available for the live session. Karmic apparently ships with version 0.5.x (but disabled). My guide is based on the latest 0.6, which will be included in kernel 2.6.33 according to the developers.
There is one major drawback to having swap on disk - in real life hard disks have two orders of magnitude less bandwidth than RAM and six orders of magnitude bigger latency. Usually when the system runs out of physical memory and starts swapping heavily, the whole machine can grind to a halt while waiting for I/O subsystem. Disk I/O is a major bottleneck and there is not much that can be done about it, except maybe placing the swap on a RAID (1+0 or 0+1) array made with very fast disks (e.g. WD Raptor), but this is in no way a solution to the problem.
There is however a solution that can improve swap performance. Compcache is an open source project implementing an innovative approach to swap. This has been done before, but not for swap. Users of DOS and early Windows versions will remember DoubleSpace/DriveSpace, which was used to virtually expand available disk space, by storing files in a compressed form. Compcache does exactly that, but for swap, by creating a new block device in the system which interfaces with the special compressed memory region in RAM. On the plus side, Compcache can also be configured to use an alternative swap device when the RAM swap area is full.
On my desktop, a Dell Precision S390 with 2GB DDR2 RAM and a Maxtor Diamond Max 9 80GB drive, I am getting the following hdparm results (average of three runs) for my disk swap, and my compcache swap:
- Swap on disk: 58MB/s
- Compcache swap: 557MB/s
So how can you get Compcache on your system? Installing it is relatively simple. First, ensure that you have kernel headers installed for the kernel version your system is running, then get the Compcache source code for version 0.6 and uncompress it in /usr/src/. Next, all you need to do is make the binaries and install them. Here's how (on Ubuntu 9.10):
cd /usr/src/compcache-0.6
makeIf the make finishes without errors, you are ready to install:
cd /usr/src/compcache-0.6
mkdir -p /lib/modules/`uname -r`/kernel/arch/x86/compcache/
find . -maxdepth 1 -name "*.ko" -exec sudo install -m 0644 -o root -g root \
{} /lib/modules/`uname -r`/kernel/arch/x86/compcache/ \;
sudo depmod -a
sudo install -m 0755 -o root -g root ./sub-projects/rzscontrol /sbin/You should now have a system ready to use Compcache, but it still needs a few things before proceeding. First, make sure to disable all disk swap devices present in /etc/fstab. We will use one of them as alternative swap. Next, download my Compcache init.d script and its config file.
mkdir /tmp/compcache
cd !$
wget http://www.szmit.eu/files/linux/compcache/compcache
wget http://www.szmit.eu/files/linux/compcache/config
sudo install -m 0755 -o root -g root ./compcache /etc/init.d/
sudo install -m 0644 -o root -g root ./config /etc/default/compcache
By default my config will set Compcache to use 20% of RAM for the cache, and use /dev/sda2 as alternative swap. It will also increase system "swappiness" to 30, from the default 60. Please adjust the configuration to your liking and system layout by editing /etc/default/compcache. When done, run:
sudo /etc/init.d/compcache start
swapon -sIf Compcache starts without errors, you should see something like the following after issuing swapon -s:
Filename Type Size Used Priority
/dev/ramzswap0 partition 514868 0 -1
Now it's time to make it run at system boot:
sudo update-rc.d compcache defaultsReboot, then run swapon -s again and see if /dev/ramzswap0 is there. If so, your shiny new Compcache swap is up and running!
UPDATE: Apperently people have experienced problems with Compcache on Ubuntu in the past - but that was version 0.4.x, which was used on LiveCD to increase the amount of memory available for the live session. Karmic apparently ships with version 0.5.x (but disabled). My guide is based on the latest 0.6, which will be included in kernel 2.6.33 according to the developers.
1 comments:
Thank you for such an useful post ;)
Post a Comment