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