Merge branch 'locking-urgent-for-linus' of git://git.kernel.org/pub/scm/linux/kernel...
[cris-mirror.git] / tools / testing / selftests / zram / zram_lib.sh
blobf6a9c73e7a442e7988b0820ebc809a342981df91
1 #!/bin/sh
2 # Copyright (c) 2015 Oracle and/or its affiliates. All Rights Reserved.
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.
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.
14 # Author: Alexey Kodanev <alexey.kodanev@oracle.com>
15 # Modified: Naresh Kamboju <naresh.kamboju@linaro.org>
17 MODULE=0
18 dev_makeswap=-1
19 dev_mounted=-1
21 trap INT
23 check_prereqs()
25 local msg="skip all tests:"
26 local uid=$(id -u)
28 if [ $uid -ne 0 ]; then
29 echo $msg must be run as root >&2
30 exit 0
34 zram_cleanup()
36 echo "zram cleanup"
37 local i=
38 for i in $(seq 0 $dev_makeswap); do
39 swapoff /dev/zram$i
40 done
42 for i in $(seq 0 $dev_mounted); do
43 umount /dev/zram$i
44 done
46 for i in $(seq 0 $(($dev_num - 1))); do
47 echo 1 > /sys/block/zram${i}/reset
48 rm -rf zram$i
49 done
53 zram_unload()
55 if [ $MODULE -ne 0 ] ; then
56 echo "zram rmmod zram"
57 rmmod zram > /dev/null 2>&1
61 zram_load()
63 # check zram module exists
64 MODULE_PATH=/lib/modules/`uname -r`/kernel/drivers/block/zram/zram.ko
65 if [ -f $MODULE_PATH ]; then
66 MODULE=1
67 echo "create '$dev_num' zram device(s)"
68 modprobe zram num_devices=$dev_num
69 if [ $? -ne 0 ]; then
70 echo "failed to insert zram module"
71 exit 1
74 dev_num_created=$(ls /dev/zram* | wc -w)
76 if [ "$dev_num_created" -ne "$dev_num" ]; then
77 echo "unexpected num of devices: $dev_num_created"
78 ERR_CODE=-1
79 else
80 echo "zram load module successful"
82 elif [ -b /dev/zram0 ]; then
83 echo "/dev/zram0 device file found: OK"
84 else
85 echo "ERROR: No zram.ko module or no /dev/zram0 device found"
86 echo "$TCID : CONFIG_ZRAM is not set"
87 exit 1
91 zram_max_streams()
93 echo "set max_comp_streams to zram device(s)"
95 local i=0
96 for max_s in $zram_max_streams; do
97 local sys_path="/sys/block/zram${i}/max_comp_streams"
98 echo $max_s > $sys_path || \
99 echo "FAIL failed to set '$max_s' to $sys_path"
100 sleep 1
101 local max_streams=$(cat $sys_path)
103 [ "$max_s" -ne "$max_streams" ] && \
104 echo "FAIL can't set max_streams '$max_s', get $max_stream"
106 i=$(($i + 1))
107 echo "$sys_path = '$max_streams' ($i/$dev_num)"
108 done
110 echo "zram max streams: OK"
113 zram_compress_alg()
115 echo "test that we can set compression algorithm"
117 local algs=$(cat /sys/block/zram0/comp_algorithm)
118 echo "supported algs: $algs"
119 local i=0
120 for alg in $zram_algs; do
121 local sys_path="/sys/block/zram${i}/comp_algorithm"
122 echo "$alg" > $sys_path || \
123 echo "FAIL can't set '$alg' to $sys_path"
124 i=$(($i + 1))
125 echo "$sys_path = '$alg' ($i/$dev_num)"
126 done
128 echo "zram set compression algorithm: OK"
131 zram_set_disksizes()
133 echo "set disk size to zram device(s)"
134 local i=0
135 for ds in $zram_sizes; do
136 local sys_path="/sys/block/zram${i}/disksize"
137 echo "$ds" > $sys_path || \
138 echo "FAIL can't set '$ds' to $sys_path"
140 i=$(($i + 1))
141 echo "$sys_path = '$ds' ($i/$dev_num)"
142 done
144 echo "zram set disksizes: OK"
147 zram_set_memlimit()
149 echo "set memory limit to zram device(s)"
151 local i=0
152 for ds in $zram_mem_limits; do
153 local sys_path="/sys/block/zram${i}/mem_limit"
154 echo "$ds" > $sys_path || \
155 echo "FAIL can't set '$ds' to $sys_path"
157 i=$(($i + 1))
158 echo "$sys_path = '$ds' ($i/$dev_num)"
159 done
161 echo "zram set memory limit: OK"
164 zram_makeswap()
166 echo "make swap with zram device(s)"
167 local i=0
168 for i in $(seq 0 $(($dev_num - 1))); do
169 mkswap /dev/zram$i > err.log 2>&1
170 if [ $? -ne 0 ]; then
171 cat err.log
172 echo "FAIL mkswap /dev/zram$1 failed"
175 swapon /dev/zram$i > err.log 2>&1
176 if [ $? -ne 0 ]; then
177 cat err.log
178 echo "FAIL swapon /dev/zram$1 failed"
181 echo "done with /dev/zram$i"
182 dev_makeswap=$i
183 done
185 echo "zram making zram mkswap and swapon: OK"
188 zram_swapoff()
190 local i=
191 for i in $(seq 0 $dev_makeswap); do
192 swapoff /dev/zram$i > err.log 2>&1
193 if [ $? -ne 0 ]; then
194 cat err.log
195 echo "FAIL swapoff /dev/zram$i failed"
197 done
198 dev_makeswap=-1
200 echo "zram swapoff: OK"
203 zram_makefs()
205 local i=0
206 for fs in $zram_filesystems; do
207 # if requested fs not supported default it to ext2
208 which mkfs.$fs > /dev/null 2>&1 || fs=ext2
210 echo "make $fs filesystem on /dev/zram$i"
211 mkfs.$fs /dev/zram$i > err.log 2>&1
212 if [ $? -ne 0 ]; then
213 cat err.log
214 echo "FAIL failed to make $fs on /dev/zram$i"
216 i=$(($i + 1))
217 echo "zram mkfs.$fs: OK"
218 done
221 zram_mount()
223 local i=0
224 for i in $(seq 0 $(($dev_num - 1))); do
225 echo "mount /dev/zram$i"
226 mkdir zram$i
227 mount /dev/zram$i zram$i > /dev/null || \
228 echo "FAIL mount /dev/zram$i failed"
229 dev_mounted=$i
230 done
232 echo "zram mount of zram device(s): OK"