• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1#!/bin/sh
2# Copyright (c) 2015 Oracle and/or its affiliates. All Rights Reserved.
3# Copyright (c) 2019-2022 Petr Vorel <pvorel@suse.cz>
4# Author: Alexey Kodanev <alexey.kodanev@oracle.com>
5
6dev_makeswap=-1
7dev_mounted=-1
8dev_start=0
9dev_end=-1
10module_load=-1
11sys_control=-1
12
13TST_NEEDS_TMPDIR=1
14TST_NEEDS_ROOT=1
15TST_SETUP="${TST_SETUP:-zram_load}"
16TST_CLEANUP="${TST_CLEANUP:-zram_cleanup}"
17TST_NEEDS_DRIVERS="zram"
18
19zram_cleanup()
20{
21	local i
22
23	for i in $(seq $dev_start $dev_makeswap); do
24		swapoff /dev/zram$i
25	done
26
27	for i in $(seq $dev_start $dev_mounted); do
28		umount /dev/zram$i
29	done
30
31	for i in $(seq $dev_start $dev_end); do
32		echo 1 > /sys/block/zram${i}/reset
33	done
34
35	if [ $sys_control -eq 1 ]; then
36		for i in $(seq $dev_start $dev_end); do
37			echo $i > /sys/class/zram-control/hot_remove
38		done
39	fi
40
41	if [ $module_load -eq 1 ]; then
42		rmmod zram > /dev/null 2>&1
43	fi
44}
45
46zram_load()
47{
48	local tmp
49
50	if [ -z "$dev_num" ]; then
51		dev_num=0
52		for tmp in $zram_max_streams; do
53			dev_num=$((dev_num+1))
54		done
55	fi
56
57	if [ $dev_num -le 0 ]; then
58		tst_brk TBROK "dev_num must be > 0"
59	fi
60
61	tst_set_timeout $((dev_num*450))
62
63	tst_res TINFO "create '$dev_num' zram device(s)"
64
65	# zram module loaded, new kernel
66	if [ -d "/sys/class/zram-control" ]; then
67		tst_res TINFO "zram module already loaded, kernel supports zram-control interface"
68		dev_start=$(ls /dev/zram* | wc -w)
69		dev_end=$(($dev_start + $dev_num - 1))
70		sys_control=1
71
72		for i in $(seq  $dev_start $dev_end); do
73			cat /sys/class/zram-control/hot_add > /dev/null
74		done
75
76		tst_res TPASS "all zram devices (/dev/zram$dev_start~$dev_end) successfully created"
77		return
78	fi
79
80	# detect old kernel or built-in
81	modprobe zram num_devices=$dev_num
82	if [ ! -d "/sys/class/zram-control" ]; then
83		if grep -q '^zram' /proc/modules; then
84			rmmod zram > /dev/null 2>&1 || \
85				tst_brk TCONF "zram module is being used on old kernel without zram-control interface"
86		else
87			tst_brk TCONF "test needs CONFIG_ZRAM=m on old kernel without zram-control interface"
88		fi
89		modprobe zram num_devices=$dev_num
90	fi
91
92	module_load=1
93	dev_end=$(($dev_num - 1))
94	tst_res TPASS "all zram devices (/dev/zram0~$dev_end) successfully created"
95}
96
97zram_max_streams()
98{
99	if tst_kvcmp -lt "3.15" -o -ge "4.7"; then
100		tst_res TCONF "The device attribute max_comp_streams was"\
101		               "introduced in kernel 3.15 and deprecated in 4.7"
102		return
103	fi
104
105	tst_res TINFO "set max_comp_streams to zram device(s)"
106
107	local i=$dev_start
108
109	for max_s in $zram_max_streams; do
110		local sys_path="/sys/block/zram${i}/max_comp_streams"
111		echo $max_s > $sys_path || \
112			tst_brk TFAIL "failed to set '$max_s' to $sys_path"
113		local max_streams=$(cat $sys_path)
114
115		[ "$max_s" -ne "$max_streams" ] && \
116			tst_brk TFAIL "can't set max_streams '$max_s', get $max_stream"
117
118		i=$(($i + 1))
119		tst_res TINFO "$sys_path = '$max_streams'"
120	done
121
122	tst_res TPASS "test succeeded"
123}
124
125zram_compress_alg()
126{
127	if tst_kvcmp -lt "3.15"; then
128		tst_res TCONF "device attribute comp_algorithm is"\
129			"introduced since kernel v3.15, the running kernel"\
130			"does not support it"
131		return
132	fi
133
134	local i=$dev_start
135
136	tst_res TINFO "test that we can set compression algorithm"
137	local algs="$(sed 's/[][]//g' /sys/block/zram${i}/comp_algorithm)"
138	tst_res TINFO "supported algs: $algs"
139
140	for i in $(seq $dev_start $dev_end); do
141		for alg in $algs; do
142			local sys_path="/sys/block/zram${i}/comp_algorithm"
143			echo "$alg" >  $sys_path || \
144				tst_brk TFAIL "can't set '$alg' to $sys_path"
145			tst_res TINFO "$sys_path = '$alg'"
146		done
147	done
148
149	tst_res TPASS "test succeeded"
150}
151
152zram_set_disksizes()
153{
154	local i=$dev_start
155	local ds
156
157	tst_res TINFO "set disk size to zram device(s)"
158	for ds in $zram_sizes; do
159		local sys_path="/sys/block/zram${i}/disksize"
160		echo "$ds" >  $sys_path || \
161			tst_brk TFAIL "can't set '$ds' to $sys_path"
162
163		i=$(($i + 1))
164		tst_res TINFO "$sys_path = '$ds'"
165	done
166
167	tst_res TPASS "test succeeded"
168}
169
170zram_set_memlimit()
171{
172	if tst_kvcmp -lt "3.18"; then
173		tst_res TCONF "device attribute mem_limit is"\
174			"introduced since kernel v3.18, the running kernel"\
175			"does not support it"
176		return
177	fi
178
179	local i=$dev_start
180	local ds
181
182	tst_res TINFO "set memory limit to zram device(s)"
183
184	for ds in $zram_mem_limits; do
185		local sys_path="/sys/block/zram${i}/mem_limit"
186		echo "$ds" >  $sys_path || \
187			tst_brk TFAIL "can't set '$ds' to $sys_path"
188
189		i=$(($i + 1))
190		tst_res TINFO "$sys_path = '$ds'"
191	done
192
193	tst_res TPASS "test succeeded"
194}
195
196. tst_test.sh
197