Page 1 of 1

Script to enable zram

Posted: 6. Apr 2012, 00:00
by GJones
With the Thinkpad 600E I'm currently posting from, zram (compressed swap space in RAM) is a huge deal. With a 128 MB zram disk, the computer is usable with Salix, Fluxbox and a bunch of light applications; without zram, it is not usable at all, unless you use a toy OS like Puppy.

To automate things, and since zram likes having the same number of ramdisks as CPU cores, I suggest a script like this (crappy, totally unprofessional) one be included somewhere in the next Salix release...

Code: Select all

# Number of CPU cores and bytes of physical memory
num_cpus=`grep -c processor /proc/cpuinfo`
mem_bytes=`free -b | grep Mem | tr -s " " | cut -f 2 -d " "`

# individual disk size is half of MEM_BYTES, divided by NUM_CPUS
zram_size=$(($mem_bytes / $num_cpus))

# Load zram and resize the disks
modprobe zram num_devices=$num_cpus
for i in /sys/block/zram*; do
    echo $zram_size > $i/disksize
done

# Create the swap spaces and start swapping
for i in /dev/zram*; do
    mkswap $i
    swapon -p 32767 $i
done
Assuming I did it right, this would create a number of ZRAM disks equal to the number of CPU cores, adding up to a maximum total of [edit]the entirety of physical memory. This should be okay, assuming you have any real swap space to fall back on.[/edit]

NB: I certainly don't suggest that anything like this be enabled by default... Just present in the install in case someone wants to run Salix on an old machine.

(Not that old machines with multiple cores are common, but who knows.)

Edit 05/17/2012: updated the script to be less brain damaged. Also note that, for 3.x kernels, you must use zram_num_devices rather than num_devices as the module parameter.

Edit 08/05/2012: modified the script so it doesn't bail out on computers with < 1 GB of memory. Duh!

Re: Script to enable zram

Posted: 6. Apr 2012, 01:44
by Shador
I think there's more benefit from a wiki entry. First of all its easier to catch, so more peoples would know about. Who notices some file installed someplace by the setup?
Apart inclusion in the default install adds an additional maintenance burden. As it would offer little to no benefit otherwise over the wiki (copy'n pasting a script is not much work) and would be unneeded by many people, it just doesn't seem worth it.

Do you agree?

Re: Script to enable zram

Posted: 6. Apr 2012, 10:52
by GJones
Mmm, I think you're right. And I'd forgotten about the wiki. Shame on me. :lol:

Re: Script to enable zram

Posted: 11. Apr 2012, 08:51
by JRD
Ahhhh very interresting !
I hjave a laptop with low RAM and correct CPU, I will try this.

Re: Script to enable zram

Posted: 17. May 2012, 22:53
by GJones
Updated the script above, it should actually work now. I'm rather interested in hearing how well zram works, particularly on very low-end or very high-end computers.