Posts: 4,164
rokytnji
Joined: 20 Feb 2009
#1
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.

========= SCRAPER REMOVED AN EMBEDDED LINK HERE ===========
url was:"http://weirdfellow.wordpress.com/2011/05/04/compressed-ram-with-zram/"
linktext was:"http://weirdfellow.wordpress.com/2011/0 ... with-zram/"
====================================


Just wondering if I can try this in /etc/init.d in AntiX 11 or not. It says it is a Ubuntu tweak
integrate the solution into the Ubuntu Upstart deamon
so just wondering if this is viable or not. No biggy if it aint.

Just tinkering. Figured I would try it out on my M/C shop desktop with a 2GIG /swap partition.
Posts: 1,228
secipolla
Joined: 15 Jun 2008
#2
That script is for upstart and Debian uses sysvinit (or systemd if one sets it up - I have a sid with it) so I doubt you could use that as is.
For systemd the instructions are at
========= SCRAPER REMOVED AN EMBEDDED LINK HERE ===========
url was:"http://mystilleef.blogspot.com/2011/10/enable-zram-in-fedora.html"
linktext was:"http://mystilleef.blogspot.com/2011/10/ ... edora.html"
====================================
anticapitalista
Posts: 5,955
Site Admin
Joined: 11 Sep 2007
#3
Not 100% certain, but I don't think it will work with Debian (and antiX) init scripts.

Have you tried e4rat on that desktop to speed up boot time?

secipolla beat me to it.
Posts: 4,164
rokytnji
Joined: 20 Feb 2009
#4
Have you tried e4rat on that desktop to speed up boot time?
Nope I haven't. Will give a look see.
That script is for upstart and Debian uses sysvinit (or systemd if one sets it up - I have a sid with it) so I doubt you could use that as is.
Thanks for the heads up. Like I said. No Biggy.
Posts: 75
tradetaxfree
Joined: 18 Jan 2012
#5
I got zram working in #! running testing
========= SCRAPER REMOVED AN EMBEDDED LINK HERE ===========
url was:"http://crunchbanglinux.org/forums/post/150355/#p150355"
linktext was:"here"
====================================
without systemd

I've updated the instructions to make the zram script work with kernels 3.1 & higher.
Posts: 75
tradetaxfree
Joined: 18 Jan 2012
#6
I noticed today with the Liquorix 3.4 kernel zram was not running - the module parameter for zram has reverted from zram_num_devices back to num_devices so if anyone changed their
========= SCRAPER REMOVED AN EMBEDDED LINK HERE ===========
url was:"http://crunchbanglinux.org/forums/post/150355/#p150355"
linktext was:"zram script"
====================================
to work with the 3.1 kernel you now need to change line 26 back again.
anticapitalista
Posts: 5,955
Site Admin
Joined: 11 Sep 2007
#7
tradetaxfree, zram will be included in the next antiX, set for the antiX kernel. The script is not set to run default, instead I included it as part of the antix-goodies.deb so it gets put in /usr/local/bin. Users can then decide to use it or not. This is the script.

Code: Select all

#!/bin/bash
### BEGIN INIT INFO
# Provides: zram
# Required-Start:
# Required-Stop:
# Default-Start: 2 3 4 5
# Default-Stop: 0 1 6
# Short-Description: Increased Performance In Linux With zRam (Virtual Swap Compressed in RAM)
# Description: Adapted from systemd scripts at https://github.com/mystilleef/FedoraZram
# Included as part of antix-goodies package by anticapitalista <antiX@operamail.com>
# This script was written by tradetaxfree and is found at http://crunchbanglinux.org/forums/topic/15344/zram-a-good-idea/
# Copy this script (as root) from /usr/local/bin to /etc/init.d and then #update-rc.d zram defaults
# After booting verify the module is loaded with: lsmod | grep zram
### END INIT INFO

start() {
    # get the number of CPUs
    num_cpus=$(grep -c processor /proc/cpuinfo)
    # if something goes wrong, assume we have 1
    ["$num_cpus" != 0 ] || num_cpus=1

    # set decremented number of CPUs
    decr_num_cpus=$((num_cpus - 1))

    # get the amount of memory in the machine
    mem_total_kb=$(grep MemTotal /proc/meminfo | grep -E --only-matching '[[:digit:]]+')
    mem_total=$((mem_total_kb * 1024))

    # load dependency modules
    modprobe zram zram_num_devices=$num_cpus

    # initialize the devices
    for i in $(seq 0 $decr_num_cpus); do
    echo $((mem_total / num_cpus)) > /sys/block/zram$i/disksize
    done

    # Creating swap filesystems
    for i in $(seq 0 $decr_num_cpus); do
    mkswap /dev/zram$i
    done

    # Switch the swaps on
    for i in $(seq 0 $decr_num_cpus); do
    swapon -p 100 /dev/zram$i
    done
}

stop() {
    # get the number of CPUs
    num_cpus=$(grep -c processor /proc/cpuinfo)

    # set decremented number of CPUs
    decr_num_cpus=$((num_cpus - 1))

    # Switching off swap
    for i in $(seq 0 $decr_num_cpus); do
    if ["$(grep /dev/zram$i /proc/swaps)" !="" ]; then
    swapoff /dev/zram$i
    sleep 1
    fi
    done

    sleep 1
    rmmod zram
}

case"$1" in
    start)
        start
        ;;
    stop)
        stop
        ;;
    restart)
        stop
        sleep 3
        start
        ;;
    *)
        echo"Usage: $0 {start|stop|restart}"
        RETVAL=1
esac
exit $RETVAL

Posts: 75
tradetaxfree
Joined: 18 Jan 2012
#8
modprobe zram zram_num_devices=$num_cpus

For the 3.4 Kernel needs to become:

modprobe zram num_devices=$num_cpus

In case the module parameter changes again, I added at the very beginning of the script:

Code: Select all

set -e
& after the modprobe line:

Code: Select all

echo"zram devices probed successfully"
if it's run as an init script this will show up in /var/log/boot

The updated init script is
========= SCRAPER REMOVED AN EMBEDDED LINK HERE ===========
url was:"antix.freeforu ms.org/post25137.html#p25137"
linktext was:"here"
====================================
Last edited by tradetaxfree on 24 Jun 2012, 22:52, edited 1 time in total.
Posts: 765
rust collector
Joined: 27 Dec 2011
#9
So, this is just useful if you use more ram than you have?
I don't even have a swap area now...
Just asking, as usual!
Posts: 75
tradetaxfree
Joined: 18 Jan 2012
#10
I stopped using swap years ago as with 2GB+ RAM it hardly ever got used. I also suspend rather than hibernate.

If you use an SSD zram reduces disk writes therefore extending the life of the drive. Even without an SSD applications which use a lot a RAM load more quickly (virtual machines / Office Application Suites etc..)

Even with lower RAM amounts it is probably still more efficient than using disk based swap.

As zram is now built into the 3.0+ kernels it's a simple tweak with no downside that I have found.
Posts: 765
rust collector
Joined: 27 Dec 2011
#11
Ok, so it is a good idea anyway, but I see it is"adapted from systemd scripts"
So, if I use systemd, do I also use zram? or is it something diffferent, but related-ish?
Posts: 75
tradetaxfree
Joined: 18 Jan 2012
#12
If you use systemd you can use the Fedora scripts referenced at the top of the script
========= SCRAPER REMOVED AN EMBEDDED LINK HERE ===========
url was:"https://github.com/mystilleef/FedoraZram"
linktext was:"here"
====================================
(see the README on that page for installation)
Posts: 765
rust collector
Joined: 27 Dec 2011
#13
Thanks!! I will try that.

Is it just me, or does fedora have a lot of tweaks you can do to improve performance?
Posts: 75
tradetaxfree
Joined: 18 Jan 2012
#14
Fedora does seem to innovate (most probably because it is a test bed for the commercial Red Hat server products)

I've also been looking at mSATA devices & found
========= SCRAPER REMOVED AN EMBEDDED LINK HERE ===========
url was:"http://bcache.evilpiepirate.org/"
linktext was:"bcache"
====================================
which
========= SCRAPER REMOVED AN EMBEDDED LINK HERE ===========
url was:"http://www.phoronix.com/scan.php?page=news_item&px=MTEwMDg"
linktext was:"may shortly become part of the Linux kernel"
====================================
Posts: 765
rust collector
Joined: 27 Dec 2011
#15
Hmm, I have a problem... I believe I have put all the stuff where they need to be, but it doesn't seem to want to start?

Code: Select all

root@antiberg:/home/kberg
# systemctl status zram.service
zram.service - Enable compressed swap in memory using zram
      Loaded: loaded (/etc/systemd/system/zram.service; enabled)
      Active: failed since Wed, 20 Jun 2012 13:24:41 +0200; 11s ago
     Process: 30438 ExecStart=/usr/local/sbin/zramstart (code=exited, status=255)
      CGroup: name=systemd:/system/zram.service
Any ideas?

I think I have messed it up, trying to translate it from fedora to antix, as I had no /usr/local/sbin, so I made one..
Maybe it should go somewhere else?