HOWTO: zram compressed swap or /tmp

http://code.google.com/p/compcache/

~~![](http://compcache.googlecode.com/svn/wik … pcache.png">http://compcache.googlecode.com/svn/wiki/files/compcache.png" /> > This project creates RAM based block device (NOW named zram) which acts as swap disk (AND a generic in memory self-compressing block device). Pages swapped to this disk are compressed and stored in memory itself.

FYI: They have removed the redundant backing disk option that activates if the page is not compressable. I guess this is for the short term so if it can't swap a page you still need your old swapdisk as a backup.

Compressing pages and keeping them in RAM virtually increases its capacity. This allows more applications to fit in given amount of memory. That's more for less money on your monthly linode bill.

The usual argument I get is - memory is so cheap so why bother with compression? So I list here some of the use cases. Rest depends on your imagination :)

  • Netbooks: Market is now getting flooded with these "lightweight laptops". These are memory constrained but have CPU enough to drive on compressed memory (e.g. Cloudbook features 1.2 GHz processor!).

    ** Virtualization: With compcache at hypervisor level, we can compress any part of guest memory transparently - this is true for any type of Guest OS (Linux, Windows etc.). This should allow running more number of VMs for given amount of total host memory.*

  • Embedded Devices: Memory is scarce and adding more memory increases device cost. Also, flash storage suffers from wear-leveling issues, so its useful if we can avoid using them as swap device.

Just use this if you want FASTER SWAP traded in for a smaller amount of memory. I think it sells itself :)

I wonder if Linode is over-subscribing their server's memory transparently yet using this technology…. Sounds interesting.

Please confirm or deny this on the record linode admins. I'm hoping the answer is NO but I'd just like to hear it from you guys.

Also I wonder if the competitors will do this…. With everyone on dreamhost being pushed to run debian, I'd assume they just free the duplicate pages in memory as their $$ saver.

I'd like to know because if they aren't there will be a performance boost if power users do this in some cases.

Could the linode admins confirm or deny if this is use.

[update: Crackpots comments aside, it's worth documenting exactly what kind of Xen implementation a linode IS and IS NOT

Linode doesn't admins say they don't use this

]

I'm testing now. All the more reason to roll your own kernel ;)

(or at least reconfigure the linode one).

And the testing is done! I've rolled out my implementation to a gentoo bug.

http://bugs.gentoo.org/show_bug.cgi?id=252987

Here's the ebuild and the init.d scripts for those who are interested.

compcache-0.7-r9999.ebuild

# Copyright 1999-2011 Gentoo Foundation
# Distributed under the terms of the GNU General Public License v2
# $Header: $

EAPI="2"
inherit eutils linux-mod toolchain-funcs mercurial

DESCRIPTION="Compressed RAM as fast swap"
HOMEPAGE="http://compcache.googlecode.com/"
#SRC_URI="http://compcache.googlecode.com/files/${P}.tar.gz"
EHG_REPO_URI="https://compcache.googlecode.com/hg/"
S="${WORKDIR}/compcache"

LICENSE="GPL-2 LGPL-2.1"
KEYWORDS="~arm ~amd64 ~x86"
IUSE="swap_free_notify"

DEPEND=">=virtual/linux-sources-2.6 
        dev-vcs/mercurial
        "
RDEPEND="${DEPEND}"

pkg_setup() {
        BUILD_PARAMS='KV_OUT_DIR="${KV_OUT_DIR}"'
        BUILD_TARGETS="all"
        CONFIG_CHECK="LZO_COMPRESS LZO_DECOMPRESS SWAP !RAMZSWAP"
        MODULE_NAMES="zram(compcache:${S}:${S})"
        MODULESD_ZRAM_DOCS="Changelog README"
        MODULESD_ZRAM_EXAMPLES=('zram num_devices=1')
        linux-mod_pkg_setup
}

src_prepare() {
    find . -name Makefile -exec sed -i \
        -e 's:make:$(MAKE):g' \
        -e "s:@gcc:$(tc-getCC) :g" \
        -e 's#/lib/modules/$(shell uname -r)/build#"$(KV_OUT_DIR)"#' \
        {} \; || die

    cd "${S}"
    use swap_free_notify && epatch "${FILESDIR}"/${P}-free_notify.patch
}

src_compile() {
    linux-mod_src_compile
}

src_install() {
    linux-mod_src_install

    dosbin sub-projects/scripts/zram_stats || die

    newinitd "${FILESDIR}/init.d-${PN}" ${PN} || die
    newconfd "${FILESDIR}/conf.d-${PN}" ${PN} || die

    dodoc Changelog README || die
    dodoc patches/patch_swap_notify_core_support_2.6.33.diff || die
}

init.d-compcache

#!/sbin/runscript
# Copyright 1999-2011 Gentoo Foundation
# Distributed under the terms of the GNU General Public License v2
# $Header: $

depend() {
    need localmount
    after bootmisc modules
}

start() {
    if [ "${LOAD_ON_START}" = "yes" ] ; then
        if ! grep zram /proc/devices >/dev/null ; then
            einfo "Loading zram module..."
            modprobe zram num_devices=${NUM_DEVICES}
            eend $?
        fi
    fi
    # exit if the driver is not yet installed
    grep zram /proc/devices >/dev/null || exit 1

    for I in `seq 0 \`expr ${NUM_DEVICES} - 1\`` ; do
        eval _a=\${ZRAM_SIZE_${I}}
        einfo "Enabling swap /dev/zram${I}..."
        if test ! -z ${_a} && test -f /sys/block/zram${I}/disksize ; then
            echo $(($_a*1024)) > "/sys/block/zram${I}/disksize"
            mkswap -L zram${I} /dev/zram${I} ${_a}
        fi
        if test -b "/dev/zram${I}" ; then
            swapon ${SWAPON_OPTS} "/dev/zram${I}"
        fi
        eend $?
    done
}

stop() {
    for I in `seq 0 \`expr ${NUM_DEVICES} - 1\`` ; do
        einfo "Disabling swap /dev/zram${I}..."
        swapoff "/dev/zram${I}" && \
        echo 1 > "/sys/block/zram${I}/reset"
        eend $?
    done

    if [ "${UNLOAD_ON_STOP}" = "yes" ] && grep zram /proc/modules >/dev/null  ; then
        einfo "Unloading zram module..."
        rmmod zram
        eend $?
    fi
}

info() {
    for I in `seq 0 \`expr ${NUM_DEVICES} - 1\`` ; do
        zram_stats "/dev/zram${I}"
    done
}

reload() {
    for I in `seq 0 \`expr ${NUM_DEVICES} - 1\`` ; do
        swapoff "/dev/zram${I}" && \
        echo 1 > "/sys/block/zram${I}/reset"
        swapon ${SWAPON_OPTS} "/dev/zram${I}"
    done
}

restart() {
    stop
    start
}

conf.d-compcache

# CompCache settings...

# load ramzswap kernel module on start?
LOAD_ON_START="yes"

# unload ramzswap kernel module on stop?
UNLOAD_ON_STOP="yes"

# number of device
NUM_DEVICES="4"

# uncompressed size in Kilobytes of each compressed ram swap disk 
# (numbered from 0 to $NUM_DEVICES-1)
# only allocated / used / non-zeroed memory is actually used and will be less than this figure
# example 32 MB
ZRAM_SIZE_0="32768"
ZRAM_SIZE_1="32768"
ZRAM_SIZE_2="32768"
ZRAM_SIZE_3="32768"

# options for swapon
# (if this number is higher than your disk swap drive zram will be used for swap)
SWAPON_OPTS="-p 666"

If you run gentoo you can get the latest version from source (mercurial / googlecode) and RTFM or maybe someone cares can add it to the linode library.

You need the latest version if you want to install a module under linux-2.6.34+! refer to this bug

http://code.google.com/p/compcache/issues/detail?id=68

I think mysql locks memory so it can't be swapped, so there is no substitute for the real thing.

Let's be honest, linode IO is pretty constrained so a ramzswap might be your answer to trade CPU clicks you may not be using for some more memory….

Gentoo users see here

Others see here:

http://library.linode.com/linode-platfo … led-kernel">http://library.linode.com/linode-platform/custom-instances/pv-grub-custom-compiled-kernel

All this reminds me of the days when we had constrained memory on SIMMs on the 486 and some 3rd party commercial software for windows claimed to boost performance.

If your linode is SWAPPING due to load, this seriously might deserve a look in.

Cheers,

Luke~~

12 Replies

http://don.blogs.smugmug.com/2008/05/01 … p-problem/">http://don.blogs.smugmug.com/2008/05/01/mysql-and-the-linux-swap-problem/

This ramzswap compares to regular swap in memory (tmpfs)

It's very advisable to have a swap device of some kind if you run mysql. It's more advisable to have a swap disk in ram with a higher priority.

ramzswap compares relatively well in performance.

Remember to use 4 ramdisks instead of one to make use of the 4 cpu cores linode gives us access to.

oh and check out the backing device feature in ramzswap.

compressed IO to the linode disks. i'm recreating my single swap partition as 4 seperate xvd devices for a little more grunt ;)

like anything in performance there are tradeoffs. your trading cpu cycles for more efficent use of memory (assuming you NEED to swap).

PS. psandin told me linode doesn't use this on their hypervisor (xen dom0). they shouldn't either!

I highly doubt linode use it.

I'd be interested to see what performance gains you get (if any), let us know.

@hojuruku:

I wonder if Linode is over-subscribing their server's memory transparently yet using this technology.

Please confirm or deny this on the record linode admins. I'm hoping the answer is NO but I'd just like to hear it from you guys.

That's right, admins, you better answer every crackpot theory about how you might be oversubscribing!

I was sold a Linode with 1.0 Jeds, but I've heard rumours that Linode does not have enough Jeds to supply every customer with a full Jed, and that they must be oversold. Please confirm or deny this on the record! How many Jeds are available at Linode?

Linode has patched Xen to allow virtualization of Jeds - you appear to have your own Jed, even though it is actually shared among many linodes.

EDIT: I'm not sure linode has worked out the whole Jed compression thing, but I'm sure they are trying ;)

I've got this working in production with the init script i just patched for the sysfs configured zram.ko that replaces the in kernel ramzsawp.ko (broken on 2.6.35 at least with the latest user-space tools now redundant too)

It shouldn't be hard for you guys to knock up a kernel module from hg version (see the bug above) and modify the init script for your distro.

Other who have tried this seem happy so far.

Refer to the bug. You need something related to the Gentoo version or manually install from source if you want to use a module with a linode kernel 2.6.35.4 otherwise it wont work.

Keep whingeing or try it out. Feedback welcome.

EDIT: What is this jed compression thingy? something to do with a linode admin called jed?

@hojuruku:

EDIT: What is this jed compression thingy? something to do with a linode admin called jed?
~~![](<URL url=)http://forum.linode.com/images/avatars/ … 12dcda.jpg">http://forum.linode.com/images/avatars/7969550994ced50d12dcda.jpg" /> <– Jed, Linode are trying to clone him and compress him so they can fit him into your linode's RAM give you uber support before you ask.

…Also if you google images for "Linode Jed" you get some reaally strange things (nsfw).~~

> Just use this if you want FASTER SWAP traded in for a smaller amount of memory. I think it sells itself

So you get faster bandwith on your swap partition, but since you have less ram you're going to be swapping more. Does the speed benefit actually outweigh the extra swapping?

This isn't about getting faster swap, it's about fitting more into RAM.

See also: Connectix Ram Doubler, released 17 years ago. Touted as an alternative to Apple's virtual memory implementation (as Apple called swap in the early 90s), it took a two-tier approach, first extending existing RAM by compressing less used blocks, and then swapping those blocks to disk if it needed still more RAM. It's effectively the exact same thing as compcache, just almost two decades sooner.

Of course, I'm sure somebody will point out some similar software/memory manager that did this even earlier.

as i said before even android phones get a peformance boost. They have limited memory right?

http://linux-tipps.blogspot.com/2010/08 … zswap.html">http://linux-tipps.blogspot.com/2010/08/fun-with-compcacheramzswap.html

If you run mysql due to the nature of liinux you are going to swap at some point (see the link above). Do you want to swap to disk or use some faster memory.

I tested it using C++ compiling and here's the stats:

# zram_stats

/sys/block/zram3
disksize:           67108864
num_reads:               223
num_writes:             8928
invalid_io:                0
notify_free:               0
zero_pages:             4814
orig_data_size:     16846848
compr_data_size:     2132626
mem_used_total:      2252800
avg_compr_ratio:          12 %
mem_overhead:              5 %

When the backing feature comes back you'll have compressed access to a swap disk = less disk IO.

Zero pages are NOT written to disk. It's more intelligent swap too.

You could even run this for your /tmp partition , or uses it with a big instance of mcached. There possibilities are endless.

So you hit your CPU more, so what, you don't use it all the time. If your more memory constained than CPU you get more bang for your buck on your linode.

For other users you might get better compression.

Cheers,

Luke

Compressed or not, you want a bunch of stuff to be paged to disk. Idle memory is as wasted as free memory; it can be used for caching.

Reply

Please enter an answer
Tips:

You can mention users to notify them: @username

You can use Markdown to format your question. For more examples see the Markdown Cheatsheet.

> I’m a blockquote.

I’m a blockquote.

[I'm a link] (https://www.google.com)

I'm a link

**I am bold** I am bold

*I am italicized* I am italicized

Community Code of Conduct