• 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#
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_brk TFAIL "failed to make $fs on /dev/zram$i"
86		fi
87
88		i=$(($i + 1))
89	done
90
91	tst_res TPASS "zram_makefs succeeded"
92}
93
94zram_mount()
95{
96	local i
97
98	for i in $(seq $dev_start $dev_end); do
99		tst_res TINFO "mount /dev/zram$i"
100		mkdir zram$i
101		ROD mount /dev/zram$i zram$i
102		dev_mounted=$i
103	done
104
105	tst_res TPASS "mount of zram device(s) succeeded"
106}
107
108read_mem_used_total()
109{
110	echo $(awk '{print $3}' $1)
111}
112
113# Reads /sys/block/zram*/mm_stat until mem_used_total is not 0.
114check_read_mem_used_total()
115{
116	local file="$1"
117	local mem_used_total
118
119	tst_res TINFO "$file"
120	cat $file >&2
121
122	mem_used_total=$(read_mem_used_total $file)
123	[ "$mem_used_total" -eq 0 ] && return 1
124
125	return 0
126}
127
128zram_fill_fs()
129{
130	local mem_used_total
131	local b i r v
132
133	for i in $(seq $dev_start $dev_end); do
134		tst_res TINFO "filling zram$i (it can take long time)"
135		b=0
136		while true; do
137			dd conv=notrunc if=/dev/zero of=zram${i}/file \
138				oflag=append count=1 bs=1024 status=none \
139				>/dev/null 2>err.txt || break
140			b=$(($b + 1))
141		done
142		if [ $b -eq 0 ]; then
143			[ -s err.txt ] && tst_res TWARN "dd error: $(cat err.txt)"
144			tst_brk TBROK "cannot fill zram $i"
145		fi
146		tst_res TPASS "zram$i was filled with '$b' KB"
147
148		if [ ! -f "/sys/block/zram$i/mm_stat" ]; then
149			if [ $i -eq 0 ]; then
150				tst_res TCONF "zram compression ratio test requires zram mm_stat sysfs file"
151			fi
152
153			continue
154		fi
155
156		TST_RETRY_FUNC "check_read_mem_used_total /sys/block/zram$i/mm_stat" 0
157		mem_used_total=$(read_mem_used_total /sys/block/zram$i/mm_stat)
158		tst_res TINFO "mem_used_total: $mem_used_total"
159
160		v=$((100 * 1024 * $b / $mem_used_total))
161		r=$(echo "scale=2; $v / 100 " | bc)
162
163		if [ "$v" -lt 100 ]; then
164			tst_res TFAIL "compression ratio: $r:1"
165			break
166		fi
167
168		tst_res TPASS "compression ratio: $r:1"
169	done
170}
171
172do_test()
173{
174	case $1 in
175	 1) zram_max_streams;;
176	 2) zram_compress_alg;;
177	 3) zram_set_disksizes;;
178	 4) zram_set_memlimit;;
179	 5) zram_makefs;;
180	 6) zram_mount;;
181	 7) zram_fill_fs;;
182	esac
183}
184
185. zram_lib.sh
186tst_run
187