Skip to content
This repository has been archived by the owner on Nov 2, 2020. It is now read-only.

Commit

Permalink
Merge pull request #2 from highvoltage/devel
Browse files Browse the repository at this point in the history
Devel
  • Loading branch information
Jonathan Carter committed Aug 10, 2015
2 parents 880d518 + 99529bc commit d045535
Show file tree
Hide file tree
Showing 5 changed files with 94 additions and 69 deletions.
12 changes: 6 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,27 +1,27 @@
zram-tools
==========

Scripts for managing zram swap devices.
Scripts for managing zram devices, currently only for zramswap.

zramswap-start
zramswap start
--------------

Sets up zram devices and initializes swap. zram doesn't natively support
multiple processors, so by default a zram device is set up for every
core and then swap is initialized on those devices. This is configurable
in the zramswap config file.

zramswap-stop
zramswap stop
-------------

Removes all current zram swap spaces and devices.

zramswap-stat
-------------
zramswap status
---------------

shows information on data stored in zram swap.

/etc/default/zramswap
---------------------

Configuration file for zramswap-start
Configuration file for zramswap
43 changes: 0 additions & 43 deletions zramswap-start.sh

This file was deleted.

14 changes: 0 additions & 14 deletions zramswap-stat.sh

This file was deleted.

6 changes: 0 additions & 6 deletions zramswap-stop.sh

This file was deleted.

88 changes: 88 additions & 0 deletions zramswap.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
#!/bin/bash
# This script does the following:
# zramswap start:
# * zram doesn't natively support multiple CPU cores, so one zram device
# is created for every CPU core minus one)
# * Space is assigned to each zram device, then swap is initialized on
# there
# zramswap stop:
# * Somewhat potentially dangerous, removes zram module at the end

function start {
#Set some defaults:
ALLOCATION=100 # ZRAM Swap you want assigned, in MiB
PRIORITY=100 # Swap priority, see swapon(2) for more details

# Get amount of available CPU cores, set to 1 if not detected correctly
if [ ! -f /proc/cpuinfo ]; then
echo "WARNING: Can't find /proc/cpuinfo, is proc mounted?"
echo " Using a single core for zramswap..."
cores=1
else
cores=$(grep -c processor /proc/cpuinfo)
fi

# Override above from config file, if it exists
if [ -f /etc/default/zramswap ]; then
. /etc/default/zramswap
fi

ALLOCATION=$((ALLOCATION * 1024 * 1024)) # convert amount from MiB to bytes

if [ -n "$PERCENTAGE" ]; then
totalmemory=$(awk '/MemTotal/{print $2}' /proc/meminfo) # in KiB
ALLOCATION=$((totalmemory * 1024 * $PERCENTAGE / 100))
fi

# Initialize zram devices, one device per CPU core
modprobe zram num_devices=$cores

# Assign memory to zram devices, initialize swap and activate
# Decrementing $core, because cores start counting at 0
for core in $(seq 0 $(($cores - 1))); do
echo $(($ALLOCATION / $cores)) > /sys/block/zram$core/disksize
mkswap /dev/zram$core
swapon -p $PRIORITY /dev/zram$core
done
}

function status {
for f in /sys/block/zram*/*_data_size ; do
read size < $f
what=$(basename $f)
eval "$what=\$(($what + $size))"
done
echo "compr_data_size: $((compr_data_size / 1024)) KiB"
echo "orig_data_size: $((orig_data_size / 1024)) KiB"
echo "print \"compression-ratio: \"; scale=2; $orig_data_size / $compr_data_size" | bc
}

function stop {
for swapspace in $(swapon -s | awk '/zram/{print $1}'); do
swapoff $swapspace
done
modprobe -r zram
}

function usage {
echo "Usage:"
echo " zramswap start - start zram swap"
echo " zramswap stop - stop zram swap"
echo " zramswap status - prints some statistics"
}

if [ "$1" = "start" ]; then
start
fi

if [ "$1" = "stop" ]; then
stop
fi

if [ "$1" = "status" ]; then
status
fi

if [ "$1" = "" ]; then
usage
fi

0 comments on commit d045535

Please sign in to comment.