xref: /aosp_15_r20/external/ltp/testcases/kernel/device-drivers/zram/zram01.sh (revision 49cdfc7efb34551c7342be41a7384b9c40d7cab7)
1#!/bin/sh
2# Copyright (c) 2015 Oracle and/or its affiliates. All Rights Reserved.
3# Copyright (c) 2019-2022 Petr Vorel <[email protected]>
4# Author: Alexey Kodanev <[email protected]>
5#
6# Test creates several zram devices with different filesystems on them.
7# It fills each device with zeros and checks that compression works.
8
9TST_CNT=7
10TST_TESTFUNC="do_test"
11TST_NEEDS_CMDS="awk bc dd"
12TST_SETUP="setup"
13
14check_space_for_fs()
15{
16	local fs="$1"
17	local ram_size
18
19	ram_size=$(awk '/MemTotal:/ {print $2}' /proc/meminfo)
20	if [ "$ram_size" -lt 1048576 ]; then
21		tst_res TINFO "not enough space for $fs"
22		return 1
23	fi
24	return 0
25}
26
27# List of parameters for zram devices.
28# For each number the test creates own zram device.
29# NOTE about size:
30# The zram sysfs node 'disksize' value can be either in bytes,
31# or you can use mem suffixes. But in some old kernels, mem
32# suffixes are not supported, for example, in RHEL6.6GA's kernel
33# layer, it uses strict_strtoull() to parse disksize which does
34# not support mem suffixes, in some newer kernels, they use
35# memparse() which supports mem suffixes. So here we just use
36# bytes to make sure everything works correctly.
37initialize_vars()
38{
39	local fs limit size stream=-1
40	dev_num=0
41
42	for fs in $(tst_supported_fs -s tmpfs); do
43		size="26214400"
44		limit="25M"
45
46		if [ "$fs" = "btrfs" -o "$fs" = "xfs" ]; then
47			check_space_for_fs "$fs" || continue
48
49			if [ "$fs" = "btrfs" ]; then
50				size="402653184"
51			elif [ "$fs" = "xfs" ]; then
52				size=314572800
53			fi
54			limit="$((size/1024/1024))M"
55		fi
56
57		stream=$((stream+3))
58		dev_num=$((dev_num+1))
59		zram_filesystems="$zram_filesystems $fs"
60		zram_mem_limits="$zram_mem_limits $limit"
61		zram_sizes="$zram_sizes $size"
62		zram_max_streams="$zram_max_streams $stream"
63	done
64
65	[ $dev_num -eq 0 ] && \
66		tst_brk TCONF "no suitable filesystem"
67}
68
69setup()
70{
71	initialize_vars
72	zram_load
73}
74
75zram_makefs()
76{
77	local i=$dev_start
78	local fs
79
80	for fs in $zram_filesystems; do
81		tst_res TINFO "make $fs filesystem on /dev/zram$i"
82		mkfs.$fs /dev/zram$i > err.log 2>&1
83		if [ $? -ne 0 ]; then
84			cat err.log
85			tst_res TFAIL "Failed to make $fs on /dev/zram$i"
86			tst_brk TBROK "Can't continue with mounting the FS"
87		fi
88
89		i=$(($i + 1))
90	done
91
92	tst_res TPASS "zram_makefs succeeded"
93}
94
95zram_mount()
96{
97	local i
98
99	for i in $(seq $dev_start $dev_end); do
100		tst_res TINFO "mount /dev/zram$i"
101		mkdir zram$i
102		ROD mount /dev/zram$i zram$i
103		dev_mounted=$i
104	done
105
106	tst_res TPASS "mount of zram device(s) succeeded"
107}
108
109read_mem_used_total()
110{
111	echo $(awk '{print $3}' $1)
112}
113
114# Reads /sys/block/zram*/mm_stat until mem_used_total is not 0.
115check_read_mem_used_total()
116{
117	local file="$1"
118	local mem_used_total
119
120	tst_res TINFO "$file"
121	cat $file >&2
122
123	mem_used_total=$(read_mem_used_total $file)
124	[ "$mem_used_total" -eq 0 ] && return 1
125
126	return 0
127}
128
129zram_fill_fs()
130{
131	local mem_used_total
132	local b i r v
133
134	for i in $(seq $dev_start $dev_end); do
135		tst_res TINFO "filling zram$i (it can take long time)"
136		b=0
137		while true; do
138			dd conv=notrunc if=/dev/zero of=zram${i}/file \
139				oflag=append count=1 bs=1024 status=none \
140				>/dev/null 2>err.txt || break
141			b=$(($b + 1))
142		done
143		if [ $b -eq 0 ]; then
144			[ -s err.txt ] && tst_res TWARN "dd error: $(cat err.txt)"
145			tst_brk TBROK "cannot fill zram $i"
146		fi
147		tst_res TPASS "zram$i was filled with '$b' KB"
148
149		if [ ! -f "/sys/block/zram$i/mm_stat" ]; then
150			if [ $i -eq 0 ]; then
151				tst_res TCONF "zram compression ratio test requires zram mm_stat sysfs file"
152			fi
153
154			continue
155		fi
156
157		TST_RETRY_FN_EXP_BACKOFF "check_read_mem_used_total /sys/block/zram$i/mm_stat" 0 10
158		mem_used_total=$(read_mem_used_total /sys/block/zram$i/mm_stat)
159		tst_res TINFO "mem_used_total: $mem_used_total"
160
161		v=$((100 * 1024 * $b / $mem_used_total))
162		r=$(echo "scale=2; $v / 100 " | bc)
163
164		if [ "$v" -lt 100 ]; then
165			tst_res TFAIL "compression ratio: $r:1"
166			break
167		fi
168
169		tst_res TPASS "compression ratio: $r:1"
170	done
171}
172
173do_test()
174{
175	case $1 in
176	 1) zram_max_streams;;
177	 2) zram_compress_alg;;
178	 3) zram_set_disksizes;;
179	 4) zram_set_memlimit;;
180	 5) zram_makefs;;
181	 6) zram_mount;;
182	 7) zram_fill_fs;;
183	esac
184}
185
186. zram_lib.sh
187tst_run
188