Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux

selftests/zram: Adding zram tests

zram: Compressed RAM based block devices
----------------------------------------
The zram module creates RAM based block devices named /dev/zram<id>
(<id> = 0, 1, ...). Pages written to these disks are compressed and stored
in memory itself. These disks allow very fast I/O and compression provides
good amounts of memory savings. Some of the usecases include /tmp storage,
use as swap disks, various caches under /var and maybe many more :)

Statistics for individual zram devices are exported through sysfs nodes at
/sys/block/zram<id>/

This patch is to validate the zram functionality. Test interacts with block
device /dev/zram<id> and sysfs nodes /sys/block/zram<id>/

zram.sh: sanity check of CONFIG_ZRAM and to run zram01 and zram02 tests
zram01.sh: creates general purpose ram disks with different filesystems
zram02.sh: creates block device for swap
zram_lib.sh: create library with initialization/cleanup functions
README: ZRAM introduction and Kconfig required.
Makefile: To run zram tests

zram test output
-----------------
./zram.sh
--------------------
running zram tests
--------------------
/dev/zram0 device file found: OK
set max_comp_streams to zram device(s)
/sys/block/zram0/max_comp_streams = '2' (1/1)
zram max streams: OK
test that we can set compression algorithm
supported algs: [lzo] lz4
/sys/block/zram0/comp_algorithm = 'lzo' (1/1)
zram set compression algorithm: OK
set disk size to zram device(s)
/sys/block/zram0/disksize = '2097152' (1/1)
zram set disksizes: OK
set memory limit to zram device(s)
/sys/block/zram0/mem_limit = '2M' (1/1)
zram set memory limit: OK
make ext4 filesystem on /dev/zram0
zram mkfs.ext4: OK
mount /dev/zram0
zram mount of zram device(s): OK
fill zram0...
zram0 can be filled with '1932' KB
zram used 3M, zram disk sizes 2097152M
zram compression ratio: 699050.66:1: OK
zram cleanup
zram01 : [PASS]

/dev/zram0 device file found: OK
set max_comp_streams to zram device(s)
/sys/block/zram0/max_comp_streams = '2' (1/1)
zram max streams: OK
set disk size to zram device(s)
/sys/block/zram0/disksize = '1048576' (1/1)
zram set disksizes: OK
set memory limit to zram device(s)
/sys/block/zram0/mem_limit = '1M' (1/1)
zram set memory limit: OK
make swap with zram device(s)
done with /dev/zram0
zram making zram mkswap and swapon: OK
zram swapoff: OK
zram cleanup
zram02 : [PASS]

CC: Shuah Khan <shuahkh@osg.samsung.com>
CC: Tyler Baker <tyler.baker@linaro.org>
CC: Milosz Wasilewski <milosz.wasilewski@linaro.org>
CC: Alexey Kodanev <alexey.kodanev@oracle.com>
Signed-off-by: Naresh Kamboju <naresh.kamboju@linaro.org>
Signed-off-by: Alexey Kodanev <alexey.kodanev@oracle.com>
Reviewed-By: Tyler Baker <tyler.baker@linaro.org>
Signed-off-by: Shuah Khan <shuahkh@osg.samsung.com>

authored by

Naresh Kamboju and committed by
Shuah Khan
f21fb798 d770e558

+449
+1
tools/testing/selftests/Makefile
··· 22 22 TARGETS += user 23 23 TARGETS += vm 24 24 TARGETS += x86 25 + TARGETS += zram 25 26 #Please keep the TARGETS list alphabetically sorted 26 27 # Run "make quicktest=1 run_tests" or 27 28 # "make quicktest=1 kselftest from top level Makefile
+12
tools/testing/selftests/zram/Makefile
··· 1 + all: 2 + 3 + TEST_PROGS := zram.sh 4 + TEST_FILES := zram01.sh zram02.sh zram_lib.sh 5 + 6 + include ../lib.mk 7 + 8 + run_tests: 9 + @/bin/bash ./zram.sh 10 + 11 + clean: 12 + $(RM) err.log
+40
tools/testing/selftests/zram/README
··· 1 + zram: Compressed RAM based block devices 2 + ---------------------------------------- 3 + * Introduction 4 + 5 + The zram module creates RAM based block devices named /dev/zram<id> 6 + (<id> = 0, 1, ...). Pages written to these disks are compressed and stored 7 + in memory itself. These disks allow very fast I/O and compression provides 8 + good amounts of memory savings. Some of the usecases include /tmp storage, 9 + use as swap disks, various caches under /var and maybe many more :) 10 + 11 + Statistics for individual zram devices are exported through sysfs nodes at 12 + /sys/block/zram<id>/ 13 + 14 + Kconfig required: 15 + CONFIG_ZRAM=y 16 + CONFIG_ZRAM_LZ4_COMPRESS=y 17 + CONFIG_ZPOOL=y 18 + CONFIG_ZSMALLOC=y 19 + 20 + ZRAM Testcases 21 + -------------- 22 + zram_lib.sh: create library with initialization/cleanup functions 23 + zram.sh: For sanity check of CONFIG_ZRAM and to run zram01 and zram02 24 + 25 + Two functional tests: zram01 and zram02: 26 + zram01.sh: creates general purpose ram disks with ext4 filesystems 27 + zram02.sh: creates block device for swap 28 + 29 + Commands required for testing: 30 + - bc 31 + - dd 32 + - free 33 + - awk 34 + - mkswap 35 + - swapon 36 + - swapoff 37 + - mkfs/ mkfs.ext4 38 + 39 + For more information please refer: 40 + kernel-source-tree/Documentation/blockdev/zram.txt
+23
tools/testing/selftests/zram/zram.sh
··· 1 + #!/bin/bash 2 + TCID="zram.sh" 3 + 4 + run_zram () { 5 + echo "--------------------" 6 + echo "running zram tests" 7 + echo "--------------------" 8 + ./zram01.sh 9 + echo "" 10 + ./zram02.sh 11 + } 12 + 13 + # check zram module exists 14 + MODULE_PATH=/lib/modules/`uname -r`/kernel/drivers/block/zram/zram.ko 15 + if [ -f $MODULE_PATH ]; then 16 + run_zram 17 + elif [ -b /dev/zram0 ]; then 18 + run_zram 19 + else 20 + echo "$TCID : No zram.ko module or /dev/zram0 device file not found" 21 + echo "$TCID : CONFIG_ZRAM is not set" 22 + exit 1 23 + fi
+98
tools/testing/selftests/zram/zram01.sh
··· 1 + #!/bin/bash 2 + # Copyright (c) 2015 Oracle and/or its affiliates. All Rights Reserved. 3 + # 4 + # This program is free software; you can redistribute it and/or 5 + # modify it under the terms of the GNU General Public License as 6 + # published by the Free Software Foundation; either version 2 of 7 + # the License, or (at your option) any later version. 8 + # 9 + # This program is distributed in the hope that it would be useful, 10 + # but WITHOUT ANY WARRANTY; without even the implied warranty of 11 + # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 + # GNU General Public License for more details. 13 + # 14 + # Test creates several zram devices with different filesystems on them. 15 + # It fills each device with zeros and checks that compression works. 16 + # 17 + # Author: Alexey Kodanev <alexey.kodanev@oracle.com> 18 + # Modified: Naresh Kamboju <naresh.kamboju@linaro.org> 19 + 20 + TCID="zram01" 21 + ERR_CODE=0 22 + 23 + . ./zram_lib.sh 24 + 25 + # Test will create the following number of zram devices: 26 + dev_num=1 27 + # This is a list of parameters for zram devices. 28 + # Number of items must be equal to 'dev_num' parameter. 29 + zram_max_streams="2" 30 + 31 + # The zram sysfs node 'disksize' value can be either in bytes, 32 + # or you can use mem suffixes. But in some old kernels, mem 33 + # suffixes are not supported, for example, in RHEL6.6GA's kernel 34 + # layer, it uses strict_strtoull() to parse disksize which does 35 + # not support mem suffixes, in some newer kernels, they use 36 + # memparse() which supports mem suffixes. So here we just use 37 + # bytes to make sure everything works correctly. 38 + zram_sizes="2097152" # 2MB 39 + zram_mem_limits="2M" 40 + zram_filesystems="ext4" 41 + zram_algs="lzo" 42 + 43 + zram_fill_fs() 44 + { 45 + local mem_free0=$(free -m | awk 'NR==2 {print $4}') 46 + 47 + for i in $(seq 0 $(($dev_num - 1))); do 48 + echo "fill zram$i..." 49 + local b=0 50 + while [ true ]; do 51 + dd conv=notrunc if=/dev/zero of=zram${i}/file \ 52 + oflag=append count=1 bs=1024 status=none \ 53 + > /dev/null 2>&1 || break 54 + b=$(($b + 1)) 55 + done 56 + echo "zram$i can be filled with '$b' KB" 57 + done 58 + 59 + local mem_free1=$(free -m | awk 'NR==2 {print $4}') 60 + local used_mem=$(($mem_free0 - $mem_free1)) 61 + 62 + local total_size=0 63 + for sm in $zram_sizes; do 64 + local s=$(echo $sm | sed 's/M//') 65 + total_size=$(($total_size + $s)) 66 + done 67 + 68 + echo "zram used ${used_mem}M, zram disk sizes ${total_size}M" 69 + 70 + local v=$((100 * $total_size / $used_mem)) 71 + 72 + if [ "$v" -lt 100 ]; then 73 + echo "FAIL compression ratio: 0.$v:1" 74 + ERR_CODE=-1 75 + zram_cleanup 76 + return 77 + fi 78 + 79 + echo "zram compression ratio: $(echo "scale=2; $v / 100 " | bc):1: OK" 80 + } 81 + 82 + zram_load 83 + zram_max_streams 84 + zram_compress_alg 85 + zram_set_disksizes 86 + zram_set_memlimit 87 + zram_makefs 88 + zram_mount 89 + 90 + zram_fill_fs 91 + zram_cleanup 92 + zram_unload 93 + 94 + if [ $ERR_CODE -ne 0 ]; then 95 + echo "$TCID : [FAIL]" 96 + else 97 + echo "$TCID : [PASS]" 98 + fi
+53
tools/testing/selftests/zram/zram02.sh
··· 1 + #!/bin/bash 2 + # Copyright (c) 2015 Oracle and/or its affiliates. All Rights Reserved. 3 + # 4 + # This program is free software; you can redistribute it and/or 5 + # modify it under the terms of the GNU General Public License as 6 + # published by the Free Software Foundation; either version 2 of 7 + # the License, or (at your option) any later version. 8 + # 9 + # This program is distributed in the hope that it would be useful, 10 + # but WITHOUT ANY WARRANTY; without even the implied warranty of 11 + # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 + # GNU General Public License for more details. 13 + # 14 + # Test checks that we can create swap zram device. 15 + # 16 + # Author: Alexey Kodanev <alexey.kodanev@oracle.com> 17 + # Modified: Naresh Kamboju <naresh.kamboju@linaro.org> 18 + 19 + TCID="zram02" 20 + ERR_CODE=0 21 + 22 + . ./zram_lib.sh 23 + 24 + # Test will create the following number of zram devices: 25 + dev_num=1 26 + # This is a list of parameters for zram devices. 27 + # Number of items must be equal to 'dev_num' parameter. 28 + zram_max_streams="2" 29 + 30 + # The zram sysfs node 'disksize' value can be either in bytes, 31 + # or you can use mem suffixes. But in some old kernels, mem 32 + # suffixes are not supported, for example, in RHEL6.6GA's kernel 33 + # layer, it uses strict_strtoull() to parse disksize which does 34 + # not support mem suffixes, in some newer kernels, they use 35 + # memparse() which supports mem suffixes. So here we just use 36 + # bytes to make sure everything works correctly. 37 + zram_sizes="1048576" # 1M 38 + zram_mem_limits="1M" 39 + 40 + zram_load 41 + zram_max_streams 42 + zram_set_disksizes 43 + zram_set_memlimit 44 + zram_makeswap 45 + zram_swapoff 46 + zram_cleanup 47 + zram_unload 48 + 49 + if [ $ERR_CODE -ne 0 ]; then 50 + echo "$TCID : [FAIL]" 51 + else 52 + echo "$TCID : [PASS]" 53 + fi
+222
tools/testing/selftests/zram/zram_lib.sh
··· 1 + #!/bin/sh 2 + # Copyright (c) 2015 Oracle and/or its affiliates. All Rights Reserved. 3 + # 4 + # This program is free software; you can redistribute it and/or 5 + # modify it under the terms of the GNU General Public License as 6 + # published by the Free Software Foundation; either version 2 of 7 + # the License, or (at your option) any later version. 8 + # 9 + # This program is distributed in the hope that it would be useful, 10 + # but WITHOUT ANY WARRANTY; without even the implied warranty of 11 + # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 + # GNU General Public License for more details. 13 + # 14 + # Author: Alexey Kodanev <alexey.kodanev@oracle.com> 15 + # Modified: Naresh Kamboju <naresh.kamboju@linaro.org> 16 + 17 + MODULE=0 18 + dev_makeswap=-1 19 + dev_mounted=-1 20 + 21 + trap INT 22 + 23 + zram_cleanup() 24 + { 25 + echo "zram cleanup" 26 + local i= 27 + for i in $(seq 0 $dev_makeswap); do 28 + swapoff /dev/zram$i 29 + done 30 + 31 + for i in $(seq 0 $dev_mounted); do 32 + umount /dev/zram$i 33 + done 34 + 35 + for i in $(seq 0 $(($dev_num - 1))); do 36 + echo 1 > /sys/block/zram${i}/reset 37 + rm -rf zram$i 38 + done 39 + 40 + } 41 + 42 + zram_unload() 43 + { 44 + if [ $MODULE -ne 0 ] ; then 45 + echo "zram rmmod zram" 46 + rmmod zram > /dev/null 2>&1 47 + fi 48 + } 49 + 50 + zram_load() 51 + { 52 + # check zram module exists 53 + MODULE_PATH=/lib/modules/`uname -r`/kernel/drivers/block/zram/zram.ko 54 + if [ -f $MODULE_PATH ]; then 55 + MODULE=1 56 + echo "create '$dev_num' zram device(s)" 57 + modprobe zram num_devices=$dev_num 58 + if [ $? -ne 0 ]; then 59 + echo "failed to insert zram module" 60 + exit 1 61 + fi 62 + 63 + dev_num_created=$(ls /dev/zram* | wc -w) 64 + 65 + if [ "$dev_num_created" -ne "$dev_num" ]; then 66 + echo "unexpected num of devices: $dev_num_created" 67 + ERR_CODE=-1 68 + else 69 + echo "zram load module successful" 70 + fi 71 + elif [ -b /dev/zram0 ]; then 72 + echo "/dev/zram0 device file found: OK" 73 + else 74 + echo "ERROR: No zram.ko module or no /dev/zram0 device found" 75 + echo "$TCID : CONFIG_ZRAM is not set" 76 + exit 1 77 + fi 78 + } 79 + 80 + zram_max_streams() 81 + { 82 + echo "set max_comp_streams to zram device(s)" 83 + 84 + local i=0 85 + for max_s in $zram_max_streams; do 86 + local sys_path="/sys/block/zram${i}/max_comp_streams" 87 + echo $max_s > $sys_path || \ 88 + echo "FAIL failed to set '$max_s' to $sys_path" 89 + sleep 1 90 + local max_streams=$(cat $sys_path) 91 + 92 + [ "$max_s" -ne "$max_streams" ] && \ 93 + echo "FAIL can't set max_streams '$max_s', get $max_stream" 94 + 95 + i=$(($i + 1)) 96 + echo "$sys_path = '$max_streams' ($i/$dev_num)" 97 + done 98 + 99 + echo "zram max streams: OK" 100 + } 101 + 102 + zram_compress_alg() 103 + { 104 + echo "test that we can set compression algorithm" 105 + 106 + local algs=$(cat /sys/block/zram0/comp_algorithm) 107 + echo "supported algs: $algs" 108 + local i=0 109 + for alg in $zram_algs; do 110 + local sys_path="/sys/block/zram${i}/comp_algorithm" 111 + echo "$alg" > $sys_path || \ 112 + echo "FAIL can't set '$alg' to $sys_path" 113 + i=$(($i + 1)) 114 + echo "$sys_path = '$alg' ($i/$dev_num)" 115 + done 116 + 117 + echo "zram set compression algorithm: OK" 118 + } 119 + 120 + zram_set_disksizes() 121 + { 122 + echo "set disk size to zram device(s)" 123 + local i=0 124 + for ds in $zram_sizes; do 125 + local sys_path="/sys/block/zram${i}/disksize" 126 + echo "$ds" > $sys_path || \ 127 + echo "FAIL can't set '$ds' to $sys_path" 128 + 129 + i=$(($i + 1)) 130 + echo "$sys_path = '$ds' ($i/$dev_num)" 131 + done 132 + 133 + echo "zram set disksizes: OK" 134 + } 135 + 136 + zram_set_memlimit() 137 + { 138 + echo "set memory limit to zram device(s)" 139 + 140 + local i=0 141 + for ds in $zram_mem_limits; do 142 + local sys_path="/sys/block/zram${i}/mem_limit" 143 + echo "$ds" > $sys_path || \ 144 + echo "FAIL can't set '$ds' to $sys_path" 145 + 146 + i=$(($i + 1)) 147 + echo "$sys_path = '$ds' ($i/$dev_num)" 148 + done 149 + 150 + echo "zram set memory limit: OK" 151 + } 152 + 153 + zram_makeswap() 154 + { 155 + echo "make swap with zram device(s)" 156 + local i=0 157 + for i in $(seq 0 $(($dev_num - 1))); do 158 + mkswap /dev/zram$i > err.log 2>&1 159 + if [ $? -ne 0 ]; then 160 + cat err.log 161 + echo "FAIL mkswap /dev/zram$1 failed" 162 + fi 163 + 164 + swapon /dev/zram$i > err.log 2>&1 165 + if [ $? -ne 0 ]; then 166 + cat err.log 167 + echo "FAIL swapon /dev/zram$1 failed" 168 + fi 169 + 170 + echo "done with /dev/zram$i" 171 + dev_makeswap=$i 172 + done 173 + 174 + echo "zram making zram mkswap and swapon: OK" 175 + } 176 + 177 + zram_swapoff() 178 + { 179 + local i= 180 + for i in $(seq 0 $dev_makeswap); do 181 + swapoff /dev/zram$i > err.log 2>&1 182 + if [ $? -ne 0 ]; then 183 + cat err.log 184 + echo "FAIL swapoff /dev/zram$i failed" 185 + fi 186 + done 187 + dev_makeswap=-1 188 + 189 + echo "zram swapoff: OK" 190 + } 191 + 192 + zram_makefs() 193 + { 194 + local i=0 195 + for fs in $zram_filesystems; do 196 + # if requested fs not supported default it to ext2 197 + which mkfs.$fs > /dev/null 2>&1 || fs=ext2 198 + 199 + echo "make $fs filesystem on /dev/zram$i" 200 + mkfs.$fs /dev/zram$i > err.log 2>&1 201 + if [ $? -ne 0 ]; then 202 + cat err.log 203 + echo "FAIL failed to make $fs on /dev/zram$i" 204 + fi 205 + i=$(($i + 1)) 206 + echo "zram mkfs.$fs: OK" 207 + done 208 + } 209 + 210 + zram_mount() 211 + { 212 + local i=0 213 + for i in $(seq 0 $(($dev_num - 1))); do 214 + echo "mount /dev/zram$i" 215 + mkdir zram$i 216 + mount /dev/zram$i zram$i > /dev/null || \ 217 + echo "FAIL mount /dev/zram$i failed" 218 + dev_mounted=$i 219 + done 220 + 221 + echo "zram mount of zram device(s): OK" 222 + }