Discovering zram and replacing all tmpfs

I have just discovered zram[2] :) woohoo... and what a thing that is!

I was so sold on tmpfs, that I used it for everything I possibly could. Since discovering zram, I am using tmpfs for nothing. All my tmpfs needs are met by zram much more efficiently.

It used to be called compcache (compressed cache). Now called zram, and even more efficient mainlined in kernel[2]. The initial primary purpose was to augment low memory systems with ram-based compressed swap devices. But it's so efficient that it's found it's way into many other use cases, including tmpfs purposes. Because it is compressed cache, it can consume much more space than tmpfs.

You need to enable zram in kernel or as a module. I prefer everything as modules, to keep the kernel as small as possible.
CONFIG_ZRAM=m

OS specific configuration might be different for everyone. If you don't use gentoo[4], the following might still be useful as a reference to customise according to your distro.

Install zram-init and configure it accordingly for your particular needs. Below is mine on gentoo.

# emerge zram-init
# cat /etc/conf.d/zram-init
load_on_start=yes
unload_on_stop=yes
num_devices=3

type0=swap
size0=1024

type1=/tmp
size1=2048
flag1=btrfs
opts1="defaults,nosuid,noatime,nodev,autodefrag,nobarrier,nodatacow"
mode1=1777

type2=/var/tmp
size2=3072
flag2=btrfs
opts2="defaults,nosuid,noatime,nodev,autodefrag,nobarrier,nodatacow"
mode2=1777

In this case, I have configured zram-init to create three devices, one for swap and the other two for my tmpfs. They are compressed lzo, probably the least compression but faster. You could compress further, but I'm yet to hit the limit.

We carve out these devices in memory, but all our memory is still available. Like tmpfs, zram only claims space when used.

Remember to remove those entries from /etc/fstab.

We need to start zram-init at boot, as /tmp is required at system initialisation.
# rc-update add zram-init boot

Check again that you did remove those entries from /etc/fstab, and then reboot.

We should now find three new devices...
$ /sbin/zramctl
NAME       ALGORITHM DISKSIZE  DATA  COMPR TOTAL STREAMS MOUNTPOINT
/dev/zram0 lzo             1G    4K    81B    4K       2 [SWAP]
/dev/zram1 lzo             2G  7.9M   5.2M  5.6M       2 
/dev/zram2 lzo             3G  2.6M 124.6K  252K       2
$ /sbin/swapon -s | grep zram
/dev/zram0                              partition 1048572 0 16383
$ df -h | grep zram
/dev/zram1      2.0G   20M  1.8G   2% /tmp
/dev/zram2      3.0G   17M  2.8G   1% /var/tmp

Something to note and think about is that it is very much possible to create a zram device bigger than physical memory. I have done so successfully, now do so on a regular basis in production.

And I try never to use swap, as I find massive performance hit if any data ever hits swap on disk.


1. nixventure: memory
2. http://kernel.org/doc/Documentation/blockdev/zram.txt
3. http://wikipedia.org/wiki/zram
4. http://wiki.gentoo.org/wiki/zram

No comments:

Post a Comment

most viewed