Merge tag 'trace-printf-v6.13' of git://git.kernel.org/pub/scm/linux/kernel/git/trace...
[drm/drm-misc.git] / tools / testing / selftests / zram / zram_lib.sh
blob21ec1966de76ca4047fe50eea82015bc03986c98
1 #!/bin/sh
2 # SPDX-License-Identifier: GPL-2.0-or-later
3 # Copyright (c) 2015 Oracle and/or its affiliates. All Rights Reserved.
5 # Author: Alexey Kodanev <alexey.kodanev@oracle.com>
6 # Modified: Naresh Kamboju <naresh.kamboju@linaro.org>
8 dev_makeswap=-1
9 dev_mounted=-1
10 dev_start=0
11 dev_end=-1
12 module_load=-1
13 sys_control=-1
14 # Kselftest framework requirement - SKIP code is 4.
15 ksft_skip=4
16 kernel_version=`uname -r | cut -d'.' -f1,2`
17 kernel_major=${kernel_version%.*}
18 kernel_minor=${kernel_version#*.}
20 trap INT
22 check_prereqs()
24 local msg="skip all tests:"
25 local uid=$(id -u)
27 if [ $uid -ne 0 ]; then
28 echo $msg must be run as root >&2
29 exit $ksft_skip
33 kernel_gte()
35 major=${1%.*}
36 minor=${1#*.}
38 if [ $kernel_major -gt $major ]; then
39 return 0
40 elif [[ $kernel_major -eq $major && $kernel_minor -ge $minor ]]; then
41 return 0
44 return 1
47 zram_cleanup()
49 echo "zram cleanup"
50 local i=
51 for i in $(seq $dev_start $dev_makeswap); do
52 swapoff /dev/zram$i
53 done
55 for i in $(seq $dev_start $dev_mounted); do
56 umount /dev/zram$i
57 done
59 for i in $(seq $dev_start $dev_end); do
60 echo 1 > /sys/block/zram${i}/reset
61 rm -rf zram$i
62 done
64 if [ $sys_control -eq 1 ]; then
65 for i in $(seq $dev_start $dev_end); do
66 echo $i > /sys/class/zram-control/hot_remove
67 done
70 if [ $module_load -eq 1 ]; then
71 rmmod zram > /dev/null 2>&1
75 zram_load()
77 echo "create '$dev_num' zram device(s)"
79 # zram module loaded, new kernel
80 if [ -d "/sys/class/zram-control" ]; then
81 echo "zram modules already loaded, kernel supports" \
82 "zram-control interface"
83 dev_start=$(ls /dev/zram* | wc -w)
84 dev_end=$(($dev_start + $dev_num - 1))
85 sys_control=1
87 for i in $(seq $dev_start $dev_end); do
88 cat /sys/class/zram-control/hot_add > /dev/null
89 done
91 echo "all zram devices (/dev/zram$dev_start~$dev_end" \
92 "successfully created"
93 return 0
96 # detect old kernel or built-in
97 modprobe zram num_devices=$dev_num
98 if [ ! -d "/sys/class/zram-control" ]; then
99 if grep -q '^zram' /proc/modules; then
100 rmmod zram > /dev/null 2>&1
101 if [ $? -ne 0 ]; then
102 echo "zram module is being used on old kernel" \
103 "without zram-control interface"
104 exit $ksft_skip
106 else
107 echo "test needs CONFIG_ZRAM=m on old kernel without" \
108 "zram-control interface"
109 exit $ksft_skip
111 modprobe zram num_devices=$dev_num
114 module_load=1
115 dev_end=$(($dev_num - 1))
116 echo "all zram devices (/dev/zram0~$dev_end) successfully created"
119 zram_max_streams()
121 echo "set max_comp_streams to zram device(s)"
123 kernel_gte 4.7
124 if [ $? -eq 0 ]; then
125 echo "The device attribute max_comp_streams was"\
126 "deprecated in 4.7"
127 return 0
130 local i=$dev_start
131 for max_s in $zram_max_streams; do
132 local sys_path="/sys/block/zram${i}/max_comp_streams"
133 echo $max_s > $sys_path || \
134 echo "FAIL failed to set '$max_s' to $sys_path"
135 sleep 1
136 local max_streams=$(cat $sys_path)
138 [ "$max_s" -ne "$max_streams" ] && \
139 echo "FAIL can't set max_streams '$max_s', get $max_stream"
141 i=$(($i + 1))
142 echo "$sys_path = '$max_streams'"
143 done
145 echo "zram max streams: OK"
148 zram_compress_alg()
150 echo "test that we can set compression algorithm"
152 local i=$dev_start
153 local algs=$(cat /sys/block/zram${i}/comp_algorithm)
154 echo "supported algs: $algs"
156 for alg in $zram_algs; do
157 local sys_path="/sys/block/zram${i}/comp_algorithm"
158 echo "$alg" > $sys_path || \
159 echo "FAIL can't set '$alg' to $sys_path"
160 i=$(($i + 1))
161 echo "$sys_path = '$alg'"
162 done
164 echo "zram set compression algorithm: OK"
167 zram_set_disksizes()
169 echo "set disk size to zram device(s)"
170 local i=$dev_start
171 for ds in $zram_sizes; do
172 local sys_path="/sys/block/zram${i}/disksize"
173 echo "$ds" > $sys_path || \
174 echo "FAIL can't set '$ds' to $sys_path"
176 i=$(($i + 1))
177 echo "$sys_path = '$ds'"
178 done
180 echo "zram set disksizes: OK"
183 zram_set_memlimit()
185 echo "set memory limit to zram device(s)"
187 local i=$dev_start
188 for ds in $zram_mem_limits; do
189 local sys_path="/sys/block/zram${i}/mem_limit"
190 echo "$ds" > $sys_path || \
191 echo "FAIL can't set '$ds' to $sys_path"
193 i=$(($i + 1))
194 echo "$sys_path = '$ds'"
195 done
197 echo "zram set memory limit: OK"
200 zram_makeswap()
202 echo "make swap with zram device(s)"
203 local i=$dev_start
204 for i in $(seq $dev_start $dev_end); do
205 mkswap /dev/zram$i > err.log 2>&1
206 if [ $? -ne 0 ]; then
207 cat err.log
208 echo "FAIL mkswap /dev/zram$1 failed"
211 swapon /dev/zram$i > err.log 2>&1
212 if [ $? -ne 0 ]; then
213 cat err.log
214 echo "FAIL swapon /dev/zram$1 failed"
217 echo "done with /dev/zram$i"
218 dev_makeswap=$i
219 done
221 echo "zram making zram mkswap and swapon: OK"
224 zram_swapoff()
226 local i=
227 for i in $(seq $dev_start $dev_end); do
228 swapoff /dev/zram$i > err.log 2>&1
229 if [ $? -ne 0 ]; then
230 cat err.log
231 echo "FAIL swapoff /dev/zram$i failed"
233 done
234 dev_makeswap=-1
236 echo "zram swapoff: OK"
239 zram_makefs()
241 local i=$dev_start
242 for fs in $zram_filesystems; do
243 # if requested fs not supported default it to ext2
244 which mkfs.$fs > /dev/null 2>&1 || fs=ext2
246 echo "make $fs filesystem on /dev/zram$i"
247 mkfs.$fs /dev/zram$i > err.log 2>&1
248 if [ $? -ne 0 ]; then
249 cat err.log
250 echo "FAIL failed to make $fs on /dev/zram$i"
252 i=$(($i + 1))
253 echo "zram mkfs.$fs: OK"
254 done
257 zram_mount()
259 local i=0
260 for i in $(seq $dev_start $dev_end); do
261 echo "mount /dev/zram$i"
262 mkdir zram$i
263 mount /dev/zram$i zram$i > /dev/null || \
264 echo "FAIL mount /dev/zram$i failed"
265 dev_mounted=$i
266 done
268 echo "zram mount of zram device(s): OK"