Posts Tagged ‘compcache’


Compressing RAM with zRam

In Ubuntu on 04/05/2011 by weirdfellow Tagged: , , , , , , ,

A successor to compcache, zram, has been already integrated in the Linux kernel for a while now. This means that no additional compilation nor tweaking is required to benefit from compressing memory on the fly and massively reduced swapping.

As with compache, I wanted to nicely integrate the solution into the Ubuntu Upstart deamon – hence this short article. After a couple of minutes of playing the configuration was ready.

Create file zramswap.conf in /etc/init and put the following content in it.

description "Initializes zram swaping"
start on runlevel [2345]
stop on runlevel [!2345]
pre-start script
# load dependency modules
modprobe zram num_devices=2
# initialize the devices
echo 1073741824 > /sys/block/zram0/disksize
echo 1073741824 > /sys/block/zram1/disksize
# Creating swap filesystems
mkswap /dev/zram0
mkswap /dev/zram1
# Switch the swaps on
swapon -p 5 /dev/zram0
swapon -p 5 /dev/zram1
end script
post-stop script
# Switching off swap
swapoff /dev/zram0
swapoff /dev/zram1
rmmod zram
end script

Now you can start the service with sudo start zramswap (it will be automatically started on after the reboot as well).
You will benefit from 2x1GB swap files, which will be compressed and stored in the RAM. Tested on Ubuntu 11.04.


Ubuntu Lucid and Compcache

In Uncategorized on 15/07/2010 by weirdfellow Tagged: , , ,

I have been very excited about memory compression for years. The performance boost achieved with Quarterdeck MagnaRAM on Windows 98 still brings smile on my face. Without memory compression my 64MB system sounded like it was chopping parsley – definitely there was some swapping. Switching MagnaRAM on kept it quiet (to be precise – it was loading the next level of Colin McRae Rally 2). Not to mention the difference in required time. Unfortunately, I couldn’t find similar tool for any Windows NT system.

Recently I started to build a development machine on Ubuntu Lucid Lynx and decided to give the Compcache a chance. Compilation caused me absolutely no problems – just download the package, unpack it and execute make. Although there are some patches for the kernel, so it can use the memory even more efficient, they are not required and Compcache will work with stock kernel. Compilation generates two things: a kernel module ramzswap.ko and a user-space utility rzscontrol.

It is pretty simple to initialize compressed swap disk by hand, but I wanted a slick and elegant integration with the services management subsystem. A couple of experiments and voila: my Upstart script for Compcache looks like that:

description "Initializes Compcache"

start on runlevel [2345]
stop on runlevel [!2345]

pre-start script

# load dependency modules
modprobe lzo_compress
modprobe lzo_decompress

# load ramzswap module
insmod $DIR/ramzswap.ko num_devices=2
sleep 1s

# Initialize devices with 512MB upper limit.
# These devices does not have a backing swap.
$CONTROL /dev/ramzswap0 --disksize_kb=524288 --init
$CONTROL /dev/ramzswap1 --disksize_kb=524288 --init

# Switch the swaps on
swapon -p 5 /dev/ramzswap0
swapon -p 5 /dev/ramzswap1
end script

post-stop script

swapoff /dev/ramzswap0
swapoff /dev/ramzswap1

$CONTROL /dev/ramzswap0 --reset
$CONTROL /dev/ramzswap1 --reset

rmmod ramzswap
end script

Update the paths, save the file as /etc/init/compcache.conf and restart your system. Your system should have two additional swap drives – 512MB each – already configured and switched on. Such configuration also enables switching Compcache off by using Upstart commands too.

In my environment Compcache achieved ~65% compression – 1GB of swapped pages would take approximately 350MB of memory. In result system can allocate 1.7GB and use only 1GB of physical memory without any sign of HDD swapping!

My setup uses two separate swapping devices in order to keep both of the CPU cores busy. If running on Quad Core CPU, it is beneficial to create four devices. Kernel utilizes them equally.

Some useful commands to track your swapping:

sudo swapon -s #displays active swap devices and their usage
sudo stop compcache #switches Compcache off.
sudo start compcache #switches Compcache on
sudo rzscontrol /dev/ramzswap0 --stats #displays the stats of the first swapping device.

I am running this environment for couple of days and I see only advantages. Give it a try!