zram: add package to use compressed ram disk for swap
authorHauke Mehrtens <hauke@hauke-m.de>
Sun, 6 Jan 2013 16:21:29 +0000 (16:21 +0000)
committerHauke Mehrtens <hauke@hauke-m.de>
Sun, 6 Jan 2013 16:21:29 +0000 (16:21 +0000)
The busybox size is increased by 3.2KByte uncompressed on bcm47xx with
this commit.

This zram-swap automatically creates a zram device, a swap partition on
it and make the kernel swap pages to it.

Thank you Bastian Bittorf <bittorf@bluebottle.com> for idea and the
script.

SVN-Revision: 35025

package/busybox/config/util-linux/Config.in
package/kernel/modules/other.mk
package/zram-swap/Makefile [new file with mode: 0644]
package/zram-swap/files/etc/init.d/zram [new file with mode: 0644]

index 14d4777beaa1f5988b88fad33bba6a22f6b7ffa5..d60b139a8b3b944025953afb33e3f42fee747c55 100644 (file)
@@ -474,7 +474,7 @@ config BUSYBOX_CONFIG_FEATURE_MDEV_LOAD_FIRMWARE
 
 config BUSYBOX_CONFIG_MKSWAP
        bool "mkswap"
-       default n
+       default y
        help
          The mkswap utility is used to configure a file or disk partition as
          Linux swap space. This allows Linux to use the entire file or
@@ -652,7 +652,7 @@ config BUSYBOX_CONFIG_SETARCH
 
 config BUSYBOX_CONFIG_SWAPONOFF
        bool "swaponoff"
-       default n
+       default y
        select BUSYBOX_CONFIG_PLATFORM_LINUX
        help
          This option enables both the 'swapon' and the 'swapoff' utilities.
index ddaef8d9015c75b0d4ad1092e746595bad51b9a9..5d3e00a87f26cf6af09fd9619c92b15117ad58a6 100644 (file)
@@ -747,3 +747,24 @@ define KernelPackage/ikconfig/description
 endef
 
 $(eval $(call KernelPackage,ikconfig))
+
+
+define KernelPackage/zram
+  SUBMENU:=$(OTHER_MENU)
+  TITLE:=ZRAM
+  DEPENDS:=@!LINUX_3_3 +kmod-lib-lzo
+  KCONFIG:= \
+       CONFIG_ZSMALLOC \
+       CONFIG_ZRAM \
+       CONFIG_ZRAM_DEBUG=n
+  FILES:= \
+       $(LINUX_DIR)/drivers/staging/zsmalloc/zsmalloc.ko \
+       $(LINUX_DIR)/drivers/staging/zram/zram.ko       
+  AUTOLOAD:=$(call AutoLoad,20,zsmalloc zram)
+endef
+
+define KernelPackage/zram/description
+ Compressed RAM block device support
+endef
+
+$(eval $(call KernelPackage,zram))
diff --git a/package/zram-swap/Makefile b/package/zram-swap/Makefile
new file mode 100644 (file)
index 0000000..59a55c4
--- /dev/null
@@ -0,0 +1,46 @@
+# 
+# Copyright (C) 2013 OpenWrt.org
+#
+# This is free software, licensed under the GNU General Public License v2.
+# See /LICENSE for more information.
+#
+
+include $(TOPDIR)/rules.mk
+
+PKG_NAME:=zram-swap
+PKG_VERSION:=1
+PKG_RELEASE:=1
+
+PKG_BUILD_DIR := $(BUILD_DIR)/$(PKG_NAME)
+
+include $(INCLUDE_DIR)/package.mk
+
+define Package/zram-swap
+  SECTION:=utils
+  CATEGORY:=Base system
+  DEPENDS:=+kmod-zram +!(BUSYBOX_CONFIG_MKSWAP&&BUSYBOX_CONFIG_SWAPONOFF):swap-utils
+  TITLE:=ZRAM swap scripts
+  PKGARCH:=all
+endef
+
+define Package/zram-swap/description
+ A script to activate swaping on a compressed zram partition. This 
+ could be used to increase the available memory, by using compressed 
+ memory.
+endef
+
+define Build/Prepare
+endef
+
+define Build/Configure
+endef
+
+define Build/Compile
+endef
+
+define Package/zram-swap/install
+       $(INSTALL_DIR) $(1)
+       $(CP) ./files/* $(1)/
+endef
+
+$(eval $(call BuildPackage,zram-swap))
diff --git a/package/zram-swap/files/etc/init.d/zram b/package/zram-swap/files/etc/init.d/zram
new file mode 100644 (file)
index 0000000..23de915
--- /dev/null
@@ -0,0 +1,123 @@
+#!/bin/sh /etc/rc.common
+
+START=15
+
+ram_size()
+{
+       local line
+
+       while read line; do case "$line" in MemTotal:*) set $line; echo "$2"; break ;; esac; done </proc/meminfo
+}
+
+zram_size()    # in megabytes
+{
+       local zram_size="$( uci -q get system.@system[0].zram_size_mb )"
+       local ram_size="$( ram_size )"
+
+       if [ -z "$zram_size" ]; then
+               # e.g. 6mb for 16mb-routers or 61mb for 128mb-routers
+               echo $(( $ram_size / 2048 ))
+       else
+               echo "$zram_size"
+       fi
+}
+
+zram_applicable()
+{
+       local zram_dev="$1"
+
+       grep -sq ^"$zram_dev " /proc/swaps && {
+               logger -s -t zram_applicable -p daemon.notice "[OK] '$zram_dev' already active"
+               return 1
+       }
+
+       [ -e "$zram_dev" ] || {
+               logger -s -t zram_applicable -p daemon.crit "[ERROR] device '$zram_dev' not found"
+               return 1
+       }
+
+       which mkswap >/dev/null || {
+               logger -s -t zram_applicable -p daemon.err "[ERROR] 'mkswap' not installed"
+               return 1
+       }
+
+       which swapon >/dev/null || {
+               logger -s -t zram_applicable -p daemon.err "[ERROR] 'swapon' not installed"
+               return 1
+       }
+
+       which swapoff >/dev/null || {
+               logger -s -t zram_applicable -p daemon.err "[ERROR] 'swapoff' not installed"
+               return 1
+       }
+}
+
+zram_dev()
+{
+       local core="$1"
+
+       echo "/dev/zram${core:-0}"
+}
+
+zram_reset()
+{
+       local dev="$1"
+       local message="$2"
+       local proc_entry="/sys/block/$( basename "$dev" )/reset"
+
+       logger -s -t zram_reset -p daemon.debug "$message via $proc_entry"
+       echo "1" >"$proc_entry"
+}
+
+list_cpu_idx()
+{
+       local line i=0
+
+       while read line; do {
+               case "$line" in
+                       [Pp]rocessor*)
+                               echo $i
+                               i=$(( $i + 1 ))
+                       ;;
+               esac
+       } done <"/proc/cpuinfo"
+}
+
+start()
+{
+       # http://shmilyxbq-compcache.googlecode.com/hg/README
+       # if >1 cpu_core, reinit kmodule with e.g. num_devices=4
+
+       local zram_size="$( zram_size )"
+       local zram_dev core
+
+       for core in $( list_cpu_idx ); do {
+               zram_dev="$( zram_dev "$core" )"
+               zram_applicable "$zram_dev" || return 1
+
+               logger -s -t zram_start -p daemon.debug "activating '$zram_dev' for swapping ($zram_size MegaBytes)"
+
+               zram_reset "$zram_dev" "enforcing defaults"
+               echo $(( $zram_size * 1024 * 1024 )) >"/sys/block/$( basename $zram_dev )/disksize"
+               mkswap "$zram_dev"
+               swapon "$zram_dev"
+       } done
+}
+
+stop()
+{
+       local zram_dev proc_entry
+
+       for core in $( list_cpu_idx ); do {
+               zram_dev="$( zram_dev "$core" )"
+               proc_entry="/sys/block/$( basename "$zram_dev" )/reset"
+
+               grep -sq ^"$zram_dev " /proc/swaps && {
+                       logger -s -t zram_stop -p daemon.debug "deactivate swap $zram_dev"
+                       swapoff "$zram_dev"
+               }
+
+               zram_reset "$zram_dev" "claiming memory back"
+       } done
+}
+