xref: /linux/tools/testing/selftests/zram/zram_lib.sh (revision 01dabed2)
1f21fb798SNaresh Kamboju#!/bin/sh
27170066eSThomas Gleixner# SPDX-License-Identifier: GPL-2.0-or-later
3f21fb798SNaresh Kamboju# Copyright (c) 2015 Oracle and/or its affiliates. All Rights Reserved.
4f21fb798SNaresh Kamboju#
5f21fb798SNaresh Kamboju# Author: Alexey Kodanev <alexey.kodanev@oracle.com>
6f21fb798SNaresh Kamboju# Modified: Naresh Kamboju <naresh.kamboju@linaro.org>
7f21fb798SNaresh Kamboju
8f21fb798SNaresh Kambojudev_makeswap=-1
9f21fb798SNaresh Kambojudev_mounted=-1
10*01dabed2SYang Xudev_start=0
11*01dabed2SYang Xudev_end=-1
12*01dabed2SYang Xumodule_load=-1
13*01dabed2SYang Xusys_control=-1
1468581446SShuah Khan (Samsung OSG)# Kselftest framework requirement - SKIP code is 4.
1568581446SShuah Khan (Samsung OSG)ksft_skip=4
16fc4eb486SYang Xukernel_version=`uname -r | cut -d'.' -f1,2`
17fc4eb486SYang Xukernel_major=${kernel_version%.*}
18fc4eb486SYang Xukernel_minor=${kernel_version#*.}
1968581446SShuah Khan (Samsung OSG)
20f21fb798SNaresh Kambojutrap INT
21f21fb798SNaresh Kamboju
222dfb28e3SNaresh Kambojucheck_prereqs()
232dfb28e3SNaresh Kamboju{
242dfb28e3SNaresh Kamboju	local msg="skip all tests:"
257ef7cc9fSZhang Zhen	local uid=$(id -u)
262dfb28e3SNaresh Kamboju
277ef7cc9fSZhang Zhen	if [ $uid -ne 0 ]; then
282dfb28e3SNaresh Kamboju		echo $msg must be run as root >&2
2968581446SShuah Khan (Samsung OSG)		exit $ksft_skip
302dfb28e3SNaresh Kamboju	fi
312dfb28e3SNaresh Kamboju}
322dfb28e3SNaresh Kamboju
33fc4eb486SYang Xukernel_gte()
34fc4eb486SYang Xu{
35fc4eb486SYang Xu	major=${1%.*}
36fc4eb486SYang Xu	minor=${1#*.}
37fc4eb486SYang Xu
38fc4eb486SYang Xu	if [ $kernel_major -gt $major ]; then
39fc4eb486SYang Xu		return 0
40fc4eb486SYang Xu	elif [[ $kernel_major -eq $major && $kernel_minor -ge $minor ]]; then
41fc4eb486SYang Xu		return 0
42fc4eb486SYang Xu	fi
43fc4eb486SYang Xu
44fc4eb486SYang Xu	return 1
45fc4eb486SYang Xu}
46fc4eb486SYang Xu
47f21fb798SNaresh Kambojuzram_cleanup()
48f21fb798SNaresh Kamboju{
49f21fb798SNaresh Kamboju	echo "zram cleanup"
50f21fb798SNaresh Kamboju	local i=
51*01dabed2SYang Xu	for i in $(seq $dev_start $dev_makeswap); do
52f21fb798SNaresh Kamboju		swapoff /dev/zram$i
53f21fb798SNaresh Kamboju	done
54f21fb798SNaresh Kamboju
55*01dabed2SYang Xu	for i in $(seq $dev_start $dev_mounted); do
56f21fb798SNaresh Kamboju		umount /dev/zram$i
57f21fb798SNaresh Kamboju	done
58f21fb798SNaresh Kamboju
59*01dabed2SYang Xu	for i in $(seq $dev_start $dev_end); do
60f21fb798SNaresh Kamboju		echo 1 > /sys/block/zram${i}/reset
61f21fb798SNaresh Kamboju		rm -rf zram$i
62f21fb798SNaresh Kamboju	done
63f21fb798SNaresh Kamboju
64*01dabed2SYang Xu	if [ $sys_control -eq 1 ]; then
65*01dabed2SYang Xu		for i in $(seq $dev_start $dev_end); do
66*01dabed2SYang Xu			echo $i > /sys/class/zram-control/hot_remove
67*01dabed2SYang Xu		done
68*01dabed2SYang Xu	fi
69f21fb798SNaresh Kamboju
70*01dabed2SYang Xu	if [ $module_load -eq 1 ]; then
71f21fb798SNaresh Kamboju		rmmod zram > /dev/null 2>&1
72f21fb798SNaresh Kamboju	fi
73f21fb798SNaresh Kamboju}
74f21fb798SNaresh Kamboju
75f21fb798SNaresh Kambojuzram_load()
76f21fb798SNaresh Kamboju{
77f21fb798SNaresh Kamboju	echo "create '$dev_num' zram device(s)"
78*01dabed2SYang Xu
79*01dabed2SYang Xu	# zram module loaded, new kernel
80*01dabed2SYang Xu	if [ -d "/sys/class/zram-control" ]; then
81*01dabed2SYang Xu		echo "zram modules already loaded, kernel supports" \
82*01dabed2SYang Xu			"zram-control interface"
83*01dabed2SYang Xu		dev_start=$(ls /dev/zram* | wc -w)
84*01dabed2SYang Xu		dev_end=$(($dev_start + $dev_num - 1))
85*01dabed2SYang Xu		sys_control=1
86*01dabed2SYang Xu
87*01dabed2SYang Xu		for i in $(seq $dev_start $dev_end); do
88*01dabed2SYang Xu			cat /sys/class/zram-control/hot_add > /dev/null
89*01dabed2SYang Xu		done
90*01dabed2SYang Xu
91*01dabed2SYang Xu		echo "all zram devices (/dev/zram$dev_start~$dev_end" \
92*01dabed2SYang Xu			"successfully created"
93*01dabed2SYang Xu		return 0
94*01dabed2SYang Xu	fi
95*01dabed2SYang Xu
96*01dabed2SYang Xu	# detect old kernel or built-in
97f21fb798SNaresh Kamboju	modprobe zram num_devices=$dev_num
98*01dabed2SYang Xu	if [ ! -d "/sys/class/zram-control" ]; then
99*01dabed2SYang Xu		if grep -q '^zram' /proc/modules; then
100*01dabed2SYang Xu			rmmod zram > /dev/null 2>&1
101f21fb798SNaresh Kamboju			if [ $? -ne 0 ]; then
102*01dabed2SYang Xu				echo "zram module is being used on old kernel" \
103*01dabed2SYang Xu					"without zram-control interface"
104*01dabed2SYang Xu				exit $ksft_skip
105*01dabed2SYang Xu			fi
106*01dabed2SYang Xu		else
107*01dabed2SYang Xu			echo "test needs CONFIG_ZRAM=m on old kernel without" \
108*01dabed2SYang Xu				"zram-control interface"
109*01dabed2SYang Xu			exit $ksft_skip
110*01dabed2SYang Xu		fi
111*01dabed2SYang Xu		modprobe zram num_devices=$dev_num
112f21fb798SNaresh Kamboju	fi
113f21fb798SNaresh Kamboju
114*01dabed2SYang Xu	module_load=1
115*01dabed2SYang Xu	dev_end=$(($dev_num - 1))
116*01dabed2SYang Xu	echo "all zram devices (/dev/zram0~$dev_end) successfully created"
117f21fb798SNaresh Kamboju}
118f21fb798SNaresh Kamboju
119f21fb798SNaresh Kambojuzram_max_streams()
120f21fb798SNaresh Kamboju{
121f21fb798SNaresh Kamboju	echo "set max_comp_streams to zram device(s)"
122f21fb798SNaresh Kamboju
123fc4eb486SYang Xu	kernel_gte 4.7
124fc4eb486SYang Xu	if [ $? -eq 0 ]; then
125fc4eb486SYang Xu		echo "The device attribute max_comp_streams was"\
126fc4eb486SYang Xu		               "deprecated in 4.7"
127fc4eb486SYang Xu		return 0
128fc4eb486SYang Xu	fi
129fc4eb486SYang Xu
130*01dabed2SYang Xu	local i=$dev_start
131f21fb798SNaresh Kamboju	for max_s in $zram_max_streams; do
132f21fb798SNaresh Kamboju		local sys_path="/sys/block/zram${i}/max_comp_streams"
133f21fb798SNaresh Kamboju		echo $max_s > $sys_path || \
134f21fb798SNaresh Kamboju			echo "FAIL failed to set '$max_s' to $sys_path"
135f21fb798SNaresh Kamboju		sleep 1
136f21fb798SNaresh Kamboju		local max_streams=$(cat $sys_path)
137f21fb798SNaresh Kamboju
138f21fb798SNaresh Kamboju		[ "$max_s" -ne "$max_streams" ] && \
139f21fb798SNaresh Kamboju			echo "FAIL can't set max_streams '$max_s', get $max_stream"
140f21fb798SNaresh Kamboju
141f21fb798SNaresh Kamboju		i=$(($i + 1))
142*01dabed2SYang Xu		echo "$sys_path = '$max_streams'"
143f21fb798SNaresh Kamboju	done
144f21fb798SNaresh Kamboju
145f21fb798SNaresh Kamboju	echo "zram max streams: OK"
146f21fb798SNaresh Kamboju}
147f21fb798SNaresh Kamboju
148f21fb798SNaresh Kambojuzram_compress_alg()
149f21fb798SNaresh Kamboju{
150f21fb798SNaresh Kamboju	echo "test that we can set compression algorithm"
151f21fb798SNaresh Kamboju
152*01dabed2SYang Xu	local i=$dev_start
153*01dabed2SYang Xu	local algs=$(cat /sys/block/zram${i}/comp_algorithm)
154f21fb798SNaresh Kamboju	echo "supported algs: $algs"
155*01dabed2SYang Xu
156f21fb798SNaresh Kamboju	for alg in $zram_algs; do
157f21fb798SNaresh Kamboju		local sys_path="/sys/block/zram${i}/comp_algorithm"
158f21fb798SNaresh Kamboju		echo "$alg" >	$sys_path || \
159f21fb798SNaresh Kamboju			echo "FAIL can't set '$alg' to $sys_path"
160f21fb798SNaresh Kamboju		i=$(($i + 1))
161*01dabed2SYang Xu		echo "$sys_path = '$alg'"
162f21fb798SNaresh Kamboju	done
163f21fb798SNaresh Kamboju
164f21fb798SNaresh Kamboju	echo "zram set compression algorithm: OK"
165f21fb798SNaresh Kamboju}
166f21fb798SNaresh Kamboju
167f21fb798SNaresh Kambojuzram_set_disksizes()
168f21fb798SNaresh Kamboju{
169f21fb798SNaresh Kamboju	echo "set disk size to zram device(s)"
170*01dabed2SYang Xu	local i=$dev_start
171f21fb798SNaresh Kamboju	for ds in $zram_sizes; do
172f21fb798SNaresh Kamboju		local sys_path="/sys/block/zram${i}/disksize"
173f21fb798SNaresh Kamboju		echo "$ds" >	$sys_path || \
174f21fb798SNaresh Kamboju			echo "FAIL can't set '$ds' to $sys_path"
175f21fb798SNaresh Kamboju
176f21fb798SNaresh Kamboju		i=$(($i + 1))
177*01dabed2SYang Xu		echo "$sys_path = '$ds'"
178f21fb798SNaresh Kamboju	done
179f21fb798SNaresh Kamboju
180f21fb798SNaresh Kamboju	echo "zram set disksizes: OK"
181f21fb798SNaresh Kamboju}
182f21fb798SNaresh Kamboju
183f21fb798SNaresh Kambojuzram_set_memlimit()
184f21fb798SNaresh Kamboju{
185f21fb798SNaresh Kamboju	echo "set memory limit to zram device(s)"
186f21fb798SNaresh Kamboju
187*01dabed2SYang Xu	local i=$dev_start
188f21fb798SNaresh Kamboju	for ds in $zram_mem_limits; do
189f21fb798SNaresh Kamboju		local sys_path="/sys/block/zram${i}/mem_limit"
190f21fb798SNaresh Kamboju		echo "$ds" >	$sys_path || \
191f21fb798SNaresh Kamboju			echo "FAIL can't set '$ds' to $sys_path"
192f21fb798SNaresh Kamboju
193f21fb798SNaresh Kamboju		i=$(($i + 1))
194*01dabed2SYang Xu		echo "$sys_path = '$ds'"
195f21fb798SNaresh Kamboju	done
196f21fb798SNaresh Kamboju
197f21fb798SNaresh Kamboju	echo "zram set memory limit: OK"
198f21fb798SNaresh Kamboju}
199f21fb798SNaresh Kamboju
200f21fb798SNaresh Kambojuzram_makeswap()
201f21fb798SNaresh Kamboju{
202f21fb798SNaresh Kamboju	echo "make swap with zram device(s)"
203*01dabed2SYang Xu	local i=$dev_start
204*01dabed2SYang Xu	for i in $(seq $dev_start $dev_end); do
205f21fb798SNaresh Kamboju		mkswap /dev/zram$i > err.log 2>&1
206f21fb798SNaresh Kamboju		if [ $? -ne 0 ]; then
207f21fb798SNaresh Kamboju			cat err.log
208f21fb798SNaresh Kamboju			echo "FAIL mkswap /dev/zram$1 failed"
209f21fb798SNaresh Kamboju		fi
210f21fb798SNaresh Kamboju
211f21fb798SNaresh Kamboju		swapon /dev/zram$i > err.log 2>&1
212f21fb798SNaresh Kamboju		if [ $? -ne 0 ]; then
213f21fb798SNaresh Kamboju			cat err.log
214f21fb798SNaresh Kamboju			echo "FAIL swapon /dev/zram$1 failed"
215f21fb798SNaresh Kamboju		fi
216f21fb798SNaresh Kamboju
217f21fb798SNaresh Kamboju		echo "done with /dev/zram$i"
218f21fb798SNaresh Kamboju		dev_makeswap=$i
219f21fb798SNaresh Kamboju	done
220f21fb798SNaresh Kamboju
221f21fb798SNaresh Kamboju	echo "zram making zram mkswap and swapon: OK"
222f21fb798SNaresh Kamboju}
223f21fb798SNaresh Kamboju
224f21fb798SNaresh Kambojuzram_swapoff()
225f21fb798SNaresh Kamboju{
226f21fb798SNaresh Kamboju	local i=
227*01dabed2SYang Xu	for i in $(seq $dev_start $dev_end); do
228f21fb798SNaresh Kamboju		swapoff /dev/zram$i > err.log 2>&1
229f21fb798SNaresh Kamboju		if [ $? -ne 0 ]; then
230f21fb798SNaresh Kamboju			cat err.log
231f21fb798SNaresh Kamboju			echo "FAIL swapoff /dev/zram$i failed"
232f21fb798SNaresh Kamboju		fi
233f21fb798SNaresh Kamboju	done
234f21fb798SNaresh Kamboju	dev_makeswap=-1
235f21fb798SNaresh Kamboju
236f21fb798SNaresh Kamboju	echo "zram swapoff: OK"
237f21fb798SNaresh Kamboju}
238f21fb798SNaresh Kamboju
239f21fb798SNaresh Kambojuzram_makefs()
240f21fb798SNaresh Kamboju{
241*01dabed2SYang Xu	local i=$dev_start
242f21fb798SNaresh Kamboju	for fs in $zram_filesystems; do
243f21fb798SNaresh Kamboju		# if requested fs not supported default it to ext2
244f21fb798SNaresh Kamboju		which mkfs.$fs > /dev/null 2>&1 || fs=ext2
245f21fb798SNaresh Kamboju
246f21fb798SNaresh Kamboju		echo "make $fs filesystem on /dev/zram$i"
247f21fb798SNaresh Kamboju		mkfs.$fs /dev/zram$i > err.log 2>&1
248f21fb798SNaresh Kamboju		if [ $? -ne 0 ]; then
249f21fb798SNaresh Kamboju			cat err.log
250f21fb798SNaresh Kamboju			echo "FAIL failed to make $fs on /dev/zram$i"
251f21fb798SNaresh Kamboju		fi
252f21fb798SNaresh Kamboju		i=$(($i + 1))
253f21fb798SNaresh Kamboju		echo "zram mkfs.$fs: OK"
254f21fb798SNaresh Kamboju	done
255f21fb798SNaresh Kamboju}
256f21fb798SNaresh Kamboju
257f21fb798SNaresh Kambojuzram_mount()
258f21fb798SNaresh Kamboju{
259f21fb798SNaresh Kamboju	local i=0
260*01dabed2SYang Xu	for i in $(seq $dev_start $dev_end); do
261f21fb798SNaresh Kamboju		echo "mount /dev/zram$i"
262f21fb798SNaresh Kamboju		mkdir zram$i
263f21fb798SNaresh Kamboju		mount /dev/zram$i zram$i > /dev/null || \
264f21fb798SNaresh Kamboju			echo "FAIL mount /dev/zram$i failed"
265f21fb798SNaresh Kamboju		dev_mounted=$i
266f21fb798SNaresh Kamboju	done
267f21fb798SNaresh Kamboju
268f21fb798SNaresh Kamboju	echo "zram mount of zram device(s): OK"
269f21fb798SNaresh Kamboju}
270