2014-07-29

How to enable swap to zRAM on Android 4.1-4.3

1. Enable the following kernel options use zRAM swap. For example kernel/arch/arm/configs/xxx_defconfig.

CONFIG_SWAP=y
CONFIG_CGROUP_MEM_RES_CTLR=y
CONFIG_CGROUP_MEM_RES_CTLR_SWAP=y
CONFIG_ZRAM=y
CONFIG_QCACHE=y
CONFIG_ZSMALLOC=y

2. zramsize is mandatory and indicates how much uncompressed memory you want the zram area to hold. Compression ratios in the 30-50% range are usually observed as google mentioned. In below case, I set it as 64M. File: device/qcom/msm7627a/fstab.qcom

  /dev/block/platform/msm_sdcc.3/by-num/p23  /variant  ext4  ro,barrier=1  wait
  /dev/block/platform/msm_sdcc.3/by-num/p25  /preload  ext4  ro,barrier=1  wait
  /dev/block/zram0                           none      swap  defaults      wait,zramsize=65536000

3. Add 'swapon_all xxx' after mount_all in init.rc. For example device/qcom/msm7627a/init.target.rc

  on emmc-fs
      mount_all fstab.qcom
      swapon_all fstab.qcom
 
  on fs
      mount_all fstab.nand.msm7627a
      swapon_all fstab.nand.msm7627a

3. By default, the Linux kernel swaps in 8 pages of memory at a time. When using ZRAM, the incremental cost of reading 1 page at a time is negligible and may help in case the device is under extreme memory pressure. To read only 1 page at a time, add the following to your init.rc as following. File: system/core/rootdir/init.rc

on post-fs  
   # Force kernel read only 1 page at a time
   write /proc/sys/vm/page-cluster 0

4. Other common stuff is in attached zram.diff, the changes are: - Add swapon/off implementation. - Remove X86 dependence and add ZSMALLOC, otherwise we can not build kernel with zRAM with ARM tool chain - Add ZSMALLOC stuff in kernel used by 'swap to zRAM' feature - Align fs_mgr to 4.4 to support swap stuff. There's one change done by me in f_mgr.c, you can search 'YangMing' in that file.

5. After done, you can check if zRAM works by checking the following parameters.

$root@aosp:/ # cat /proc/swaps                                                 
Filename                Type        Size    Used    Priority
/dev/block/zram0                        partition    63996    3928    -1
 
$root@aosp:/dev/block # df /dev/block/zram0
Filesystem             Size   Used   Free   Blksize
/dev/block/zram0       189M    64K   189M   4096
 
$root@aosp:/dev/block # cat /sys/block/zram0/num_reads                         
452

 

No comments: